Script.js 11 KB

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