Neatening up element and tooltip caches to reduce duplication in add button config and shutdown.

This commit is contained in:
Jonathan Kingston 2017-02-16 15:12:38 +00:00
parent 1c1f3fbea0
commit 76f934cae4

View file

@ -865,21 +865,17 @@ ContainerWindow.prototype = {
_style: null, _style: null,
_panelElement: null, _panelElement: null,
_timeoutId: 0, _timeoutId: 0,
_fileMenuElements: [], _elementCache: {},
_contextMenuElements: [], _tooltipCache: {},
_plusButtonElements: [], _plusButton: null,
_plusButtonTooltip: "", _overflowPlusButton: null,
_overflowPlusButtonElements: [],
_overflowPlusButtonTooltip: "",
_button: null,
_overflowButton: null,
_tabsElement: null, _tabsElement: null,
_init(window) { _init(window) {
this._window = window; this._window = window;
this._tabsElement = this._window.document.getElementById("tabbrowser-tabs"); this._tabsElement = this._window.document.getElementById("tabbrowser-tabs");
this._button = this._window.document.getAnonymousElementByAttribute(this._tabsElement, "anonid", "tabs-newtab-button"); this._plusButton = this._window.document.getAnonymousElementByAttribute(this._tabsElement, "anonid", "tabs-newtab-button");
this._overflowButton = this._window.document.getElementById("new-tab-button"); this._overflowPlusButton = this._window.document.getElementById("new-tab-button");
this._style = Style({ uri: self.data.url("usercontext.css") }); this._style = Style({ uri: self.data.url("usercontext.css") });
attachTo(this._style, this._window); attachTo(this._style, this._window);
}, },
@ -906,8 +902,8 @@ ContainerWindow.prototype = {
case "mouseout": case "mouseout":
while(el) { while(el) {
if (el === this._panelElement || if (el === this._panelElement ||
el === this._button || el === this._plusButton ||
el === this._overflowButton) { el === this._overflowPlusButton) {
this._createTimeout(); this._createTimeout();
return; return;
} }
@ -922,17 +918,19 @@ ContainerWindow.prototype = {
this._panelElement.openPopup(buttonElement); this._panelElement.openPopup(buttonElement);
}, },
_configurePlusButtonMenuElement(name, buttonElement) {
// Let's remove the tooltip because it can go over our panel.
this._tooltipCache[name] = buttonElement.getAttribute("tooltip");
buttonElement.setAttribute("tooltip", "");
this._disableElement(buttonElement, name);
buttonElement.addEventListener("mouseover", this);
buttonElement.addEventListener("click", this);
buttonElement.addEventListener("mouseout", this);
},
_configurePlusButtonMenu() { _configurePlusButtonMenu() {
const mainPopupSetElement = this._window.document.getElementById("mainPopupSet"); const mainPopupSetElement = this._window.document.getElementById("mainPopupSet");
this._disableElement(this._button, "_plusButtonElements");
this._disableElement(this._overflowButton, "_overflowPlusButtonElements");
// Let's remove the tooltip because it can go over our panel.
this._plusButtonTooltip = this._button.getAttribute("tooltip");
this._button.setAttribute("tooltip", "");
this._overflowPlusButtonTooltip = this._overflowButton.getAttribute("tooltip");
this._overflowButton.setAttribute("tooltip", "");
// Let's remove all the previous panels. // Let's remove all the previous panels.
if (this._panelElement) { if (this._panelElement) {
@ -949,12 +947,8 @@ ContainerWindow.prototype = {
this._panelElement.setAttribute("consumeoutsideclicks", "never"); this._panelElement.setAttribute("consumeoutsideclicks", "never");
mainPopupSetElement.appendChild(this._panelElement); mainPopupSetElement.appendChild(this._panelElement);
this._configurePlusButtonMenuElement("plusButton", this._plusButton);
[this._button, this._overflowButton].forEach((buttonElement) => { this._configurePlusButtonMenuElement("overflowPlusButton", this._overflowPlusButton);
buttonElement.addEventListener("mouseover", this);
buttonElement.addEventListener("click", this);
buttonElement.addEventListener("mouseout", this);
});
this._panelElement.addEventListener("mouseout", this); this._panelElement.addEventListener("mouseout", this);
@ -1013,7 +1007,7 @@ ContainerWindow.prototype = {
userContextId: userContextId, userContextId: userContextId,
source: "file-menu" source: "file-menu"
}); });
}, "_fileMenuElements"); }, "fileMenu");
}, },
_configureContextMenu() { _configureContextMenu() {
@ -1028,15 +1022,15 @@ ContainerWindow.prototype = {
// next FF releases. // next FF releases.
this._window.gContextMenu.openLinkInTab(e); this._window.gContextMenu.openLinkInTab(e);
}, },
"_contextMenuElements" "contextMenu"
); );
}, },
// Generic menu configuration. // Generic menu configuration.
_configureMenu(menuId, excludedContainerCb, clickCb, arrayName) { _configureMenu(menuId, excludedContainerCb, clickCb, elementKey) {
const menu = this._window.document.getElementById(menuId); const menu = this._window.document.getElementById(menuId);
this._disableElement(menu, arrayName); this._disableElement(menu, elementKey);
if (!this._disableElement(menu, arrayName)) { if (!this._disableElement(menu, elementKey)) {
// Delete stale menu that isn't native elements // Delete stale menu that isn't native elements
while (menu.firstChild) { while (menu.firstChild) {
menu.removeChild(menu.firstChild); menu.removeChild(menu.firstChild);
@ -1126,55 +1120,56 @@ ContainerWindow.prototype = {
this._shutdownContextMenu(); this._shutdownContextMenu();
}, },
_shutDownPlusButtonMenuElement(name, buttonElement) {
this._shutdownElement(buttonElement, name);
buttonElement.setAttribute("tooltip", this._tooltipCache[name]);
buttonElement.removeEventListener("mouseover", this);
buttonElement.removeEventListener("click", this);
buttonElement.removeEventListener("mouseout", this);
},
_shutdownPlusButtonMenu() { _shutdownPlusButtonMenu() {
[this._button, this._overflowButton].forEach((buttonElement) => { this._shutDownPlusButtonMenuElement("plusButton", this._plusButton);
buttonElement.removeEventListener("mouseover", this); this._shutDownPlusButtonMenuElement("overflowPlusButton", this._overflowPlusButton);
buttonElement.removeEventListener("click", this);
buttonElement.removeEventListener("mouseout", this);
});
this._shutdownElement(this._button, "_plusButtonElements");
this._button.setAttribute("tooltip", this._plusButtonTooltip);
this._shutdownElement(this._overflowButton, "_overflowPlusButtonElements");
this._overflowButton.setAttribute("tooltip", this._overflowPlusButtonTooltip);
}, },
_shutdownFileMenu() { _shutdownFileMenu() {
this._shutdownMenu("menu_newUserContext", "_fileMenuElements"); this._shutdownMenu("menu_newUserContext", "fileMenu");
}, },
_shutdownContextMenu() { _shutdownContextMenu() {
this._shutdownMenu("context-openlinkinusercontext-menu", this._shutdownMenu("context-openlinkinusercontext-menu",
"_contextMenuElements"); "contextMenu");
}, },
_shutdownMenu(menuId, arrayName) { _shutdownMenu(menuId, elementKey) {
const menu = this._window.document.getElementById(menuId); const menu = this._window.document.getElementById(menuId);
this._shutdownElement(menu, arrayName); this._shutdownElement(menu, elementKey);
}, },
_shutdownElement(element, arrayName) { _shutdownElement(element, elementKey) {
// Let's remove our elements. // Let's remove our elements.
while (element.firstChild) { while (element.firstChild) {
element.firstChild.remove(); element.firstChild.remove();
} }
for (let e of this[arrayName]) { // eslint-disable-line prefer-const for (let e of this._elementCache[elementKey]) { // eslint-disable-line prefer-const
element.appendChild(e); element.appendChild(e);
} }
}, },
_disableElement(element, arrayName) { _disableElement(element, elementKey) {
// Nothing to disable. // Nothing to disable.
if (!element || this[arrayName].length) { if (!element || this._elementCache[elementKey]) {
return false; return false;
} }
this._elementCache[elementKey] = [];
// Let's store the previous elements so that we can repopulate it in case // Let's store the previous elements so that we can repopulate it in case
// the addon is uninstalled. // the addon is uninstalled.
while (element.firstChild) { while (element.firstChild) {
this[arrayName].push(element.removeChild(element.firstChild)); this._elementCache[elementKey].push(element.removeChild(element.firstChild));
} }
return true; return true;