Source.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. if (!this.json.isDelay){
  102. this._getO2Uri();
  103. this._invoke(function(){
  104. this._loadSub(this.node);
  105. this.fireEvent("loadData");
  106. }.bind(this));
  107. }
  108. },
  109. active: function(){
  110. this._getO2Uri();
  111. this._invoke(function(){
  112. this._loadSub(this.node);
  113. this.fireEvent("loadData");
  114. }.bind(this));
  115. },
  116. _invoke: function(callback){
  117. MWF.restful(this.json.httpMethod, this.uri, JSON.encode(this.body), function(json){
  118. /**
  119. * @summary 该属性获取当前数据源的数据,当数据源加载完成后才有值。
  120. * @member {Array|Object|String|Number|Boolean|Null}
  121. * @example
  122. * var field = this.form.get("fieldId").data; //获取数据源数据
  123. */
  124. this.data = json;
  125. this.fireEvent("postLoadData");
  126. if (callback) callback();
  127. }.bind(this), true, true);
  128. },
  129. setBody: function(data){
  130. this.body = data;
  131. },
  132. /**
  133. * @summary 替换全部的url参数,但不刷新组件
  134. * @param {Object} url参数
  135. * @example
  136. * //如,原来的组件url参数为:
  137. * { "page" : 1, "count" : 10 }
  138. *
  139. * this.form.get("fieldId").setParameters({"id":"662ede34-4e21-428a-9c3b-f1bf14d15650"});
  140. *
  141. * //执行后变为
  142. * {"id":"662ede34-4e21-428a-9c3b-f1bf14d15650"}
  143. */
  144. setParameters: function(json){
  145. this.json.parameters = json;
  146. this._getO2Address();
  147. this._getO2Uri();
  148. },
  149. /**
  150. * @summary 新增url参数,但不刷新组件。如果该参数key已经存在,则覆盖
  151. * @param {Object} url参数
  152. * @example
  153. * * //如,原来的组件url参数为:
  154. * { "page" : 1, "count" : 10 }
  155. *
  156. * this.form.get("fieldId").addParameters({
  157. * "page" : 2,
  158. * "id":"662ede34-4e21-428a-9c3b-f1bf14d15650"
  159. * });
  160. *
  161. * //执行后变为
  162. * {
  163. * "page" : 2,
  164. * "count" : 10
  165. * "id":"662ede34-4e21-428a-9c3b-f1bf14d15650"
  166. * }
  167. */
  168. addParameters: function(json){
  169. if (!this.json.parameters) this.json.parameters={};
  170. Object.each(json, function(v, k){
  171. this.json.parameters[k] = v;
  172. }.bind(this));
  173. this._getO2Address();
  174. this._getO2Uri();
  175. },
  176. /**
  177. * @summary 重新加载组件。会触发loadData事件
  178. * @param {Boolean} notInit - false表示不重新初始化子数据源和数据文本,true表示重新初始化,默认为false
  179. * @param {Function} callback 加载完成后的回调
  180. * @example
  181. * this.form.get("fieldId").reload(); //重新加载组件
  182. */
  183. reload: function(notInit, callback){
  184. this._getO2Uri();
  185. this._invoke(function(){
  186. this._loadSub(this.node, notInit);
  187. this.fireEvent("loadData");
  188. if (callback) callback();
  189. }.bind(this));
  190. },
  191. _loadSub: function(dom, notInit){
  192. var subDom = dom.getFirst();
  193. var module = null;
  194. while (subDom){
  195. module = null;
  196. module = subDom.retrieve("module", null);
  197. if (module){
  198. if (module._loadJsonData) module._loadJsonData(notInit);
  199. }else{
  200. this._loadSub(subDom);
  201. }
  202. //var type = subDom.get("MWFtype");
  203. //if (type){
  204. // if (type=="sourceText"){
  205. // module = subDom.retrieve("module");
  206. // module._loadData();
  207. // }else if (type=="subSource"){
  208. // module = subDom.retrieve("module");
  209. // module._loadData();
  210. // }else{
  211. // this._loadSub(subDom);
  212. // }
  213. //}else{
  214. // this._loadSub(subDom);
  215. //}
  216. subDom = subDom.getNext();
  217. }
  218. }
  219. });