TreeTabsMigration/scripts/refresh.js

328 lines
11 KiB
JavaScript
Raw Normal View History

2017-11-12 22:00:40 +01:00
// Copyright (c) 2017 kroppy. All rights reserved.
// Use of this source code is governed by a Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0) license
// that can be found at https://creativecommons.org/licenses/by-nc-nd/4.0/
// ********** REFRESH GUI ***************
2018-05-22 02:11:29 +02:00
async function RefreshGUI() {
2018-03-13 14:39:34 +01:00
let toolbar = document.getElementById("toolbar");
2018-05-22 02:11:29 +02:00
let toolbarHeight = 27;
2018-03-13 14:39:34 +01:00
if (toolbar.children.length > 0) {
toolbar.style.height = "";
toolbar.style.width = "";
toolbar.style.display = "";
toolbar.style.border = "";
toolbar.style.padding = "";
if (document.querySelector(".on.button") != null) {
toolbar.style.height = "53px";
2018-05-22 02:11:29 +02:00
toolbarHeight = 54;
2017-11-12 22:00:40 +01:00
} else {
2018-03-13 14:39:34 +01:00
toolbar.style.height = "26px";
2017-11-12 22:00:40 +01:00
}
} else {
2018-03-13 14:39:34 +01:00
toolbar.style.height = "0px";
2018-05-22 02:11:29 +02:00
toolbarHeight = 0;
2018-03-13 14:39:34 +01:00
toolbar.style.width = "0px";
toolbar.style.display = "none";
toolbar.style.border = "none";
toolbar.style.padding = "0";
2017-11-12 22:00:40 +01:00
}
2018-05-22 02:11:29 +02:00
let group_list = document.getElementById("group_list");
group_list.style.width = document.body.clientWidth + 50 + "px";
2018-03-13 14:39:34 +01:00
let pin_list = document.getElementById("pin_list");
if (pin_list.children.length > 0) {
2018-05-22 02:11:29 +02:00
pin_list.style.top = toolbarHeight + "px";
2018-03-13 14:39:34 +01:00
pin_list.style.height = "";
2018-05-22 02:11:29 +02:00
pin_list.style.width = "";
2018-03-13 14:39:34 +01:00
pin_list.style.display = "";
pin_list.style.border = "";
pin_list.style.padding = "";
2017-11-12 22:00:40 +01:00
} else {
2018-03-13 14:39:34 +01:00
pin_list.style.top = "0px";
pin_list.style.height = "0px";
pin_list.style.width = "0px";
pin_list.style.display = "none";
pin_list.style.border = "none";
pin_list.style.padding = "0";
2017-11-12 22:00:40 +01:00
}
2018-05-22 02:11:29 +02:00
let pin_listHeight = pin_list.getBoundingClientRect().height;
2018-03-13 14:39:34 +01:00
let toolbar_groups = document.getElementById("toolbar_groups");
2018-05-22 02:11:29 +02:00
toolbar_groups.style.top = toolbarHeight + pin_listHeight + "px";
toolbar_groups.style.height = document.body.clientHeight - toolbarHeight - pin_listHeight + "px";
let toolbar_groupsWidth = toolbar_groups.getBoundingClientRect().width;
2018-03-13 14:39:34 +01:00
if (opt.show_counter_groups) {
document.querySelectorAll(".group").forEach(function(s){
let groupLabel = document.getElementById("_gte"+s.id);
if (groupLabel) {
2018-07-03 20:36:38 +02:00
groupLabel.textContent = (tt.groups[s.id] ? tt.groups[s.id].name : labels.noname_group) + " (" + document.querySelectorAll("#"+s.id+" .tab").length + ")";
2018-03-13 14:39:34 +01:00
}
});
} else {
document.querySelectorAll(".group").forEach(function(s){
let groupLabel = document.getElementById("_gte"+s.id);
if (groupLabel) {
2018-07-03 20:36:38 +02:00
groupLabel.textContent = tt.groups[s.id] ? tt.groups[s.id].name : labels.noname_group;
2018-03-13 14:39:34 +01:00
}
});
}
document.querySelectorAll(".group_button").forEach(function(s){
s.style.height = s.firstChild.getBoundingClientRect().height + "px";
2017-11-12 22:00:40 +01:00
});
2018-03-13 14:39:34 +01:00
let groups = document.getElementById("groups");
2018-05-22 02:11:29 +02:00
let groupsHeight = document.body.clientHeight - toolbarHeight - pin_listHeight;
2018-07-03 20:36:38 +02:00
let groupsWidth = document.body.clientWidth - toolbar_groupsWidth - 1;
2018-05-22 02:11:29 +02:00
groups.style.top = toolbarHeight + pin_listHeight + "px";
groups.style.left = toolbar_groupsWidth + "px";
groups.style.height = groupsHeight + "px";
2018-07-03 20:36:38 +02:00
groups.style.width = groupsWidth + "px";
2018-05-22 02:11:29 +02:00
2018-07-03 20:36:38 +02:00
// let bottom_floating_buttons = document.getElementById("status_bar");
// let active_group_tabs = document.getElementById("ct"+tt.active_group);
// bottom_floating_buttons.style.left = toolbar_groupsWidth + "px";
// bottom_floating_buttons.style.width = toolbar_groupsWidth + active_group_tabs.clientWidth + "px";
2018-05-22 02:11:29 +02:00
let PanelList = document.querySelector(".mw_pan_on>.manager_window_list");
let PanelListHeight = 3 + PanelList.children.length * 18;
let ManagerWindowPanelButtons = document.querySelector(".mw_pan_on>.manager_window_panel_buttons");
let ManagerWindowPanelButtonsHeight = ManagerWindowPanelButtons.clientHeight;
let MaxAllowedHeight = document.body.clientHeight - 140;
if (PanelListHeight + ManagerWindowPanelButtonsHeight < MaxAllowedHeight) {
PanelList.style.height = PanelListHeight + "px";
} else {
PanelList.style.height = MaxAllowedHeight - ManagerWindowPanelButtonsHeight + "px";
}
let ManagerWindow = document.getElementById("manager_window");
ManagerWindow.style.height = PanelList.clientHeight + ManagerWindowPanelButtonsHeight + 56 + "px";
2017-11-12 22:00:40 +01:00
}
// set discarded class
function RefreshDiscarded(tabId) {
2018-03-13 14:39:34 +01:00
let t = document.getElementById(tabId);
if (t != null) {
2017-11-12 22:00:40 +01:00
chrome.tabs.get(parseInt(tabId), function(tab) {
if (tab) {
if (tab.discarded) {
2018-03-13 14:39:34 +01:00
t.classList.add("discarded");
2017-11-12 22:00:40 +01:00
} else {
2018-03-13 14:39:34 +01:00
t.classList.remove("discarded");
2018-07-03 20:36:38 +02:00
t.classList.remove("audible");
t.classList.remove("muted");
2017-11-12 22:00:40 +01:00
}
}
});
}
}
// set discarded class
function SetAttentionIcon(tabId) {
2018-03-13 14:39:34 +01:00
let t = document.getElementById(tabId);
if (t != null) {
t.classList.add("attention");
2017-11-12 22:00:40 +01:00
}
}
// change media icon
function RefreshMediaIcon(tabId) {
2018-03-13 14:39:34 +01:00
let t = document.getElementById(tabId);
if (t != null) {
2017-11-12 22:00:40 +01:00
chrome.tabs.get(parseInt(tabId), function(tab) {
if (tab) {
2018-07-03 20:36:38 +02:00
if (tab.mutedInfo.muted && !tab.discarded) {
2018-03-13 14:39:34 +01:00
t.classList.remove("audible");
t.classList.add("muted");
2017-11-12 22:00:40 +01:00
}
2018-07-03 20:36:38 +02:00
if (!tab.mutedInfo.muted && tab.audible && !tab.discarded) {
2018-03-13 14:39:34 +01:00
t.classList.remove("muted");
t.classList.add("audible");
2017-11-12 22:00:40 +01:00
}
2018-07-03 20:36:38 +02:00
if ((!tab.mutedInfo.muted && !tab.audible) || tab.discarded) {
2018-03-13 14:39:34 +01:00
t.classList.remove("audible");
t.classList.remove("muted");
2017-11-12 22:00:40 +01:00
}
}
});
}
}
// Vivaldi does not have changeInfo.audible listener, this is my own implementation, hopefully this will not affect performance too much
function VivaldiRefreshMediaIcons() {
2017-11-20 18:14:07 +01:00
setInterval(function() {
2018-05-22 02:11:29 +02:00
chrome.tabs.query({currentWindow: true, audible: true}, function(tabs) {
2018-03-13 14:39:34 +01:00
document.querySelectorAll(".audible, .muted").forEach(function(s){
s.classList.remove("audible");
s.classList.remove("muted");
});
2017-11-20 18:14:07 +01:00
let tc = tabs.length;
for (var ti = 0; ti < tc; ti++) {
if (tabs[ti].audible) {
2018-03-13 14:39:34 +01:00
document.getElementById(tabs[ti].id).classList.add("audible");
2017-11-20 18:14:07 +01:00
}
if (tabs[ti].mutedInfo.muted) {
2018-03-13 14:39:34 +01:00
document.getElementById(tabs[ti].id).classList.add("muted");
2017-11-20 18:14:07 +01:00
}
2017-11-12 22:00:40 +01:00
}
2017-11-20 18:14:07 +01:00
});
2018-05-22 02:11:29 +02:00
}, 2000);
2017-11-12 22:00:40 +01:00
}
2018-07-03 20:36:38 +02:00
async function LoadFavicon(tabId, Img, TryUrls, TabHeaderNode, i) {
if (TabHeaderNode){
Img.src = TryUrls[i];
Img.onload = function() {
TabHeaderNode.style.backgroundImage = "url(" + TryUrls[i] + ")";
if (browserId == "F") { // cache Firefox favicon - solution for bug with empty favicons in unloaded tabs
browser.sessions.setTabValue(tabId, "CachedFaviconUrl", TryUrls[i]);
}
};
Img.onerror = function() {
if (i < TryUrls.length) {
LoadFavicon(tabId, Img, TryUrls, TabHeaderNode, (i+1));
}
}
}
}
2018-05-22 02:11:29 +02:00
async function GetFaviconAndTitle(tabId, addCounter) {
2018-03-13 14:39:34 +01:00
let t = document.getElementById(tabId);
if (t != null) {
2018-07-03 20:36:38 +02:00
let CachedFavicon;
if (browserId == "F") {
let ttf = Promise.resolve(browser.sessions.getTabValue(tabId, "CachedFaviconUrl")).then(function(FaviconUrl) {
CachedFavicon = FaviconUrl;
});
}
2017-11-12 22:00:40 +01:00
chrome.tabs.get(parseInt(tabId), function(tab) {
if (tab){
2017-11-20 18:14:07 +01:00
let title = tab.title ? tab.title : tab.url;
2018-05-22 02:11:29 +02:00
let tHeader = t.childNodes[0];
let tTitle = t.childNodes[0].childNodes[2];
2018-07-03 20:36:38 +02:00
if (tab.status == "complete" || tab.discarded) {
2018-03-13 14:39:34 +01:00
t.classList.remove("loading");
2018-07-03 20:36:38 +02:00
2018-03-13 14:39:34 +01:00
tTitle.textContent = title;
tHeader.title = title;
if (opt.show_counter_tabs_hints) {
tHeader.setAttribute("tabTitle", title);
}
2018-07-03 20:36:38 +02:00
let Img = new Image();
if (browserId != "F") {
CachedFavicon = "chrome://favicon/"+tab.url;
2017-11-12 22:00:40 +01:00
}
2018-07-03 20:36:38 +02:00
let TryCases = [tab.favIconUrl, CachedFavicon, , "./theme/icon_empty.svg"];
LoadFavicon(tabId, Img, TryCases, tHeader, 0);
2017-11-12 22:00:40 +01:00
}
2018-07-03 20:36:38 +02:00
if (tab.status == "loading" && tab.discarded == false) {
title = tab.title ? tab.title : labels.loading;
2018-03-13 14:39:34 +01:00
t.classList.add("loading");
tHeader.style.backgroundImage = "";
2018-07-03 20:36:38 +02:00
tHeader.title = labels.loading;
if (opt.show_counter_tabs_hints) {
tHeader.setAttribute("tabTitle", labels.loading);
}
2018-07-03 20:36:38 +02:00
tTitle.textContent = labels.loading;
2017-11-12 22:00:40 +01:00
setTimeout(function() {
2018-03-13 14:39:34 +01:00
if (document.getElementById(tab.id) != null) GetFaviconAndTitle(tab.id, addCounter);
2017-11-12 22:00:40 +01:00
}, 1000);
}
2018-01-01 19:50:56 +01:00
if (addCounter && (opt.show_counter_tabs || opt.show_counter_tabs_hints)) {
RefreshTabCounter(tabId);
2017-11-20 18:14:07 +01:00
}
2017-11-12 22:00:40 +01:00
}
});
}
}
// refresh open closed trees states
2018-05-22 02:11:29 +02:00
async function RefreshExpandStates() {
2018-07-03 20:36:38 +02:00
document.querySelectorAll("#"+tt.active_group+" .folder").forEach(function(s){
2018-05-22 02:11:29 +02:00
if (s.childNodes[1].children.length == 0 && s.childNodes[2].children.length == 0) {
2018-03-13 14:39:34 +01:00
s.classList.remove("o");
s.classList.remove("c");
2018-01-01 19:50:56 +01:00
} else {
2018-03-13 14:39:34 +01:00
if (s.classList.contains("o") == false && s.classList.contains("c") == false) {
s.classList.add("o");
2017-11-20 18:14:07 +01:00
}
2018-01-01 19:50:56 +01:00
}
});
2018-07-03 20:36:38 +02:00
document.querySelectorAll("#"+tt.active_group+" .tab").forEach(function(s){
2018-05-22 02:11:29 +02:00
if (s.childNodes[1].children.length == 0) {
2018-03-13 14:39:34 +01:00
s.classList.remove("o");
s.classList.remove("c");
2017-11-12 22:00:40 +01:00
} else {
2018-03-13 14:39:34 +01:00
if (s.classList.contains("o") == false && s.classList.contains("c") == false) {
s.classList.add("o");
2017-11-20 18:14:07 +01:00
}
2017-11-12 22:00:40 +01:00
}
});
2017-11-20 18:14:07 +01:00
}
2018-05-22 02:11:29 +02:00
async function RefreshCounters() {
2018-01-01 19:50:56 +01:00
if (opt.show_counter_tabs || opt.show_counter_tabs_hints) {
// if (opt.show_counter_tabs_hints) {
// document.querySelectorAll("#"+tt.active_group+" .tab").forEach(function(s){
// let title = s.childNodes[0].getAttribute("tabTitle");
// if (title != null) {
// s.childNodes[0].title = title;
// s.childNodes[0].childNodes[1].textContent = title;
// }
// });
// }
2018-07-03 20:36:38 +02:00
document.querySelectorAll("#"+tt.active_group+" .o.tab, #"+tt.active_group+" .c.tab").forEach(function(s){
if (opt.show_counter_tabs) {
s.childNodes[0].childNodes[1].childNodes[0].textContent = document.querySelectorAll("[id='" + s.id + "'] .tab").length;
2018-01-01 19:50:56 +01:00
}
if (opt.show_counter_tabs_hints) {
let title = s.childNodes[0].getAttribute("tabTitle");
s.childNodes[0].title = (document.querySelectorAll("[id='" + s.id + "'] .tab").length +" • ") + title;
2018-01-01 19:50:56 +01:00
}
});
// ·
2018-07-03 20:36:38 +02:00
document.querySelectorAll("#"+tt.active_group+" .folder").forEach(function(s){
if (opt.show_counter_tabs && tt.folders[s.id]) {
s.childNodes[0].childNodes[1].childNodes[0].textContent = document.querySelectorAll("[id='" + s.id + "'] .tab").length;
2018-01-01 19:50:56 +01:00
}
2018-07-03 20:36:38 +02:00
if (opt.show_counter_tabs_hints && tt.folders[s.id]) {
s.childNodes[0].title = (document.querySelectorAll("[id='" + s.id + "'] .tab").length +" • ") + tt.folders[s.id].name;
2018-01-01 19:50:56 +01:00
}
});
}
}
2018-05-22 02:11:29 +02:00
async function RefreshTabCounter(tabId) {
2018-03-13 14:39:34 +01:00
let t = document.getElementById(tabId);
2018-07-03 20:36:38 +02:00
if (t != null && t.childNodes[0]) {
2018-05-22 02:11:29 +02:00
let title = t.childNodes[0].getAttribute("tabTitle");
if (t != null && title != null) {
if (t.classList.contains("o") || t.classList.contains("c")) {
if (opt.show_counter_tabs) {
t.childNodes[0].childNodes[1].childNodes[0].textContent = document.querySelectorAll("[id='" + t.id + "'] .tab").length;
2018-05-22 02:11:29 +02:00
}
if (opt.show_counter_tabs_hints) {
t.childNodes[0].title = (document.querySelectorAll("[id='" + t.id + "'] .tab").length +" • ") + title;
2018-05-22 02:11:29 +02:00
}
} else {
t.childNodes[0].title = title;
2017-11-20 18:14:07 +01:00
}
}
}
}