Move event handlers to a handleEvent function to simplify removal, Remove event handlers to remove uninstall issue. Fixes #182

This commit is contained in:
Jonathan Kingston 2017-02-16 14:38:15 +00:00
parent bd1ef4e645
commit 1c1f3fbea0

101
index.js
View file

@ -871,9 +871,15 @@ ContainerWindow.prototype = {
_plusButtonTooltip: "", _plusButtonTooltip: "",
_overflowPlusButtonElements: [], _overflowPlusButtonElements: [],
_overflowPlusButtonTooltip: "", _overflowPlusButtonTooltip: "",
_button: null,
_overflowButton: null,
_tabsElement: null,
_init(window) { _init(window) {
this._window = window; this._window = window;
this._tabsElement = this._window.document.getElementById("tabbrowser-tabs");
this._button = this._window.document.getAnonymousElementByAttribute(this._tabsElement, "anonid", "tabs-newtab-button");
this._overflowButton = 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);
}, },
@ -888,22 +894,45 @@ ContainerWindow.prototype = {
]); ]);
}, },
handleEvent(e) {
let el = e.target;
switch (e.type) {
case "mouseover":
this.showPopup(el);
break;
case "click":
this.hidePanel();
break;
case "mouseout":
while(el) {
if (el === this._panelElement ||
el === this._button ||
el === this._overflowButton) {
this._createTimeout();
return;
}
el = el.parentElement;
}
break;
}
},
showPopup(buttonElement) {
this._cleanTimeout();
this._panelElement.openPopup(buttonElement);
},
_configurePlusButtonMenu() { _configurePlusButtonMenu() {
const tabsElement = this._window.document.getElementById("tabbrowser-tabs");
const mainPopupSetElement = this._window.document.getElementById("mainPopupSet"); const mainPopupSetElement = this._window.document.getElementById("mainPopupSet");
const button = this._window.document.getAnonymousElementByAttribute(tabsElement, "anonid", "tabs-newtab-button"); this._disableElement(this._button, "_plusButtonElements");
this._disableElement(button, "_plusButtonElements"); this._disableElement(this._overflowButton, "_overflowPlusButtonElements");
const overflowButton = this._window.document.getElementById("new-tab-button");
this._disableElement(overflowButton, "_overflowPlusButtonElements");
// Let's remove the tooltip because it can go over our panel. // Let's remove the tooltip because it can go over our panel.
this._plusButtonTooltip = button.getAttribute("tooltip"); this._plusButtonTooltip = this._button.getAttribute("tooltip");
button.setAttribute("tooltip", ""); this._button.setAttribute("tooltip", "");
this._overflowPlusButtonTooltip = overflowButton.getAttribute("tooltip"); this._overflowPlusButtonTooltip = this._overflowButton.getAttribute("tooltip");
overflowButton.setAttribute("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) {
@ -920,35 +949,14 @@ ContainerWindow.prototype = {
this._panelElement.setAttribute("consumeoutsideclicks", "never"); this._panelElement.setAttribute("consumeoutsideclicks", "never");
mainPopupSetElement.appendChild(this._panelElement); mainPopupSetElement.appendChild(this._panelElement);
const showPopup = (buttonElement) => {
this._cleanTimeout();
this._panelElement.openPopup(buttonElement);
};
const mouseoutHandle = (e) => { [this._button, this._overflowButton].forEach((buttonElement) => {
let el = e.target; buttonElement.addEventListener("mouseover", this);
while(el) { buttonElement.addEventListener("click", this);
if (el === this._panelElement || buttonElement.addEventListener("mouseout", this);
el === button ||
el === overflowButton) {
this._createTimeout();
return;
}
el = el.parentElement;
}
};
[button, overflowButton].forEach((buttonElement) => {
buttonElement.addEventListener("mouseover", () => {
showPopup(buttonElement);
});
buttonElement.addEventListener("click", () => {
this.hidePanel();
});
buttonElement.addEventListener("mouseout", mouseoutHandle);
}); });
this._panelElement.addEventListener("mouseout", mouseoutHandle); this._panelElement.addEventListener("mouseout", this);
this._panelElement.addEventListener("mouseover", () => { this._panelElement.addEventListener("mouseover", () => {
this._cleanTimeout(); this._cleanTimeout();
@ -976,7 +984,7 @@ ContainerWindow.prototype = {
this._cleanTimeout(); this._cleanTimeout();
}); });
menuItemElement.addEventListener("mouseout", mouseoutHandle); menuItemElement.addEventListener("mouseout", this);
this._panelElement.appendChild(menuItemElement); this._panelElement.appendChild(menuItemElement);
}); });
@ -1119,14 +1127,17 @@ ContainerWindow.prototype = {
}, },
_shutdownPlusButtonMenu() { _shutdownPlusButtonMenu() {
const tabsElement = this._window.document.getElementById("tabbrowser-tabs"); [this._button, this._overflowButton].forEach((buttonElement) => {
const button = this._window.document.getAnonymousElementByAttribute(tabsElement, "anonid", "tabs-newtab-button"); buttonElement.removeEventListener("mouseover", this);
this._shutdownElement(button, "_plusButtonElements"); buttonElement.removeEventListener("click", this);
button.setAttribute("tooltip", this._plusButtonTooltip); buttonElement.removeEventListener("mouseout", this);
});
const overflowButton = this._window.document.getElementById("new-tab-button"); this._shutdownElement(this._button, "_plusButtonElements");
this._shutdownElement(overflowButton, "_overflowPlusButtonElements"); this._button.setAttribute("tooltip", this._plusButtonTooltip);
overflowButton.setAttribute("tooltip", this._overflowPlusButtonTooltip);
this._shutdownElement(this._overflowButton, "_overflowPlusButtonElements");
this._overflowButton.setAttribute("tooltip", this._overflowPlusButtonTooltip);
}, },
_shutdownFileMenu() { _shutdownFileMenu() {