Agent.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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.designer.currentScript = this;
  67. this.setPropertyContent();
  68. //this.setIncludeNode();
  69. if (this.editor.editor){
  70. this.editor.editor.focus();
  71. //this.editor.editor.navigateFileStart();
  72. }
  73. }.bind(this));
  74. this.page.addEvent("queryClose", function(){
  75. if (this.autoSaveTimerID) window.clearInterval(this.autoSaveTimerID);
  76. this.saveSilence();
  77. if (this.lisNode) this.lisNode.setStyles(this.designer.css.listAgentItem);
  78. }.bind(this));
  79. this.page.tabNode.addEvent("dblclick", this.designer.maxOrReturnEditor.bind(this.designer));
  80. this.editor = new MWF.widget.JavascriptEditor(this.areaNode);
  81. this.editor.load(function(){
  82. if (this.data.text){
  83. this.editor.editor.setValue(this.data.text);
  84. }else{
  85. var defaultText = "/********************\n";
  86. defaultText += "resources.getEntityManagerContainer(); //实体管理器\n";
  87. defaultText += "resources.getContext(); //上下文根\n";
  88. defaultText += "resources.getOrganization(); //组织访问\n";
  89. defaultText += "resources.getWebservicesClient();//webSerivces客户端\n";
  90. defaultText += "********************/\n";
  91. this.editor.editor.setValue(defaultText);
  92. }
  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.agentReferenceMenu){
  104. MWF.require("MWF.widget.ScriptHelp", function(){
  105. this.agentReferenceMenu = new MWF.widget.ScriptHelp(null, this.editor.editor, {
  106. "onPostLoad": function(){
  107. this.showReferenceMenu();
  108. }.bind(this)
  109. });
  110. this.agentReferenceMenu.getEditor = function(){return this.editor.editor;}.bind(this)
  111. }.bind(this));
  112. }else{
  113. this.showReferenceMenu();
  114. }
  115. }.bind(this));
  116. var options = this.designer.styleSelectNode.options;
  117. for (var i=0; i<options.length; i++){
  118. var option = options[i];
  119. if (option.value==this.editor.theme){
  120. option.set("selected", true);
  121. break;
  122. }
  123. }
  124. }.bind(this));
  125. if (this.options.showTab) this.page.showTabIm();
  126. },
  127. showReferenceMenu: function(){
  128. var pos = this.editor.getCursorPixelPosition();
  129. var e = {"page": {}};
  130. e.page.x = pos.left;
  131. e.page.y = pos.top;
  132. this.agentReferenceMenu.menu.showIm(e);
  133. },
  134. setIncludeNode: function(){
  135. this.designer.propertyIncludeListArea.empty();
  136. this.data.dependAgentList.each(function(name){
  137. this.designer.addIncludeToList(name);
  138. }.bind(this));
  139. },
  140. setPropertyContent: function(){
  141. this.designer.propertyIdNode.set("text", this.data.id || "");
  142. this.designer.propertyNameNode.set("value", this.data.name || "");
  143. this.designer.propertyAliasNode.set("value", this.data.alias || "");
  144. this.designer.propertyEnableNode.set("text", this.data.enable ? this.designer.lp.true : this.designer.lp.false );
  145. this.designer.propertyCronNode.set("value", this.data.cron || "");
  146. this.designer.cronValue = this.data.cron || "";
  147. //this.designer.cronPicker.setCronValue( this.data.cron || "" );
  148. this.designer.propertyLastStartTimeNode.set("text", this.data.lastStartTime || "");
  149. this.designer.propertyLastEndTimeNode.set("text", this.data.lastEndTime || "");
  150. this.designer.propertyAppointmentTimeNode.set("text", this.data.appointmentTime || "");
  151. this.designer.propertyDescriptionNode.set("value", this.data.description || "");
  152. this.setButton()
  153. },
  154. setButton : function(){
  155. this.designer.propertyEnableButton.store("id", this.data.id);
  156. this.designer.propertyDisableButton.store("id", this.data.id);
  157. if( this.data.enable ){
  158. this.designer.propertyEnableButton.setStyle("display","none");
  159. this.designer.propertyDisableButton.setStyle("display", this.data.isNewAgent ? "none" : "" );
  160. }else{
  161. this.designer.propertyEnableButton.setStyle("display",this.data.isNewAgent ? "none" : "");
  162. this.designer.propertyDisableButton.setStyle("display", "none" );
  163. }
  164. },
  165. setAreaNodeSize: function(){
  166. var size = this.node.getSize();
  167. var tabSize = this.tab.tabNodeContainer.getSize();
  168. var y = size.y - tabSize.y;
  169. this.areaNode.setStyle("height", ""+y+"px");
  170. if (this.editor) if (this.editor.editor) this.editor.editor.resize();
  171. },
  172. addInclude: function(){
  173. },
  174. saveAgent: function (data, success, failure) {
  175. if (data.isNewAgent) {
  176. this.designer.actions.createAgent(data, success, failure);
  177. } else {
  178. this.designer.actions.updateAgent(data.id, data, success, failure);
  179. }
  180. },
  181. save: function(callback){
  182. if (!this.isSave){
  183. var session = this.editor.editor.getSession();
  184. var annotations = session.getAnnotations();
  185. var validated = true;
  186. for (var i=0; i<annotations.length; i++){
  187. if (annotations[i].type=="error"){
  188. validated = false;
  189. break;
  190. }
  191. }
  192. var name = this.designer.propertyNameNode.get("value");
  193. var alias = this.designer.propertyAliasNode.get("value");
  194. var description = this.designer.propertyDescriptionNode.get("value");
  195. var cron = this.designer.propertyCronNode.get("value");
  196. if (!name){
  197. this.designer.notice(this.designer.lp.notice.inputName, "error");
  198. return false;
  199. }
  200. if(!cron){
  201. this.designer.notice(this.designer.lp.notice.inputCron, "error");
  202. return false;
  203. }
  204. this.data.name = name;
  205. this.data.alias = alias;
  206. this.data.description = description;
  207. this.data.cron = cron;
  208. this.data.validated = validated;
  209. this.data.text = this.editor.editor.getValue();
  210. this.isSave = true;
  211. this.saveAgent(this.data, function(json){
  212. this.isSave = false;
  213. if( this.data.isNewAgent ){
  214. this.data.isNewAgent = false;
  215. this.setButton();
  216. }
  217. this.isChanged = false;
  218. this.page.textNode.set("text", this.data.name);
  219. if (this.lisNode) {
  220. this.lisNode.getLast().set("text", this.data.name);
  221. }
  222. this.designer.notice(this.designer.lp.notice.save_success, "success", this.node, {"x": "left", "y": "bottom"});
  223. this.data.id = json.data.id;
  224. this.designer.propertyIdNode.set("text", this.data.id );
  225. if (callback) callback();
  226. }.bind(this), function(xhr, text, error){
  227. this.isSave = false;
  228. var errorText = error+":"+text;
  229. if (xhr) errorText = xhr.responseText;
  230. MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  231. }.bind(this));
  232. }else{
  233. MWF.xDesktop.notice("info", {x: "right", y:"top"}, this.designer.lp.isSave);
  234. }
  235. },
  236. saveSilence: function(callback){
  237. if (!this.isSave){
  238. var session = this.editor.editor.getSession();
  239. var annotations = session.getAnnotations();
  240. var validated = true;
  241. for (var i=0; i<annotations.length; i++){
  242. if (annotations[i].type=="error"){
  243. validated = false;
  244. break;
  245. }
  246. }
  247. if( this.designer.currentScript == this ){
  248. var name = this.designer.propertyNameNode.get("value");
  249. var alias = this.designer.propertyAliasNode.get("value");
  250. var description = this.designer.propertyDescriptionNode.get("value");
  251. var cron = this.designer.propertyCronNode.get("value");
  252. if (!name){
  253. this.designer.notice(this.designer.lp.notice.inputName, "error");
  254. return false;
  255. }
  256. if(!cron){
  257. this.designer.notice(this.designer.lp.notice.inputCron, "error");
  258. return false;
  259. }
  260. this.data.name = name;
  261. this.data.alias = alias;
  262. this.data.description = description;
  263. this.data.cron = cron;
  264. this.data.validated = validated;
  265. }
  266. this.data.text = this.editor.editor.getValue();
  267. this.isSave = true;
  268. this.saveAgent(this.data, function(json){
  269. this.isSave = false;
  270. if( this.data.isNewAgent ){
  271. this.data.isNewAgent = false;
  272. if( this.designer.currentScript == this ) {
  273. this.setButton();
  274. }
  275. }
  276. this.data.isNewAgent = false;
  277. this.isChanged = false;
  278. this.page.textNode.set("text", this.data.name);
  279. if (this.lisNode) {
  280. this.lisNode.getLast().set("text", this.data.name);
  281. }
  282. this.data.id = json.data.id;
  283. if( this.designer.currentScript == this ) {
  284. this.designer.propertyIdNode.set("text", this.data.id);
  285. }
  286. if (callback) callback();
  287. }.bind(this), function(xhr, text, error){
  288. this.isSave = false;
  289. //
  290. //var errorText = error+":"+text;
  291. //if (xhr) errorText = xhr.responseText;
  292. //MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  293. }.bind(this));
  294. }else{
  295. MWF.xDesktop.notice("info", {x: "right", y:"top"}, this.designer.lp.isSave);
  296. }
  297. },
  298. saveAs: function(){},
  299. explode: function(){},
  300. implode: function(){}
  301. });