Agent.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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){
  70. this.editor.focus();
  71. }else{
  72. this.loadEditor();
  73. }
  74. this.setAreaNodeSize();
  75. //if (this.editor.editor){
  76. // this.editor.editor.focus();
  77. //this.editor.editor.navigateFileStart();
  78. //}
  79. }.bind(this));
  80. var _self = this;
  81. this.page.addEvent("queryClose", function(){
  82. if (_self.autoSaveTimerID) window.clearInterval(_self.autoSaveTimerID);
  83. this.showIm();
  84. //this.saveSilence();
  85. if (_self.lisNode) _self.lisNode.setStyles(_self.designer.css.listAgentItem);
  86. });
  87. this.page.tabNode.addEvent("dblclick", this.designer.maxOrReturnEditor.bind(this.designer));
  88. if (this.options.showTab) this.page.showTabIm();
  89. },
  90. loadEditor:function(){
  91. this.editor = new MWF.widget.JavascriptEditor(this.areaNode, {"runtime": "service", "option": {"value": this.data.text}});
  92. this.editor.load(function(){
  93. if (this.data.text){
  94. this.editor.editor.setValue(this.data.text);
  95. }else{
  96. // var defaultText = "/********************\n";
  97. // defaultText += "resources.getEntityManagerContainer(); //实体管理器\n";
  98. // defaultText += "resources.getContext(); //上下文根\n";
  99. // defaultText += "resources.getOrganization(); //组织访问\n";
  100. // defaultText += "resources.getWebservicesClient();//webSerivces客户端\n";
  101. // defaultText += "********************/\n";
  102. var lp = this.designer.lp.comment;
  103. var url = new URI("../api");
  104. var p =url.get("port");
  105. p = (!p || p===80) ? "" : ":"+p;
  106. var h = url.get("host");
  107. url.get("scheme")+"://"+h+p+"/api"
  108. var defaultText = "/********************\n";
  109. // defaultText += "this.entityManager; //"+lp.entityManager+"\n";
  110. // defaultText += "this.applications; //"+lp.applications+"\n";
  111. // defaultText += "this.organization; //"+lp.organization+"\n";
  112. // defaultText += "this.org; //"+lp.org+"\n";
  113. // defaultText += "this.service; ///"+lp.service+"\n";
  114. defaultText += "API Document: "+url.get("scheme")+"://"+h+p+"/api"+"\n";
  115. defaultText += "********************/\n";
  116. this.editor.editor.setValue(defaultText);
  117. }
  118. this.editor.addEditorEvent("change", function(e){
  119. if (!this.isChanged){
  120. this.isChanged = true;
  121. this.page.textNode.set("text", " * "+this.page.textNode.get("text"));
  122. }
  123. }.bind(this));
  124. // this.editor.editor.on("change", function(e){
  125. // if (!this.isChanged){
  126. // this.isChanged = true;
  127. // this.page.textNode.set("text", " * "+this.page.textNode.get("text"));
  128. // }
  129. // }.bind(this));
  130. this.editor.addEvent("save", function(){
  131. this.save();
  132. }.bind(this));
  133. // this.editor.addEvent("reference", function(editor, e, e1){
  134. // if (!this.agentReferenceMenu){
  135. // MWF.require("MWF.widget.ScriptHelp", function(){
  136. // this.agentReferenceMenu = new MWF.widget.ScriptHelp(null, this.editor.editor, {
  137. // "onPostLoad": function(){
  138. // this.showReferenceMenu();
  139. // }.bind(this)
  140. // });
  141. // this.agentReferenceMenu.getEditor = function(){return this.editor.editor;}.bind(this)
  142. // }.bind(this));
  143. // }else{
  144. // this.showReferenceMenu();
  145. // }
  146. // }.bind(this));
  147. var options = this.designer.styleSelectNode.options;
  148. for (var i=0; i<options.length; i++){
  149. var option = options[i];
  150. if (option.value==this.editor.theme){
  151. option.set("selected", true);
  152. break;
  153. }
  154. }
  155. var options = this.designer.fontsizeSelectNode.options;
  156. for (var i=0; i<options.length; i++){
  157. var option = options[i];
  158. if (option.value==this.editor.fontSize){
  159. option.set("selected", true);
  160. break;
  161. }
  162. }
  163. options = this.designer.editorSelectNode.options;
  164. for (var i=0; i<options.length; i++){
  165. var option = options[i];
  166. if (option.value==this.editor.options.type){
  167. option.set("selected", true);
  168. break;
  169. }
  170. }
  171. options = this.designer.monacoStyleSelectNode.options;
  172. for (var i=0; i<options.length; i++){
  173. var option = options[i];
  174. if (option.value==this.editor.theme){
  175. option.set("selected", true);
  176. break;
  177. }
  178. }
  179. if (this.editor.options.type=="ace"){
  180. this.designer.monacoStyleSelectNode.hide();
  181. this.designer.styleSelectNode.show();
  182. }else{
  183. this.designer.monacoStyleSelectNode.show();
  184. this.designer.styleSelectNode.hide();
  185. }
  186. }.bind(this));
  187. },
  188. showReferenceMenu: function(){
  189. var pos = this.editor.getCursorPixelPosition();
  190. var e = {"page": {}};
  191. e.page.x = pos.left;
  192. e.page.y = pos.top;
  193. this.agentReferenceMenu.menu.showIm(e);
  194. },
  195. setIncludeNode: function(){
  196. this.designer.propertyIncludeListArea.empty();
  197. this.data.dependAgentList.each(function(name){
  198. this.designer.addIncludeToList(name);
  199. }.bind(this));
  200. },
  201. setPropertyContent: function(){
  202. this.designer.propertyIdNode.set("text", this.data.id || "");
  203. this.designer.propertyNameNode.set("value", this.data.name || "");
  204. this.designer.propertyAliasNode.set("value", this.data.alias || "");
  205. this.designer.propertyEnableNode.set("text", this.data.enable ? this.designer.lp.true : this.designer.lp.false );
  206. this.designer.propertyCronNode.set("value", this.data.cron || "");
  207. this.designer.cronValue = this.data.cron || "";
  208. //this.designer.cronPicker.setCronValue( this.data.cron || "" );
  209. this.designer.propertyLastStartTimeNode.set("text", this.data.lastStartTime || "");
  210. this.designer.propertyLastEndTimeNode.set("text", this.data.lastEndTime || "");
  211. //this.designer.propertyAppointmentTimeNode.set("text", this.data.appointmentTime || "");
  212. this.designer.propertyDescriptionNode.set("value", this.data.description || "");
  213. this.setButton()
  214. },
  215. setButton : function(){
  216. this.designer.propertyEnableButton.store("id", this.data.id);
  217. this.designer.propertyDisableButton.store("id", this.data.id);
  218. this.designer.propertyExecuteButton.store("id", this.data.id);
  219. if( this.data.enable ){
  220. this.designer.propertyEnableButton.setStyle("display","none");
  221. this.designer.propertyDisableButton.setStyle("display", this.data.isNewAgent ? "none" : "" );
  222. }else{
  223. this.designer.propertyEnableButton.setStyle("display",this.data.isNewAgent ? "none" : "");
  224. this.designer.propertyDisableButton.setStyle("display", "none" );
  225. }
  226. },
  227. setAreaNodeSize: function(){
  228. if( !this.areaNode.offsetParent )return;
  229. //var size = this.node.getSize();
  230. var size = this.node.getComputedSize();
  231. size.y = size.height;
  232. var tabSize = this.tab.tabNodeContainer.getSize();
  233. var y = size.y - tabSize.y;
  234. this.areaNode.setStyle("height", ""+y+"px");
  235. //if (this.editor) if (this.editor.editor) this.editor.editor.resize();
  236. if (this.editor) this.editor.resize(y);
  237. },
  238. addInclude: function(){
  239. },
  240. saveAgent: function (data, success, failure) {
  241. if (data.isNewAgent) {
  242. this.designer.actions.createAgent(data, success, failure);
  243. } else {
  244. this.designer.actions.updateAgent(data.id, data, success, failure);
  245. }
  246. },
  247. save: function(callback){
  248. if (!this.isSave){
  249. /*var session = this.editor.editor.getSession();
  250. var annotations = session.getAnnotations();
  251. var validated = true;
  252. for (var i=0; i<annotations.length; i++){
  253. if (annotations[i].type=="error"){
  254. validated = false;
  255. break;
  256. }
  257. }*/
  258. var validated = this.editor.validated();
  259. var name = this.designer.propertyNameNode.get("value");
  260. var alias = this.designer.propertyAliasNode.get("value");
  261. var description = this.designer.propertyDescriptionNode.get("value");
  262. var cron = this.designer.propertyCronNode.get("value");
  263. if (!name){
  264. this.designer.notice(this.designer.lp.notice.inputName, "error");
  265. return false;
  266. }
  267. if(!cron){
  268. this.designer.notice(this.designer.lp.notice.inputCron, "error");
  269. return false;
  270. }
  271. this.data.name = name;
  272. this.data.alias = alias;
  273. this.data.description = description;
  274. this.data.cron = cron;
  275. this.data.validated = validated;
  276. this.data.text = this.editor.editor.getValue();
  277. this.isSave = true;
  278. this.saveAgent(this.data, function(json){
  279. this.isSave = false;
  280. if( this.data.isNewAgent ){
  281. this.data.isNewAgent = false;
  282. this.setButton();
  283. }
  284. this.isChanged = false;
  285. this.page.textNode.set("text", this.data.name);
  286. if (this.lisNode) {
  287. this.lisNode.getLast().set("text", this.data.name);
  288. }
  289. this.designer.notice(this.designer.lp.notice.save_success, "success", this.node, {"x": "left", "y": "bottom"});
  290. this.data.id = json.data.id;
  291. this.designer.propertyIdNode.set("text", this.data.id );
  292. if (callback) callback();
  293. }.bind(this), function(xhr, text, error){
  294. this.isSave = false;
  295. var errorText = error+":"+text;
  296. if (xhr) errorText = xhr.responseText;
  297. MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  298. }.bind(this));
  299. }else{
  300. MWF.xDesktop.notice("info", {x: "right", y:"top"}, this.designer.lp.isSave);
  301. }
  302. },
  303. saveSilence: function(callback){
  304. if (!this.isSave){
  305. var session = this.editor.editor.getSession();
  306. var annotations = session.getAnnotations();
  307. var validated = true;
  308. for (var i=0; i<annotations.length; i++){
  309. if (annotations[i].type=="error"){
  310. validated = false;
  311. break;
  312. }
  313. }
  314. if( this.designer.currentScript == this ){
  315. var name = this.designer.propertyNameNode.get("value");
  316. var alias = this.designer.propertyAliasNode.get("value");
  317. var description = this.designer.propertyDescriptionNode.get("value");
  318. var cron = this.designer.propertyCronNode.get("value");
  319. if (!name){
  320. this.designer.notice(this.designer.lp.notice.inputName, "error");
  321. return false;
  322. }
  323. if(!cron){
  324. this.designer.notice(this.designer.lp.notice.inputCron, "error");
  325. return false;
  326. }
  327. this.data.name = name;
  328. this.data.alias = alias;
  329. this.data.description = description;
  330. this.data.cron = cron;
  331. this.data.validated = validated;
  332. }
  333. this.data.text = this.editor.editor.getValue();
  334. this.isSave = true;
  335. this.saveAgent(this.data, function(json){
  336. this.isSave = false;
  337. if( this.data.isNewAgent ){
  338. this.data.isNewAgent = false;
  339. if( this.designer.currentScript == this ) {
  340. this.setButton();
  341. }
  342. }
  343. this.data.isNewAgent = false;
  344. this.isChanged = false;
  345. this.page.textNode.set("text", this.data.name);
  346. if (this.lisNode) {
  347. this.lisNode.getLast().set("text", this.data.name);
  348. }
  349. this.data.id = json.data.id;
  350. if( this.designer.currentScript == this ) {
  351. this.designer.propertyIdNode.set("text", this.data.id);
  352. }
  353. if (callback) callback();
  354. }.bind(this), function(xhr, text, error){
  355. this.isSave = false;
  356. //
  357. //var errorText = error+":"+text;
  358. //if (xhr) errorText = xhr.responseText;
  359. //MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  360. }.bind(this));
  361. }else{
  362. MWF.xDesktop.notice("info", {x: "right", y:"top"}, this.designer.lp.isSave);
  363. }
  364. },
  365. saveAs: function(){},
  366. explode: function(){},
  367. implode: function(){}
  368. });