SubSource.js 4.7 KB

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