Script.js 12 KB

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