Source.js 7.2 KB

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