Invoke.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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.currentPage = this.page;
  56. this.designer.invokeListAreaNode.getChildren().each(function(node){
  57. var scrtip = node.retrieve("invoke");
  58. if (scrtip.id==this.data.id){
  59. if (this.designer.currentListInvokeItem){
  60. this.designer.currentListInvokeItem.setStyles(this.designer.css.listInvokeItem);
  61. }
  62. node.setStyles(this.designer.css.listInvokeItem_current);
  63. this.designer.currentListInvokeItem = node;
  64. this.lisNode = node;
  65. }
  66. }.bind(this));
  67. this.designer.currentScript = this;
  68. this.setPropertyContent();
  69. //this.setIncludeNode();
  70. if (this.editor){
  71. this.editor.focus();
  72. }else{
  73. this.loadEditor();
  74. }
  75. this.setAreaNodeSize();
  76. //if (this.editor.editor){
  77. // this.editor.editor.focus();
  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.listInvokeItem);
  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 += "requestText//请求正文\n";
  102. // defaultText += "request//请求\n";
  103. // defaultText += "effectivePerson//当前用户\n";
  104. // defaultText += "********************/\n";
  105. var lp = this.designer.lp.comment;
  106. var url = new URI("../api");
  107. var p =url.get("port");
  108. p = (!p || p===80) ? "" : ":"+p;
  109. var h = url.get("host");
  110. url.get("scheme")+"://"+h+p+"/api"
  111. var defaultText = "/********************\n";
  112. // defaultText += "this.entityManager; //"+lp.entityManager+"\n";
  113. // defaultText += "this.applications; //"+lp.applications+"\n";
  114. defaultText += "this.requestText//"+lp.requestText+"\n";
  115. // defaultText += "this.request//"+lp.request+"\n";
  116. defaultText += "this.currentPerson//"+lp.currentPerson+"\n";
  117. defaultText += "this.response//"+lp.response+"\n";
  118. // defaultText += "this.organization; //"+lp.organization+"\n";
  119. defaultText += "this.org; //"+lp.org+"\n";
  120. // defaultText += "this.service; //"+lp.service+"\n";
  121. defaultText += "API Document: "+url.get("scheme")+"://"+h+p+"/api"+"\n";
  122. defaultText += "********************/\n";
  123. this.editor.editor.setValue(defaultText);
  124. }
  125. this.editor.addEditorEvent("change", function(e){
  126. if (!this.isChanged){
  127. this.isChanged = true;
  128. this.page.textNode.set("text", " * "+this.page.textNode.get("text"));
  129. }
  130. }.bind(this));
  131. // this.editor.editor.on("change", function(e){
  132. // if (!this.isChanged){
  133. // this.isChanged = true;
  134. // this.page.textNode.set("text", " * "+this.page.textNode.get("text"));
  135. // }
  136. // }.bind(this));
  137. this.editor.addEvent("save", function(){
  138. this.save();
  139. }.bind(this));
  140. //this.editor.addEvent("reference", function(editor, e, e1){
  141. // if (!this.invokeReferenceMenu){
  142. // MWF.require("MWF.widget.ScriptHelp", function(){
  143. // this.invokeReferenceMenu = new MWF.widget.ScriptHelp(null, this.editor.editor, {
  144. // "onPostLoad": function(){
  145. // this.showReferenceMenu();
  146. // }.bind(this)
  147. // });
  148. // this.invokeReferenceMenu.getEditor = function(){return this.editor.editor;}.bind(this)
  149. // }.bind(this));
  150. // }else{
  151. // this.showReferenceMenu();
  152. // }
  153. //}.bind(this));
  154. var options = this.designer.styleSelectNode.options;
  155. for (var i=0; i<options.length; i++){
  156. var option = options[i];
  157. if (option.value==this.editor.theme){
  158. option.set("selected", true);
  159. break;
  160. }
  161. }
  162. var options = this.designer.fontsizeSelectNode.options;
  163. for (var i=0; i<options.length; i++){
  164. var option = options[i];
  165. if (option.value==this.editor.fontSize){
  166. option.set("selected", true);
  167. break;
  168. }
  169. }
  170. options = this.designer.editorSelectNode.options;
  171. for (var i=0; i<options.length; i++){
  172. var option = options[i];
  173. if (option.value==this.editor.options.type){
  174. option.set("selected", true);
  175. break;
  176. }
  177. }
  178. options = this.designer.monacoStyleSelectNode.options;
  179. for (var i=0; i<options.length; i++){
  180. var option = options[i];
  181. if (option.value==this.editor.theme){
  182. option.set("selected", true);
  183. break;
  184. }
  185. }
  186. if (this.editor.options.type=="ace"){
  187. this.designer.monacoStyleSelectNode.hide();
  188. this.designer.styleSelectNode.show();
  189. }else{
  190. this.designer.monacoStyleSelectNode.show();
  191. this.designer.styleSelectNode.hide();
  192. }
  193. }.bind(this));
  194. },
  195. showReferenceMenu: function(){
  196. var pos = this.editor.getCursorPixelPosition();
  197. var e = {"page": {}};
  198. e.page.x = pos.left;
  199. e.page.y = pos.top;
  200. this.invokeReferenceMenu.menu.showIm(e);
  201. },
  202. setIncludeNode: function(){
  203. this.designer.propertyIncludeListArea.empty();
  204. this.data.dependInvokeList.each(function(name){
  205. this.designer.addIncludeToList(name);
  206. }.bind(this));
  207. },
  208. setPropertyContent: function(){
  209. this.designer.propertyIdNode.set("text", this.data.id || "");
  210. this.designer.propertyNameNode.set("value", this.data.name || "");
  211. this.designer.propertyAliasNode.set("value", this.data.alias || "");
  212. this.designer.propertyEnableTokenNode.getElement("option[value='"+ this.data.enableToken +"']").set("selected", true );
  213. this.designer.propertyEnableNode.getElement("option[value='"+ this.data.enable +"']").set("selected", true );
  214. this.designer.propertyRemoteAddrRegexNode.set("value", this.data.remoteAddrRegex || "");
  215. this.designer.propertyLastStartTimeNode.set("text", this.data.lastStartTime || "");
  216. this.designer.propertyLastEndTimeNode.set("text", this.data.lastEndTime || "");
  217. this.designer.propertyDescriptionNode.set("value", this.data.description || "");
  218. if( this.designer.propertyEnableTokenChangeEvent ){
  219. this.designer.propertyEnableTokenNode.removeEvent("change", this.designer.propertyEnableTokenChangeEvent);
  220. }
  221. this.designer.propertyEnableTokenChangeEvent = function(){
  222. this.setInvokeUrlText();
  223. this.checkPropertyTokenNode();
  224. }.bind(this);
  225. this.designer.propertyEnableTokenNode.addEvent("change", this.designer.propertyEnableTokenChangeEvent);
  226. this.designer.propertyEnableTokenChangeEvent();
  227. var data = {};
  228. if( this.data.data ){
  229. try{ data = JSON.parse( this.data.data ) }catch (e) {}
  230. }
  231. if(this.page){
  232. //this.designer.propertyRequireBodyNode.set("value", this.page.requireBody || "");
  233. if( this.designer.propertyRequireBodyScriptArea.jsEditor ){
  234. this.designer.propertyRequireBodyScriptArea.jsEditor.setValue(this.page.requireBody || data.requireBodyScript || "");
  235. }
  236. this.designer.propertyRunResultNode.set("text", this.page.executeResult || "");
  237. if(this.designer.propertyTokenNode){
  238. this.designer.propertyTokenNode.getElements("option").each(function ( opt ) {
  239. if( data.simulaToken === opt.value )opt.selected = true;
  240. });
  241. }
  242. }
  243. this.setButton();
  244. },
  245. setButton : function(){
  246. this.designer.propertyExecuteButton.store("id", this.data.id);
  247. this.designer.propertyExecuteButton.store("alias", this.data.alias);
  248. this.designer.propertyExecuteButton.store("name", this.data.name);
  249. },
  250. checkPropertyTokenNode: function(){
  251. var enableToken = true;
  252. this.designer.propertyEnableTokenNode.getElements("option").each( function(option){
  253. if( option.selected ){
  254. enableToken = (option.value == "true");
  255. }
  256. });
  257. this.designer.propertyTokenNode.getPrevious().setStyle("display", enableToken ? "" : "none" );
  258. this.designer.propertyTokenNode.setStyle("display", enableToken ? "" : "none" );
  259. },
  260. setInvokeUrlText: function(){
  261. debugger
  262. var action = this.designer.actions.action;
  263. var uri;
  264. var enableToken = true;
  265. this.designer.propertyEnableTokenNode.getElements("option").each( function(option){
  266. if( option.selected ){
  267. enableToken = (option.value == "true");
  268. }
  269. });
  270. if (enableToken===true){
  271. uri = action.address + action.actions.executeToken.uri ;
  272. uri = uri.replace("{flag}", this.data.alias || this.data.name || this.data.id );
  273. }else{
  274. uri = action.address + action.actions.executeInvoke.uri ;
  275. uri = uri.replace("{flag}", this.data.alias || this.data.name || this.data.id );
  276. }
  277. var url = o2.filterUrl(uri);
  278. this.designer.propertyInvokeUriNode.set("text", url);
  279. },
  280. setAreaNodeSize: function(){
  281. if( !this.areaNode.offsetParent )return;
  282. //var size = this.node.getSize();
  283. var size = this.node.getComputedSize();
  284. size.y = size.height;
  285. var tabSize = this.tab.tabNodeContainer.getSize();
  286. var y = size.y - tabSize.y;
  287. this.areaNode.setStyle("height", ""+y+"px");
  288. //if (this.editor) if (this.editor.editor) this.editor.editor.resize();
  289. if (this.editor) this.editor.resize(y);
  290. },
  291. addInclude: function(){
  292. },
  293. saveInvoke: function (data, success, failure) {
  294. if (data.isNewInvoke) {
  295. this.designer.actions.createInvoke(data, success, failure);
  296. } else {
  297. this.designer.actions.updateInvoke(data.id, data, success, failure);
  298. }
  299. },
  300. save: function(callback){
  301. if (!this.isSave){
  302. /*var session = this.editor.editor.getSession();
  303. var annotations = session.getAnnotations();
  304. var validated = true;
  305. for (var i=0; i<annotations.length; i++){
  306. if (annotations[i].type=="error"){
  307. validated = false;
  308. break;
  309. }
  310. }*/
  311. var validated = this.editor.validated();
  312. var name = this.designer.propertyNameNode.get("value");
  313. var alias = this.designer.propertyAliasNode.get("value");
  314. var description = this.designer.propertyDescriptionNode.get("value");
  315. var remoteAddrRegex = this.designer.propertyRemoteAddrRegexNode.get("value");
  316. var enable = true;
  317. this.designer.propertyEnableNode.getElements("option").each( function(option){
  318. if( option.selected ){
  319. enable = (option.value == "true");
  320. }
  321. });
  322. var enableToken = true;
  323. this.designer.propertyEnableTokenNode.getElements("option").each( function(option){
  324. if( option.selected ){
  325. enableToken = (option.value == "true");
  326. }
  327. });
  328. var requireBodyScript = "";
  329. if(this.designer.propertyRequireBodyScriptArea){
  330. requireBodyScript = this.designer.propertyRequireBodyScriptArea.toJson().code;
  331. }
  332. var simulaToken = "";
  333. if(this.designer.propertyTokenNode){
  334. this.designer.propertyTokenNode.getElements("option").each( function(option){
  335. if( option.selected ){
  336. simulaToken = option.value;
  337. }
  338. });
  339. }
  340. if (!name){
  341. this.designer.notice(this.designer.lp.notice.inputName, "error");
  342. return false;
  343. }
  344. //if(!remoteAddrRegex){
  345. // this.designer.notice(this.designer.lp.notice.inputRemoteAddrRegex, "error");
  346. // return false;
  347. //}
  348. this.data.name = name;
  349. this.data.alias = alias;
  350. this.data.description = description;
  351. this.data.remoteAddrRegex = remoteAddrRegex;
  352. this.data.validated = validated;
  353. this.data.text = this.editor.editor.getValue();
  354. this.data.enable = enable;
  355. this.data.enableToken = enableToken;
  356. this.data.data = JSON.stringify({
  357. requireBodyScript: requireBodyScript,
  358. simulaToken: simulaToken
  359. });
  360. this.isSave = true;
  361. this.saveInvoke(this.data, function(json){
  362. this.isSave = false;
  363. if( this.data.isNewInvoke ){
  364. this.data.isNewInvoke = false;
  365. this.setButton();
  366. }
  367. this.isChanged = false;
  368. this.page.textNode.set("text", this.data.name);
  369. if (this.lisNode) {
  370. this.lisNode.getLast().set("text", this.data.name);
  371. }
  372. this.designer.notice(this.designer.lp.notice.save_success, "success", this.node, {"x": "left", "y": "bottom"});
  373. this.data.id = json.data.id;
  374. this.designer.propertyIdNode.set("text", this.data.id );
  375. this.setInvokeUrlText()
  376. // var action = this.designer.actions.action;
  377. // var uri = action.address + action.actions.executeInvoke.uri ;
  378. // uri = uri.replace("{flag}", this.data.alias || this.data.name || this.data.id );
  379. // this.designer.propertyInvokeUriNode.set("text", uri);
  380. if (callback) callback();
  381. }.bind(this), function(xhr, text, error){
  382. this.isSave = false;
  383. var errorText = error+":"+text;
  384. if (xhr) errorText = xhr.responseText;
  385. MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  386. }.bind(this));
  387. }else{
  388. MWF.xDesktop.notice("info", {x: "right", y:"top"}, this.designer.lp.isSave);
  389. }
  390. },
  391. saveSilence: function(callback){
  392. if (!this.isSave){
  393. var session = this.editor.editor.getSession();
  394. var annotations = session.getAnnotations();
  395. var validated = true;
  396. for (var i=0; i<annotations.length; i++){
  397. if (annotations[i].type=="error"){
  398. validated = false;
  399. break;
  400. }
  401. }
  402. if( this.designer.currentScript == this ){
  403. var name = this.designer.propertyNameNode.get("value");
  404. var alias = this.designer.propertyAliasNode.get("value");
  405. var description = this.designer.propertyDescriptionNode.get("value");
  406. var remoteAddrRegex = this.designer.propertyRemoteAddrRegexNode.get("value");
  407. var enable = true;
  408. this.designer.propertyEnableNode.getElements("option").each( function(option){
  409. if( option.selected ){
  410. enable = (option.value == "true");
  411. }
  412. });
  413. var requireBodyScript = "";
  414. if(this.designer.propertyRequireBodyScriptArea){
  415. requireBodyScript = this.designer.propertyRequireBodyScriptArea.toJson().code;
  416. }
  417. var simulaToken = "";
  418. if(this.designer.propertyTokenNode){
  419. this.designer.propertyTokenNode.getElements("option").each( function(option){
  420. if( option.selected ){
  421. simulaToken = option.value;
  422. }
  423. });
  424. }
  425. if (!name){
  426. this.designer.notice(this.designer.lp.notice.inputName, "error");
  427. return false;
  428. }
  429. //if(!remoteAddrRegex){
  430. // this.designer.notice(this.designer.lp.notice.inputRemoteAddrRegex, "error");
  431. // return false;
  432. //}
  433. this.data.name = name;
  434. this.data.alias = alias;
  435. this.data.description = description;
  436. this.data.remoteAddrRegex = remoteAddrRegex;
  437. this.data.validated = validated;
  438. this.data.enable = enable;
  439. this.data.data = JSON.stringify({
  440. requireBodyScript: requireBodyScript,
  441. simulaToken: simulaToken
  442. });
  443. }
  444. this.data.text = this.editor.editor.getValue();
  445. this.isSave = true;
  446. this.saveInvoke(this.data, function(json){
  447. this.isSave = false;
  448. if( this.data.isNewInvoke ){
  449. this.data.isNewInvoke = false;
  450. this.setButton();
  451. }
  452. this.data.isNewInvoke = false;
  453. this.isChanged = false;
  454. this.page.textNode.set("text", this.data.name);
  455. if (this.lisNode) {
  456. this.lisNode.getLast().set("text", this.data.name);
  457. }
  458. this.data.id = json.data.id;
  459. if( this.designer.currentScript == this ){
  460. this.designer.propertyIdNode.set("text", this.data.id );
  461. this.setInvokeUrlText();
  462. // var action = this.designer.actions.action;
  463. // var uri = action.address + action.actions.executeInvoke.uri ;
  464. // uri = uri.replace("{flag}", this.data.alias || this.data.name || this.data.id );
  465. // this.designer.propertyInvokeUriNode.set("text", uri);
  466. }
  467. if (callback) callback();
  468. }.bind(this), function(xhr, text, error){
  469. this.isSave = false;
  470. //
  471. //var errorText = error+":"+text;
  472. //if (xhr) errorText = xhr.responseText;
  473. //MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
  474. }.bind(this));
  475. }else{
  476. MWF.xDesktop.notice("info", {x: "right", y:"top"}, this.designer.lp.isSave);
  477. }
  478. },
  479. saveAs: function(){},
  480. explode: function(){},
  481. implode: function(){}
  482. });