ScriptArea.js 9.3 KB

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