Invoke.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. MWF.xApplication = MWF.xApplication || {};
  2. MWF.xApplication.service = MWF.xApplication.service || {};
  3. MWF.xApplication.service.InvokeDesigner = MWF.xApplication.service.InvokeDesigner || {};
  4. MWF.SRVID = MWF.xApplication.service.InvokeDesigner;
  5. MWF.require("MWF.widget.Common", null, false);
  6. MWF.xDesktop.requireApp("service.InvokeDesigner", "lp."+MWF.language, null, false);
  7. MWF.require("MWF.widget.JavascriptEditor", null, false);
  8. MWF.xApplication.service.InvokeDesigner.Invoke = 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_InvokeDesigner/$Invoke/";
  18. this.cssPath = "../x_component_service_InvokeDesigner/$Invoke/"+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.invokeTab;
  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.isNewInvoke = (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("#MWFInvokeAutoSaveCheck");
  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.invokePropertyNode = new Element("div", {"styles": this.css.invokePropertyNode}).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.newInvoke, (!this.data.isNewInvoke && this.data.id!=this.designer.options.id));
  53. this.page.invoke = this;
  54. this.page.addEvent("show", function(){
  55. this.designer.invokeListAreaNode.getChildren().each(function(node){
  56. var scrtip = node.retrieve("invoke");
  57. if (scrtip.id==this.data.id){
  58. if (this.designer.currentListInvokeItem){
  59. this.designer.currentListInvokeItem.setStyles(this.designer.css.listInvokeItem);
  60. }
  61. node.setStyles(this.designer.css.listInvokeItem_current);
  62. this.designer.currentListInvokeItem = 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.listInvokeItem);
  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 += "requestText//请求正文\n";
  91. // defaultText += "request//请求\n";
  92. // defaultText += "effectivePerson//当前用户\n";
  93. // defaultText += "********************/\n";
  94. var defaultText = "/********************\n";
  95. defaultText += "this.entityManager; //实体管理器\n";
  96. defaultText += "this.applications; //访问系统内服务\n";
  97. defaultText += "this.requestText//请求正文\n";
  98. defaultText += "this.request//请求\n";
  99. defaultText += "this.currentPerson//当前用户\n";
  100. defaultText += "this.response//响应对象。通过this.response.setBody(data)设置响应内容\n";
  101. defaultText += "this.organization; //组织访问\n";
  102. defaultText += "this.org; //组织快速访问方法\n";
  103. defaultText += "this.service; ///webSerivces客户端\n";
  104. defaultText += "********************/\n";
  105. this.editor.editor.setValue(defaultText);
  106. }
  107. this.editor.addEditorEvent("change", function(){
  108. if (!this.isChanged){
  109. this.isChanged = true;
  110. this.page.textNode.set("text", " * "+this.page.textNode.get("text"));
  111. }
  112. }.bind(this));
  113. // this.editor.editor.on("change", function(e){
  114. // if (!this.isChanged){
  115. // this.isChanged = true;
  116. // this.page.textNode.set("text", " * "+this.page.textNode.get("text"));
  117. // }
  118. // }.bind(this));
  119. this.editor.addEvent("save", function(){
  120. this.save();
  121. }.bind(this));
  122. //this.editor.addEvent("reference", function(editor, e, e1){
  123. // if (!this.invokeReferenceMenu){
  124. // MWF.require("MWF.widget.ScriptHelp", function(){
  125. // this.invokeReferenceMenu = new MWF.widget.ScriptHelp(null, this.editor.editor, {
  126. // "onPostLoad": function(){
  127. // this.showReferenceMenu();
  128. // }.bind(this)
  129. // });
  130. // this.invokeReferenceMenu.getEditor = function(){return this.editor.editor;}.bind(this)
  131. // }.bind(this));
  132. // }else{
  133. // this.showReferenceMenu();
  134. // }
  135. //}.bind(this));
  136. var options = this.designer.styleSelectNode.options;
  137. for (var i=0; i<options.length; i++){
  138. var option = options[i];
  139. if (option.value==this.editor.theme){
  140. option.set("selected", true);
  141. break;
  142. }
  143. }
  144. var options = this.designer.fontsizeSelectNode.options;
  145. for (var i=0; i<options.length; i++){
  146. var option = options[i];
  147. if (option.value==this.editor.fontSize){
  148. option.set("selected", true);
  149. break;
  150. }
  151. }
  152. options = this.designer.editorSelectNode.options;
  153. for (var i=0; i<options.length; i++){
  154. var option = options[i];
  155. if (option.value==this.editor.options.type){
  156. option.set("selected", true);
  157. break;
  158. }
  159. }
  160. options = this.designer.monacoStyleSelectNode.options;
  161. for (var i=0; i<options.length; i++){
  162. var option = options[i];
  163. if (option.value==this.editor.theme){
  164. option.set("selected", true);
  165. break;
  166. }
  167. }
  168. if (this.editor.options.type=="ace"){
  169. this.designer.monacoStyleSelectNode.hide();
  170. this.designer.styleSelectNode.show();
  171. }else{
  172. this.designer.monacoStyleSelectNode.show();
  173. this.designer.styleSelectNode.hide();
  174. }
  175. }.bind(this));
  176. if (this.options.showTab) this.page.showTabIm();
  177. },
  178. showReferenceMenu: function(){
  179. var pos = this.editor.getCursorPixelPosition();
  180. var e = {"page": {}};
  181. e.page.x = pos.left;
  182. e.page.y = pos.top;
  183. this.invokeReferenceMenu.menu.showIm(e);
  184. },
  185. setIncludeNode: function(){
  186. this.designer.propertyIncludeListArea.empty();
  187. this.data.dependInvokeList.each(function(name){
  188. this.designer.addIncludeToList(name);
  189. }.bind(this));
  190. },
  191. setPropertyContent: function(){
  192. this.designer.propertyIdNode.set("text", this.data.id || "");
  193. this.designer.propertyNameNode.set("value", this.data.name || "");
  194. this.designer.propertyAliasNode.set("value", this.data.alias || "");
  195. this.designer.propertyEnableTokenNode.getElement("option[value='"+ this.data.enableToken +"']").set("selected", true );
  196. this.designer.propertyEnableNode.getElement("option[value='"+ this.data.enable +"']").set("selected", true );
  197. this.designer.propertyRemoteAddrRegexNode.set("value", this.data.remoteAddrRegex || "");
  198. this.designer.propertyLastStartTimeNode.set("text", this.data.lastStartTime || "");
  199. this.designer.propertyLastEndTimeNode.set("text", this.data.lastEndTime || "");
  200. this.designer.propertyDescriptionNode.set("value", this.data.description || "");
  201. this.setInvokeUrlText();
  202. this.designer.propertyEnableTokenNode.addEvent("change", this.setInvokeUrlText.bind(this));
  203. },
  204. setInvokeUrlText: function(){
  205. debugger
  206. var action = this.designer.actions.action;
  207. var url;
  208. var enableToken = true;
  209. this.designer.propertyEnableTokenNode.getElements("option").each( function(option){
  210. if( option.selected ){
  211. enableToken = (option.value == "true");
  212. }
  213. });
  214. if (enableToken===true){
  215. uri = action.address + action.actions.executeToken.uri ;
  216. uri = uri.replace("{flag}", this.data.alias || this.data.name || this.data.id );
  217. }else{
  218. var uri = action.address + action.actions.executeInvoke.uri ;
  219. uri = uri.replace("{flag}", this.data.alias || this.data.name || this.data.id );
  220. }
  221. this.designer.propertyInvokeUriNode.set("text", uri);
  222. },
  223. setAreaNodeSize: function(){
  224. var size = this.node.getSize();
  225. var tabSize = this.tab.tabNodeContainer.getSize();
  226. var y = size.y - tabSize.y;
  227. this.areaNode.setStyle("height", ""+y+"px");
  228. //if (this.editor) if (this.editor.editor) this.editor.editor.resize();
  229. if (this.editor) this.editor.resize(y);
  230. },
  231. addInclude: function(){
  232. },
  233. saveInvoke: function (data, success, failure) {
  234. if (data.isNewInvoke) {
  235. this.designer.actions.createInvoke(data, success, failure);
  236. } else {
  237. this.designer.actions.updateInvoke(data.id, data, success, failure);
  238. }
  239. },
  240. save: function(callback){
  241. if (!this.isSave){
  242. /*var session = this.editor.editor.getSession();
  243. var annotations = session.getAnnotations();
  244. var validated = true;
  245. for (var i=0; i<annotations.length; i++){
  246. if (annotations[i].type=="error"){
  247. validated = false;
  248. break;
  249. }
  250. }*/
  251. var validated = this.editor.validated();
  252. var name = this.designer.propertyNameNode.get("value");
  253. var alias = this.designer.propertyAliasNode.get("value");
  254. var description = this.designer.propertyDescriptionNode.get("value");
  255. var remoteAddrRegex = this.designer.propertyRemoteAddrRegexNode.get("value");
  256. var enable = true;
  257. this.designer.propertyEnableNode.getElements("option").each( function(option){
  258. if( option.selected ){
  259. enable = (option.value == "true");
  260. }
  261. });
  262. var enableToken = true;
  263. this.designer.propertyEnableTokenNode.getElements("option").each( function(option){
  264. if( option.selected ){
  265. enableToken = (option.value == "true");
  266. }
  267. });
  268. if (!name){
  269. this.designer.notice(this.designer.lp.notice.inputName, "error");
  270. return false;
  271. }
  272. //if(!remoteAddrRegex){
  273. // this.designer.notice(this.designer.lp.notice.inputRemoteAddrRegex, "error");
  274. // return false;
  275. //}
  276. this.data.name = name;
  277. this.data.alias = alias;
  278. this.data.description = description;
  279. this.data.remoteAddrRegex = remoteAddrRegex;
  280. this.data.validated = validated;
  281. this.data.text = this.editor.editor.getValue();
  282. this.data.enable = enable;
  283. this.data.enableToken = enableToken;
  284. this.isSave = true;
  285. this.saveInvoke(this.data, function(json){
  286. this.isSave = false;
  287. if( this.data.isNewInvoke ){
  288. this.data.isNewInvoke = false;
  289. //this.setButton();
  290. }
  291. this.isChanged = false;
  292. this.page.textNode.set("text", this.data.name);
  293. if (this.lisNode) {
  294. this.lisNode.getLast().set("text", this.data.name);
  295. }
  296. this.designer.notice(this.designer.lp.notice.save_success, "success", this.node, {"x": "left", "y": "bottom"});
  297. this.data.id = json.data.id;
  298. this.designer.propertyIdNode.set("text", this.data.id );
  299. this.setInvokeUrlText()
  300. // var action = this.designer.actions.action;
  301. // var uri = action.address + action.actions.executeInvoke.uri ;
  302. // uri = uri.replace("{flag}", this.data.alias || this.data.name || this.data.id );
  303. // this.designer.propertyInvokeUriNode.set("text", uri);
  304. if (callback) callback();
  305. }.bind(this), function(xhr, text, error){
  306. this.isSave = false;
  307. var errorText = error+":"+text;
  308. if (xhr) errorText = xhr.responseText;
  309. MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  310. }.bind(this));
  311. }else{
  312. MWF.xDesktop.notice("info", {x: "right", y:"top"}, this.designer.lp.isSave);
  313. }
  314. },
  315. saveSilence: function(callback){
  316. if (!this.isSave){
  317. var session = this.editor.editor.getSession();
  318. var annotations = session.getAnnotations();
  319. var validated = true;
  320. for (var i=0; i<annotations.length; i++){
  321. if (annotations[i].type=="error"){
  322. validated = false;
  323. break;
  324. }
  325. }
  326. if( this.designer.currentScript == this ){
  327. var name = this.designer.propertyNameNode.get("value");
  328. var alias = this.designer.propertyAliasNode.get("value");
  329. var description = this.designer.propertyDescriptionNode.get("value");
  330. var remoteAddrRegex = this.designer.propertyRemoteAddrRegexNode.get("value");
  331. var enable = true;
  332. this.designer.propertyEnableNode.getElements("option").each( function(option){
  333. if( option.selected ){
  334. enable = (option.value == "true");
  335. }
  336. });
  337. if (!name){
  338. this.designer.notice(this.designer.lp.notice.inputName, "error");
  339. return false;
  340. }
  341. //if(!remoteAddrRegex){
  342. // this.designer.notice(this.designer.lp.notice.inputRemoteAddrRegex, "error");
  343. // return false;
  344. //}
  345. this.data.name = name;
  346. this.data.alias = alias;
  347. this.data.description = description;
  348. this.data.remoteAddrRegex = remoteAddrRegex;
  349. this.data.validated = validated;
  350. this.data.enable = enable;
  351. }
  352. this.data.text = this.editor.editor.getValue();
  353. this.isSave = true;
  354. this.saveInvoke(this.data, function(json){
  355. this.isSave = false;
  356. if( this.data.isNewInvoke ){
  357. this.data.isNewInvoke = false;
  358. //this.setButton();
  359. }
  360. this.data.isNewInvoke = false;
  361. this.isChanged = false;
  362. this.page.textNode.set("text", this.data.name);
  363. if (this.lisNode) {
  364. this.lisNode.getLast().set("text", this.data.name);
  365. }
  366. this.data.id = json.data.id;
  367. if( this.designer.currentScript == this ){
  368. this.designer.propertyIdNode.set("text", this.data.id );
  369. this.setInvokeUrlText();
  370. // var action = this.designer.actions.action;
  371. // var uri = action.address + action.actions.executeInvoke.uri ;
  372. // uri = uri.replace("{flag}", this.data.alias || this.data.name || this.data.id );
  373. // this.designer.propertyInvokeUriNode.set("text", uri);
  374. }
  375. if (callback) callback();
  376. }.bind(this), function(xhr, text, error){
  377. this.isSave = false;
  378. //
  379. //var errorText = error+":"+text;
  380. //if (xhr) errorText = xhr.responseText;
  381. //MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  382. }.bind(this));
  383. }else{
  384. MWF.xDesktop.notice("info", {x: "right", y:"top"}, this.designer.lp.isSave);
  385. }
  386. },
  387. saveAs: function(){},
  388. explode: function(){},
  389. implode: function(){}
  390. });