ScriptArea.js 8.8 KB

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