PropertiesViewContextMenu.js (3342B)
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 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const { 8 L10N, 9 } = require("resource://devtools/client/netmonitor/src/utils/l10n.js"); 10 const { 11 contextMenuFormatters, 12 } = require("resource://devtools/client/netmonitor/src/utils/context-menu-utils.js"); 13 14 loader.lazyRequireGetter( 15 this, 16 "copyString", 17 "resource://devtools/shared/platform/clipboard.js", 18 true 19 ); 20 loader.lazyRequireGetter( 21 this, 22 "showMenu", 23 "resource://devtools/client/shared/components/menu/utils.js", 24 true 25 ); 26 27 class PropertiesViewContextMenu { 28 constructor(props = {}) { 29 this.props = props; 30 this.copyAll = this.copyAll.bind(this); 31 this.copyValue = this.copyValue.bind(this); 32 } 33 34 /** 35 * Handle the context menu opening. 36 * 37 * @param {object} event open event 38 * @param {object} selection object representing the current selection 39 * @param {object} data object containing information 40 * @param {object} data.member member of the right-clicked row 41 * @param {object} data.object the whole tree data (can be optional) 42 */ 43 open(event = {}, selection, { member, object }) { 44 const menuItems = [ 45 { 46 id: "properties-view-context-menu-copyvalue", 47 label: L10N.getStr("netmonitor.context.copyValue"), 48 accesskey: L10N.getStr("netmonitor.context.copyValue.accesskey"), 49 click: () => this.copyValue(member, selection), 50 }, 51 { 52 id: "properties-view-context-menu-copyall", 53 label: L10N.getStr("netmonitor.context.copyAll"), 54 accesskey: L10N.getStr("netmonitor.context.copyAll.accesskey"), 55 visible: !!object, 56 click: () => this.copyAll(object, selection), 57 }, 58 ]; 59 60 showMenu(menuItems, { 61 screenX: event.screenX, 62 screenY: event.screenY, 63 }); 64 } 65 66 /** 67 * Copies all. 68 * 69 * @param {object} object the whole tree data 70 * @param {object} selection object representing the current selection 71 */ 72 copyAll(object, selection) { 73 let buffer = ""; 74 if (selection.toString() !== "") { 75 buffer = selection.toString(); 76 } else { 77 const { customFormatters } = this.props; 78 buffer = contextMenuFormatters.baseCopyAllFormatter(object); 79 if (customFormatters?.copyAllFormatter) { 80 buffer = customFormatters.copyAllFormatter( 81 object, 82 contextMenuFormatters.baseCopyAllFormatter 83 ); 84 } 85 } 86 try { 87 copyString(buffer); 88 } catch (error) {} 89 } 90 91 /** 92 * Copies the value of a single item. 93 * 94 * @param {object} member member of the right-clicked row 95 * @param {object} selection object representing the current selection 96 */ 97 copyValue(member, selection) { 98 let buffer = ""; 99 if (selection.toString() !== "") { 100 buffer = selection.toString(); 101 } else { 102 const { customFormatters } = this.props; 103 buffer = contextMenuFormatters.baseCopyFormatter(member); 104 if (customFormatters?.copyFormatter) { 105 buffer = customFormatters.copyFormatter( 106 member, 107 contextMenuFormatters.baseCopyFormatter 108 ); 109 } 110 } 111 try { 112 copyString(buffer); 113 } catch (error) {} 114 } 115 } 116 117 module.exports = PropertiesViewContextMenu;