Source.js 7.9 KB

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