SubSource.js 5.5 KB

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