Script.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. MWF.xApplication = MWF.xApplication || {};
  2. MWF.xApplication.portal = MWF.xApplication.portal || {};
  3. MWF.xApplication.ConfigDesigner = MWF.xApplication.ConfigDesigner || {};
  4. MWF.require("MWF.widget.Common", null, false);
  5. MWF.xDesktop.requireApp("ConfigDesigner", "lp."+MWF.language, null, false);
  6. MWF.require("MWF.widget.JavascriptEditor", null, false);
  7. MWF.xApplication.ConfigDesigner.Script = new Class({
  8. Extends: MWF.widget.Common,
  9. Implements: [Options, Events],
  10. options: {
  11. "style": "default",
  12. "showTab": true
  13. },
  14. initialize: function(designer, data, options){
  15. this.setOptions(options);
  16. this.path = "../x_component_portal_ScriptDesigner/$Script/";
  17. this.cssPath = "../x_component_portal_ScriptDesigner/$Script/"+this.options.style+"/css.wcss";
  18. this._loadCss();
  19. this.isChanged = false;
  20. this.designer = designer;
  21. this.data = data;
  22. if (!this.data.text) this.data.text = "";
  23. this.node = this.designer.designNode;
  24. this.tab = this.designer.scriptTab;
  25. this.areaNode = new Element("div", {"styles": {"overflow": "hidden", "height": "700px"}});
  26. this.propertyIncludeNode = this.designer.propertyDomArea;
  27. this.propertyNode = this.designer.propertyContentArea
  28. if(this.designer.application) this.data.applicationName = this.designer.application.name;
  29. if(this.designer.application) this.data.application = this.designer.application.id;
  30. this.isNewScript = (this.data.id) ? false : true;
  31. // this.createProperty();
  32. this.autoSave();
  33. this.designer.addEvent("queryClose", function(){
  34. if (this.autoSaveTimerID) window.clearInterval(this.autoSaveTimerID);
  35. }.bind(this));
  36. },
  37. autoSave: function(){
  38. this.autoSaveTimerID = window.setInterval(function(){
  39. if (!this.autoSaveCheckNode) this.autoSaveCheckNode = this.designer.contentToolbarNode.getElement("#MWFScriptAutoSaveCheck");
  40. if (this.autoSaveCheckNode){
  41. if (this.autoSaveCheckNode.get("checked")){
  42. if (this.isChanged) this.saveSilence();
  43. }
  44. }
  45. }.bind(this), 60000);
  46. },
  47. //createProperty: function(){
  48. // this.scriptPropertyNode = new Element("div", {"styles": this.css.scriptPropertyNode}).inject(this.propertyNode);
  49. //},
  50. load : function(){
  51. this.setAreaNodeSize();
  52. this.designer.addEvent("resize", this.setAreaNodeSize.bind(this));
  53. this.page = this.tab.addTab(this.areaNode, this.data.name || this.designer.lp.newScript, (!this.data.isNewScript && this.data.id!=this.designer.options.id));
  54. this.page.script = this;
  55. this.page.addEvent("show", function(){
  56. this.designer.scriptListAreaNode.getChildren().each(function(node){
  57. var scrtip = node.retrieve("script");
  58. if (scrtip.id==this.data.id){
  59. if (this.designer.currentListScriptItem){
  60. this.designer.currentListScriptItem.setStyles(this.designer.css.listScriptItem);
  61. }
  62. node.setStyles(this.designer.css.listScriptItem_current);
  63. this.designer.currentListScriptItem = node;
  64. this.lisNode = node;
  65. }
  66. }.bind(this));
  67. this.designer.currentScript = this;
  68. this.setPropertyContent();
  69. // this.setIncludeNode();
  70. if (this.editor){
  71. this.editor.focus();
  72. //this.editor.editor.navigateFileStart();
  73. }
  74. }.bind(this));
  75. this.page.addEvent("queryClose", function(){
  76. if (this.autoSaveTimerID) window.clearInterval(this.autoSaveTimerID);
  77. //if (this.isChanged) this.saveSilence();
  78. if (this.lisNode) this.lisNode.setStyles(this.designer.css.listScriptItem);
  79. }.bind(this));
  80. this.page.tabNode.addEvent("dblclick", this.designer.maxOrReturnEditor.bind(this.designer));
  81. this.editor = new MWF.widget.JavascriptEditor(this.areaNode,{"option": {
  82. value: "",
  83. mode: "json",
  84. "lineNumbers": true
  85. }});
  86. this.editor.load(function(){
  87. if (this.data.text){
  88. this.editor.setValue(this.data.text);
  89. }
  90. this.editor.addEditorEvent("change", function(){
  91. if (!this.isChanged){
  92. this.isChanged = true;
  93. this.page.textNode.set("text", " * "+this.page.textNode.get("text"));
  94. }
  95. }.bind(this));
  96. // this.editor.editor.on("change", function(e){
  97. // if (!this.isChanged){
  98. // this.isChanged = true;
  99. // this.page.textNode.set("text", " * "+this.page.textNode.get("text"));
  100. // }
  101. // }.bind(this));
  102. this.editor.addEvent("save", function(){
  103. this.save();
  104. }.bind(this));
  105. // this.editor.addEvent("reference", function(editor, e, e1){
  106. // if (!this.scriptReferenceMenu){
  107. // MWF.require("MWF.widget.ScriptHelp", function(){
  108. // this.scriptReferenceMenu = new MWF.widget.ScriptHelp(null, this.editor.editor, {
  109. // "onPostLoad": function(){
  110. // this.showReferenceMenu();
  111. // }.bind(this)
  112. // });
  113. // this.scriptReferenceMenu.getEditor = function(){return this.editor.editor;}.bind(this)
  114. // }.bind(this));
  115. // }else{
  116. // this.showReferenceMenu();
  117. // }
  118. // }.bind(this));
  119. var options = this.designer.styleSelectNode.options;
  120. for (var i=0; i<options.length; i++){
  121. var option = options[i];
  122. if (option.value==this.editor.theme){
  123. option.set("selected", true);
  124. break;
  125. }
  126. }
  127. options = this.designer.fontsizeSelectNode.options;
  128. for (var i=0; i<options.length; i++){
  129. var option = options[i];
  130. if (option.value==this.editor.fontSize){
  131. option.set("selected", true);
  132. break;
  133. }
  134. }
  135. options = this.designer.editorSelectNode.options;
  136. for (var i=0; i<options.length; i++){
  137. var option = options[i];
  138. if (option.value==this.editor.options.type){
  139. option.set("selected", true);
  140. break;
  141. }
  142. }
  143. options = this.designer.monacoStyleSelectNode.options;
  144. for (var i=0; i<options.length; i++){
  145. var option = options[i];
  146. if (option.value==this.editor.theme){
  147. option.set("selected", true);
  148. break;
  149. }
  150. }
  151. if (this.editor.options.type=="ace"){
  152. this.designer.monacoStyleSelectNode.hide();
  153. this.designer.styleSelectNode.show();
  154. }else{
  155. this.designer.monacoStyleSelectNode.show();
  156. this.designer.styleSelectNode.hide();
  157. }
  158. }.bind(this));
  159. if (this.options.showTab) this.page.showTabIm();
  160. },
  161. showReferenceMenu: function(){
  162. var pos = this.editor.getCursorPixelPosition();
  163. var e = {"page": {}};
  164. e.page.x = pos.left;
  165. e.page.y = pos.top;
  166. this.scriptReferenceMenu.menu.showIm(e);
  167. },
  168. setIncludeNode: function(){
  169. this.designer.propertyIncludeListArea.empty();
  170. this.data.dependScriptList.each(function(name){
  171. this.designer.addIncludeToList(name);
  172. }.bind(this));
  173. },
  174. setPropertyContent: function(){
  175. this.designer.propertyIdNode.set("text", this.data.id || "");
  176. this.designer.propertyNameNode.set("text", this.data.name || "");
  177. this.designer.propertyDescriptionNode.set("text", this.data.description || "");
  178. },
  179. setAreaNodeSize: function(){
  180. var size = this.node.getSize();
  181. var tabSize = this.tab.tabNodeContainer.getSize();
  182. var y = size.y - tabSize.y;
  183. this.areaNode.setStyle("height", ""+y+"px");
  184. if (this.editor) this.editor.resize();
  185. },
  186. addInclude: function(){
  187. },
  188. save: function(callback){
  189. if (!this.isSave){
  190. var validated = this.editor.validated();
  191. this.data.fileName = this.data.id;
  192. this.data.text = this.editor.getValue();
  193. this.data.fileContent = this.editor.getValue();
  194. this.data.nodeName = this.designer.propertyServerNode.getElement("option:selected").get("text");
  195. this.data.nodePort = this.designer.propertyServerNode.getElement("option:selected").get("value");
  196. this.isSave = true;
  197. this.designer.actions.ConfigAction.save(this.data, function(json){
  198. this.isSave = false;
  199. this.data.isNewScript = false;
  200. this.isChanged = false;
  201. this.page.textNode.set("text", this.data.name);
  202. if (this.lisNode) {
  203. this.lisNode.getLast().set("text", this.data.name);
  204. }
  205. this.designer.notice(this.designer.lp.notice.save_success, "success", this.node, {"x": "left", "y": "bottom"});
  206. //this.data.id = json.data.id;
  207. if (callback) callback();
  208. }.bind(this), function(xhr, text, error){
  209. this.isSave = false;
  210. var errorText = error+":"+text;
  211. if (xhr) errorText = xhr.responseText;
  212. MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  213. }.bind(this));
  214. }else{
  215. MWF.xDesktop.notice("info", {x: "right", y:"top"}, this.designer.lp.isSave);
  216. }
  217. },
  218. saveSilence: function(callback){
  219. if (!this.isSave){
  220. // var session = this.editor.editor.getSession();
  221. // var annotations = session.getAnnotations();
  222. // var validated = true;
  223. // for (var i=0; i<annotations.length; i++){
  224. // if (annotations[i].type=="error"){
  225. // validated = false;
  226. // break;
  227. // }
  228. // }
  229. var validated = this.editor.validated();
  230. if( this.designer.currentScript == this ){
  231. var name = this.designer.propertyNameNode.get("value");
  232. var alias = this.designer.propertyAliasNode.get("value");
  233. var description = this.designer.propertyDescriptionNode.get("value");
  234. if (!name){
  235. this.designer.notice(this.designer.lp.notice.inputName, "error");
  236. return false;
  237. }
  238. this.data.name = name;
  239. this.data.alias = alias;
  240. this.data.description = description;
  241. this.data.validated = validated;
  242. }
  243. this.data.text = this.editor.getValue();
  244. this.isSave = true;
  245. this.designer.actions.saveScript(this.data, function(json){
  246. this.isSave = false;
  247. this.data.isNewScript = false;
  248. this.isChanged = false;
  249. this.page.textNode.set("text", this.data.name);
  250. if (this.lisNode) {
  251. this.lisNode.getLast().set("text", this.data.name);
  252. }
  253. this.data.id = json.data.id;
  254. if (callback) callback();
  255. }.bind(this), function(xhr, text, error){
  256. this.isSave = false;
  257. //
  258. //var errorText = error+":"+text;
  259. //if (xhr) errorText = xhr.responseText;
  260. //MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  261. }.bind(this));
  262. }else{
  263. MWF.xDesktop.notice("info", {x: "right", y:"top"}, this.designer.lp.isSave);
  264. }
  265. },
  266. saveAs: function(){},
  267. explode: function(){},
  268. implode: function(){}
  269. });