summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/components/menu/utils.js
diff options
context:
space:
mode:
Diffstat (limited to 'devtools/client/shared/components/menu/utils.js')
-rw-r--r--devtools/client/shared/components/menu/utils.js62
1 files changed, 62 insertions, 0 deletions
diff --git a/devtools/client/shared/components/menu/utils.js b/devtools/client/shared/components/menu/utils.js
new file mode 100644
index 0000000000..e6fca96822
--- /dev/null
+++ b/devtools/client/shared/components/menu/utils.js
@@ -0,0 +1,62 @@
+/* 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 Menu = require("resource://devtools/client/framework/menu.js");
+const MenuItem = require("resource://devtools/client/framework/menu-item.js");
+
+/**
+ * Helper function for opening context menu.
+ *
+ * @param {Array} items
+ * List of menu items.
+ * @param {Object} options:
+ * @property {Element} button
+ * Button element used to open the menu.
+ * @property {Number} screenX
+ * Screen x coordinate of the menu on the screen.
+ * @property {Number} screenY
+ * Screen y coordinate of the menu on the screen.
+ */
+function showMenu(items, options) {
+ if (items.length === 0) {
+ return;
+ }
+
+ // Build the menu object from provided menu items.
+ const menu = new Menu();
+ items.forEach(item => {
+ if (item == "-") {
+ item = { type: "separator" };
+ }
+
+ const menuItem = new MenuItem(item);
+ const subItems = item.submenu;
+
+ if (subItems) {
+ const subMenu = new Menu();
+ subItems.forEach(subItem => {
+ subMenu.append(new MenuItem(subItem));
+ });
+ menuItem.submenu = subMenu;
+ }
+
+ menu.append(menuItem);
+ });
+
+ // Calculate position on the screen according to
+ // the parent button if available.
+ if (options.button) {
+ menu.popupAtTarget(options.button);
+ } else {
+ const screenX = options.screenX;
+ const screenY = options.screenY;
+ menu.popup(screenX, screenY, window.document);
+ }
+}
+
+module.exports = {
+ showMenu,
+};