Source.js 4.6 KB

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