Source.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. MWF.xDesktop.requireApp("process.Xform", "Div", null, false);
  2. /** @class Source 数据源组件。
  3. * @example
  4. * //可以在脚本中获取该组件
  5. * //方法1:
  6. * var source = this.form.get("fieldId"); //获取数据源组件
  7. * //方法2
  8. * var source = this.target; //在组件本身的脚本中获取
  9. * @extends MWF.xApplication.process.Xform.Div
  10. * @o2category FormComponents
  11. * @o2range {Portal}
  12. * @hideconstructor
  13. */
  14. MWF.xApplication.process.Xform.Source = MWF.APPSource = new Class(
  15. /** @lends MWF.xApplication.process.Xform.Source# */
  16. {
  17. Extends: MWF.APPDiv,
  18. options: {
  19. /**
  20. * 加载数据后执行,但这时还未加载下属组件,可以可以使用this.target.data获取数据进行修改。
  21. * @event MWF.xApplication.process.Xform.Source#postLoadData
  22. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  23. */
  24. /**
  25. * 加载数据、下属组件后执行。
  26. * @event MWF.xApplication.process.Xform.Source#loadData
  27. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  28. */
  29. "moduleEvents": ["queryLoad","postLoad","load", "postLoadData", "loadData"]
  30. },
  31. _loadUserInterface: function(){
  32. this.data = null;
  33. if (this.json.path){
  34. if (this.json.sourceType=="o2"){
  35. if (this.json.path) this._getO2Source();
  36. }
  37. }
  38. },
  39. _getO2Address: function(){
  40. try {
  41. this.json.service = JSON.parse(this.json.contextRoot);
  42. }catch(e){
  43. this.json.service = {"root": this.json.contextRoot, "action":"", "method": "", "url": ""};
  44. }
  45. var addressObj = layout.serviceAddressList[this.json.service.root];
  46. if (addressObj){
  47. this.address = layout.config.app_protocol+"//"+addressObj.host+(addressObj.port==80 ? "" : ":"+addressObj.port)+addressObj.context;
  48. }else{
  49. var host = layout.desktop.centerServer.host || window.location.hostname;
  50. var port = layout.desktop.centerServer.port;
  51. this.address = layout.config.app_protocol+"//"+host+(port=="80" ? "" : ":"+port)+"/x_program_center";
  52. }
  53. },
  54. //_getConfigParameters: function(){
  55. //
  56. // return pars;
  57. //},
  58. _getO2Uri: function(){
  59. //var uri = this.json.path || this.json.selectPath;
  60. var uri = this.json.path;
  61. var pars = {};
  62. if (this.json.parameters){
  63. Object.each(this.json.parameters, function(v, key){
  64. if (uri.indexOf("{"+key+"}")!=-1){
  65. var reg = new RegExp("{"+key+"}", "g");
  66. uri = uri.replace(reg, encodeURIComponent((v && v.code) ? (this.form.Macro.exec(v.code, this) || "") : v));
  67. }else{
  68. pars[key] = v;
  69. }
  70. }.bind(this));
  71. }
  72. var data = null;
  73. if (this.json.requestBody){
  74. if (this.json.requestBody.code){
  75. data = this.form.Macro.exec(this.json.requestBody.code, this)
  76. }
  77. }
  78. if (this.json.httpMethod=="GET" || this.json.httpMethod=="OPTIONS" || this.json.httpMethod=="HEAD" || this.json.httpMethod=="DELETE"){
  79. var tag = "?";
  80. if (uri.indexOf("?")!=-1) tag = "&";
  81. Object.each(pars, function(v, k){
  82. var value = (v && v.code) ? (this.form.Macro.exec(v.code, this) || "") : v;
  83. uri = uri+tag+k+"="+value;
  84. }.bind(this));
  85. }else{
  86. Object.each(pars, function(v, k){
  87. if (!data) data = {};
  88. var value = (v && v.code) ? (this.form.Macro.exec(v.code, this) || "") : v;
  89. data[k] = value;
  90. }.bind(this));
  91. }
  92. this.body = data;
  93. this.uri = this.address+uri;
  94. },
  95. _getO2Source: function(){
  96. this._getO2Address();
  97. this._getO2Uri();
  98. this._invoke(function(){
  99. this._loadSub(this.node);
  100. this.fireEvent("loadData");
  101. }.bind(this));
  102. },
  103. _invoke: function(callback){
  104. MWF.restful(this.json.httpMethod, this.uri, JSON.encode(this.body), function(json){
  105. this.data = json;
  106. this.fireEvent("postLoadData");
  107. if (callback) callback();
  108. }.bind(this), true, true);
  109. },
  110. setBody: function(data){
  111. this.body = data;
  112. },
  113. /**
  114. * @summary 替换全部的url参数,但不刷新组件
  115. * @param {Object} url参数
  116. * @example
  117. * //如,原来的组件url参数为:
  118. * { "page" : 1, "count" : 10 }
  119. *
  120. * this.form.get("fieldId").setParameters({"id":"662ede34-4e21-428a-9c3b-f1bf14d15650"});
  121. *
  122. * //执行后变为
  123. * {"id":"662ede34-4e21-428a-9c3b-f1bf14d15650"}
  124. */
  125. setParameters: function(json){
  126. this.json.parameters = json;
  127. this._getO2Address();
  128. this._getO2Uri();
  129. },
  130. /**
  131. * @summary 新增url参数,但不刷新组件。如果该参数key已经存在,则覆盖
  132. * @param {Object} url参数
  133. * @example
  134. * * //如,原来的组件url参数为:
  135. * { "page" : 1, "count" : 10 }
  136. *
  137. * this.form.get("fieldId").addParameters({
  138. * "page" : 2,
  139. * "id":"662ede34-4e21-428a-9c3b-f1bf14d15650"
  140. * });
  141. *
  142. * //执行后变为
  143. * {
  144. * "page" : 2,
  145. * "count" : 10
  146. * "id":"662ede34-4e21-428a-9c3b-f1bf14d15650"
  147. * }
  148. */
  149. addParameters: function(json){
  150. if (!this.json.parameters) this.json.parameters={};
  151. Object.each(json, function(v, k){
  152. this.json.parameters[k] = v;
  153. }.bind(this));
  154. this._getO2Address();
  155. this._getO2Uri();
  156. },
  157. /**
  158. * @summary 重新加载组件。会触发loadData事件
  159. * @param {Boolean} notInit - false表示不重新初始化子数据源和数据文本,true表示重新初始化,默认为false
  160. * @param {Function} callback 加载完成后的回调
  161. * @example
  162. * this.form.get("fieldId").reload(); //重新加载组件
  163. */
  164. reload: function(notInit, callback){
  165. this._getO2Uri();
  166. this._invoke(function(){
  167. this._loadSub(this.node, notInit);
  168. this.fireEvent("loadData");
  169. if (callback) callback();
  170. }.bind(this));
  171. },
  172. _loadSub: function(dom, notInit){
  173. var subDom = dom.getFirst();
  174. var module = null;
  175. while (subDom){
  176. module = null;
  177. module = subDom.retrieve("module", null);
  178. if (module){
  179. if (module._loadJsonData) module._loadJsonData(notInit);
  180. }else{
  181. this._loadSub(subDom);
  182. }
  183. //var type = subDom.get("MWFtype");
  184. //if (type){
  185. // if (type=="sourceText"){
  186. // module = subDom.retrieve("module");
  187. // module._loadData();
  188. // }else if (type=="subSource"){
  189. // module = subDom.retrieve("module");
  190. // module._loadData();
  191. // }else{
  192. // this._loadSub(subDom);
  193. // }
  194. //}else{
  195. // this._loadSub(subDom);
  196. //}
  197. subDom = subDom.getNext();
  198. }
  199. }
  200. });