ScriptArea.js 8.7 KB

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