census.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. const { DOM: dom, createClass, PropTypes, createFactory } = require("devtools/client/shared/vendor/react");
  5. const Tree = createFactory(require("devtools/client/shared/components/tree"));
  6. const CensusTreeItem = createFactory(require("./census-tree-item"));
  7. const { createParentMap } = require("../utils");
  8. const { TREE_ROW_HEIGHT } = require("../constants");
  9. const { censusModel, diffingModel } = require("../models");
  10. const Census = module.exports = createClass({
  11. displayName: "Census",
  12. propTypes: {
  13. census: censusModel,
  14. onExpand: PropTypes.func.isRequired,
  15. onCollapse: PropTypes.func.isRequired,
  16. onFocus: PropTypes.func.isRequired,
  17. onViewSourceInDebugger: PropTypes.func.isRequired,
  18. onViewIndividuals: PropTypes.func.isRequired,
  19. diffing: diffingModel,
  20. },
  21. render() {
  22. let {
  23. census,
  24. onExpand,
  25. onCollapse,
  26. onFocus,
  27. diffing,
  28. onViewSourceInDebugger,
  29. onViewIndividuals,
  30. } = this.props;
  31. const report = census.report;
  32. let parentMap = census.parentMap;
  33. const { totalBytes, totalCount } = report;
  34. const getPercentBytes = totalBytes === 0
  35. ? _ => 0
  36. : bytes => (bytes / totalBytes) * 100;
  37. const getPercentCount = totalCount === 0
  38. ? _ => 0
  39. : count => (count / totalCount) * 100;
  40. return Tree({
  41. autoExpandDepth: 0,
  42. focused: census.focused,
  43. getParent: node => {
  44. const parent = parentMap[node.id];
  45. return parent === report ? null : parent;
  46. },
  47. getChildren: node => node.children || [],
  48. isExpanded: node => census.expanded.has(node.id),
  49. onExpand,
  50. onCollapse,
  51. onFocus,
  52. renderItem: (item, depth, focused, arrow, expanded) =>
  53. new CensusTreeItem({
  54. onViewSourceInDebugger,
  55. item,
  56. depth,
  57. focused,
  58. arrow,
  59. expanded,
  60. getPercentBytes,
  61. getPercentCount,
  62. diffing,
  63. inverted: census.display.inverted,
  64. onViewIndividuals,
  65. }),
  66. getRoots: () => report.children || [],
  67. getKey: node => node.id,
  68. itemHeight: TREE_ROW_HEIGHT,
  69. });
  70. }
  71. });