Script.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. this.setAreaNodeSize();
  80. }.bind(this));
  81. var _self = this;
  82. this.page.addEvent("queryClose", function(){
  83. if (_self.autoSaveTimerID) window.clearInterval(_self.autoSaveTimerID);
  84. this.showIm();
  85. //_self.saveSilence();
  86. if (_self.lisNode) _self.lisNode.setStyles(_self.designer.css.listScriptItem);
  87. });
  88. this.page.tabNode.addEvent("dblclick", this.designer.maxOrReturnEditor.bind(this.designer));
  89. if (this.options.showTab) this.page.showTabIm();
  90. },
  91. loadEditor:function(){
  92. this.editor = new MWF.widget.JavascriptEditor(this.areaNode, {"option": {"value": this.data.text}});
  93. this.editor.load(function(){
  94. if (this.data.text) this.editor.setValue(this.data.text);
  95. // this.editor.addEditorEvent("onDidChangeModelContent", function(e){
  96. // if (!this.isChanged){
  97. // this.isChanged = true;
  98. // this.page.textNode.set("text", " * "+this.page.textNode.get("text"));
  99. // }
  100. // }.bind(this));
  101. this.editor.addEditorEvent("change", function(e){
  102. if (!this.isChanged){
  103. this.isChanged = true;
  104. this.page.textNode.set("text", " * "+this.page.textNode.get("text"));
  105. }
  106. }.bind(this));
  107. this.editor.addEvent("save", function(){
  108. this.save();
  109. }.bind(this));
  110. // this.editor.addEvent("reference", function(editor, e, e1){
  111. // if (!this.scriptReferenceMenu){
  112. // MWF.require("MWF.widget.ScriptHelp", function(){
  113. // this.scriptReferenceMenu = new MWF.widget.ScriptHelp(null, this.editor.editor, {
  114. // "onPostLoad": function(){
  115. // this.showReferenceMenu();
  116. // }.bind(this)
  117. // });
  118. // this.scriptReferenceMenu.getEditor = function(){return this.editor.editor;}.bind(this)
  119. // }.bind(this));
  120. // }else{
  121. // this.showReferenceMenu();
  122. // }
  123. // }.bind(this));
  124. var options = this.designer.styleSelectNode.options;
  125. for (var i=0; i<options.length; i++){
  126. var option = options[i];
  127. if (option.value==this.editor.theme){
  128. option.set("selected", true);
  129. break;
  130. }
  131. }
  132. var options = this.designer.fontsizeSelectNode.options;
  133. for (var i=0; i<options.length; i++){
  134. var option = options[i];
  135. if (option.value==this.editor.fontSize){
  136. option.set("selected", true);
  137. break;
  138. }
  139. }
  140. options = this.designer.editorSelectNode.options;
  141. for (var i=0; i<options.length; i++){
  142. var option = options[i];
  143. if (option.value==this.editor.options.type){
  144. option.set("selected", true);
  145. break;
  146. }
  147. }
  148. options = this.designer.monacoStyleSelectNode.options;
  149. for (var i=0; i<options.length; i++){
  150. var option = options[i];
  151. if (option.value==this.editor.theme){
  152. option.set("selected", true);
  153. break;
  154. }
  155. }
  156. if (this.editor.options.type=="ace"){
  157. this.designer.monacoStyleSelectNode.hide();
  158. this.designer.styleSelectNode.show();
  159. }else{
  160. this.designer.monacoStyleSelectNode.show();
  161. this.designer.styleSelectNode.hide();
  162. }
  163. }.bind(this));
  164. },
  165. showReferenceMenu: function(){
  166. var pos = this.editor.getCursorPixelPosition();
  167. var e = {"page": {}};
  168. e.page.x = pos.left;
  169. e.page.y = pos.top;
  170. this.scriptReferenceMenu.menu.showIm(e);
  171. },
  172. setIncludeNode: function(){
  173. this.designer.propertyIncludeListArea.empty();
  174. this.data.dependScriptList.each(function(name){
  175. this.designer.addIncludeToList(name);
  176. }.bind(this));
  177. },
  178. setPropertyContent: function(){
  179. this.designer.propertyIdNode.set("text", this.data.id || "");
  180. this.designer.propertyNameNode.set("value", this.data.name || "");
  181. this.designer.propertyAliasNode.set("value", this.data.alias || "");
  182. this.designer.propertyDescriptionNode.set("value", this.data.description || "");
  183. },
  184. setAreaNodeSize: function(){
  185. if( !this.areaNode.offsetParent )return;
  186. //var size = this.node.getSize();
  187. var size = this.node.getComputedSize();
  188. size.y = size.height;
  189. var tabSize = this.tab.tabNodeContainer.getSize();
  190. var y = size.y - tabSize.y;
  191. this.areaNode.setStyle("height", ""+y+"px");
  192. if (this.editor) this.editor.resize(y);
  193. },
  194. addInclude: function(){
  195. },
  196. save: function(callback){
  197. if (!this.isSave){
  198. debugger;
  199. // var m = monaco.editor.getModelMarkers();
  200. // var mod = this.editor.editor.getModel();
  201. // var ms = monaco.editor.getModelMarkers({"resource": mod.uri});
  202. //
  203. // var session = this.editor.editor.getSession();
  204. // var annotations = session.getAnnotations();
  205. var validated = this.editor.validated();
  206. // for (var i=0; i<annotations.length; i++){
  207. // if (annotations[i].type=="error"){
  208. // validated = false;
  209. // break;
  210. // }
  211. // }
  212. var name = this.designer.propertyNameNode.get("value");
  213. var alias = this.designer.propertyAliasNode.get("value");
  214. var description = this.designer.propertyDescriptionNode.get("value");
  215. if (!name){
  216. this.designer.notice(this.designer.lp.notice.inputName, "error");
  217. return false;
  218. }
  219. this.data.name = name;
  220. this.data.alias = alias;
  221. this.data.description = description;
  222. this.data.validated = validated;
  223. this.data.text = this.editor.getValue();
  224. this.isSave = true;
  225. this.designer.actions.saveScript(this.data, function(json){
  226. this.isSave = false;
  227. this.data.isNewScript = false;
  228. this.isChanged = false;
  229. this.page.textNode.set("text", this.data.name);
  230. if (this.lisNode) {
  231. this.lisNode.getLast().set("text", this.data.name);
  232. }
  233. this.designer.notice(this.designer.lp.notice.save_success, "success", this.node, {"x": "left", "y": "bottom"});
  234. this.data.id = json.data.id;
  235. if (callback) callback();
  236. }.bind(this), function(xhr, text, error){
  237. this.isSave = false;
  238. var errorText = error+":"+text;
  239. if (xhr) errorText = xhr.responseText;
  240. MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  241. }.bind(this));
  242. }else{
  243. MWF.xDesktop.notice("info", {x: "right", y:"top"}, this.designer.lp.isSave);
  244. }
  245. },
  246. saveSilence: function(callback){
  247. if (!this.isSave){
  248. // var session = this.editor.editor.getSession();
  249. // var annotations = session.getAnnotations();
  250. // var validated = true;
  251. // for (var i=0; i<annotations.length; i++){
  252. // if (annotations[i].type=="error"){
  253. // validated = false;
  254. // break;
  255. // }
  256. // }
  257. var validated = this.editor.validated();
  258. if( this.designer.currentScript == this ) {
  259. var name = this.designer.propertyNameNode.get("value");
  260. var alias = this.designer.propertyAliasNode.get("value");
  261. var description = this.designer.propertyDescriptionNode.get("value");
  262. if (!name) {
  263. this.designer.notice(this.designer.lp.notice.inputName, "error");
  264. return false;
  265. }
  266. this.data.name = name;
  267. this.data.alias = alias;
  268. this.data.description = description;
  269. this.data.validated = validated;
  270. }
  271. this.data.text = this.editor.getValue();
  272. this.isSave = true;
  273. this.designer.actions.saveScript(this.data, function(json){
  274. this.isSave = false;
  275. this.data.isNewScript = false;
  276. this.isChanged = false;
  277. this.page.textNode.set("text", this.data.name);
  278. if (this.lisNode) {
  279. this.lisNode.getLast().set("text", this.data.name);
  280. }
  281. this.data.id = json.data.id;
  282. if (callback) callback();
  283. }.bind(this), function(xhr, text, error){
  284. this.isSave = false;
  285. //
  286. //var errorText = error+":"+text;
  287. //if (xhr) errorText = xhr.responseText;
  288. //MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  289. }.bind(this));
  290. }else{
  291. MWF.xDesktop.notice("info", {x: "right", y:"top"}, this.designer.lp.isSave);
  292. }
  293. },
  294. saveAs: function(){},
  295. explode: function(){},
  296. implode: function(){}
  297. });