Agent.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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, {"runtime": "service"});
  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.addEditorEvent("change", function(){
  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.editor.on("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.agentReferenceMenu){
  117. MWF.require("MWF.widget.ScriptHelp", function(){
  118. this.agentReferenceMenu = new MWF.widget.ScriptHelp(null, this.editor.editor, {
  119. "onPostLoad": function(){
  120. this.showReferenceMenu();
  121. }.bind(this)
  122. });
  123. this.agentReferenceMenu.getEditor = function(){return this.editor.editor;}.bind(this)
  124. }.bind(this));
  125. }else{
  126. this.showReferenceMenu();
  127. }
  128. }.bind(this));
  129. var options = this.designer.styleSelectNode.options;
  130. for (var i=0; i<options.length; i++){
  131. var option = options[i];
  132. if (option.value==this.editor.theme){
  133. option.set("selected", true);
  134. break;
  135. }
  136. }
  137. var options = this.designer.fontsizeSelectNode.options;
  138. for (var i=0; i<options.length; i++){
  139. var option = options[i];
  140. if (option.value==this.editor.fontSize){
  141. option.set("selected", true);
  142. break;
  143. }
  144. }
  145. options = this.designer.editorSelectNode.options;
  146. for (var i=0; i<options.length; i++){
  147. var option = options[i];
  148. if (option.value==this.editor.options.type){
  149. option.set("selected", true);
  150. break;
  151. }
  152. }
  153. options = this.designer.monacoStyleSelectNode.options;
  154. for (var i=0; i<options.length; i++){
  155. var option = options[i];
  156. if (option.value==this.editor.theme){
  157. option.set("selected", true);
  158. break;
  159. }
  160. }
  161. if (this.editor.options.type=="ace"){
  162. this.designer.monacoStyleSelectNode.hide();
  163. this.designer.styleSelectNode.show();
  164. }else{
  165. this.designer.monacoStyleSelectNode.show();
  166. this.designer.styleSelectNode.hide();
  167. }
  168. }.bind(this));
  169. if (this.options.showTab) this.page.showTabIm();
  170. },
  171. showReferenceMenu: function(){
  172. var pos = this.editor.getCursorPixelPosition();
  173. var e = {"page": {}};
  174. e.page.x = pos.left;
  175. e.page.y = pos.top;
  176. this.agentReferenceMenu.menu.showIm(e);
  177. },
  178. setIncludeNode: function(){
  179. this.designer.propertyIncludeListArea.empty();
  180. this.data.dependAgentList.each(function(name){
  181. this.designer.addIncludeToList(name);
  182. }.bind(this));
  183. },
  184. setPropertyContent: function(){
  185. this.designer.propertyIdNode.set("text", this.data.id || "");
  186. this.designer.propertyNameNode.set("value", this.data.name || "");
  187. this.designer.propertyAliasNode.set("value", this.data.alias || "");
  188. this.designer.propertyEnableNode.set("text", this.data.enable ? this.designer.lp.true : this.designer.lp.false );
  189. this.designer.propertyCronNode.set("value", this.data.cron || "");
  190. this.designer.cronValue = this.data.cron || "";
  191. //this.designer.cronPicker.setCronValue( this.data.cron || "" );
  192. this.designer.propertyLastStartTimeNode.set("text", this.data.lastStartTime || "");
  193. this.designer.propertyLastEndTimeNode.set("text", this.data.lastEndTime || "");
  194. //this.designer.propertyAppointmentTimeNode.set("text", this.data.appointmentTime || "");
  195. this.designer.propertyDescriptionNode.set("value", this.data.description || "");
  196. this.setButton()
  197. },
  198. setButton : function(){
  199. this.designer.propertyEnableButton.store("id", this.data.id);
  200. this.designer.propertyDisableButton.store("id", this.data.id);
  201. if( this.data.enable ){
  202. this.designer.propertyEnableButton.setStyle("display","none");
  203. this.designer.propertyDisableButton.setStyle("display", this.data.isNewAgent ? "none" : "" );
  204. }else{
  205. this.designer.propertyEnableButton.setStyle("display",this.data.isNewAgent ? "none" : "");
  206. this.designer.propertyDisableButton.setStyle("display", "none" );
  207. }
  208. },
  209. setAreaNodeSize: function(){
  210. var size = this.node.getSize();
  211. var tabSize = this.tab.tabNodeContainer.getSize();
  212. var y = size.y - tabSize.y;
  213. this.areaNode.setStyle("height", ""+y+"px");
  214. if (this.editor) if (this.editor.editor) this.editor.editor.resize();
  215. },
  216. addInclude: function(){
  217. },
  218. saveAgent: function (data, success, failure) {
  219. if (data.isNewAgent) {
  220. this.designer.actions.createAgent(data, success, failure);
  221. } else {
  222. this.designer.actions.updateAgent(data.id, data, success, failure);
  223. }
  224. },
  225. save: function(callback){
  226. if (!this.isSave){
  227. var session = this.editor.editor.getSession();
  228. var annotations = session.getAnnotations();
  229. var validated = true;
  230. for (var i=0; i<annotations.length; i++){
  231. if (annotations[i].type=="error"){
  232. validated = false;
  233. break;
  234. }
  235. }
  236. var name = this.designer.propertyNameNode.get("value");
  237. var alias = this.designer.propertyAliasNode.get("value");
  238. var description = this.designer.propertyDescriptionNode.get("value");
  239. var cron = this.designer.propertyCronNode.get("value");
  240. if (!name){
  241. this.designer.notice(this.designer.lp.notice.inputName, "error");
  242. return false;
  243. }
  244. if(!cron){
  245. this.designer.notice(this.designer.lp.notice.inputCron, "error");
  246. return false;
  247. }
  248. this.data.name = name;
  249. this.data.alias = alias;
  250. this.data.description = description;
  251. this.data.cron = cron;
  252. this.data.validated = validated;
  253. this.data.text = this.editor.editor.getValue();
  254. this.isSave = true;
  255. this.saveAgent(this.data, function(json){
  256. this.isSave = false;
  257. if( this.data.isNewAgent ){
  258. this.data.isNewAgent = false;
  259. this.setButton();
  260. }
  261. this.isChanged = false;
  262. this.page.textNode.set("text", this.data.name);
  263. if (this.lisNode) {
  264. this.lisNode.getLast().set("text", this.data.name);
  265. }
  266. this.designer.notice(this.designer.lp.notice.save_success, "success", this.node, {"x": "left", "y": "bottom"});
  267. this.data.id = json.data.id;
  268. this.designer.propertyIdNode.set("text", this.data.id );
  269. if (callback) callback();
  270. }.bind(this), function(xhr, text, error){
  271. this.isSave = false;
  272. var errorText = error+":"+text;
  273. if (xhr) errorText = xhr.responseText;
  274. MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  275. }.bind(this));
  276. }else{
  277. MWF.xDesktop.notice("info", {x: "right", y:"top"}, this.designer.lp.isSave);
  278. }
  279. },
  280. saveSilence: function(callback){
  281. if (!this.isSave){
  282. var session = this.editor.editor.getSession();
  283. var annotations = session.getAnnotations();
  284. var validated = true;
  285. for (var i=0; i<annotations.length; i++){
  286. if (annotations[i].type=="error"){
  287. validated = false;
  288. break;
  289. }
  290. }
  291. if( this.designer.currentScript == this ){
  292. var name = this.designer.propertyNameNode.get("value");
  293. var alias = this.designer.propertyAliasNode.get("value");
  294. var description = this.designer.propertyDescriptionNode.get("value");
  295. var cron = this.designer.propertyCronNode.get("value");
  296. if (!name){
  297. this.designer.notice(this.designer.lp.notice.inputName, "error");
  298. return false;
  299. }
  300. if(!cron){
  301. this.designer.notice(this.designer.lp.notice.inputCron, "error");
  302. return false;
  303. }
  304. this.data.name = name;
  305. this.data.alias = alias;
  306. this.data.description = description;
  307. this.data.cron = cron;
  308. this.data.validated = validated;
  309. }
  310. this.data.text = this.editor.editor.getValue();
  311. this.isSave = true;
  312. this.saveAgent(this.data, function(json){
  313. this.isSave = false;
  314. if( this.data.isNewAgent ){
  315. this.data.isNewAgent = false;
  316. if( this.designer.currentScript == this ) {
  317. this.setButton();
  318. }
  319. }
  320. this.data.isNewAgent = false;
  321. this.isChanged = false;
  322. this.page.textNode.set("text", this.data.name);
  323. if (this.lisNode) {
  324. this.lisNode.getLast().set("text", this.data.name);
  325. }
  326. this.data.id = json.data.id;
  327. if( this.designer.currentScript == this ) {
  328. this.designer.propertyIdNode.set("text", this.data.id);
  329. }
  330. if (callback) callback();
  331. }.bind(this), function(xhr, text, error){
  332. this.isSave = false;
  333. //
  334. //var errorText = error+":"+text;
  335. //if (xhr) errorText = xhr.responseText;
  336. //MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  337. }.bind(this));
  338. }else{
  339. MWF.xDesktop.notice("info", {x: "right", y:"top"}, this.designer.lp.isSave);
  340. }
  341. },
  342. saveAs: function(){},
  343. explode: function(){},
  344. implode: function(){}
  345. });