ScriptArea.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. o2.widget = o2.widget || {};
  2. //o2.require("o2.widget.ScriptEditor", null, false);
  3. o2.require("o2.widget.JavascriptEditor", null, false);
  4. o2.widget.ScriptArea = new Class({
  5. Implements: [Options, Events],
  6. Extends: o2.widget.Common,
  7. options: {
  8. "title": "ScriptArea",
  9. "style": "default",
  10. "helpStyle" : "default",
  11. "maxObj": document.body,
  12. "maxPosition": "relative",
  13. "isload": false,
  14. "isbind": true,
  15. "mode": "javascript",
  16. "runtime": "all",
  17. "key": "code",
  18. "forceType": null,
  19. "type": "web" //"service" or "web"
  20. },
  21. initialize: function(node, options){
  22. this.setOptions(options);
  23. this.node = $(node);
  24. this.container = new Element("div");
  25. this.path = o2.session.path+"/widget/$ScriptArea/";
  26. this.cssPath = o2.session.path+"/widget/$ScriptArea/"+this.options.style+"/css.wcss";
  27. this._loadCss();
  28. },
  29. load: function(content){
  30. this.contentCode = content || {code: ''};
  31. if (this.fireEvent("queryLoad")){
  32. this.container.set("styles", this.css.container);
  33. this.container.inject(this.node);
  34. this.createTitleNode();
  35. this.createContent(content);
  36. this.fireEvent("postLoad");
  37. }
  38. },
  39. setData: function(data){
  40. this.contentCode = {code: data.code || data};
  41. if (this.jsEditor){
  42. this.jsEditor.setValue(data.code || data);
  43. }
  44. },
  45. createTitleNode: function(){
  46. this.titleNode = new Element("div", {
  47. "styles": this.css.titleNode,
  48. "events": {
  49. "dblclick": this.toggleSize.bind(this)
  50. }
  51. }).inject(this.container);
  52. this.titleActionNode = new Element("div", {
  53. "styles": this.css.titleActionNode,
  54. "events": {
  55. "click": this.toggleSize.bind(this)
  56. }
  57. }).inject(this.titleNode);
  58. this.titleActionNode.hide();
  59. this.referenceNode = new Element("div", {
  60. "styles": this.css.referenceNode,
  61. "title": "open API",
  62. "events": {
  63. "click": this.openApi.bind(this)
  64. }
  65. }).inject(this.titleNode);
  66. this.titleTextNode = new Element("div", {
  67. "styles": this.css.titleTextNode,
  68. "text": this.options.title
  69. }).inject(this.titleNode);
  70. },
  71. setTitle: function(title){
  72. if (this.titleTextNode){
  73. this.titleTextNode.set('text', title);
  74. }
  75. },
  76. openApi: function(){
  77. o2.openWindow(this.options.api || "../api")
  78. },
  79. toggleSize: function(e){
  80. var status = this.titleActionNode.retrieve("status", "max");
  81. if (status=="max"){
  82. this.maxSize();
  83. }else{
  84. this.returnSize();
  85. }
  86. },
  87. maxSize: function(){
  88. if (!this.jsEditor) return;
  89. var obj = this.options.maxObj;
  90. var coordinates = obj.getCoordinates(obj.getOffsetParent());
  91. this.container.store("size", {"height": this.container.getStyle("height"), "width": this.container.getStyle("width")});
  92. this.jsEditor.showLineNumbers();
  93. this.jsEditor.max();
  94. this.container.inject(obj, "top");
  95. this.container.setStyles({
  96. "position": this.options.maxPosition || "relative",
  97. // "top": coordinates.top,
  98. // "left": coordinates.left,
  99. //"top": coordinates.top+"px",
  100. //"left": coordinates.left+"px",
  101. //"width": coordinates.width,
  102. "width": "100%",
  103. "height": coordinates.height-2,
  104. "z-index": 20001
  105. });
  106. this.resizeContentNodeSize();
  107. this.titleActionNode.setStyle("background", "url("+this.path+this.options.style+"/icon/return.png) center center no-repeat");
  108. this.titleActionNode.store("status", "return");
  109. this.jsEditor.focus();
  110. this.fireEvent("maxSize");
  111. },
  112. returnSize: function(){
  113. var size = this.container.retrieve("size");
  114. //this.editor.setOption("lineNumbers", false);
  115. this.jsEditor.hideLineNumbers();
  116. this.container.inject(this.node);
  117. this.container.setStyles({
  118. "position": "static",
  119. "top": 0,
  120. "left": 0,
  121. "width": "auto",
  122. "height": size.height
  123. });
  124. this.resizeContentNodeSize();
  125. this.titleActionNode.setStyle("background", "url("+this.path+this.options.style+"/icon/max.png) center center no-repeat");
  126. this.titleActionNode.store("status", "max");
  127. this.jsEditor.focus();
  128. this.fireEvent("returnSize");
  129. },
  130. resizeContentNodeSize: function(){
  131. var titleSize = this.titleNode.getSize();
  132. var size = this.container.getSize();
  133. var h = this.container.getStyle("height").toInt();
  134. var th = this.titleNode.getStyle("height").toInt();
  135. var height = (size.y || h)-(titleSize.y || th)-2-6;
  136. this.contentNode.setStyle("height", ""+height+"px");
  137. if (this.jsEditor) this.jsEditor.resize();
  138. },
  139. toJson: function(){
  140. return (this.editor) ? {"code": this.editor.getValue(), "html": this.editor.getValue()} : this.contentCode;
  141. },
  142. getData: function(){
  143. return (this.editor) ? this.editor.getValue() : (this.contentCode.code || this.contentCode);
  144. },
  145. createContent: function(content){
  146. var codeIcon = (this.options.type!=="service") ? "web_code.png" : "code.png";
  147. var codeEmptyIcon = (this.options.type!=="service") ? "web_code_empty.png" : "code_empty.png";
  148. this.contentNode = new Element("div", {
  149. "styles": this.css.contentNode
  150. }).inject(this.container);
  151. this.resizeContentNodeSize();
  152. if (!content || !content.code){
  153. this.referenceNode.setStyle("background", "url("+o2.session.path+"/widget/$ScriptArea/"+this.options.style+"/icon/"+codeEmptyIcon+") center center no-repeat");
  154. }else{
  155. this.referenceNode.setStyle("background", "url("+o2.session.path+"/widget/$ScriptArea/"+this.options.style+"/icon/"+codeIcon+") center center no-repeat");
  156. }
  157. if (this.options.isload){
  158. this.loadEditor(content);
  159. }else{
  160. var inforNode = new Element("div", {"styles": this.css.inforNode, "text": o2.LP.widget.scriptAreaEditNotice}).inject(this.contentNode);
  161. var _self = this;
  162. inforNode.addEvent("click", function(){
  163. this.remove();
  164. _self.loadEditor(content);
  165. });
  166. this.inforNode = inforNode;
  167. }
  168. },
  169. bind: function(content){
  170. if( o2.typeOf(content) !== "object" )return;
  171. this.value = content.code;
  172. this.html = content.code;
  173. if (content.editors){
  174. content.editors.push(this.jsEditor);
  175. }else{
  176. Object.defineProperty(content, "editors", {
  177. configurable : false,
  178. enumerable : false,
  179. writable: true,
  180. "value": []
  181. });
  182. content.editors.push(this.jsEditor);
  183. Object.defineProperty(content, this.options.key, {
  184. configurable : true,
  185. enumerable : true,
  186. "get": function(){return this.value;}.bind(this),
  187. "set": function(v){
  188. content.editors.each(function(editor){
  189. if (editor.editor){
  190. if (v!==editor.editor.getValue()) editor.editor.setValue(v,1);
  191. }else{
  192. editor.reload();
  193. }
  194. });
  195. this.value = v;
  196. }.bind(this)
  197. });
  198. }
  199. },
  200. loadEditor: function(content){
  201. var value=(content) ? content.code : "";
  202. value = (value) ? value : "";
  203. this.jsEditor = new o2.widget.JavascriptEditor(this.contentNode,{
  204. "runtime": this.options.runtime || "all",
  205. "forceType": this.options.forceType,
  206. "option": {
  207. "value": value,
  208. "lineNumbers": false,
  209. "mode": this.options.mode
  210. },
  211. "onPostLoad": function(){
  212. this.editor = this.jsEditor.editor;
  213. this.jsEditor.addEditorEvent("change", function() {
  214. this.fireEvent("change");
  215. }.bind(this));
  216. this.jsEditor.addEditorEvent("blur", function() {
  217. this.fireEvent("blur");
  218. }.bind(this));
  219. this.jsEditor.resize();
  220. this.fireEvent("postLoadEditor");
  221. }.bind(this),
  222. "onSave": function(){
  223. this.fireEvent("change");
  224. this.fireEvent("save");
  225. }.bind(this)
  226. });
  227. this.jsEditor.load();
  228. if (this.options.isbind) this.bind(content);
  229. this.titleActionNode.show();
  230. var codeIcon = (this.options.type!=="service") ? "web_code.png" : "code.png";
  231. this.referenceNode.setStyle("background", "url("+o2.session.path+"/widget/$ScriptArea/"+this.options.style+"/icon/"+codeIcon+") center center no-repeat");
  232. },
  233. createScriptReferenceMenu: function(callback){
  234. o2.require("o2.widget.ScriptHelp", function(){
  235. this.scriptReferenceMenu = new o2.widget.ScriptHelp(this.referenceNode, this.jsEditor.editor, {
  236. "style" : this.options.helpStyle,
  237. "event": "click",
  238. "onPostLoad": function(){
  239. if (callback) callback();
  240. }.bind(this)
  241. });
  242. this.scriptReferenceMenu.getEditor = function(){return this.jsEditor.editor;}.bind(this)
  243. }.bind(this));
  244. },
  245. showReferenceMenu: function(){
  246. var pos = this.jsEditor.getCursorPixelPosition();
  247. var e = {"page": {}};
  248. e.page.x = pos.left;
  249. e.page.y = pos.top;
  250. this.scriptReferenceMenu.menu.showIm(e);
  251. },
  252. focus: function(){
  253. if (this.jsEditor) this.jsEditor.focus();
  254. },
  255. destroy: function(){
  256. this.fireEvent("destroy");
  257. if (this.jsEditor){
  258. this.jsEditor.destroy();
  259. }
  260. this.container.destroy();
  261. o2.release(this);
  262. }
  263. });