Agent.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. var defaultText = "/********************\n";
  92. defaultText += "this.entityManager; //实体管理器\n";
  93. defaultText += "this.applications; //访问系统内服务\n";
  94. defaultText += "this.organization(); //组织访问\n";
  95. defaultText += "this.org(); //组织快速访问方法\n";
  96. defaultText += "this.service(); ///webSerivces客户端\n";
  97. defaultText += "********************/\n";
  98. this.editor.editor.setValue(defaultText);
  99. }
  100. this.editor.editor.on("change", function(e){
  101. if (!this.isChanged){
  102. this.isChanged = true;
  103. this.page.textNode.set("text", " * "+this.page.textNode.get("text"));
  104. }
  105. }.bind(this));
  106. this.editor.addEvent("save", function(){
  107. this.save();
  108. }.bind(this));
  109. this.editor.addEvent("reference", function(editor, e, e1){
  110. if (!this.agentReferenceMenu){
  111. MWF.require("MWF.widget.ScriptHelp", function(){
  112. this.agentReferenceMenu = new MWF.widget.ScriptHelp(null, this.editor.editor, {
  113. "onPostLoad": function(){
  114. this.showReferenceMenu();
  115. }.bind(this)
  116. });
  117. this.agentReferenceMenu.getEditor = function(){return this.editor.editor;}.bind(this)
  118. }.bind(this));
  119. }else{
  120. this.showReferenceMenu();
  121. }
  122. }.bind(this));
  123. var options = this.designer.styleSelectNode.options;
  124. for (var i=0; i<options.length; i++){
  125. var option = options[i];
  126. if (option.value==this.editor.theme){
  127. option.set("selected", true);
  128. break;
  129. }
  130. }
  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. }.bind(this));
  140. if (this.options.showTab) this.page.showTabIm();
  141. },
  142. showReferenceMenu: function(){
  143. var pos = this.editor.getCursorPixelPosition();
  144. var e = {"page": {}};
  145. e.page.x = pos.left;
  146. e.page.y = pos.top;
  147. this.agentReferenceMenu.menu.showIm(e);
  148. },
  149. setIncludeNode: function(){
  150. this.designer.propertyIncludeListArea.empty();
  151. this.data.dependAgentList.each(function(name){
  152. this.designer.addIncludeToList(name);
  153. }.bind(this));
  154. },
  155. setPropertyContent: function(){
  156. this.designer.propertyIdNode.set("text", this.data.id || "");
  157. this.designer.propertyNameNode.set("value", this.data.name || "");
  158. this.designer.propertyAliasNode.set("value", this.data.alias || "");
  159. this.designer.propertyEnableNode.set("text", this.data.enable ? this.designer.lp.true : this.designer.lp.false );
  160. this.designer.propertyCronNode.set("value", this.data.cron || "");
  161. this.designer.cronValue = this.data.cron || "";
  162. //this.designer.cronPicker.setCronValue( this.data.cron || "" );
  163. this.designer.propertyLastStartTimeNode.set("text", this.data.lastStartTime || "");
  164. this.designer.propertyLastEndTimeNode.set("text", this.data.lastEndTime || "");
  165. //this.designer.propertyAppointmentTimeNode.set("text", this.data.appointmentTime || "");
  166. this.designer.propertyDescriptionNode.set("value", this.data.description || "");
  167. this.setButton()
  168. },
  169. setButton : function(){
  170. this.designer.propertyEnableButton.store("id", this.data.id);
  171. this.designer.propertyDisableButton.store("id", this.data.id);
  172. if( this.data.enable ){
  173. this.designer.propertyEnableButton.setStyle("display","none");
  174. this.designer.propertyDisableButton.setStyle("display", this.data.isNewAgent ? "none" : "" );
  175. }else{
  176. this.designer.propertyEnableButton.setStyle("display",this.data.isNewAgent ? "none" : "");
  177. this.designer.propertyDisableButton.setStyle("display", "none" );
  178. }
  179. },
  180. setAreaNodeSize: function(){
  181. var size = this.node.getSize();
  182. var tabSize = this.tab.tabNodeContainer.getSize();
  183. var y = size.y - tabSize.y;
  184. this.areaNode.setStyle("height", ""+y+"px");
  185. if (this.editor) if (this.editor.editor) this.editor.editor.resize();
  186. },
  187. addInclude: function(){
  188. },
  189. saveAgent: function (data, success, failure) {
  190. if (data.isNewAgent) {
  191. this.designer.actions.createAgent(data, success, failure);
  192. } else {
  193. this.designer.actions.updateAgent(data.id, data, success, failure);
  194. }
  195. },
  196. save: function(callback){
  197. if (!this.isSave){
  198. var session = this.editor.editor.getSession();
  199. var annotations = session.getAnnotations();
  200. var validated = true;
  201. for (var i=0; i<annotations.length; i++){
  202. if (annotations[i].type=="error"){
  203. validated = false;
  204. break;
  205. }
  206. }
  207. var name = this.designer.propertyNameNode.get("value");
  208. var alias = this.designer.propertyAliasNode.get("value");
  209. var description = this.designer.propertyDescriptionNode.get("value");
  210. var cron = this.designer.propertyCronNode.get("value");
  211. if (!name){
  212. this.designer.notice(this.designer.lp.notice.inputName, "error");
  213. return false;
  214. }
  215. if(!cron){
  216. this.designer.notice(this.designer.lp.notice.inputCron, "error");
  217. return false;
  218. }
  219. this.data.name = name;
  220. this.data.alias = alias;
  221. this.data.description = description;
  222. this.data.cron = cron;
  223. this.data.validated = validated;
  224. this.data.text = this.editor.editor.getValue();
  225. this.isSave = true;
  226. this.saveAgent(this.data, function(json){
  227. this.isSave = false;
  228. if( this.data.isNewAgent ){
  229. this.data.isNewAgent = false;
  230. this.setButton();
  231. }
  232. this.isChanged = false;
  233. this.page.textNode.set("text", this.data.name);
  234. if (this.lisNode) {
  235. this.lisNode.getLast().set("text", this.data.name);
  236. }
  237. this.designer.notice(this.designer.lp.notice.save_success, "success", this.node, {"x": "left", "y": "bottom"});
  238. this.data.id = json.data.id;
  239. this.designer.propertyIdNode.set("text", this.data.id );
  240. if (callback) callback();
  241. }.bind(this), function(xhr, text, error){
  242. this.isSave = false;
  243. var errorText = error+":"+text;
  244. if (xhr) errorText = xhr.responseText;
  245. MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  246. }.bind(this));
  247. }else{
  248. MWF.xDesktop.notice("info", {x: "right", y:"top"}, this.designer.lp.isSave);
  249. }
  250. },
  251. saveSilence: function(callback){
  252. if (!this.isSave){
  253. var session = this.editor.editor.getSession();
  254. var annotations = session.getAnnotations();
  255. var validated = true;
  256. for (var i=0; i<annotations.length; i++){
  257. if (annotations[i].type=="error"){
  258. validated = false;
  259. break;
  260. }
  261. }
  262. if( this.designer.currentScript == this ){
  263. var name = this.designer.propertyNameNode.get("value");
  264. var alias = this.designer.propertyAliasNode.get("value");
  265. var description = this.designer.propertyDescriptionNode.get("value");
  266. var cron = this.designer.propertyCronNode.get("value");
  267. if (!name){
  268. this.designer.notice(this.designer.lp.notice.inputName, "error");
  269. return false;
  270. }
  271. if(!cron){
  272. this.designer.notice(this.designer.lp.notice.inputCron, "error");
  273. return false;
  274. }
  275. this.data.name = name;
  276. this.data.alias = alias;
  277. this.data.description = description;
  278. this.data.cron = cron;
  279. this.data.validated = validated;
  280. }
  281. this.data.text = this.editor.editor.getValue();
  282. this.isSave = true;
  283. this.saveAgent(this.data, function(json){
  284. this.isSave = false;
  285. if( this.data.isNewAgent ){
  286. this.data.isNewAgent = false;
  287. if( this.designer.currentScript == this ) {
  288. this.setButton();
  289. }
  290. }
  291. this.data.isNewAgent = false;
  292. this.isChanged = false;
  293. this.page.textNode.set("text", this.data.name);
  294. if (this.lisNode) {
  295. this.lisNode.getLast().set("text", this.data.name);
  296. }
  297. this.data.id = json.data.id;
  298. if( this.designer.currentScript == this ) {
  299. this.designer.propertyIdNode.set("text", this.data.id);
  300. }
  301. if (callback) callback();
  302. }.bind(this), function(xhr, text, error){
  303. this.isSave = false;
  304. //
  305. //var errorText = error+":"+text;
  306. //if (xhr) errorText = xhr.responseText;
  307. //MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  308. }.bind(this));
  309. }else{
  310. MWF.xDesktop.notice("info", {x: "right", y:"top"}, this.designer.lp.isSave);
  311. }
  312. },
  313. saveAs: function(){},
  314. explode: function(){},
  315. implode: function(){}
  316. });