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

202 lines
7.0 KiB
JavaScript
Raw Normal View History

const storageArea = browser.storage.local;
2018-05-06 01:02:50 +00:00
const tabsAction = browser.tabs;
2020-03-27 00:29:05 +00:00
const windowsAction = browser.windows;
2018-05-06 01:02:50 +00:00
const searchBar = document.getElementById("searchBar");
const errHandler = document.getElementById("errorZone");
const listZone = document.getElementById("listZone");
const notFoundText = document.createTextNode("Search not found...");
const tabImg = browser.extension.getURL("icons/tab.png");
2019-05-04 23:12:22 +00:00
let hoverTarget = undefined
let oldElm = undefined;
let currentWinId = undefined;
let newWinId = undefined;
let focusedWinID = undefined;
let windowIndex = 0;
2018-04-20 06:06:32 +00:00
2020-05-01 03:49:20 +00:00
function popoutSelectedTab(id) {
const randomWinId = Math.floor(Math.random() * 99999);
windowsAction.create({tabId: id});
}
2020-07-04 10:35:06 +00:00
async function duplicateTab(id, pos) {
tab = await tabsAction.duplicate(id, {index: pos, active: true});
createContainer(tab, pos);
2020-06-30 21:36:00 +00:00
}
2020-07-04 10:35:06 +00:00
function unhideSelectedTab(id) { tabsAction.show(id).then(successMsg, errMsg); }
function hideSelectedTab(id) { tabsAction.hide(id).then(successMsg, errMsg); }
function muteSelectedTab(id) { tabsAction.update(id, {muted: true}) }
function unmuteSelectedTab(id) { tabsAction.update(id, {muted: false}) }
2020-05-01 03:49:20 +00:00
2020-07-04 10:35:06 +00:00
function resetWinIndex() { windowIndex = 0; }
function createWin() { windowsAction.create({}); }
function onError(error) { console.log(`Error: ${error}`); }
2020-05-01 03:49:20 +00:00
2020-07-04 10:35:06 +00:00
function createTab() {
tabsAction.create({}).then(function (tab) { createContainer(tab); });
2020-05-01 03:49:20 +00:00
}
2018-04-20 06:06:32 +00:00
function logTabs(tabs) {
windowIndex = 0;
2018-09-30 22:03:56 +00:00
tabsAction.query({currentWindow: true, active: true}).then((tab) => {
focusedWinID = tab[0].windowId;
2018-09-30 22:03:56 +00:00
}, focusedWinID).then(() => {
for (let tab of tabs) {
currentWinId = tab.windowId;
2020-07-04 10:35:06 +00:00
2018-09-30 22:03:56 +00:00
if (currentWinId == newWinId) {
createContainer(tab);
} else {
2018-09-30 22:03:56 +00:00
if (windowIndex != 0) {
var pTag = document.createElement("P");
var msg = (focusedWinID == tab.windowId)
2020-07-04 10:35:06 +00:00
? "[ Current Window ] " : "Window: " + tab.windowId;
2018-09-30 22:03:56 +00:00
var pText = document.createTextNode(msg);
pTag.className = "windowIdHeaders";
pTag.appendChild(pText);
listZone.appendChild(pTag);
windowIndex++;
createContainer(tab);
} else {
createContainer(tab);
windowIndex = 1;
}
}
2018-09-30 22:03:56 +00:00
newWinId = currentWinId;
}
2018-09-30 22:03:56 +00:00
});
newWinId = undefined;
2020-03-27 19:34:04 +00:00
scrollToView();
2018-04-20 06:06:32 +00:00
}
2020-07-04 10:35:06 +00:00
function createContainer(tab, pos = null) {
var template = document.querySelector('#tabContainerTemplate');
var clone = document.importNode(template.content, true);
var spanTag = clone.querySelector("#iconElm");
var closeImgTag = clone.querySelector("#closeBttn");
var hidnStImgTag = clone.querySelector("#hideTgglBttn");
var pTag = clone.querySelector(".pTagTitleText");
var icoImgTag = document.createElement("IMG"); // Used to detect image load failure
var iconText = document.createTextNode(tab.title);
var id = tab.id;
2018-04-20 06:06:32 +00:00
2018-09-30 22:03:56 +00:00
// Set oldElm so eventListeners.js has starting ref
if (tab.active == true) {
browser.windows.getCurrent().then((window) => {
if (tab.windowId == window.id) {
spanTag.className = "block block-focused";
if (oldElm) {
oldElm.setAttribute("class", "block");
}
oldElm = spanTag;
}
}, tab, oldElm, spanTag);
}
2018-05-06 01:02:50 +00:00
spanTag.setAttribute("tabID", tab.id);
2020-07-04 10:35:06 +00:00
spanTag.title = tab.title;
2020-07-04 10:35:06 +00:00
if (!tab.hidden) {
spanTag.className = "block";
2020-07-04 10:35:06 +00:00
} else {
spanTag.className = "block hiddenBGColor";
2020-07-04 10:35:06 +00:00
}
2020-07-04 10:35:06 +00:00
spanTag.style.backgroundImage = "url(" + tab.favIconUrl + ")";
icoImgTag.src = tab.favIconUrl;
icoImgTag.onerror = function() { spanTag.style.backgroundImage = "url(" + tabImg + ")"; }
2019-05-04 23:12:22 +00:00
spanTag.addEventListener("mouseenter", function (eve) {
moveTabControlTo(eve.target);
});
pTag.appendChild(iconText);
2020-07-04 10:35:06 +00:00
if (pos == null) {
listZone.appendChild(clone);
} else {
listZone.insertBefore(clone, listZone.children[pos]);
}
2018-05-06 01:02:50 +00:00
}
2018-04-20 06:06:32 +00:00
2020-05-01 03:49:20 +00:00
async function moveTabControlTo(elm) {
2020-07-04 10:35:06 +00:00
let tabControls = document.getElementById("tabControls");
let tabControls2 = document.getElementById("tabControls2");
let hideTgglBttn = tabControls.querySelector("#hideTgglBttn")
let muteTgglBttn = tabControls.querySelector("#muteTgglBttn")
let rect = elm.getBoundingClientRect();
tabControls.style.left = (rect.left) + "px";
tabControls.style.top = (rect.top) + "px";
2020-06-30 21:36:00 +00:00
tabControls2.style.left = (rect.left - 46) + "px";
tabControls2.style.top = rect.top + "px";
2020-07-04 10:35:06 +00:00
hoverTarget = elm;
2019-05-04 23:12:22 +00:00
2020-07-04 10:35:06 +00:00
if (elm.className == "block hiddenBGColor") {
2020-03-27 00:29:05 +00:00
hideTgglBttn.src = "../icons/eyeClosed.png";
2020-07-04 10:35:06 +00:00
} else {
2020-03-27 00:29:05 +00:00
hideTgglBttn.src = "../icons/eyeOpen.png";
2020-07-04 10:35:06 +00:00
}
2020-05-01 03:49:20 +00:00
id = parseInt(hoverTarget.getAttribute("tabID"));
tab = await tabsAction.get(id);
2020-07-04 10:35:06 +00:00
if (tab.mutedInfo.muted == false) {
2020-05-01 03:49:20 +00:00
muteTgglBttn.src = "../icons/isNotMuted.png";
2020-07-04 10:35:06 +00:00
} else {
2020-05-01 03:49:20 +00:00
muteTgglBttn.src = "../icons/isMuted.png";
2020-07-04 10:35:06 +00:00
}
2020-05-01 03:49:20 +00:00
2020-06-30 21:36:00 +00:00
tabControls.style.display = "";
tabControls2.style.display = "";
2019-05-04 23:12:22 +00:00
}
function getTabs() {
clearNodes(listZone);
storageArea.get("searchMode").then((results) => {
var target = document.getElementById("searchMode");
if (Object.keys(results).length > 0) {
var fileKeys = Object.keys(results);
for (let fileKey of fileKeys) {
var key = results[fileKey];
if (key) {
target.title = "Searching curent windows...";
target.src = "../icons/windows.png";
target.setAttribute("searchwindowsmode", true);
tabsAction.query({}).then(logTabs, onError)
.then(resetWinIndex, onError);
} else {
target.title = "Searching curent window...";
target.src = "../icons/window.png";
target.setAttribute("searchwindowsmode", false);
tabsAction.query({currentWindow: true})
.then(logTabs, onError);
}
}
} else {
setSearchMode(target, "curent window", "window", false);
}
});
}
async function setSearchMode(target, text, img, state) {
2020-07-04 10:35:06 +00:00
target.title = "Searching " + text + "...";
target.src = "../icons/" + img + ".png";
target.setAttribute("searchwindowsmode", state);
await storageArea.set({"searchMode": state });
getTabs(); // No loop b/c object keys will be greater than 0 after setup
}
function clearNodes(targetNode) {
while (targetNode.firstChild) {
targetNode.removeChild(targetNode.firstChild);
}
2018-05-06 01:02:50 +00:00
}