Script.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. MWF.xApplication = MWF.xApplication || {};
  2. MWF.xApplication.cms = MWF.xApplication.cms || {};
  3. MWF.xApplication.cms.ScriptDesigner = MWF.xApplication.cms.ScriptDesigner || {};
  4. MWF.CMSSD = MWF.xApplication.cms.ScriptDesigner;
  5. MWF.require("MWF.widget.Common", null, false);
  6. MWF.xDesktop.requireApp("cms.ScriptDesigner", "lp."+MWF.language, null, false);
  7. MWF.require("MWF.widget.JavascriptEditor", null, false);
  8. MWF.xApplication.cms.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_cms_ScriptDesigner/$Script/";
  18. this.cssPath = "../x_component_cms_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) this.saveSilence();
  44. }
  45. }
  46. }.bind(this), 60000);
  47. },
  48. //createProperty: function(){
  49. // this.scriptPropertyNode = new Element("div", {"styles": this.css.scriptPropertyNode}).inject(this.propertyNode);
  50. //},
  51. load : function(){
  52. this.setAreaNodeSize();
  53. this.designer.addEvent("resize", this.setAreaNodeSize.bind(this));
  54. this.page = this.tab.addTab(this.areaNode, this.data.name || this.designer.lp.newScript, (!this.data.isNewScript && this.data.id!=this.designer.options.id));
  55. this.page.script = this;
  56. this.page.addEvent("show", function(){
  57. this.designer.scriptListAreaNode.getChildren().each(function(node){
  58. var scrtip = node.retrieve("script");
  59. if (scrtip.id==this.data.id){
  60. if (this.designer.currentListScriptItem){
  61. this.designer.currentListScriptItem.setStyles(this.designer.css.listScriptItem);
  62. }
  63. node.setStyles(this.designer.css.listScriptItem_current);
  64. this.designer.currentListScriptItem = node;
  65. this.lisNode = node;
  66. }
  67. }.bind(this));
  68. this.designer.currentScript = this;
  69. this.setPropertyContent();
  70. this.setIncludeNode();
  71. if (this.editor){
  72. this.editor.focus();
  73. }else{
  74. this.loadEditor();
  75. }
  76. this.setAreaNodeSize();
  77. //if (this.editor.editor){
  78. // this.editor.editor.focus();
  79. //}
  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. //this.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("change", 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.addEvent("save", function () {
  102. this.save();
  103. }.bind(this));
  104. //this.editor.addEvent("reference", function (editor, e, e1) {
  105. // if (!this.scriptReferenceMenu) {
  106. // MWF.require("MWF.widget.ScriptHelp", function () {
  107. // this.scriptReferenceMenu = new MWF.widget.ScriptHelp(null, this.editor.editor, {
  108. // "onPostLoad": function () {
  109. // this.showReferenceMenu();
  110. // }.bind(this)
  111. // });
  112. // this.scriptReferenceMenu.getEditor = function () {
  113. // return this.editor.editor;
  114. // }.bind(this)
  115. // }.bind(this));
  116. // } else {
  117. // this.showReferenceMenu();
  118. // }
  119. //}.bind(this));
  120. if (this.designer.styleSelectNode) {
  121. var options = this.designer.styleSelectNode.options;
  122. for (var i = 0; i < options.length; i++) {
  123. var option = options[i];
  124. if (option.value == this.editor.theme) {
  125. option.set("selected", true);
  126. break;
  127. }
  128. }
  129. }
  130. if(this.designer.fontsizeSelectNode){
  131. var 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. }
  140. if(this.designer.editorSelectNode) {
  141. var options = this.designer.editorSelectNode.options;
  142. for (var i = 0; i < options.length; i++) {
  143. var option = options[i];
  144. if (option.value == this.editor.options.type) {
  145. option.set("selected", true);
  146. break;
  147. }
  148. }
  149. }
  150. if (this.designer.monacoStyleSelectNode){
  151. options = this.designer.monacoStyleSelectNode.options;
  152. for (var i=0; i<options.length; i++){
  153. var option = options[i];
  154. if (option.value==this.editor.theme){
  155. option.set("selected", true);
  156. break;
  157. }
  158. }
  159. }
  160. if (this.designer.styleSelectNode && this.designer.monacoStyleSelectNode){
  161. if (this.editor.options.type=="ace"){
  162. this.designer.monacoStyleSelectNode.hide();
  163. this.designer.styleSelectNode.show();
  164. }else{
  165. this.designer.monacoStyleSelectNode.show();
  166. this.designer.styleSelectNode.hide();
  167. }
  168. }
  169. }.bind(this));
  170. },
  171. showReferenceMenu: function(){
  172. var pos = this.editor.getCursorPixelPosition();
  173. var e = {"page": {}};
  174. e.page.x = pos.left;
  175. e.page.y = pos.top;
  176. this.scriptReferenceMenu.menu.showIm(e);
  177. },
  178. setIncludeNode: function(){
  179. this.designer.propertyIncludeListArea.empty();
  180. this.data.dependScriptList.each(function(name){
  181. this.designer.addIncludeToList(name);
  182. }.bind(this));
  183. },
  184. setPropertyContent: function(){
  185. this.designer.propertyIdNode.set("text", this.data.id || "");
  186. this.designer.propertyNameNode.set("value", this.data.name || "");
  187. this.designer.propertyAliasNode.set("value", this.data.alias || "");
  188. this.designer.propertyDescriptionNode.set("value", this.data.description || "");
  189. },
  190. setAreaNodeSize: function(){
  191. if( !this.areaNode.offsetParent )return;
  192. //var size = this.node.getSize();
  193. var size = this.node.getComputedSize();
  194. size.y = size.height;
  195. var tabSize = this.tab.tabNodeContainer.getSize();
  196. var y = size.y - tabSize.y;
  197. this.areaNode.setStyle("height", ""+y+"px");
  198. if (this.editor) this.editor.resize(y);
  199. //if (this.editor) if (this.editor.editor && this.editor.editor.resize) this.editor.editor.resize();
  200. },
  201. addInclude: function(){
  202. },
  203. save: function(callback){
  204. // var session = this.editor.editor.getSession();
  205. // var annotations = session.getAnnotations();
  206. // var validated = true;
  207. // for (var i=0; i<annotations.length; i++){
  208. // if (annotations[i].type=="error"){
  209. // validated = false;
  210. // break;
  211. // }
  212. // }
  213. var validated = this.editor.validated();
  214. var name = this.designer.propertyNameNode.get("value");
  215. var alias = this.designer.propertyAliasNode.get("value");
  216. var description = this.designer.propertyDescriptionNode.get("value");
  217. if (!name){
  218. this.designer.notice(this.designer.lp.notice.inputName, "error");
  219. return false;
  220. }
  221. this.data.name = name;
  222. this.data.alias = alias;
  223. this.data.description = description;
  224. this.data.validated = validated;
  225. //this.data.text = this.editor.editor.getValue();
  226. this.data.text = this.editor.getValue();
  227. this.designer.actions.saveScript(this.data, function(json){
  228. this.data.isNewScript = false;
  229. this.isChanged = false;
  230. this.page.textNode.set("text", this.data.name);
  231. if (this.lisNode) {
  232. this.lisNode.getLast().set("text", this.data.name);
  233. }
  234. this.designer.notice(this.designer.lp.notice.save_success, "success", this.node, {"x": "left", "y": "bottom"});
  235. this.data.id = json.data.id;
  236. if (callback) callback();
  237. }.bind(this));
  238. },
  239. saveSilence: function(callback){
  240. // var session = this.editor.editor.getSession();
  241. // var annotations = session.getAnnotations();
  242. // var validated = true;
  243. // for (var i=0; i<annotations.length; i++){
  244. // if (annotations[i].type=="error"){
  245. // validated = false;
  246. // break;
  247. // }
  248. // }
  249. var validated = this.editor.validated();
  250. if( this.designer.currentScript == this ) {
  251. var name = this.designer.propertyNameNode.get("value");
  252. var alias = this.designer.propertyAliasNode.get("value");
  253. var description = this.designer.propertyDescriptionNode.get("value");
  254. if (!name) {
  255. this.designer.notice(this.designer.lp.notice.inputName, "error");
  256. return false;
  257. }
  258. this.data.name = name;
  259. this.data.alias = alias;
  260. this.data.description = description;
  261. this.data.validated = validated;
  262. }
  263. //this.data.text = this.editor.editor.getValue();
  264. this.data.text = this.editor.getValue();
  265. this.designer.actions.saveScript(this.data, function(json){
  266. this.data.isNewScript = false;
  267. this.isChanged = false;
  268. this.page.textNode.set("text", this.data.name);
  269. if (this.lisNode) {
  270. this.lisNode.getLast().set("text", this.data.name);
  271. }
  272. this.data.id = json.data.id;
  273. if (callback) callback();
  274. }.bind(this));
  275. },
  276. saveAs: function(){},
  277. explode: function(){},
  278. implode: function(){}
  279. });