SubSource.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. _loadUserInterface: function(){
  9. this.loopNodes = [];
  10. this.subSourceItems = [];
  11. var node = new Element("div").inject(this.node, "before");
  12. this.node.inject(node);
  13. this.loopNode = this.node.dispose();
  14. this.node = node;
  15. var id = node.get("id");
  16. node.set("id", "");
  17. this.node.set({
  18. "id": id,
  19. "mwftype": node.get("mwftype")
  20. });
  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.json.type=="SubSource"){
  76. module._initSubSource();
  77. }else{
  78. MWF.release(module);
  79. }
  80. }.bind(this));
  81. this.node.empty();
  82. }
  83. this.loopNodes = [];
  84. this.subSourceItems = [];
  85. },
  86. _loadJsonData: function(notInit){
  87. if (!notInit) this._initSubSource();
  88. this.source = this._getSource();
  89. if (this.source){
  90. if (this.source.data){
  91. this._getSourceData(this.source.data);
  92. if (typeOf(this.data)=="array"){
  93. this._loopData();
  94. this.fireEvent("loadData");
  95. }else{
  96. this._loadModules(this.node);
  97. }
  98. //this.tmpDiv = new Element("div");
  99. //var html = "{loop:"+this.json.jsonPath+"}"+this.node.outerHTML+"{/loop:"+this.json.jsonPath+"}";
  100. ////this.template = new Template();
  101. ////var loopHtml = this.template.substitute("{"+this.json.jsonPath+"}", this.source.data);
  102. //this.node.set("text", this.text);
  103. }
  104. }
  105. }
  106. });