ScriptArea.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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": size.width,
  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 height = size.y-titleSize.y-2-6;
  95. this.contentNode.setStyle("height", ""+height+"px");
  96. if (this.editor) this.editor.resize();
  97. },
  98. toJson: function(){
  99. return (this.editor) ? {"code": this.editor.getValue(), "html": this.editor.getValue()} : this.contentCode;
  100. },
  101. createContent: function(content){
  102. this.contentNode = new Element("div", {
  103. "styles": this.css.contentNode
  104. }).inject(this.container);
  105. this.resizeContentNodeSize();
  106. var inforNode = new Element("div", {"styles": this.css.inforNode, "text": "点击此处,编写脚本代码"}).inject(this.contentNode);
  107. if (!content || !content.code){
  108. this.referenceNode.setStyle("background", "url("+MWF.defaultPath+"/widget/$ScriptArea/"+this.options.style+"/icon/reference_empty.png) center center no-repeat")
  109. }
  110. this.contentCode = content;
  111. var _self = this;
  112. inforNode.addEvent("click", function(){
  113. this.destroy();
  114. _self.loadEditor(content);
  115. });
  116. },
  117. loadEditor: function(content){
  118. var value=(content) ? content.code : "";
  119. value = (value) ? value : "";
  120. this.jsEditor = new MWF.widget.JavascriptEditor(this.contentNode,{
  121. "option": {
  122. "value": value,
  123. "lineNumbers": false
  124. },
  125. "onPostLoad": function(){
  126. this.editor = this.jsEditor.editor;
  127. this.editor.on("change", function() {
  128. this.fireEvent("change");
  129. }.bind(this));
  130. this.fireEvent("postLoad");
  131. }.bind(this),
  132. "onSave": function(){
  133. this.fireEvent("change");
  134. this.fireEvent("save");
  135. }.bind(this)
  136. });
  137. this.jsEditor.load();
  138. this.createScriptReferenceMenu();
  139. this.jsEditor.addEvent("reference", function(editor, e, e1){
  140. if (!this.scriptReferenceMenu){
  141. this.createScriptReferenceMenu(this.showReferenceMenu.bind(this));
  142. }else{
  143. this.showReferenceMenu();
  144. }
  145. }.bind(this));
  146. this.referenceNode.setStyle("background", "url("+MWF.defaultPath+"/widget/$ScriptArea/"+this.options.style+"/icon/reference.png) center center no-repeat")
  147. },
  148. createScriptReferenceMenu: function(callback){
  149. MWF.require("MWF.widget.ScriptHelp", function(){
  150. this.scriptReferenceMenu = new MWF.widget.ScriptHelp(this.referenceNode, this.jsEditor.editor, {
  151. "style" : this.options.helpStyle,
  152. "event": "click",
  153. "onPostLoad": function(){
  154. if (callback) callback();
  155. }.bind(this)
  156. });
  157. this.scriptReferenceMenu.getEditor = function(){return this.jsEditor.editor;}.bind(this)
  158. }.bind(this));
  159. },
  160. showReferenceMenu: function(){
  161. var pos = this.jsEditor.getCursorPixelPosition();
  162. var e = {"page": {}};
  163. e.page.x = pos.left;
  164. e.page.y = pos.top;
  165. this.scriptReferenceMenu.menu.showIm(e);
  166. },
  167. focus: function(){
  168. if (this.editor) this.editor.focus();
  169. }
  170. });