Invoke.js 15 KB

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