BaseFolder.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  6. var _lodash = require('lodash');
  7. var _lodash2 = _interopRequireDefault(_lodash);
  8. var _Node2 = require('./Node');
  9. var _Node3 = _interopRequireDefault(_Node2);
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  12. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  13. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  14. var BaseFolder = function (_Node) {
  15. _inherits(BaseFolder, _Node);
  16. function BaseFolder(name, parent) {
  17. _classCallCheck(this, BaseFolder);
  18. var _this = _possibleConstructorReturn(this, (BaseFolder.__proto__ || Object.getPrototypeOf(BaseFolder)).call(this, name, parent));
  19. _this.children = Object.create(null);
  20. return _this;
  21. }
  22. _createClass(BaseFolder, [{
  23. key: 'getChild',
  24. value: function getChild(name) {
  25. return this.children[name];
  26. }
  27. }, {
  28. key: 'addChildModule',
  29. value: function addChildModule(module) {
  30. var name = module.name;
  31. var currentChild = this.children[name];
  32. // For some reason we already have this node in children and it's a folder.
  33. if (currentChild && currentChild instanceof BaseFolder) return;
  34. if (currentChild) {
  35. // We already have this node in children and it's a module.
  36. // Merging it's data.
  37. currentChild.mergeData(module.data);
  38. } else {
  39. // Pushing new module
  40. module.parent = this;
  41. this.children[name] = module;
  42. }
  43. delete this._size;
  44. delete this._src;
  45. }
  46. }, {
  47. key: 'addChildFolder',
  48. value: function addChildFolder(folder) {
  49. folder.parent = this;
  50. this.children[folder.name] = folder;
  51. delete this._size;
  52. delete this._src;
  53. return folder;
  54. }
  55. }, {
  56. key: 'walk',
  57. value: function walk(walker) {
  58. var state = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  59. var deep = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
  60. var stopped = false;
  61. _lodash2.default.each(this.children, function (child) {
  62. if (deep && child.walk) {
  63. state = child.walk(walker, state, stop);
  64. } else {
  65. state = walker(child, state, stop);
  66. }
  67. if (stopped) return false;
  68. });
  69. return state;
  70. function stop(finalState) {
  71. stopped = true;
  72. return finalState;
  73. }
  74. }
  75. }, {
  76. key: 'toChartData',
  77. value: function toChartData() {
  78. return {
  79. label: this.name,
  80. path: this.path,
  81. statSize: this.size,
  82. groups: _lodash2.default.invokeMap(this.children, 'toChartData')
  83. };
  84. }
  85. }, {
  86. key: 'src',
  87. get: function get() {
  88. if (!_lodash2.default.has(this, '_src')) {
  89. this._src = this.walk(function (node, src) {
  90. return src += node.src || '';
  91. }, '', false);
  92. }
  93. return this._src;
  94. }
  95. }, {
  96. key: 'size',
  97. get: function get() {
  98. if (!_lodash2.default.has(this, '_size')) {
  99. this._size = this.walk(function (node, size) {
  100. return size + node.size;
  101. }, 0, false);
  102. }
  103. return this._size;
  104. }
  105. }]);
  106. return BaseFolder;
  107. }(_Node3.default);
  108. exports.default = BaseFolder;
  109. ;