tree-map.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. "use strict";
  5. const { DOM: dom, createClass } = require("devtools/client/shared/vendor/react");
  6. const { treeMapModel } = require("../models");
  7. const startVisualization = require("./tree-map/start");
  8. module.exports = createClass({
  9. propTypes: {
  10. treeMap: treeMapModel
  11. },
  12. displayName: "TreeMap",
  13. getInitialState() {
  14. return {};
  15. },
  16. componentDidMount() {
  17. const { treeMap } = this.props;
  18. if (treeMap && treeMap.report) {
  19. this._startVisualization();
  20. }
  21. },
  22. shouldComponentUpdate(nextProps) {
  23. const oldTreeMap = this.props.treeMap;
  24. const newTreeMap = nextProps.treeMap;
  25. return oldTreeMap !== newTreeMap;
  26. },
  27. componentDidUpdate(prevProps) {
  28. this._stopVisualization();
  29. if (this.props.treeMap && this.props.treeMap.report) {
  30. this._startVisualization();
  31. }
  32. },
  33. componentWillUnmount() {
  34. if (this.state.stopVisualization) {
  35. this.state.stopVisualization();
  36. }
  37. },
  38. _stopVisualization() {
  39. if (this.state.stopVisualization) {
  40. this.state.stopVisualization();
  41. this.setState({ stopVisualization: null });
  42. }
  43. },
  44. _startVisualization() {
  45. const { container } = this.refs;
  46. const { report } = this.props.treeMap;
  47. const stopVisualization = startVisualization(container, report);
  48. this.setState({ stopVisualization });
  49. },
  50. render() {
  51. return dom.div(
  52. {
  53. ref: "container",
  54. className: "tree-map-container"
  55. }
  56. );
  57. }
  58. });