Source.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. if (this.json.path) 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. debugger;
  70. this._loadSub(this.node);
  71. this.fireEvent("loadData");
  72. }.bind(this));
  73. },
  74. _invoke: function(callback){
  75. MWF.restful(this.json.httpMethod, this.uri, JSON.encode(this.body), function(json){
  76. this.data = json;
  77. if (callback) callback();
  78. }.bind(this), true, true);
  79. },
  80. setBody: function(data){
  81. this.body = data;
  82. },
  83. setParameters: function(json){
  84. this.json.parameters = json;
  85. this._getO2Address();
  86. this._getO2Uri();
  87. },
  88. addParameters: function(json){
  89. if (!this.json.parameters) this.json.parameters={};
  90. Object.each(json, function(v, k){
  91. this.json.parameters[k] = v;
  92. }.bind(this));
  93. this._getO2Address();
  94. this._getO2Uri();
  95. },
  96. reload: function(notInit, callback){
  97. this._invoke(function(){
  98. this._loadSub(this.node, notInit);
  99. this.fireEvent("loadData");
  100. if (callback) callback();
  101. }.bind(this));
  102. },
  103. _loadSub: function(dom, notInit){
  104. var subDom = dom.getFirst();
  105. var module = null;
  106. while (subDom){
  107. module = null;
  108. module = subDom.retrieve("module", null);
  109. if (module){
  110. if (module._loadJsonData) module._loadJsonData(notInit);
  111. }else{
  112. this._loadSub(subDom);
  113. }
  114. //var type = subDom.get("MWFtype");
  115. //if (type){
  116. // if (type=="sourceText"){
  117. // module = subDom.retrieve("module");
  118. // module._loadData();
  119. // }else if (type=="subSource"){
  120. // module = subDom.retrieve("module");
  121. // module._loadData();
  122. // }else{
  123. // this._loadSub(subDom);
  124. // }
  125. //}else{
  126. // this._loadSub(subDom);
  127. //}
  128. subDom = subDom.getNext();
  129. }
  130. }
  131. });