SubSource.js 4.3 KB

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