SubSource.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. //COMMON.AjaxModule.load("JSONTemplate", null, false);
  3. MWF.xApplication.process.Xform.SubSource = MWF.APPSubSource = new Class({
  4. Extends: MWF.APP$Module,
  5. options: {
  6. "moduleEvents": ["queryLoad","postLoad","load", "postLoadData", "loadData"]
  7. },
  8. load: function(){
  9. this._loadModuleEvents();
  10. this._queryLoaded();
  11. this._loadUserInterface();
  12. //this._loadStyles();
  13. //this._loadEvents();
  14. this._loadDomEvents();
  15. this._afterLoaded();
  16. },
  17. _loadUserInterface: function(){
  18. this.loopNodes = [];
  19. this.subSourceItems = [];
  20. var node = new Element("div").inject(this.node, "before");
  21. this.node.inject(node);
  22. this.loopNode = this.node.dispose();
  23. this.node = node;
  24. var id = node.get("id");
  25. node.set("id", "");
  26. this.node.set({
  27. "id": id,
  28. "mwftype": node.get("mwftype")
  29. });
  30. this.node.store("module", this);
  31. this._loadJsonData();
  32. },
  33. _getSource: function(){
  34. var parent = this.node.getParent();
  35. while(parent && (parent.get("MWFtype")!="source" && parent.get("MWFtype")!="subSource" && parent.get("MWFtype")!="subSourceItem")) parent = parent.getParent();
  36. return (parent) ? parent.retrieve("module") : null;
  37. },
  38. _getSourceData: function(sourceData){
  39. var data = sourceData;
  40. if (this.json.jsonPath!="."){
  41. var paths = this.json.jsonPath.split(".");
  42. paths.each(function(p){
  43. data = data[p];
  44. }.bind(this));
  45. }
  46. this.data = data;
  47. },
  48. _loopSub: function(dom, i){
  49. var moduleNodes = this.form._getModuleNodes(dom);
  50. moduleNodes.each(function(node){
  51. var json = this.form._getDomjson(node);
  52. var subJson = Object.clone(json);
  53. subJson.id = subJson.id+"_"+i;
  54. node.set("id", subJson.id);
  55. var module = this.form._loadModule(subJson, node);
  56. //this.modules.push(module);
  57. }.bind(this));
  58. },
  59. _loopData: function(){
  60. this.data.each(function(d, i){
  61. var node = this.loopNode.clone(true, true);
  62. node.inject(this.node);
  63. var json = Object.clone(this.json);
  64. json.id = json.id+"_"+i;
  65. json.type = "SubSourceItem";
  66. node.set({
  67. "id": json.id,
  68. "mwftype": "subSourceItem"
  69. });
  70. var module = this.form._loadModule(json, node, function(){
  71. this.data = d;
  72. this.position = i;
  73. });
  74. this.subSourceItems.push(module);
  75. this.loopNodes.push(node);
  76. this._loopSub(node, i);
  77. }.bind(this));
  78. },
  79. _initSubSource: function(){
  80. if (this.loopNode){
  81. var moduleNodes = this.form._getModuleNodes(this.node);
  82. moduleNodes.each(function(node){
  83. var module = node.retrieve("module");
  84. if (module){
  85. if (module.json.type=="SubSource"){
  86. module._initSubSource();
  87. }else{
  88. MWF.release(module);
  89. }
  90. }
  91. }.bind(this));
  92. this.node.empty();
  93. }
  94. this.loopNodes = [];
  95. this.subSourceItems = [];
  96. },
  97. _loadJsonData: function(notInit){
  98. if (!notInit) this._initSubSource();
  99. this.source = this._getSource();
  100. if (this.source){
  101. if (this.source.data){
  102. this._getSourceData(this.source.data);
  103. this.fireEvent("postLoadData");
  104. if (typeOf(this.data)!=="array") this.data = [this.data];
  105. if (typeOf(this.data)=="array"){
  106. this._loopData();
  107. this.fireEvent("loadData");
  108. }else{
  109. this.form._loadModules(this.node);
  110. }
  111. //this.tmpDiv = new Element("div");
  112. //var html = "{loop:"+this.json.jsonPath+"}"+this.node.outerHTML+"{/loop:"+this.json.jsonPath+"}";
  113. ////this.template = new Template();
  114. ////var loopHtml = this.template.substitute("{"+this.json.jsonPath+"}", this.source.data);
  115. //this.node.set("text", this.text);
  116. }
  117. }
  118. }
  119. });
  120. MWF.xApplication.process.Xform.SubSourceItem = MWF.APPSubSourceItem = new Class({
  121. Extends: MWF.APP$Module,
  122. _loadUserInterface: function(){
  123. this.loopNodes = [];
  124. this.subSourceItems = [];
  125. }
  126. });