SubSource.js 5.5 KB

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