Script.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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), 6000);
  49. },
  50. //createProperty: function(){
  51. // this.scriptPropertyNode = new Element("div", {"styles": this.css.scriptPropertyNode}).inject(this.propertyNode);
  52. //},
  53. load : function(){
  54. this.setAreaNodeSize();
  55. this.designer.addEvent("resize", this.setAreaNodeSize.bind(this));
  56. this.page = this.tab.addTab(this.areaNode, this.data.name || this.designer.lp.newScript, (!this.data.isNewScript && this.data.id!=this.designer.options.id));
  57. this.page.script = this;
  58. this.page.addEvent("show", function(){
  59. this.designer.scriptListAreaNode.getChildren().each(function(node){
  60. var scrtip = node.retrieve("script");
  61. if (scrtip.id==this.data.id){
  62. if (this.designer.currentListScriptItem){
  63. this.designer.currentListScriptItem.setStyles(this.designer.css.listScriptItem);
  64. }
  65. node.setStyles(this.designer.css.listScriptItem_current);
  66. this.designer.currentListScriptItem = node;
  67. this.lisNode = node;
  68. }
  69. }.bind(this));
  70. this.setPropertyContent();
  71. this.setIncludeNode();
  72. if (this.editor.editor){
  73. this.editor.editor.focus();
  74. this.editor.editor.navigateFileStart();
  75. }
  76. }.bind(this));
  77. this.page.addEvent("queryClose", function(){
  78. if (this.autoSaveTimerID) window.clearInterval(this.autoSaveTimerID);
  79. this.saveSilence();
  80. if (this.lisNode) this.lisNode.setStyles(this.designer.css.listScriptItem);
  81. }.bind(this))
  82. this.page.tabNode.addEvent("dblclick", this.designer.maxOrReturnEditor.bind(this.designer));
  83. this.editor = new MWF.widget.JavascriptEditor(this.areaNode);
  84. this.editor.load(function(){
  85. if (this.data.text){
  86. this.editor.editor.setValue(this.data.text);
  87. }
  88. this.editor.editor.on("change", function(e){
  89. if (!this.isChanged){
  90. this.isChanged = true;
  91. this.page.textNode.set("text", " * "+this.page.textNode.get("text"));
  92. }
  93. }.bind(this));
  94. this.editor.addEvent("save", function(){
  95. this.save();
  96. }.bind(this));
  97. this.editor.addEvent("reference", function(editor, e, e1){
  98. if (!this.scriptReferenceMenu){
  99. MWF.require("MWF.widget.ScriptHelp", function(){
  100. this.scriptReferenceMenu = new MWF.widget.ScriptHelp(null, this.editor.editor, {
  101. "onPostLoad": function(){
  102. this.showReferenceMenu();
  103. }.bind(this)
  104. });
  105. this.scriptReferenceMenu.getEditor = function(){return this.editor.editor;}.bind(this)
  106. }.bind(this));
  107. }else{
  108. this.showReferenceMenu();
  109. }
  110. }.bind(this));
  111. var options = this.designer.styleSelectNode.options;
  112. for (var i=0; i<options.length; i++){
  113. var option = options[i];
  114. if (option.value==this.editor.theme){
  115. option.set("selected", true);
  116. break;
  117. }
  118. }
  119. }.bind(this));
  120. if (this.options.showTab) this.page.showTabIm();
  121. },
  122. showReferenceMenu: function(){
  123. var pos = this.editor.getCursorPixelPosition();
  124. var e = {"page": {}};
  125. e.page.x = pos.left;
  126. e.page.y = pos.top;
  127. this.scriptReferenceMenu.menu.showIm(e);
  128. },
  129. setIncludeNode: function(){
  130. this.designer.propertyIncludeListArea.empty();
  131. this.data.dependScriptList.each(function(name){
  132. this.designer.addIncludeToList(name);
  133. }.bind(this));
  134. },
  135. setPropertyContent: function(){
  136. this.designer.propertyIdNode.set("text", this.data.id || "");
  137. this.designer.propertyNameNode.set("value", this.data.name || "");
  138. this.designer.propertyAliasNode.set("value", this.data.alias || "");
  139. this.designer.propertyDescriptionNode.set("value", this.data.description || "");
  140. },
  141. setAreaNodeSize: function(){
  142. var size = this.node.getSize();
  143. var tabSize = this.tab.tabNodeContainer.getSize();
  144. var y = size.y - tabSize.y;
  145. this.areaNode.setStyle("height", ""+y+"px");
  146. if (this.editor) if (this.editor.editor) this.editor.editor.resize();
  147. },
  148. addInclude: function(){
  149. },
  150. save: function(callback){
  151. if (!this.isSave){
  152. var session = this.editor.editor.getSession();
  153. var annotations = session.getAnnotations();
  154. var validated = true;
  155. for (var i=0; i<annotations.length; i++){
  156. if (annotations[i].type=="error"){
  157. validated = false;
  158. break;
  159. }
  160. }
  161. var name = this.designer.propertyNameNode.get("value");
  162. var alias = this.designer.propertyAliasNode.get("value");
  163. var description = this.designer.propertyDescriptionNode.get("value");
  164. if (!name){
  165. this.designer.notice(this.designer.lp.notice.inputName, "error");
  166. return false;
  167. }
  168. this.data.name = name;
  169. this.data.alias = alias;
  170. this.data.description = description;
  171. this.data.validated = validated;
  172. this.data.text = this.editor.editor.getValue();
  173. this.isSave = true;
  174. this.designer.actions.saveScript(this.data, function(json){
  175. this.isSave = false;
  176. this.data.isNewScript = false;
  177. this.isChanged = false;
  178. this.page.textNode.set("text", this.data.name);
  179. if (this.lisNode) {
  180. this.lisNode.getLast().set("text", this.data.name);
  181. }
  182. this.designer.notice(this.designer.lp.notice.save_success, "success", this.node, {"x": "left", "y": "bottom"});
  183. this.data.id = json.data.id;
  184. if (callback) callback();
  185. }.bind(this), function(xhr, text, error){
  186. this.isSave = false;
  187. var errorText = error+":"+text;
  188. if (xhr) errorText = xhr.responseText;
  189. MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  190. }.bind(this));
  191. }else{
  192. MWF.xDesktop.notice("info", {x: "right", y:"top"}, this.designer.lp.isSave);
  193. }
  194. },
  195. saveSilence: function(callback){
  196. if (!this.isSave){
  197. var session = this.editor.editor.getSession();
  198. var annotations = session.getAnnotations();
  199. var validated = true;
  200. for (var i=0; i<annotations.length; i++){
  201. if (annotations[i].type=="error"){
  202. validated = false;
  203. break;
  204. }
  205. }
  206. var name = this.designer.propertyNameNode.get("value");
  207. var alias = this.designer.propertyAliasNode.get("value");
  208. var description = this.designer.propertyDescriptionNode.get("value");
  209. if (!name){
  210. this.designer.notice(this.designer.lp.notice.inputName, "error");
  211. return false;
  212. }
  213. this.data.name = name;
  214. this.data.alias = alias;
  215. this.data.description = description;
  216. this.data.validated = validated;
  217. this.data.text = this.editor.editor.getValue();
  218. this.isSave = true;
  219. this.designer.actions.saveScript(this.data, function(json){
  220. this.isSave = false;
  221. this.data.isNewScript = false;
  222. this.isChanged = false;
  223. this.page.textNode.set("text", this.data.name);
  224. if (this.lisNode) {
  225. this.lisNode.getLast().set("text", this.data.name);
  226. }
  227. this.data.id = json.data.id;
  228. if (callback) callback();
  229. }.bind(this), function(xhr, text, error){
  230. this.isSave = false;
  231. //
  232. //var errorText = error+":"+text;
  233. //if (xhr) errorText = xhr.responseText;
  234. //MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  235. }.bind(this));
  236. }else{
  237. MWF.xDesktop.notice("info", {x: "right", y:"top"}, this.designer.lp.isSave);
  238. }
  239. },
  240. saveAs: function(){},
  241. explode: function(){},
  242. implode: function(){}
  243. });