2020-01-08 23:36:19 +01:00
|
|
|
var SysTrayX = {};
|
|
|
|
|
|
|
|
|
|
SysTrayX.Accounts = {
|
|
|
|
|
initialized: false,
|
2020-01-09 21:03:06 +01:00
|
|
|
|
2020-05-03 14:18:28 +02:00
|
|
|
init: function () {
|
2020-01-08 23:36:19 +01:00
|
|
|
this.getAccounts().then(this.getAccountsCb);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Use the messages API to get the unread messages (Promise)
|
|
|
|
|
* Be aware that the data is only avaiable inside the callback
|
|
|
|
|
*/
|
2020-05-03 14:18:28 +02:00
|
|
|
getAccounts: async function () {
|
2020-01-08 23:36:19 +01:00
|
|
|
return await browser.accounts.list();
|
|
|
|
|
},
|
2020-01-09 21:03:06 +01:00
|
|
|
|
2020-01-08 23:36:19 +01:00
|
|
|
/*
|
|
|
|
|
* Callback for getAccounts
|
|
|
|
|
*/
|
2020-05-03 14:18:28 +02:00
|
|
|
getAccountsCb: function (mailAccount) {
|
|
|
|
|
function createFolderTree(folders) {
|
|
|
|
|
let result = [];
|
|
|
|
|
let level = { result };
|
|
|
|
|
|
|
|
|
|
folders.forEach((folder) => {
|
|
|
|
|
folder.path
|
|
|
|
|
.slice(1)
|
|
|
|
|
.split("/")
|
|
|
|
|
.reduce((r, name, i, a) => {
|
|
|
|
|
if (!r[name]) {
|
|
|
|
|
r[name] = { result: [] };
|
|
|
|
|
r.result.push({ name: folder.name, children: r[name].result });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r[name];
|
|
|
|
|
}, level);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-08 23:36:19 +01:00
|
|
|
let accounts = new Object();
|
2020-01-09 21:03:06 +01:00
|
|
|
|
2020-01-23 23:42:33 +01:00
|
|
|
for (let i = 0; i < mailAccount.length; i++) {
|
2020-05-03 14:18:28 +02:00
|
|
|
if (true) {
|
|
|
|
|
console.debug("SysTrayX accounts id: " + mailAccount[i].id);
|
|
|
|
|
console.debug("SysTrayX accounts name: " + mailAccount[i].name);
|
|
|
|
|
console.debug("SysTrayX accounts type: " + mailAccount[i].type);
|
|
|
|
|
for (let j = 0; j < mailAccount[i].folders.length; j++) {
|
|
|
|
|
console.debug(
|
|
|
|
|
"SysTrayX accounts folder: " +
|
|
|
|
|
mailAccount[i].folders[j].name +
|
|
|
|
|
", " +
|
|
|
|
|
mailAccount[i].folders[j].type
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-09 21:03:06 +01:00
|
|
|
|
2020-01-09 23:37:33 +01:00
|
|
|
if (!accounts[mailAccount[i].type]) {
|
|
|
|
|
accounts[mailAccount[i].type] = [];
|
|
|
|
|
}
|
2020-01-09 21:03:06 +01:00
|
|
|
accounts[mailAccount[i].type].push({
|
|
|
|
|
id: mailAccount[i].id,
|
2020-01-11 23:21:00 +01:00
|
|
|
name: mailAccount[i].name,
|
2020-05-03 14:18:28 +02:00
|
|
|
folders: mailAccount[i].folders,
|
2020-01-09 21:03:06 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 23:37:33 +01:00
|
|
|
/*
|
|
|
|
|
* Build tree
|
|
|
|
|
*/
|
2020-01-08 23:36:19 +01:00
|
|
|
|
|
|
|
|
// Get base
|
2020-01-23 23:42:33 +01:00
|
|
|
const treeBase = document.getElementById("accountsTree");
|
2020-01-08 23:36:19 +01:00
|
|
|
|
2020-01-09 21:03:06 +01:00
|
|
|
for (let prop in accounts) {
|
2020-01-23 23:42:33 +01:00
|
|
|
const typeLi = document.createElement("li");
|
2020-01-08 23:36:19 +01:00
|
|
|
|
2020-01-23 23:42:33 +01:00
|
|
|
const typeSpan = document.createElement("span");
|
2020-01-09 23:37:33 +01:00
|
|
|
if (accounts[prop]) {
|
|
|
|
|
typeSpan.setAttribute("class", "caret");
|
|
|
|
|
}
|
2020-01-23 23:42:33 +01:00
|
|
|
const typeText = document.createTextNode(prop);
|
2020-01-08 23:36:19 +01:00
|
|
|
typeSpan.appendChild(typeText);
|
2020-01-09 21:03:06 +01:00
|
|
|
typeLi.appendChild(typeSpan);
|
2020-01-08 23:36:19 +01:00
|
|
|
|
2020-01-09 23:37:33 +01:00
|
|
|
if (accounts[prop]) {
|
2020-01-23 23:42:33 +01:00
|
|
|
const typeUl = document.createElement("ul");
|
2020-01-12 01:15:19 +01:00
|
|
|
typeUl.setAttribute("class", "nested active");
|
2020-01-09 23:37:33 +01:00
|
|
|
|
|
|
|
|
for (let i = 0; i < accounts[prop].length; ++i) {
|
2020-01-23 23:42:33 +01:00
|
|
|
const typeLi = document.createElement("li");
|
2020-05-03 14:18:28 +02:00
|
|
|
|
|
|
|
|
const typeSubSpan = document.createElement("span");
|
|
|
|
|
typeSubSpan.setAttribute("class", "caret");
|
|
|
|
|
typeLi.appendChild(typeSubSpan);
|
|
|
|
|
|
2020-01-23 23:42:33 +01:00
|
|
|
const typeInput = document.createElement("input");
|
2020-01-09 23:37:33 +01:00
|
|
|
typeInput.setAttribute("type", "checkbox");
|
2020-01-11 23:21:00 +01:00
|
|
|
typeInput.setAttribute("name", accounts[prop][i].id);
|
|
|
|
|
typeInput.setAttribute("value", JSON.stringify(accounts[prop][i]));
|
2020-05-03 14:50:04 +02:00
|
|
|
|
|
|
|
|
// typeInput.setAttribute("checked", "true");
|
|
|
|
|
// typeInput.setAttribute("indeterminate", "true");
|
|
|
|
|
|
2020-01-09 23:37:33 +01:00
|
|
|
typeLi.appendChild(typeInput);
|
2020-03-02 22:16:19 +01:00
|
|
|
const typeText = document.createTextNode(
|
|
|
|
|
" " + accounts[prop][i].name
|
|
|
|
|
);
|
2020-01-09 23:37:33 +01:00
|
|
|
typeLi.appendChild(typeText);
|
2020-05-03 14:18:28 +02:00
|
|
|
|
|
|
|
|
// Create a usable folder tree
|
|
|
|
|
const folders = createFolderTree(accounts[prop][i].folders);
|
|
|
|
|
|
|
|
|
|
// Recursive list creator
|
|
|
|
|
function createListLevel(level) {
|
|
|
|
|
const typeLevelUl = document.createElement("ul");
|
|
|
|
|
typeLevelUl.setAttribute("class", "nested");
|
|
|
|
|
|
|
|
|
|
level.forEach((element) => {
|
|
|
|
|
console.debug("Name: " + element.name);
|
|
|
|
|
|
|
|
|
|
const typeEleLi = document.createElement("li");
|
|
|
|
|
|
|
|
|
|
const typeEleSpan = document.createElement("span");
|
|
|
|
|
if (element.children.length > 0) {
|
|
|
|
|
typeEleSpan.setAttribute("class", "caret");
|
|
|
|
|
} else {
|
|
|
|
|
typeEleSpan.setAttribute("class", "noncaret");
|
|
|
|
|
}
|
|
|
|
|
typeEleLi.appendChild(typeEleSpan);
|
|
|
|
|
|
|
|
|
|
const typeEleInput = document.createElement("input");
|
|
|
|
|
typeEleInput.setAttribute("type", "checkbox");
|
|
|
|
|
typeEleInput.setAttribute("name", element.name);
|
2020-05-03 14:50:04 +02:00
|
|
|
// typeEleInput.setAttribute("value", JSON.stringify(element.name));
|
|
|
|
|
// typeEleInput.setAttribute("checked", "true");
|
2020-05-03 14:18:28 +02:00
|
|
|
typeEleLi.appendChild(typeEleInput);
|
|
|
|
|
const typeEleText = document.createTextNode(" " + element.name);
|
|
|
|
|
typeEleLi.appendChild(typeEleText);
|
|
|
|
|
|
|
|
|
|
if (element.children.length > 0) {
|
|
|
|
|
typeEleLi.appendChild(createListLevel(element.children));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typeLevelUl.appendChild(typeEleLi);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return typeLevelUl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a folder list recursively
|
|
|
|
|
typeLi.appendChild(createListLevel(folders));
|
|
|
|
|
|
2020-01-09 23:37:33 +01:00
|
|
|
typeUl.appendChild(typeLi);
|
|
|
|
|
}
|
|
|
|
|
typeLi.appendChild(typeUl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
treeBase.appendChild(typeLi);
|
2020-01-11 17:17:01 +01:00
|
|
|
|
|
|
|
|
// Restore saved selection
|
2020-05-03 14:50:04 +02:00
|
|
|
|
2020-01-11 17:17:01 +01:00
|
|
|
function setAccounts(result) {
|
2020-01-23 23:42:33 +01:00
|
|
|
const treeBase = document.getElementById("accountsTree");
|
|
|
|
|
const accounts = result.accounts || [];
|
2020-01-11 17:17:01 +01:00
|
|
|
for (let i = 0; i < accounts.length; ++i) {
|
2020-01-23 23:42:33 +01:00
|
|
|
const checkbox = treeBase.querySelector(
|
2020-01-11 23:21:00 +01:00
|
|
|
`input[name=${accounts[i].id}]`
|
2020-01-11 17:17:01 +01:00
|
|
|
);
|
|
|
|
|
if (checkbox) {
|
|
|
|
|
checkbox.checked = accounts[i].checked;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onError(error) {
|
|
|
|
|
console.log(`GetAccounts Error: ${error}`);
|
|
|
|
|
}
|
2020-05-03 14:50:04 +02:00
|
|
|
/*
|
2020-01-23 23:42:33 +01:00
|
|
|
const getAccounts = browser.storage.sync.get("accounts");
|
2020-01-11 17:17:01 +01:00
|
|
|
getAccounts.then(setAccounts, onError);
|
2020-05-03 14:50:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
let checkboxes = document.querySelectorAll('input[type="checkbox"]');
|
|
|
|
|
|
|
|
|
|
for (let x = 0; x < checkboxes.length; x++) {
|
|
|
|
|
checkboxes[x].addEventListener("change", function (e) {
|
|
|
|
|
console.debug("Change detect");
|
|
|
|
|
|
|
|
|
|
let parentNode = this.parentNode;
|
|
|
|
|
|
|
|
|
|
const cbDescendants = parentNode.querySelectorAll(
|
|
|
|
|
'input[type="checkbox"]'
|
|
|
|
|
);
|
|
|
|
|
for (let y = 0; y < cbDescendants.length; y++) {
|
|
|
|
|
cbDescendants[y].checked = this.checked;
|
|
|
|
|
cbDescendants[y].indeterminate = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (["ul", "li"].indexOf(parentNode.nodeName.toLowerCase()) >= 0) {
|
|
|
|
|
const mainCb = parentNode.querySelector(
|
|
|
|
|
':scope > input[type="checkbox"]'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (mainCb && mainCb != this) {
|
|
|
|
|
mainCb.checked = this.checked;
|
|
|
|
|
|
|
|
|
|
const mainCbChildren = mainCb.parentNode.querySelectorAll(
|
|
|
|
|
'input[type="checkbox"]'
|
|
|
|
|
);
|
|
|
|
|
const numTotal = mainCbChildren.length;
|
|
|
|
|
|
|
|
|
|
let numChecked = 0;
|
|
|
|
|
for (let z = 0; z < mainCbChildren.length; z++) {
|
|
|
|
|
numChecked += mainCbChildren[z].checked;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (numTotal === numChecked) {
|
|
|
|
|
mainCb.indeterminate = false;
|
|
|
|
|
mainCb.checked = true;
|
|
|
|
|
} else {
|
|
|
|
|
if (numChecked === 0) {
|
|
|
|
|
mainCb.indeterminate = false;
|
|
|
|
|
mainCb.checked = false;
|
|
|
|
|
} else {
|
|
|
|
|
mainCb.indeterminate = true;
|
|
|
|
|
mainCb.checked = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parentNode = parentNode.parentNode;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-01-09 23:37:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Activate tree
|
|
|
|
|
*/
|
2020-01-23 23:42:33 +01:00
|
|
|
const toggler = document.getElementsByClassName("caret");
|
2020-01-09 23:37:33 +01:00
|
|
|
for (let i = 0; i < toggler.length; i++) {
|
2020-05-03 14:18:28 +02:00
|
|
|
toggler[i].addEventListener("click", function () {
|
2020-01-09 23:37:33 +01:00
|
|
|
this.parentElement.querySelector(".nested").classList.toggle("active");
|
|
|
|
|
this.classList.toggle("caret-down");
|
|
|
|
|
});
|
2020-01-09 21:03:06 +01:00
|
|
|
}
|
2020-05-03 14:18:28 +02:00
|
|
|
},
|
2020-01-08 23:36:19 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SysTrayX.Accounts.init();
|