Source.js 4.5 KB

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