Source.js 4.9 KB

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