Source.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. MWF.xDesktop.requireApp("process.Xform", "Div", null, false);
  2. MWF.xApplication.process.Xform.Source = MWF.APPSource = new Class({
  3. Extends: MWF.APPDiv,
  4. options: {
  5. "moduleEvents": ["queryLoad","postLoad","load", "postLoadData", "loadData"]
  6. },
  7. _loadUserInterface: function(){
  8. this.data = null;
  9. if (this.json.path){
  10. if (this.json.sourceType=="o2"){
  11. if (this.json.path) this._getO2Source();
  12. }
  13. }
  14. },
  15. _getO2Address: function(){
  16. try {
  17. this.json.service = JSON.parse(this.json.contextRoot);
  18. }catch(e){
  19. this.json.service = {"root": this.json.contextRoot, "action":"", "method": "", "url": ""};
  20. }
  21. var addressObj = layout.serviceAddressList[this.json.service.root];
  22. if (addressObj){
  23. this.address = layout.config.app_protocol+"//"+addressObj.host+(addressObj.port==80 ? "" : ":"+addressObj.port)+addressObj.context;
  24. }else{
  25. var host = layout.desktop.centerServer.host || window.location.hostname;
  26. var port = layout.desktop.centerServer.port;
  27. this.address = layout.config.app_protocol+"//"+host+(port=="80" ? "" : ":"+port)+"/x_program_center";
  28. }
  29. },
  30. //_getConfigParameters: function(){
  31. //
  32. // return pars;
  33. //},
  34. _getO2Uri: function(){
  35. //var uri = this.json.path || this.json.selectPath;
  36. var uri = this.json.path;
  37. var pars = {};
  38. if (this.json.parameters){
  39. Object.each(this.json.parameters, function(v, key){
  40. if (uri.indexOf("{"+key+"}")!=-1){
  41. var reg = new RegExp("{"+key+"}", "g");
  42. uri = uri.replace(reg, encodeURIComponent((v && v.code) ? (this.form.Macro.exec(v.code, this) || "") : v));
  43. }else{
  44. pars[key] = v;
  45. }
  46. }.bind(this));
  47. }
  48. var data = null;
  49. if (this.json.requestBody){
  50. if (this.json.requestBody.code){
  51. data = this.form.Macro.exec(this.json.requestBody.code, this)
  52. }
  53. }
  54. if (this.json.httpMethod=="GET" || this.json.httpMethod=="OPTIONS" || this.json.httpMethod=="HEAD" || this.json.httpMethod=="DELETE"){
  55. var tag = "?";
  56. if (uri.indexOf("?")!=-1) tag = "&";
  57. Object.each(pars, function(v, k){
  58. var value = (v && v.code) ? (this.form.Macro.exec(v.code, this) || "") : v;
  59. uri = uri+tag+k+"="+value;
  60. }.bind(this));
  61. }else{
  62. Object.each(pars, function(v, k){
  63. if (!data) data = {};
  64. var value = (v && v.code) ? (this.form.Macro.exec(v.code, this) || "") : v;
  65. data[k] = value;
  66. }.bind(this));
  67. }
  68. this.body = data;
  69. this.uri = this.address+uri;
  70. },
  71. _getO2Source: function(){
  72. this._getO2Address();
  73. this._getO2Uri();
  74. this._invoke(function(){
  75. this._loadSub(this.node);
  76. this.fireEvent("loadData");
  77. }.bind(this));
  78. },
  79. _invoke: function(callback){
  80. MWF.restful(this.json.httpMethod, this.uri, JSON.encode(this.body), function(json){
  81. this.data = json;
  82. this.fireEvent("postLoadData");
  83. if (callback) callback();
  84. }.bind(this), true, true);
  85. },
  86. setBody: function(data){
  87. this.body = data;
  88. },
  89. setParameters: function(json){
  90. this.json.parameters = json;
  91. this._getO2Address();
  92. this._getO2Uri();
  93. },
  94. addParameters: function(json){
  95. if (!this.json.parameters) this.json.parameters={};
  96. Object.each(json, function(v, k){
  97. this.json.parameters[k] = v;
  98. }.bind(this));
  99. this._getO2Address();
  100. this._getO2Uri();
  101. },
  102. reload: function(notInit, callback){
  103. this._invoke(function(){
  104. this._loadSub(this.node, notInit);
  105. this.fireEvent("loadData");
  106. if (callback) callback();
  107. }.bind(this));
  108. },
  109. _loadSub: function(dom, notInit){
  110. var subDom = dom.getFirst();
  111. var module = null;
  112. while (subDom){
  113. module = null;
  114. module = subDom.retrieve("module", null);
  115. if (module){
  116. if (module._loadJsonData) module._loadJsonData(notInit);
  117. }else{
  118. this._loadSub(subDom);
  119. }
  120. //var type = subDom.get("MWFtype");
  121. //if (type){
  122. // if (type=="sourceText"){
  123. // module = subDom.retrieve("module");
  124. // module._loadData();
  125. // }else if (type=="subSource"){
  126. // module = subDom.retrieve("module");
  127. // module._loadData();
  128. // }else{
  129. // this._loadSub(subDom);
  130. // }
  131. //}else{
  132. // this._loadSub(subDom);
  133. //}
  134. subDom = subDom.getNext();
  135. }
  136. }
  137. });