summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/components/reps/reps/window.js
blob: 2c420c2b30ff34fe4c07f6b5affc0c5810e56701 (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
/* 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";

// Make this available to both AMD and CJS environments
define(function (require, exports, module) {
  // ReactJS
  const PropTypes = require("devtools/client/shared/vendor/react-prop-types");
  const { span } = require("devtools/client/shared/vendor/react-dom-factories");

  // Reps
  const {
    getGripType,
    getURLDisplayString,
    wrapRender,
  } = require("devtools/client/shared/components/reps/reps/rep-utils");

  const {
    MODE,
  } = require("devtools/client/shared/components/reps/reps/constants");

  /**
   * Renders a grip representing a window.
   */

  WindowRep.propTypes = {
    mode: PropTypes.oneOf(Object.values(MODE)),
    object: PropTypes.object.isRequired,
    shouldRenderTooltip: PropTypes.bool,
  };

  function WindowRep(props) {
    const { mode, object } = props;

    if (mode === MODE.TINY) {
      const tinyTitle = getTitle(object);
      const title = getTitle(object, true);
      const location = getLocation(object);
      const config = getElementConfig({ ...props, title, location });

      return span(
        config,
        span({ className: tinyTitle.className }, tinyTitle.content)
      );
    }

    const title = getTitle(object, true);
    const location = getLocation(object);
    const config = getElementConfig({ ...props, title, location });

    return span(
      config,
      span({ className: title.className }, title.content),
      span({ className: "location" }, location)
    );
  }

  function getElementConfig(opts) {
    const { object, shouldRenderTooltip, title, location } = opts;
    let tooltip;

    if (location) {
      tooltip = `${title.content}${location}`;
    } else {
      tooltip = `${title.content}`;
    }

    return {
      "data-link-actor-id": object.actor,
      className: "objectBox objectBox-Window",
      title: shouldRenderTooltip ? tooltip : null,
    };
  }

  function getTitle(object, trailingSpace) {
    let title = object.displayClass || object.class || "Window";
    if (trailingSpace === true) {
      title = `${title} `;
    }
    return {
      className: "objectTitle",
      content: title,
    };
  }

  function getLocation(object) {
    return getURLDisplayString(object.preview.url);
  }

  // Registration
  function supportsObject(object, noGrip = false) {
    return object?.preview && getGripType(object, noGrip) == "Window";
  }

  // Exports from this module
  module.exports = {
    rep: wrapRender(WindowRep),
    supportsObject,
  };
});