Script.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. MWF.xApplication = MWF.xApplication || {};
  2. MWF.xApplication.portal = MWF.xApplication.portal || {};
  3. MWF.xApplication.portal.ScriptDesigner = MWF.xApplication.portal.ScriptDesigner || {};
  4. MWF.require("MWF.widget.Common", null, false);
  5. MWF.xDesktop.requireApp("portal.ScriptDesigner", "lp."+MWF.language, null, false);
  6. MWF.require("MWF.widget.JavascriptEditor", null, false);
  7. MWF.xApplication.portal.ScriptDesigner.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. }else{
  74. this.loadEditor();
  75. }
  76. this.setAreaNodeSize();
  77. }.bind(this));
  78. var _self = this;
  79. this.page.addEvent("queryClose", function(){
  80. if (_self.autoSaveTimerID) window.clearInterval(_self.autoSaveTimerID);
  81. this.showIm();
  82. //if (this.isChanged) this.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. if (this.options.showTab) this.page.showTabIm();
  87. },
  88. loadEditor:function(){
  89. this.editor = new MWF.widget.JavascriptEditor(this.areaNode, {"option": {"value": this.data.text}});
  90. this.editor.load(function(){
  91. if (this.data.text){
  92. this.editor.setValue(this.data.text);
  93. }
  94. this.editor.addEditorEvent("change", 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.editor.on("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. 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. options = this.designer.monacoStyleSelectNode.options;
  148. for (var i=0; i<options.length; i++){
  149. var option = options[i];
  150. if (option.value==this.editor.theme){
  151. option.set("selected", true);
  152. break;
  153. }
  154. }
  155. if (this.editor.options.type=="ace"){
  156. this.designer.monacoStyleSelectNode.hide();
  157. this.designer.styleSelectNode.show();
  158. }else{
  159. this.designer.monacoStyleSelectNode.show();
  160. this.designer.styleSelectNode.hide();
  161. }
  162. }.bind(this));
  163. },
  164. showReferenceMenu: function(){
  165. var pos = this.editor.getCursorPixelPosition();
  166. var e = {"page": {}};
  167. e.page.x = pos.left;
  168. e.page.y = pos.top;
  169. this.scriptReferenceMenu.menu.showIm(e);
  170. },
  171. setIncludeNode: function(){
  172. this.designer.propertyIncludeListArea.empty();
  173. this.data.dependScriptList.each(function(name){
  174. this.designer.addIncludeToList(name);
  175. }.bind(this));
  176. },
  177. setPropertyContent: function(){
  178. this.designer.propertyIdNode.set("text", this.data.id || "");
  179. this.designer.propertyNameNode.set("value", this.data.name || "");
  180. this.designer.propertyAliasNode.set("value", this.data.alias || "");
  181. this.designer.propertyDescriptionNode.set("value", this.data.description || "");
  182. },
  183. setAreaNodeSize: function(){
  184. if( !this.areaNode.offsetParent )return;
  185. //var size = this.node.getSize();
  186. var size = this.node.getComputedSize();
  187. size.y = size.height;
  188. var tabSize = this.tab.tabNodeContainer.getSize();
  189. var y = size.y - tabSize.y;
  190. this.areaNode.setStyle("height", ""+y+"px");
  191. if (this.editor) this.editor.resize(y);
  192. },
  193. addInclude: function(){
  194. },
  195. save: 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 validated = this.editor.validated();
  207. var name = this.designer.propertyNameNode.get("value");
  208. var alias = this.designer.propertyAliasNode.get("value");
  209. var description = this.designer.propertyDescriptionNode.get("value");
  210. if (!name){
  211. this.designer.notice(this.designer.lp.notice.inputName, "error");
  212. return false;
  213. }
  214. this.data.name = name;
  215. this.data.alias = alias;
  216. this.data.description = description;
  217. this.data.validated = validated;
  218. this.data.text = this.editor.getValue();
  219. this.isSave = true;
  220. this.designer.actions.saveScript(this.data, function(json){
  221. this.isSave = false;
  222. this.data.isNewScript = false;
  223. this.isChanged = false;
  224. this.page.textNode.set("text", this.data.name);
  225. if (this.lisNode) {
  226. this.lisNode.getLast().set("text", this.data.name);
  227. }
  228. this.designer.notice(this.designer.lp.notice.save_success, "success", this.node, {"x": "left", "y": "bottom"});
  229. this.data.id = json.data.id;
  230. if (callback) callback();
  231. }.bind(this), function(xhr, text, error){
  232. this.isSave = false;
  233. var errorText = error+":"+text;
  234. if (xhr) errorText = xhr.responseText;
  235. MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  236. }.bind(this));
  237. }else{
  238. MWF.xDesktop.notice("info", {x: "right", y:"top"}, this.designer.lp.isSave);
  239. }
  240. },
  241. saveSilence: function(callback){
  242. if (!this.isSave){
  243. // var session = this.editor.editor.getSession();
  244. // var annotations = session.getAnnotations();
  245. // var validated = true;
  246. // for (var i=0; i<annotations.length; i++){
  247. // if (annotations[i].type=="error"){
  248. // validated = false;
  249. // break;
  250. // }
  251. // }
  252. var validated = this.editor.validated();
  253. if( this.designer.currentScript == this ){
  254. var name = this.designer.propertyNameNode.get("value");
  255. var alias = this.designer.propertyAliasNode.get("value");
  256. var description = this.designer.propertyDescriptionNode.get("value");
  257. if (!name){
  258. this.designer.notice(this.designer.lp.notice.inputName, "error");
  259. return false;
  260. }
  261. this.data.name = name;
  262. this.data.alias = alias;
  263. this.data.description = description;
  264. this.data.validated = validated;
  265. }
  266. this.data.text = this.editor.getValue();
  267. this.isSave = true;
  268. this.designer.actions.saveScript(this.data, function(json){
  269. this.isSave = false;
  270. this.data.isNewScript = false;
  271. this.isChanged = false;
  272. this.page.textNode.set("text", this.data.name);
  273. if (this.lisNode) {
  274. this.lisNode.getLast().set("text", this.data.name);
  275. }
  276. this.data.id = json.data.id;
  277. if (callback) callback();
  278. }.bind(this), function(xhr, text, error){
  279. this.isSave = false;
  280. //
  281. //var errorText = error+":"+text;
  282. //if (xhr) errorText = xhr.responseText;
  283. //MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  284. }.bind(this));
  285. }else{
  286. MWF.xDesktop.notice("info", {x: "right", y:"top"}, this.designer.lp.isSave);
  287. }
  288. },
  289. saveAs: function(){},
  290. explode: function(){},
  291. implode: function(){}
  292. });