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

119 lines
3.4 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
2020-01-08 23:36:19 +01:00
init: function() {
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() {
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) {
let accounts = new Object();
let i;
2020-01-09 21:03:06 +01:00
for (i = 0; i < mailAccount.length; i++) {
console.debug("SysTrayX accounts id: " + mailAccount[i].id);
console.debug("SysTrayX accounts name: " + mailAccount[i].name);
console.debug("SysTrayX accounts type: " + mailAccount[i].type);
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-09 23:37:33 +01:00
let 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) {
let typeLi = document.createElement("li");
2020-01-08 23:36:19 +01:00
2020-01-09 21:03:06 +01:00
let typeSpan = document.createElement("span");
2020-01-09 23:37:33 +01:00
if (accounts[prop]) {
typeSpan.setAttribute("class", "caret");
}
2020-01-08 23:36:19 +01:00
let typeText = document.createTextNode(prop);
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]) {
let 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) {
let typeLi = document.createElement("li");
let typeInput = document.createElement("input");
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-01-11 17:17:01 +01:00
typeInput.setAttribute("checked", "true");
2020-01-09 23:37:33 +01:00
typeLi.appendChild(typeInput);
let typeText = document.createTextNode(" " + accounts[prop][i].name);
typeLi.appendChild(typeText);
typeUl.appendChild(typeLi);
}
typeLi.appendChild(typeUl);
}
treeBase.appendChild(typeLi);
2020-01-11 17:17:01 +01:00
// Restore saved selection
function setAccounts(result) {
console.debug("Restore account selection");
let treeBase = document.getElementById("accountsTree");
let accounts = result.accounts || [];
for (let i = 0; i < accounts.length; ++i) {
let 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;
}
}
console.debug("Restore account selection done");
}
function onError(error) {
console.log(`GetAccounts Error: ${error}`);
}
let getAccounts = browser.storage.sync.get("accounts");
getAccounts.then(setAccounts, onError);
2020-01-09 23:37:33 +01:00
}
/*
* Activate tree
*/
let toggler = document.getElementsByClassName("caret");
for (let i = 0; i < toggler.length; i++) {
toggler[i].addEventListener("click", function() {
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();