khoj/views/scripts/config.js

60 lines
2.1 KiB
JavaScript
Raw Normal View History

2021-11-27 16:50:05 +01:00
var showConfig = document.getElementById("show-config");
2021-11-28 00:00:47 +01:00
var rawConfig = {};
2021-11-27 16:50:05 +01:00
showConfig.addEventListener("click", () => {
var configForm = document.getElementById("config-form");
2021-11-27 16:50:05 +01:00
fetch("/config")
.then(response => response.json())
2021-11-28 00:00:47 +01:00
.then(data => {
rawConfig = data;
configForm.style.display = "block";
processChildren(configForm, data);
2021-11-27 16:50:05 +01:00
});
});
function processChildren(element, data) {
for (let key in data) {
var child = document.createElement("div");
child.id = key;
2021-11-27 20:39:05 +01:00
child.className = "config-element";
child.appendChild(document.createTextNode(key + ": "));
if (data[key] === Object(data[key])) {
processChildren(child, data[key]);
} else {
2021-11-28 00:00:47 +01:00
var value = document.createElement("span");
value.id = key+"-value";
value.textContent = data[key];
createEditButton(value);
value.addEventListener("click", (event) => {
var inputNewText = document.createElement("input");
inputNewText.type = "text";
inputNewText.class = "config-element-edit";
inputNewText.id = key+"-value";
console.log(value.parentNode);
console.log(value);
child.replaceChild(inputNewText, value);
console.log(event);
});
child.appendChild(value);
}
element.appendChild(child);
}
2021-11-28 00:00:47 +01:00
}
function createEditButton(parent) {
var editButton = document.createElement("button");
editButton.type = "button";
editButton.className = "config-edit-button";
editButton.textContent = "🖊️";
editButton.id = "parentId-" + parent.id;
// console.log(parent);
editButton.addEventListener("click", (event) => {
var inputNewText = document.createElement("input");
inputNewText.type = "text";
inputNewText.class = "config-element-edit";
parent.parentNode.replaceChild(inputNewText, parent);
// console.log(event);
})
// console.log("edit button", editButton);
parent.appendChild(editButton);
}