Script.js 12 KB

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