Script.js 13 KB

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