2012-08-04 05:22:55 +04:00
|
|
|
/* 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/. */
|
2011-08-08 21:31:32 +04:00
|
|
|
|
2014-12-16 19:21:11 +03:00
|
|
|
this.EXPORTED_SYMBOLS = ["PageMenuParent", "PageMenuChild"];
|
2011-08-08 21:31:32 +04:00
|
|
|
|
2016-03-29 06:58:43 +03:00
|
|
|
var {interfaces: Ci} = Components;
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.PageMenu = function PageMenu() {
|
2011-08-08 21:31:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PageMenu.prototype = {
|
|
|
|
PAGEMENU_ATTR: "pagemenu",
|
2011-08-18 20:37:26 +04:00
|
|
|
GENERATEDITEMID_ATTR: "generateditemid",
|
2011-08-08 21:31:32 +04:00
|
|
|
|
2014-12-16 19:21:11 +03:00
|
|
|
_popup: null,
|
2011-08-08 21:31:32 +04:00
|
|
|
|
2014-12-16 19:21:11 +03:00
|
|
|
// Only one of builder or browser will end up getting set.
|
|
|
|
_builder: null,
|
|
|
|
_browser: null,
|
|
|
|
|
|
|
|
// Given a target node, get the context menu for it or its ancestor.
|
2016-12-30 02:34:54 +03:00
|
|
|
getContextMenu(aTarget) {
|
2014-12-16 19:21:11 +03:00
|
|
|
let target = aTarget;
|
2011-08-08 21:31:32 +04:00
|
|
|
while (target) {
|
2014-12-16 19:21:11 +03:00
|
|
|
let contextMenu = target.contextMenu;
|
2011-08-08 21:31:32 +04:00
|
|
|
if (contextMenu) {
|
2014-12-16 19:21:11 +03:00
|
|
|
return contextMenu;
|
2011-08-08 21:31:32 +04:00
|
|
|
}
|
|
|
|
target = target.parentNode;
|
|
|
|
}
|
|
|
|
|
2014-12-16 19:21:11 +03:00
|
|
|
return null;
|
|
|
|
},
|
2014-12-22 16:36:02 +03:00
|
|
|
|
2014-12-16 19:21:11 +03:00
|
|
|
// Given a target node, generate a JSON object for any context menu
|
|
|
|
// associated with it, or null if there is no context menu.
|
2016-12-30 02:34:54 +03:00
|
|
|
maybeBuild(aTarget) {
|
2014-12-16 19:21:11 +03:00
|
|
|
let pageMenu = this.getContextMenu(aTarget);
|
|
|
|
if (!pageMenu) {
|
|
|
|
return null;
|
2011-08-08 21:31:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pageMenu.sendShowEvent();
|
|
|
|
// the show event is not cancelable, so no need to check a result here
|
|
|
|
|
2014-12-16 19:21:11 +03:00
|
|
|
this._builder = pageMenu.createBuilder();
|
|
|
|
if (!this._builder) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
pageMenu.build(this._builder);
|
|
|
|
|
|
|
|
// This serializes then parses again, however this could be avoided in
|
|
|
|
// the single-process case with further improvement.
|
|
|
|
let menuString = this._builder.toJSONString();
|
|
|
|
if (!menuString) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON.parse(menuString);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Given a JSON menu object and popup, add the context menu to the popup.
|
2016-12-30 02:34:54 +03:00
|
|
|
buildAndAttachMenuWithObject(aMenu, aBrowser, aPopup) {
|
2014-12-16 19:21:11 +03:00
|
|
|
if (!aMenu) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-08 21:31:32 +04:00
|
|
|
|
2014-12-16 19:21:11 +03:00
|
|
|
let insertionPoint = this.getInsertionPoint(aPopup);
|
|
|
|
if (!insertionPoint) {
|
2011-08-08 21:31:32 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-16 19:21:11 +03:00
|
|
|
let fragment = aPopup.ownerDocument.createDocumentFragment();
|
|
|
|
this.buildXULMenu(aMenu, fragment);
|
2011-08-08 21:31:32 +04:00
|
|
|
|
2014-12-16 19:21:11 +03:00
|
|
|
let pos = insertionPoint.getAttribute(this.PAGEMENU_ATTR);
|
2011-08-18 20:37:26 +04:00
|
|
|
if (pos == "start") {
|
2011-08-08 21:31:32 +04:00
|
|
|
insertionPoint.insertBefore(fragment,
|
|
|
|
insertionPoint.firstChild);
|
2014-05-29 15:08:32 +04:00
|
|
|
} else if (pos.startsWith("#")) {
|
|
|
|
insertionPoint.insertBefore(fragment, insertionPoint.querySelector(pos));
|
2011-08-18 20:37:26 +04:00
|
|
|
} else {
|
|
|
|
insertionPoint.appendChild(fragment);
|
2011-08-08 21:31:32 +04:00
|
|
|
}
|
|
|
|
|
2014-12-16 19:21:11 +03:00
|
|
|
this._browser = aBrowser;
|
|
|
|
this._popup = aPopup;
|
2011-08-08 21:31:32 +04:00
|
|
|
|
2014-12-16 19:21:11 +03:00
|
|
|
this._popup.addEventListener("command", this);
|
|
|
|
this._popup.addEventListener("popuphidden", this);
|
2011-08-08 21:31:32 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2014-12-16 19:21:11 +03:00
|
|
|
// Construct the XUL menu structure for a given JSON object.
|
2016-12-30 02:34:54 +03:00
|
|
|
buildXULMenu(aNode, aElementForAppending) {
|
2014-12-16 19:21:11 +03:00
|
|
|
let document = aElementForAppending.ownerDocument;
|
|
|
|
|
|
|
|
let children = aNode.children;
|
|
|
|
for (let child of children) {
|
|
|
|
let menuitem;
|
|
|
|
switch (child.type) {
|
|
|
|
case "menuitem":
|
|
|
|
if (!child.id) {
|
|
|
|
continue; // Ignore children without ids
|
|
|
|
}
|
|
|
|
|
|
|
|
menuitem = document.createElement("menuitem");
|
|
|
|
if (child.checkbox) {
|
|
|
|
menuitem.setAttribute("type", "checkbox");
|
|
|
|
if (child.checked) {
|
|
|
|
menuitem.setAttribute("checked", "true");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (child.label) {
|
|
|
|
menuitem.setAttribute("label", child.label);
|
|
|
|
}
|
|
|
|
if (child.icon) {
|
|
|
|
menuitem.setAttribute("image", child.icon);
|
|
|
|
menuitem.className = "menuitem-iconic";
|
|
|
|
}
|
|
|
|
if (child.disabled) {
|
|
|
|
menuitem.setAttribute("disabled", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "separator":
|
|
|
|
menuitem = document.createElement("menuseparator");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "menu":
|
|
|
|
menuitem = document.createElement("menu");
|
|
|
|
if (child.label) {
|
|
|
|
menuitem.setAttribute("label", child.label);
|
|
|
|
}
|
|
|
|
|
|
|
|
let menupopup = document.createElement("menupopup");
|
|
|
|
menuitem.appendChild(menupopup);
|
|
|
|
|
|
|
|
this.buildXULMenu(child, menupopup);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
menuitem.setAttribute(this.GENERATEDITEMID_ATTR, child.id ? child.id : 0);
|
|
|
|
aElementForAppending.appendChild(menuitem);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Called when the generated menuitem is executed.
|
2016-12-30 02:34:54 +03:00
|
|
|
handleEvent(event) {
|
2014-12-16 19:21:11 +03:00
|
|
|
let type = event.type;
|
|
|
|
let target = event.target;
|
2011-08-18 20:37:26 +04:00
|
|
|
if (type == "command" && target.hasAttribute(this.GENERATEDITEMID_ATTR)) {
|
2014-12-16 19:21:11 +03:00
|
|
|
// If a builder is assigned, call click on it directly. Otherwise, this is
|
|
|
|
// likely a menu with data from another process, so send a message to the
|
|
|
|
// browser to execute the menuitem.
|
|
|
|
if (this._builder) {
|
|
|
|
this._builder.click(target.getAttribute(this.GENERATEDITEMID_ATTR));
|
2016-12-31 05:47:25 +03:00
|
|
|
} else if (this._browser) {
|
2017-01-27 12:51:03 +03:00
|
|
|
let win = target.ownerGlobal;
|
2016-03-29 06:58:43 +03:00
|
|
|
let windowUtils = win.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils);
|
|
|
|
this._browser.messageManager.sendAsyncMessage("ContextMenu:DoCustomCommand", {
|
|
|
|
generatedItemId: target.getAttribute(this.GENERATEDITEMID_ATTR),
|
|
|
|
handlingUserInput: windowUtils.isHandlingUserInput
|
|
|
|
});
|
2014-12-16 19:21:11 +03:00
|
|
|
}
|
|
|
|
} else if (type == "popuphidden" && this._popup == target) {
|
|
|
|
this.removeGeneratedContent(this._popup);
|
2011-08-08 21:31:32 +04:00
|
|
|
|
2014-12-16 19:21:11 +03:00
|
|
|
this._popup.removeEventListener("popuphidden", this);
|
|
|
|
this._popup.removeEventListener("command", this);
|
2011-08-08 21:31:32 +04:00
|
|
|
|
2014-12-16 19:21:11 +03:00
|
|
|
this._popup = null;
|
|
|
|
this._builder = null;
|
|
|
|
this._browser = null;
|
2011-08-08 21:31:32 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-12-16 19:21:11 +03:00
|
|
|
// Get the first child of the given element with the given tag name.
|
2016-12-30 02:34:54 +03:00
|
|
|
getImmediateChild(element, tag) {
|
2014-12-16 19:21:11 +03:00
|
|
|
let child = element.firstChild;
|
2011-08-08 21:31:32 +04:00
|
|
|
while (child) {
|
|
|
|
if (child.localName == tag) {
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
child = child.nextSibling;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2014-12-16 19:21:11 +03:00
|
|
|
// Return the location where the generated items should be inserted into the
|
|
|
|
// given popup. They should be inserted as the next sibling of the returned
|
|
|
|
// element.
|
2016-12-30 02:34:54 +03:00
|
|
|
getInsertionPoint(aPopup) {
|
2011-08-08 21:31:32 +04:00
|
|
|
if (aPopup.hasAttribute(this.PAGEMENU_ATTR))
|
|
|
|
return aPopup;
|
|
|
|
|
2014-12-16 19:21:11 +03:00
|
|
|
let element = aPopup.firstChild;
|
2011-08-08 21:31:32 +04:00
|
|
|
while (element) {
|
|
|
|
if (element.localName == "menu") {
|
2014-12-16 19:21:11 +03:00
|
|
|
let popup = this.getImmediateChild(element, "menupopup");
|
2011-08-08 21:31:32 +04:00
|
|
|
if (popup) {
|
2014-12-16 19:21:11 +03:00
|
|
|
let result = this.getInsertionPoint(popup);
|
2011-08-08 21:31:32 +04:00
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
element = element.nextSibling;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2014-12-16 19:21:11 +03:00
|
|
|
// Remove the generated content from the given popup.
|
2016-12-30 02:34:54 +03:00
|
|
|
removeGeneratedContent(aPopup) {
|
2014-12-16 19:21:11 +03:00
|
|
|
let ungenerated = [];
|
2011-08-08 21:31:32 +04:00
|
|
|
ungenerated.push(aPopup);
|
|
|
|
|
2014-12-16 19:21:11 +03:00
|
|
|
let count;
|
2011-08-08 21:31:32 +04:00
|
|
|
while (0 != (count = ungenerated.length)) {
|
2014-12-16 19:21:11 +03:00
|
|
|
let last = count - 1;
|
|
|
|
let element = ungenerated[last];
|
2011-08-08 21:31:32 +04:00
|
|
|
ungenerated.splice(last, 1);
|
|
|
|
|
2014-12-16 19:21:11 +03:00
|
|
|
let i = element.childNodes.length;
|
2011-08-08 21:31:32 +04:00
|
|
|
while (i-- > 0) {
|
2014-12-16 19:21:11 +03:00
|
|
|
let child = element.childNodes[i];
|
2011-08-18 20:37:26 +04:00
|
|
|
if (!child.hasAttribute(this.GENERATEDITEMID_ATTR)) {
|
2011-08-08 21:31:32 +04:00
|
|
|
ungenerated.push(child);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
element.removeChild(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 19:21:11 +03:00
|
|
|
|
|
|
|
// This object is expected to be used from a parent process.
|
|
|
|
this.PageMenuParent = function PageMenuParent() {
|
|
|
|
}
|
|
|
|
|
|
|
|
PageMenuParent.prototype = {
|
|
|
|
__proto__ : PageMenu.prototype,
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a target node and popup, add the context menu to the popup. This is
|
|
|
|
* intended to be called when a single process is used. This is equivalent to
|
|
|
|
* calling PageMenuChild.build and PageMenuParent.addToPopup in sequence.
|
|
|
|
*
|
|
|
|
* Returns true if custom menu items were present.
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
buildAndAddToPopup(aTarget, aPopup) {
|
2014-12-16 19:21:11 +03:00
|
|
|
let menuObject = this.maybeBuild(aTarget);
|
|
|
|
if (!menuObject) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.buildAndAttachMenuWithObject(menuObject, null, aPopup);
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a JSON menu object and popup, add the context menu to the popup. This
|
|
|
|
* is intended to be called when the child page is in a different process.
|
|
|
|
* aBrowser should be the browser containing the page the context menu is
|
|
|
|
* displayed for, which may be null.
|
|
|
|
*
|
|
|
|
* Returns true if custom menu items were present.
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
addToPopup(aMenu, aBrowser, aPopup) {
|
2014-12-16 19:21:11 +03:00
|
|
|
return this.buildAndAttachMenuWithObject(aMenu, aBrowser, aPopup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This object is expected to be used from a child process.
|
|
|
|
this.PageMenuChild = function PageMenuChild() {
|
|
|
|
}
|
|
|
|
|
|
|
|
PageMenuChild.prototype = {
|
|
|
|
__proto__ : PageMenu.prototype,
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a target node, return a JSON object for the custom menu commands. The
|
|
|
|
* object will consist of a hierarchical structure of menus, menuitems or
|
|
|
|
* separators. Supported properties of each are:
|
|
|
|
* Menu: children, label, type="menu"
|
|
|
|
* Menuitems: checkbox, checked, disabled, icon, label, type="menuitem"
|
|
|
|
* Separators: type="separator"
|
|
|
|
*
|
|
|
|
* In addition, the id of each item will be used to identify the item
|
|
|
|
* when it is executed. The type will either be 'menu', 'menuitem' or
|
|
|
|
* 'separator'. The toplevel node will be a menu with a children property. The
|
|
|
|
* children property of a menu is an array of zero or more other items.
|
|
|
|
*
|
|
|
|
* If there is no menu associated with aTarget, null will be returned.
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
build(aTarget) {
|
2014-12-16 19:21:11 +03:00
|
|
|
return this.maybeBuild(aTarget);
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given the id of a menu, execute the command associated with that menu. It
|
|
|
|
* is assumed that only one command will be executed so the builder is
|
|
|
|
* cleared afterwards.
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
executeMenu(aId) {
|
2014-12-16 19:21:11 +03:00
|
|
|
if (this._builder) {
|
|
|
|
this._builder.click(aId);
|
|
|
|
this._builder = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|