c6007da9a797ef0e34ac915817611c2635105d94c0c3f7b71eebee91b2c4d67e523d64faa9b76121d0622192c345d4e839eace512227899d4062f9a981e4db 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import _ from 'lodash';
  2. import Module from './Module';
  3. import ContentModule from './ContentModule';
  4. import ContentFolder from './ContentFolder';
  5. import {getModulePathParts} from './utils';
  6. export default class ConcatenatedModule extends Module {
  7. constructor(name, data, parent) {
  8. super(name, data, parent);
  9. this.name += ' (concatenated)';
  10. this.children = Object.create(null);
  11. this.fillContentModules();
  12. }
  13. fillContentModules() {
  14. _.each(this.data.modules, moduleData => this.addContentModule(moduleData));
  15. }
  16. addContentModule(moduleData) {
  17. const pathParts = getModulePathParts(moduleData);
  18. if (!pathParts) {
  19. return;
  20. }
  21. const [folders, fileName] = [pathParts.slice(0, -1), _.last(pathParts)];
  22. let currentFolder = this;
  23. _.each(folders, folderName => {
  24. let childFolder = currentFolder.getChild(folderName);
  25. if (!childFolder) {
  26. childFolder = currentFolder.addChildFolder(new ContentFolder(folderName, this));
  27. }
  28. currentFolder = childFolder;
  29. });
  30. const module = new ContentModule(fileName, moduleData, this);
  31. currentFolder.addChildModule(module);
  32. }
  33. getChild(name) {
  34. return this.children[name];
  35. }
  36. addChildModule(module) {
  37. module.parent = this;
  38. this.children[module.name] = module;
  39. }
  40. addChildFolder(folder) {
  41. folder.parent = this;
  42. this.children[folder.name] = folder;
  43. return folder;
  44. }
  45. mergeNestedFolders() {
  46. _.invokeMap(this.children, 'mergeNestedFolders');
  47. }
  48. toChartData() {
  49. return {
  50. ...super.toChartData(),
  51. concatenated: true,
  52. groups: _.invokeMap(this.children, 'toChartData')
  53. };
  54. }
  55. };