Tab-Search-and-Manage/src/scripts/eventListeners.js

161 lines
5.1 KiB
JavaScript
Raw Normal View History

2020-01-31 18:46:44 +00:00
document.getElementById("searchBar").onkeyup = function () {
2018-10-13 22:08:32 +00:00
searchTabs();
}
2020-03-27 19:34:04 +00:00
window.onload = async () => {
getTabs();
}
2018-05-13 22:27:16 +00:00
// Set click events for tab blocks
2018-04-20 06:06:32 +00:00
document.addEventListener("click", (e) => {
var target = (!e.target.className.includes("pTagTitleText")) ? e.target : e.target.parentElement;
2018-05-06 01:02:50 +00:00
var targetID = target.id;
2018-04-20 06:06:32 +00:00
var parentElm = target.parentElement;
2018-05-06 01:02:50 +00:00
var id = 0;
2018-04-20 06:06:32 +00:00
if (targetID == "closeBttn") {
2019-05-04 23:12:22 +00:00
if (hoverTarget) {
closeBttnAction(hoverTarget, hoverTarget.id);
}
2020-05-01 03:49:20 +00:00
} else if (targetID == "hideTgglBttn") {
if (hoverTarget) {
hideSelectedTabAction(hoverTarget, hoverTarget.id);
}
} else if (targetID == "popoutBttn") {
if (hoverTarget) {
popoutSelectedTabAction(hoverTarget, hoverTarget.id);
}
2020-06-30 21:36:00 +00:00
} else if (targetID == "duplicateBttn") {
if (hoverTarget) {
duplicateTabAction(hoverTarget, hoverTarget.id);
}
2020-05-01 03:49:20 +00:00
} else if (targetID == "muteTgglBttn") {
if (hoverTarget) {
muteSelectedTabAction(hoverTarget, hoverTarget.id);
}
} else if (targetID == "iconElm") {
2018-10-13 22:08:32 +00:00
setNewTabAction(target, targetID, parentElm, id);
2018-05-06 01:02:50 +00:00
} else if (targetID == "goTop") {
2020-03-27 19:34:04 +00:00
const elm = document.getElementById("listZone");
elm.scrollTo(0,0);
2018-05-06 01:02:50 +00:00
} else if (targetID == "goBottom") {
2020-03-27 19:34:04 +00:00
const elm = document.getElementById("listZone");
elm.scrollTo(0,elm.scrollHeight);
2018-05-06 01:02:50 +00:00
} else if (targetID == "goToTab") {
2020-03-27 19:34:04 +00:00
scrollToView();
2018-05-13 22:27:16 +00:00
} else if (targetID == "newTab") {
createTab();
2020-03-27 00:29:05 +00:00
} else if (targetID == "newWin") {
createWin();
} else if (targetID == "hideAllBttn") {
2018-10-13 22:08:32 +00:00
showHideTabsAction(0)
} else if (targetID == "showAllBttn") {
2018-10-13 22:08:32 +00:00
showHideTabsAction(1);
} else if (targetID == "searchMode") {
2018-10-13 22:08:32 +00:00
setSearchModeAction(target, targetID, parentElm, id);
}
});
2019-05-04 23:12:22 +00:00
function closeBttnAction(target, targetID) {
2020-05-01 03:49:20 +00:00
let parentElm = target.parentElement;
2019-05-04 23:12:22 +00:00
if (target == oldElm) {
var index = Array.from(parentElm.children).indexOf(target);
2018-10-13 22:08:32 +00:00
(index - 1 < 0) ? index++ : index-- ; // Check what index to chose
2019-05-04 23:12:22 +00:00
var newElm = parentElm.children[index];
2018-10-13 22:08:32 +00:00
id = parseInt(newElm.getAttribute("tabID"));
browser.tabs.update(id, { active: true });
setOldElm(newElm);
}
2019-05-04 23:12:22 +00:00
id = parseInt(target.getAttribute("tabID"));
2018-10-13 22:08:32 +00:00
browser.tabs.remove(id);
2019-05-04 23:12:22 +00:00
parentElm.removeChild(target);
hoverTarget = undefined;
document.getElementById("tabControls").style.display = "none";
2018-10-13 22:08:32 +00:00
}
function setNewTabAction(target, targetID, parentElm, id) {
2018-10-13 22:08:32 +00:00
id = parseInt(target.getAttribute("tabID"));
tabsAction.get(id).then((tab) => {
browser.windows.update(tab.windowId, {focused: true});
browser.tabs.update(id, { active: true });
},id);
setOldElm(target);
2019-05-04 23:12:22 +00:00
document.getElementById("tabControls").querySelector("#hideTgglBttn").src = "../icons/eyeOpen.png";
2018-10-13 22:08:32 +00:00
}
2020-05-01 03:49:20 +00:00
function popoutSelectedTabAction(target, targetID) {
parentElm = target.parentElement;
id = parseInt(target.getAttribute("tabID"));
popoutSelectedTab(id);
}
2020-06-30 21:36:00 +00:00
function duplicateTabAction(target, targetID) {
parentElm = target.parentElement;
id = parseInt(target.getAttribute("tabID"));
duplicateTab(id);
}
2019-05-04 23:12:22 +00:00
function hideSelectedTabAction(target, targetID) {
parentElm = target.parentElement;
id = parseInt(target.getAttribute("tabID"));
control = document.getElementById("tabControls").querySelector("#hideTgglBttn");
2018-10-13 22:08:32 +00:00
if (id != oldElm.getAttribute("tabID")) {
2019-05-04 23:12:22 +00:00
if (control.src.includes("eyeClosed.png")) {
target.setAttribute("class", "block");
control.src = "../icons/eyeOpen.png";
2020-05-01 03:49:20 +00:00
// In generateview
2018-10-13 22:08:32 +00:00
unhideSelectedTab(id);
} else {
2019-05-04 23:12:22 +00:00
target.setAttribute("class", "block hiddenBGColor");
control.src = "../icons/eyeClosed.png";
2018-10-13 22:08:32 +00:00
hideSelectedTab(id);
}
2018-04-20 06:06:32 +00:00
}
2018-10-13 22:08:32 +00:00
}
2020-05-01 03:49:20 +00:00
function muteSelectedTabAction(target, targetID) {
parentElm = target.parentElement;
id = parseInt(target.getAttribute("tabID"));
control = document.getElementById("tabControls").querySelector("#muteTgglBttn");
if (control.src.includes("isMuted.png")) {
control.src = "../icons/isNotMuted.png";
// In generateview
unmuteSelectedTab(id);
} else {
control.src = "../icons/isMuted.png";
muteSelectedTab(id);
}
}
function setSearchModeAction(target, targetID, parentElm, id) {
2018-10-13 22:08:32 +00:00
var currentMode = target.getAttribute("searchwindowsmode");
if (currentMode == "true") {
// In generateview
setSearchMode(target, "curent window", "window", false);
} else {
setSearchMode(target, "all windows", "windows", true);
}
clearNodes(listZone);
}
2018-04-20 06:06:32 +00:00
function setOldElm(target) {
oldElm.setAttribute("class", "block");
oldElm = target;
target.setAttribute("class", "block block-focused");
}
2020-06-30 21:36:00 +00:00
function scrollToView(time = 400) {
setTimeout(function () {
// Go to selected and 100px up
oldElm.scrollIntoView();
window.scrollBy(0, -100);
}, time);
}