ScriptArea.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. MWF.widget = MWF.widget || {};
  2. MWF.require("MWF.widget.ScriptEditor", null, false);
  3. MWF.require("MWF.widget.JavascriptEditor", null, false);
  4. MWF.widget.ScriptArea = new Class({
  5. Implements: [Options, Events],
  6. Extends: MWF.widget.Common,
  7. options: {
  8. "title": "ScriptArea",
  9. "style": "default",
  10. "helpStyle" : "default",
  11. "maxObj": document.body
  12. },
  13. initialize: function(node, options){
  14. this.setOptions(options);
  15. this.node = $(node);
  16. this.container = new Element("div");
  17. this.path = MWF.defaultPath+"/widget/$ScriptArea/";
  18. this.cssPath = MWF.defaultPath+"/widget/$ScriptArea/"+this.options.style+"/css.wcss";
  19. this._loadCss();
  20. },
  21. load: function(content){
  22. if (this.fireEvent("queryLoad")){
  23. this.container.set("styles", this.css.container);
  24. this.container.inject(this.node);
  25. this.createTitleNode();
  26. this.createContent(content);
  27. this.fireEvent("postLoad");
  28. }
  29. },
  30. createTitleNode: function(){
  31. this.titleNode = new Element("div", {
  32. "styles": this.css.titleNode,
  33. "events": {
  34. "dblclick": this.toggleSize.bind(this)
  35. }
  36. }).inject(this.container);
  37. this.titleActionNode = new Element("div", {
  38. "styles": this.css.titleActionNode,
  39. "events": {
  40. "click": this.toggleSize.bind(this)
  41. }
  42. }).inject(this.titleNode);
  43. this.referenceNode = new Element("div", {
  44. "styles": this.css.referenceNode
  45. }).inject(this.titleNode);
  46. this.titleTextNode = new Element("div", {
  47. "styles": this.css.titleTextNode,
  48. "text": this.options.title
  49. }).inject(this.titleNode);
  50. },
  51. toggleSize: function(e){
  52. var status = this.titleActionNode.retrieve("status", "max");
  53. if (status=="max"){
  54. this.maxSize();
  55. }else{
  56. this.returnSize();
  57. }
  58. },
  59. maxSize: function(){
  60. var obj = this.options.maxObj;
  61. var coordinates = obj.getCoordinates(obj.getOffsetParent());
  62. this.container.store("size", {"height": this.container.getStyle("height"), "width": this.container.getStyle("width")});
  63. this.jsEditor.showLineNumbers();
  64. this.jsEditor.max();
  65. this.container.setStyles({
  66. "position": "absolute",
  67. "top": coordinates.top,
  68. "left": coordinates.left,
  69. "width": coordinates.width,
  70. "height": coordinates.height-2,
  71. "z-index": 20001
  72. });
  73. this.resizeContentNodeSize();
  74. this.titleActionNode.setStyle("background", "url("+this.path+this.options.style+"/icon/return.png) center center no-repeat");
  75. this.titleActionNode.store("status", "return");
  76. },
  77. returnSize: function(){
  78. var size = this.container.retrieve("size");
  79. this.editor.setOption("lineNumbers", false);
  80. this.container.setStyles({
  81. "position": "static",
  82. "top": 0,
  83. "left": 0,
  84. "width": "auto",
  85. "height": size.height
  86. });
  87. this.resizeContentNodeSize();
  88. this.titleActionNode.setStyle("background", "url("+this.path+this.options.style+"/icon/max.png) center center no-repeat");
  89. this.titleActionNode.store("status", "max");
  90. },
  91. resizeContentNodeSize: function(){
  92. var titleSize = this.titleNode.getSize();
  93. var size = this.container.getSize();
  94. var h = this.container.getStyle("height").toInt();
  95. var th = this.titleNode.getStyle("height").toInt();
  96. var height = (size.y || h)-(titleSize.y || th)-2-6;
  97. this.contentNode.setStyle("height", ""+height+"px");
  98. if (this.editor) this.editor.resize();
  99. },
  100. toJson: function(){
  101. return (this.editor) ? {"code": this.editor.getValue(), "html": this.editor.getValue()} : this.contentCode;
  102. },
  103. createContent: function(content){
  104. this.contentNode = new Element("div", {
  105. "styles": this.css.contentNode
  106. }).inject(this.container);
  107. this.resizeContentNodeSize();
  108. var inforNode = new Element("div", {"styles": this.css.inforNode, "text": "点击此处,编写脚本代码"}).inject(this.contentNode);
  109. if (!content || !content.code){
  110. this.referenceNode.setStyle("background", "url("+MWF.defaultPath+"/widget/$ScriptArea/"+this.options.style+"/icon/reference_empty.png) center center no-repeat")
  111. }
  112. this.contentCode = content;
  113. var _self = this;
  114. inforNode.addEvent("click", function(){
  115. this.destroy();
  116. _self.loadEditor(content);
  117. });
  118. },
  119. loadEditor: function(content){
  120. var value=(content) ? content.code : "";
  121. value = (value) ? value : "";
  122. this.jsEditor = new MWF.widget.JavascriptEditor(this.contentNode,{
  123. "option": {
  124. "value": value,
  125. "lineNumbers": false
  126. },
  127. "onPostLoad": function(){
  128. this.editor = this.jsEditor.editor;
  129. this.editor.on("change", function() {
  130. this.fireEvent("change");
  131. }.bind(this));
  132. debugger;
  133. this.editor.resize();
  134. this.fireEvent("postLoad");
  135. }.bind(this),
  136. "onSave": function(){
  137. this.fireEvent("change");
  138. this.fireEvent("save");
  139. }.bind(this)
  140. });
  141. this.jsEditor.load();
  142. this.createScriptReferenceMenu();
  143. this.jsEditor.addEvent("reference", function(editor, e, e1){
  144. if (!this.scriptReferenceMenu){
  145. this.createScriptReferenceMenu(this.showReferenceMenu.bind(this));
  146. }else{
  147. this.showReferenceMenu();
  148. }
  149. }.bind(this));
  150. this.referenceNode.setStyle("background", "url("+MWF.defaultPath+"/widget/$ScriptArea/"+this.options.style+"/icon/reference.png) center center no-repeat")
  151. },
  152. createScriptReferenceMenu: function(callback){
  153. MWF.require("MWF.widget.ScriptHelp", function(){
  154. this.scriptReferenceMenu = new MWF.widget.ScriptHelp(this.referenceNode, this.jsEditor.editor, {
  155. "style" : this.options.helpStyle,
  156. "event": "click",
  157. "onPostLoad": function(){
  158. if (callback) callback();
  159. }.bind(this)
  160. });
  161. this.scriptReferenceMenu.getEditor = function(){return this.jsEditor.editor;}.bind(this)
  162. }.bind(this));
  163. },
  164. showReferenceMenu: function(){
  165. var pos = this.jsEditor.getCursorPixelPosition();
  166. var e = {"page": {}};
  167. e.page.x = pos.left;
  168. e.page.y = pos.top;
  169. this.scriptReferenceMenu.menu.showIm(e);
  170. },
  171. focus: function(){
  172. if (this.editor) this.editor.focus();
  173. }
  174. });