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