diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /devtools/client/netmonitor/src/widgets/HeadersPanelContextMenu.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/client/netmonitor/src/widgets/HeadersPanelContextMenu.js')
-rw-r--r-- | devtools/client/netmonitor/src/widgets/HeadersPanelContextMenu.js | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/devtools/client/netmonitor/src/widgets/HeadersPanelContextMenu.js b/devtools/client/netmonitor/src/widgets/HeadersPanelContextMenu.js new file mode 100644 index 0000000000..6f159ded73 --- /dev/null +++ b/devtools/client/netmonitor/src/widgets/HeadersPanelContextMenu.js @@ -0,0 +1,137 @@ +/* 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 HeadersPanelContextMenu { + 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 + */ + open(event = {}, selection) { + const { target } = event; + const menuItems = [ + { + id: "headers-panel-context-menu-copyvalue", + label: L10N.getStr("netmonitor.context.copyValue"), + accesskey: L10N.getStr("netmonitor.context.copyValue.accesskey"), + click: () => { + const { name, value } = getSummaryContent( + target.closest(".tabpanel-summary-container") + ); + this.copyValue( + { name, value, object: null, hasChildren: false }, + selection + ); + }, + }, + { + id: "headers-panel-context-menu-copyall", + label: L10N.getStr("netmonitor.context.copyAll"), + accesskey: L10N.getStr("netmonitor.context.copyAll.accesskey"), + click: () => { + const root = target.closest(".summary"); + const object = {}; + if (root) { + const { children } = root; + for (let i = 0; i < children.length; i++) { + const content = getSummaryContent(children[i]); + object[content.name] = content.value; + } + } + return 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} object data object for specific node + * @param {Object} selection object representing the current selection + */ + copyValue(object, selection) { + let buffer = ""; + if (selection.toString() !== "") { + buffer = selection.toString(); + } else { + const { customFormatters } = this.props; + buffer = contextMenuFormatters.baseCopyFormatter(object); + if (customFormatters?.copyFormatter) { + buffer = customFormatters.copyFormatter( + object, + contextMenuFormatters.baseCopyFormatter + ); + } + } + try { + copyString(buffer); + } catch (error) {} + } +} + +function getSummaryContent(el) { + return { + name: el.querySelector(".tabpanel-summary-label").textContent, + value: el.querySelector(".tabpanel-summary-value").textContent, + }; +} + +module.exports = HeadersPanelContextMenu; |