Script.js 13 KB

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