Add some selection logic

This commit is contained in:
Ximi1970
2020-05-03 14:50:04 +02:00
parent f134bf835f
commit 6ddbe294dd

View File

@@ -100,7 +100,10 @@ SysTrayX.Accounts = {
typeInput.setAttribute("type", "checkbox"); typeInput.setAttribute("type", "checkbox");
typeInput.setAttribute("name", accounts[prop][i].id); typeInput.setAttribute("name", accounts[prop][i].id);
typeInput.setAttribute("value", JSON.stringify(accounts[prop][i])); typeInput.setAttribute("value", JSON.stringify(accounts[prop][i]));
typeInput.setAttribute("checked", "true");
// typeInput.setAttribute("checked", "true");
// typeInput.setAttribute("indeterminate", "true");
typeLi.appendChild(typeInput); typeLi.appendChild(typeInput);
const typeText = document.createTextNode( const typeText = document.createTextNode(
" " + accounts[prop][i].name " " + accounts[prop][i].name
@@ -132,7 +135,7 @@ SysTrayX.Accounts = {
typeEleInput.setAttribute("type", "checkbox"); typeEleInput.setAttribute("type", "checkbox");
typeEleInput.setAttribute("name", element.name); typeEleInput.setAttribute("name", element.name);
// typeEleInput.setAttribute("value", JSON.stringify(element.name)); // typeEleInput.setAttribute("value", JSON.stringify(element.name));
typeEleInput.setAttribute("checked", "true"); // typeEleInput.setAttribute("checked", "true");
typeEleLi.appendChild(typeEleInput); typeEleLi.appendChild(typeEleInput);
const typeEleText = document.createTextNode(" " + element.name); const typeEleText = document.createTextNode(" " + element.name);
typeEleLi.appendChild(typeEleText); typeEleLi.appendChild(typeEleText);
@@ -158,6 +161,7 @@ SysTrayX.Accounts = {
treeBase.appendChild(typeLi); treeBase.appendChild(typeLi);
// Restore saved selection // Restore saved selection
function setAccounts(result) { function setAccounts(result) {
const treeBase = document.getElementById("accountsTree"); const treeBase = document.getElementById("accountsTree");
const accounts = result.accounts || []; const accounts = result.accounts || [];
@@ -174,9 +178,63 @@ SysTrayX.Accounts = {
function onError(error) { function onError(error) {
console.log(`GetAccounts Error: ${error}`); console.log(`GetAccounts Error: ${error}`);
} }
/*
const getAccounts = browser.storage.sync.get("accounts"); const getAccounts = browser.storage.sync.get("accounts");
getAccounts.then(setAccounts, onError); getAccounts.then(setAccounts, onError);
*/
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;
}
});
}
} }
/* /*