summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/src/widgets/PropertiesViewContextMenu.js
blob: 8d823444c2aade88ee7738a1f6e72a9bc589dfea (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
/* 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 {
  L10N,
} = require("resource://devtools/client/netmonitor/src/utils/l10n.js");
const {
  contextMenuFormatters,
} = require("resource://devtools/client/netmonitor/src/utils/context-menu-utils.js");

loader.lazyRequireGetter(
  this,
  "copyString",
  "resource://devtools/shared/platform/clipboard.js",
  true
);
loader.lazyRequireGetter(
  this,
  "showMenu",
  "resource://devtools/client/shared/components/menu/utils.js",
  true
);

class PropertiesViewContextMenu {
  constructor(props = {}) {
    this.props = props;
    this.copyAll = this.copyAll.bind(this);
    this.copyValue = this.copyValue.bind(this);
  }

  /**
   * Handle the context menu opening.
   * @param {Object} event open event
   * @param {Object} selection object representing the current selection
   * @param {Object} data object containing information
   * @param {Object} data.member member of the right-clicked row
   * @param {Object} data.object the whole tree data
   */
  open(event = {}, selection, { member, object }) {
    const menuItems = [
      {
        id: "properties-view-context-menu-copyvalue",
        label: L10N.getStr("netmonitor.context.copyValue"),
        accesskey: L10N.getStr("netmonitor.context.copyValue.accesskey"),
        click: () => this.copyValue(member, selection),
      },
      {
        id: "properties-view-context-menu-copyall",
        label: L10N.getStr("netmonitor.context.copyAll"),
        accesskey: L10N.getStr("netmonitor.context.copyAll.accesskey"),
        click: () => this.copyAll(object, selection),
      },
    ];

    showMenu(menuItems, {
      screenX: event.screenX,
      screenY: event.screenY,
    });
  }

  /**
   * Copies all.
   * @param {Object} object the whole tree data
   * @param {Object} selection object representing the current selection
   */
  copyAll(object, selection) {
    let buffer = "";
    if (selection.toString() !== "") {
      buffer = selection.toString();
    } else {
      const { customFormatters } = this.props;
      buffer = contextMenuFormatters.baseCopyAllFormatter(object);
      if (customFormatters?.copyAllFormatter) {
        buffer = customFormatters.copyAllFormatter(
          object,
          contextMenuFormatters.baseCopyAllFormatter
        );
      }
    }
    try {
      copyString(buffer);
    } catch (error) {}
  }

  /**
   * Copies the value of a single item.
   * @param {Object} member member of the right-clicked row
   * @param {Object} selection object representing the current selection
   */
  copyValue(member, selection) {
    let buffer = "";
    if (selection.toString() !== "") {
      buffer = selection.toString();
    } else {
      const { customFormatters } = this.props;
      buffer = contextMenuFormatters.baseCopyFormatter(member);
      if (customFormatters?.copyFormatter) {
        buffer = customFormatters.copyFormatter(
          member,
          contextMenuFormatters.baseCopyFormatter
        );
      }
    }
    try {
      copyString(buffer);
    } catch (error) {}
  }
}

module.exports = PropertiesViewContextMenu;