summaryrefslogtreecommitdiffstats
path: root/devtools/client/aboutdebugging/src/components/debugtarget/DebugTargetPane.js
blob: abfa1042b8ef56a97d6ea05043b1e6e2aac570c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const {
  createFactory,
  createRef,
  PureComponent,
} = require("resource://devtools/client/shared/vendor/react.js");
const dom = require("resource://devtools/client/shared/vendor/react-dom-factories.js");
const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.js");

const FluentReact = require("resource://devtools/client/shared/vendor/fluent-react.js");

const DebugTargetList = createFactory(
  require("resource://devtools/client/aboutdebugging/src/components/debugtarget/DebugTargetList.js")
);

const Actions = require("resource://devtools/client/aboutdebugging/src/actions/index.js");
const Types = require("resource://devtools/client/aboutdebugging/src/types/index.js");

/**
 * This component provides list for debug target and name area.
 */
class DebugTargetPane extends PureComponent {
  static get propTypes() {
    return {
      actionComponent: PropTypes.any.isRequired,
      additionalActionsComponent: PropTypes.any,
      children: PropTypes.node,
      collapsibilityKey: PropTypes.string.isRequired,
      detailComponent: PropTypes.any.isRequired,
      dispatch: PropTypes.func.isRequired,
      // Provided by wrapping the component with FluentReact.withLocalization.
      getString: PropTypes.func.isRequired,
      icon: PropTypes.string.isRequired,
      isCollapsed: PropTypes.bool.isRequired,
      name: PropTypes.string.isRequired,
      targets: PropTypes.arrayOf(Types.debugTarget).isRequired,
    };
  }

  constructor(props) {
    super(props);
    this.collapsableRef = createRef();
  }

  componentDidUpdate(prevProps, prevState, snapshot) {
    if (snapshot === null) {
      return;
    }

    const el = this.collapsableRef.current;

    // Cancel existing animation which is collapsing/expanding.
    for (const animation of el.getAnimations()) {
      animation.cancel();
    }

    el.animate(
      { maxHeight: [`${snapshot}px`, `${el.clientHeight}px`] },
      { duration: 150, easing: "cubic-bezier(.07, .95, 0, 1)" }
    );
  }

  getSnapshotBeforeUpdate(prevProps) {
    if (this.props.isCollapsed !== prevProps.isCollapsed) {
      return this.collapsableRef.current.clientHeight;
    }

    return null;
  }

  toggleCollapsibility() {
    const { collapsibilityKey, dispatch, isCollapsed } = this.props;
    dispatch(
      Actions.updateDebugTargetCollapsibility(collapsibilityKey, !isCollapsed)
    );
  }

  render() {
    const {
      actionComponent,
      additionalActionsComponent,
      children,
      detailComponent,
      dispatch,
      getString,
      icon,
      isCollapsed,
      name,
      targets,
    } = this.props;

    const title = getString("about-debugging-collapse-expand-debug-targets");

    return dom.section(
      {
        className: "qa-debug-target-pane",
      },
      dom.a(
        {
          className:
            "undecorated-link debug-target-pane__title " +
            "qa-debug-target-pane-title",
          title,
          onClick: e => this.toggleCollapsibility(),
        },
        dom.h2(
          { className: "main-subheading debug-target-pane__heading" },
          dom.img({
            className: "main-subheading__icon",
            src: icon,
          }),
          `${name} (${targets.length})`,
          dom.img({
            className:
              "main-subheading__icon debug-target-pane__icon" +
              (isCollapsed ? " debug-target-pane__icon--collapsed" : ""),
            src: "chrome://devtools/skin/images/arrow-e.svg",
          })
        )
      ),
      dom.div(
        {
          className:
            "debug-target-pane__collapsable qa-debug-target-pane__collapsable" +
            (isCollapsed ? " debug-target-pane__collapsable--collapsed" : ""),
          ref: this.collapsableRef,
        },
        children,
        DebugTargetList({
          actionComponent,
          additionalActionsComponent,
          detailComponent,
          dispatch,
          isCollapsed,
          targets,
        })
      )
    );
  }
}

module.exports = FluentReact.withLocalization(DebugTargetPane);