SubSource.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. //COMMON.AjaxModule.load("JSONTemplate", null, false);
  3. /** @class SubSource 子数据源。
  4. * @o2cn 子数据源
  5. * @example
  6. * //可以在脚本中获取该组件
  7. * //方法1:
  8. * var subSource = this.form.get("fieldId"); //获取组件
  9. * //方法2
  10. * var subSource = this.target; //在组件本身的脚本中获取
  11. * @extends MWF.xApplication.process.Xform.$Module
  12. * @o2category FormComponents
  13. * @o2range {Portal}
  14. * @hideconstructor
  15. */
  16. MWF.xApplication.process.Xform.SubSource = MWF.APPSubSource = new Class(
  17. /** @lends MWF.xApplication.process.Xform.SubSource# */
  18. {
  19. Extends: MWF.APP$Module,
  20. options: {
  21. /**
  22. * 加载数据后执行,但这时还未加载下属组件,可以可以使用this.target.data获取数据进行修改。
  23. * @event MWF.xApplication.process.Xform.SubSource#postLoadData
  24. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  25. */
  26. /**
  27. * 加载数据、下属组件后执行。
  28. * @event MWF.xApplication.process.Xform.SubSource#loadData
  29. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  30. */
  31. "moduleEvents": ["queryLoad","postLoad","load", "postLoadData", "loadData"]
  32. },
  33. load: function(){
  34. /**
  35. * @ignore
  36. * @member parentLine
  37. * @memberOf MWF.xApplication.process.Xform.SubSource#
  38. */
  39. this._loadModuleEvents();
  40. this._queryLoaded();
  41. this._loadUserInterface();
  42. //this._loadStyles();
  43. //this._loadEvents();
  44. // this._loadDomEvents();
  45. this._afterLoaded();
  46. },
  47. _loadUserInterface: function(){
  48. this.loopNodes = [];
  49. this.subSourceItems = [];
  50. var node = new Element("div").inject(this.node, "before");
  51. this.node.inject(node);
  52. this.loopNode = this.node.dispose();
  53. this.node = node;
  54. var id = node.get("id");
  55. node.set("id", "");
  56. this.node.set({
  57. "id": id,
  58. "mwftype": node.get("mwftype")
  59. });
  60. this.node.store("module", this);
  61. this._loadJsonData();
  62. },
  63. _getSource: function(){
  64. var parent = this.node.getParent();
  65. while(parent && (parent.get("MWFtype")!="source" && parent.get("MWFtype")!="subSource" && parent.get("MWFtype")!="subSourceItem")) parent = parent.getParent();
  66. return (parent) ? parent.retrieve("module") : null;
  67. },
  68. _getSourceData: function(sourceData){
  69. var data = sourceData;
  70. if (this.json.jsonPath!="."){
  71. var paths = this.json.jsonPath.split(".");
  72. paths.each(function(p){
  73. data = data[p];
  74. }.bind(this));
  75. }
  76. /**
  77. * @summary 该属性获取当前子数据源的数据,当所在上级数据源加载完成后才有值。
  78. * @member {Array|Object|String|Number|Boolean|Null}
  79. * @example
  80. * var field = this.form.get("fieldId").data; //获取子数据源数据
  81. */
  82. this.data = data;
  83. },
  84. _loopSub: function(dom, i){
  85. var _self = this;
  86. var moduleNodes = this.form._getModuleNodes(dom);
  87. moduleNodes.each(function(node){
  88. var json = this.form._getDomjson(node);
  89. var subJson = Object.clone(json);
  90. subJson.id = subJson.id+"_"+i;
  91. node.set("id", subJson.id);
  92. var module = this.form._loadModule(subJson, node, function(){
  93. if( _self.widget )this.widget = _self.widget;
  94. }, true);
  95. //this.modules.push(module);
  96. }.bind(this));
  97. },
  98. _loopData: function(){
  99. var _self = this;
  100. this.data.each(function(d, i){
  101. var node = this.loopNode.clone(true, true);
  102. node.inject(this.node);
  103. var json = Object.clone(this.json);
  104. json.id = json.id+"_"+i;
  105. json.type = "SubSourceItem";
  106. node.set({
  107. "id": json.id,
  108. "mwftype": "subSourceItem"
  109. });
  110. var module = this.form._loadModule(json, node, function(){
  111. if( _self.widget )this.widget = _self.widget;
  112. this.data = d;
  113. this.position = i;
  114. }, true);
  115. this.subSourceItems.push(module);
  116. this.loopNodes.push(node);
  117. this._loopSub(node, i);
  118. }.bind(this));
  119. },
  120. _initSubSource: function(){
  121. if (this.loopNode){
  122. var moduleNodes = this.form._getModuleNodes(this.node);
  123. moduleNodes.each(function(node){
  124. var module = node.retrieve("module");
  125. if (module){
  126. if (module.json.type=="SubSource"){
  127. module._initSubSource();
  128. }else{
  129. MWF.release(module);
  130. }
  131. }
  132. }.bind(this));
  133. this.node.empty();
  134. }
  135. this.loopNodes = [];
  136. this.subSourceItems = [];
  137. },
  138. _loadJsonData: function(notInit){
  139. if (!notInit) this._initSubSource();
  140. this.source = this._getSource();
  141. if (this.source){
  142. if (this.source.data){
  143. this._getSourceData(this.source.data);
  144. this.fireEvent("postLoadData");
  145. if (typeOf(this.data)!=="array") this.data = [this.data];
  146. if (typeOf(this.data)=="array"){
  147. this._loopData();
  148. this.fireEvent("loadData");
  149. }else{
  150. var _self = this;
  151. this.form._loadModules(this.node, function () {
  152. if( _self.widget )this.widget = _self.widget;
  153. }, true);
  154. }
  155. //this.tmpDiv = new Element("div");
  156. //var html = "{loop:"+this.json.jsonPath+"}"+this.node.outerHTML+"{/loop:"+this.json.jsonPath+"}";
  157. ////this.template = new Template();
  158. ////var loopHtml = this.template.substitute("{"+this.json.jsonPath+"}", this.source.data);
  159. //this.node.set("text", this.text);
  160. }
  161. }
  162. }
  163. });
  164. MWF.xApplication.process.Xform.SubSourceItem = MWF.APPSubSourceItem = new Class({
  165. Extends: MWF.APP$Module,
  166. _loadUserInterface: function(){
  167. this.loopNodes = [];
  168. this.subSourceItems = [];
  169. }
  170. });