Files
SysTray-X/webext/js/options_accounts.js

240 lines
7.2 KiB
JavaScript
Raw Normal View History

2020-01-08 23:36:19 +01:00
var SysTrayX = {};
SysTrayX.Accounts = {
initialized: false,
2020-01-09 21:03:06 +01: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
*/
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
*/
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: [] };
2020-05-03 23:39:02 +02:00
r.result.push({
name: folder.name,
accountId: folder.accountId,
path: folder.path,
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 23:39:02 +02:00
if (false) {
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,
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");
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("value", JSON.stringify(accounts[prop][i]));
2020-05-03 23:39:02 +02:00
typeInput.setAttribute("name", accounts[prop][i].id);
2020-05-03 14:50:04 +02:00
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);
// 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) => {
const typeEleLi = document.createElement("li");
const typeEleSpan = document.createElement("span");
if (element.children.length > 0) {
typeEleSpan.setAttribute("class", "caret");
} else {
2020-05-03 23:39:02 +02:00
typeEleSpan.setAttribute("class", "caretfiller");
}
typeEleLi.appendChild(typeEleSpan);
const typeEleInput = document.createElement("input");
typeEleInput.setAttribute("type", "checkbox");
2020-05-03 23:39:02 +02:00
typeEleInput.setAttribute(
"value",
JSON.stringify({
accountId: element.accountId,
path: element.path,
})
);
if (element.children.length > 0) {
typeEleInput.setAttribute("name", "parent-" + element.name);
} else {
typeEleInput.setAttribute("name", "child-" + element.name);
}
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
2020-05-03 23:39:02 +02:00
// Setup checkbox control
let checkboxes = treeBase.querySelectorAll('input[type="checkbox"]');
2020-05-03 14:50:04 +02:00
for (let x = 0; x < checkboxes.length; x++) {
checkboxes[x].addEventListener("change", function (e) {
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++) {
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-01-08 23:36:19 +01:00
};
SysTrayX.Accounts.init();