Agent.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. MWF.xApplication = MWF.xApplication || {};
  2. MWF.xApplication.service = MWF.xApplication.service || {};
  3. MWF.xApplication.service.AgentDesigner = MWF.xApplication.service.AgentDesigner || {};
  4. MWF.SRVAD = MWF.xApplication.service.AgentDesigner;
  5. MWF.require("MWF.widget.Common", null, false);
  6. MWF.xDesktop.requireApp("service.AgentDesigner", "lp."+MWF.language, null, false);
  7. MWF.require("MWF.widget.JavascriptEditor", null, false);
  8. MWF.xApplication.service.AgentDesigner.Agent = 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_service_AgentDesigner/$Agent/";
  18. this.cssPath = "/x_component_service_AgentDesigner/$Agent/"+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.agentTab;
  26. this.areaNode = new Element("div", {"styles": {"overflow": "hidden", "height": "700px"}});
  27. //this.propertyIncludeNode = this.designer.propertyDomArea;
  28. this.propertyNode = this.designer.propertyContentArea;
  29. this.isNewAgent = (this.data.id) ? false : true;
  30. // this.createProperty();
  31. this.autoSave();
  32. this.designer.addEvent("queryClose", function(){
  33. if (this.autoSaveTimerID) window.clearInterval(this.autoSaveTimerID);
  34. }.bind(this));
  35. },
  36. autoSave: function(){
  37. this.autoSaveTimerID = window.setInterval(function(){
  38. if (!this.autoSaveCheckNode) this.autoSaveCheckNode = this.designer.contentToolbarNode.getElement("#MWFAgentAutoSaveCheck");
  39. if (this.autoSaveCheckNode){
  40. if (this.autoSaveCheckNode.get("checked")){
  41. if (this.isChanged) this.saveSilence();
  42. }
  43. }
  44. }.bind(this), 60000);
  45. },
  46. //createProperty: function(){
  47. // this.agentPropertyNode = new Element("div", {"styles": this.css.agentPropertyNode}).inject(this.propertyNode);
  48. //},
  49. load : function(){
  50. this.setAreaNodeSize();
  51. this.designer.addEvent("resize", this.setAreaNodeSize.bind(this));
  52. this.page = this.tab.addTab(this.areaNode, this.data.name || this.designer.lp.newAgent, (!this.data.isNewAgent && this.data.id!=this.designer.options.id));
  53. this.page.agent = this;
  54. this.page.addEvent("show", function(){
  55. this.designer.agentListAreaNode.getChildren().each(function(node){
  56. var scrtip = node.retrieve("agent");
  57. if (scrtip.id==this.data.id){
  58. if (this.designer.currentListAgentItem){
  59. this.designer.currentListAgentItem.setStyles(this.designer.css.listAgentItem);
  60. }
  61. node.setStyles(this.designer.css.listAgentItem_current);
  62. this.designer.currentListAgentItem = node;
  63. this.lisNode = node;
  64. }
  65. }.bind(this));
  66. this.setPropertyContent();
  67. //this.setIncludeNode();
  68. if (this.editor.editor){
  69. this.editor.editor.focus();
  70. this.editor.editor.navigateFileStart();
  71. }
  72. }.bind(this));
  73. this.page.addEvent("queryClose", function(){
  74. if (this.autoSaveTimerID) window.clearInterval(this.autoSaveTimerID);
  75. this.saveSilence();
  76. if (this.lisNode) this.lisNode.setStyles(this.designer.css.listAgentItem);
  77. }.bind(this));
  78. this.page.tabNode.addEvent("dblclick", this.designer.maxOrReturnEditor.bind(this.designer));
  79. this.editor = new MWF.widget.JavascriptEditor(this.areaNode);
  80. this.editor.load(function(){
  81. if (this.data.text){
  82. this.editor.editor.setValue(this.data.text);
  83. }else{
  84. var defaultText = "/********************\n";
  85. defaultText += "resources.getEntityManagerContainer(); //实体管理器\n";
  86. defaultText += "resources.getContext(); //上下文根\n";
  87. defaultText += "resources.getOrganization(); //组织访问\n";
  88. defaultText += "resources.getWebservicesClient();//webSerivces客户端\n";
  89. defaultText += "********************/\n";
  90. this.editor.editor.setValue(defaultText);
  91. }
  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.agentReferenceMenu){
  103. MWF.require("MWF.widget.ScriptHelp", function(){
  104. this.agentReferenceMenu = new MWF.widget.ScriptHelp(null, this.editor.editor, {
  105. "onPostLoad": function(){
  106. this.showReferenceMenu();
  107. }.bind(this)
  108. });
  109. this.agentReferenceMenu.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. }.bind(this));
  124. if (this.options.showTab) this.page.showTabIm();
  125. },
  126. showReferenceMenu: function(){
  127. var pos = this.editor.getCursorPixelPosition();
  128. var e = {"page": {}};
  129. e.page.x = pos.left;
  130. e.page.y = pos.top;
  131. this.agentReferenceMenu.menu.showIm(e);
  132. },
  133. setIncludeNode: function(){
  134. this.designer.propertyIncludeListArea.empty();
  135. this.data.dependAgentList.each(function(name){
  136. this.designer.addIncludeToList(name);
  137. }.bind(this));
  138. },
  139. setPropertyContent: function(){
  140. this.designer.propertyIdNode.set("text", this.data.id || "");
  141. this.designer.propertyNameNode.set("value", this.data.name || "");
  142. this.designer.propertyAliasNode.set("value", this.data.alias || "");
  143. this.designer.propertyEnableNode.set("text", this.data.enable ? this.designer.lp.true : this.designer.lp.false );
  144. this.designer.propertyCronNode.set("value", this.data.cron || "");
  145. this.designer.cronValue = this.data.cron || "";
  146. //this.designer.cronPicker.setCronValue( this.data.cron || "" );
  147. this.designer.propertyLastStartTimeNode.set("text", this.data.lastStartTime || "");
  148. this.designer.propertyLastEndTimeNode.set("text", this.data.lastEndTime || "");
  149. this.designer.propertyDescriptionNode.set("value", this.data.description || "");
  150. this.setButton()
  151. },
  152. setButton : function(){
  153. this.designer.propertyEnableButton.store("id", this.data.id);
  154. this.designer.propertyDisableButton.store("id", this.data.id);
  155. if( this.data.enable ){
  156. this.designer.propertyEnableButton.setStyle("display","none");
  157. this.designer.propertyDisableButton.setStyle("display", this.data.isNewAgent ? "none" : "" );
  158. }else{
  159. this.designer.propertyEnableButton.setStyle("display",this.data.isNewAgent ? "none" : "");
  160. this.designer.propertyDisableButton.setStyle("display", "none" );
  161. }
  162. },
  163. setAreaNodeSize: function(){
  164. var size = this.node.getSize();
  165. var tabSize = this.tab.tabNodeContainer.getSize();
  166. var y = size.y - tabSize.y;
  167. this.areaNode.setStyle("height", ""+y+"px");
  168. if (this.editor) if (this.editor.editor) this.editor.editor.resize();
  169. },
  170. addInclude: function(){
  171. },
  172. saveAgent: function (data, success, failure) {
  173. if (data.isNewAgent) {
  174. this.designer.actions.createAgent(data, success, failure);
  175. } else {
  176. this.designer.actions.updateAgent(data.id, data, success, failure);
  177. }
  178. },
  179. save: function(callback){
  180. debugger;
  181. if (!this.isSave){
  182. var session = this.editor.editor.getSession();
  183. var annotations = session.getAnnotations();
  184. var validated = true;
  185. for (var i=0; i<annotations.length; i++){
  186. if (annotations[i].type=="error"){
  187. validated = false;
  188. break;
  189. }
  190. }
  191. var name = this.designer.propertyNameNode.get("value");
  192. var alias = this.designer.propertyAliasNode.get("value");
  193. var description = this.designer.propertyDescriptionNode.get("value");
  194. var cron = this.designer.propertyCronNode.get("value");
  195. if (!name){
  196. this.designer.notice(this.designer.lp.notice.inputName, "error");
  197. return false;
  198. }
  199. if(!cron){
  200. this.designer.notice(this.designer.lp.notice.inputCron, "error");
  201. return false;
  202. }
  203. this.data.name = name;
  204. this.data.alias = alias;
  205. this.data.description = description;
  206. this.data.cron = cron;
  207. this.data.validated = validated;
  208. this.data.text = this.editor.editor.getValue();
  209. this.isSave = true;
  210. this.saveAgent(this.data, function(json){
  211. this.isSave = false;
  212. if( this.data.isNewAgent ){
  213. this.data.isNewAgent = false;
  214. this.setButton();
  215. }
  216. this.isChanged = false;
  217. this.page.textNode.set("text", this.data.name);
  218. if (this.lisNode) {
  219. this.lisNode.getLast().set("text", this.data.name);
  220. }
  221. this.designer.notice(this.designer.lp.notice.save_success, "success", this.node, {"x": "left", "y": "bottom"});
  222. this.data.id = json.data.id;
  223. this.designer.propertyIdNode.set("text", this.data.id );
  224. if (callback) callback();
  225. }.bind(this), function(xhr, text, error){
  226. this.isSave = false;
  227. var errorText = error+":"+text;
  228. if (xhr) errorText = xhr.responseText;
  229. MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  230. }.bind(this));
  231. }else{
  232. MWF.xDesktop.notice("info", {x: "right", y:"top"}, this.designer.lp.isSave);
  233. }
  234. },
  235. saveSilence: function(callback){
  236. if (!this.isSave){
  237. var session = this.editor.editor.getSession();
  238. var annotations = session.getAnnotations();
  239. var validated = true;
  240. for (var i=0; i<annotations.length; i++){
  241. if (annotations[i].type=="error"){
  242. validated = false;
  243. break;
  244. }
  245. }
  246. var name = this.designer.propertyNameNode.get("value");
  247. var alias = this.designer.propertyAliasNode.get("value");
  248. var description = this.designer.propertyDescriptionNode.get("value");
  249. var cron = this.designer.propertyCronNode.get("value");
  250. if (!name){
  251. this.designer.notice(this.designer.lp.notice.inputName, "error");
  252. return false;
  253. }
  254. if(!cron){
  255. this.designer.notice(this.designer.lp.notice.inputCron, "error");
  256. return false;
  257. }
  258. this.data.name = name;
  259. this.data.alias = alias;
  260. this.data.description = description;
  261. this.data.cron = cron;
  262. this.data.validated = validated;
  263. this.data.text = this.editor.editor.getValue();
  264. this.isSave = true;
  265. this.saveAgent(this.data, function(json){
  266. this.isSave = false;
  267. if( this.data.isNewAgent ){
  268. this.data.isNewAgent = false;
  269. this.setButton();
  270. }
  271. this.data.isNewAgent = false;
  272. this.isChanged = false;
  273. this.page.textNode.set("text", this.data.name);
  274. if (this.lisNode) {
  275. this.lisNode.getLast().set("text", this.data.name);
  276. }
  277. this.data.id = json.data.id;
  278. this.designer.propertyIdNode.set("text", this.data.id );
  279. if (callback) callback();
  280. }.bind(this), function(xhr, text, error){
  281. this.isSave = false;
  282. //
  283. //var errorText = error+":"+text;
  284. //if (xhr) errorText = xhr.responseText;
  285. //MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  286. }.bind(this));
  287. }else{
  288. MWF.xDesktop.notice("info", {x: "right", y:"top"}, this.designer.lp.isSave);
  289. }
  290. },
  291. saveAs: function(){},
  292. explode: function(){},
  293. implode: function(){}
  294. });