ScriptArea.js 10 KB

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