khoj/views/scripts/config.js

92 lines
3.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-28 01:17:15 +01:00
var configForm = document.getElementById("config-form");
var emptyValueDefault = "🖊️";
2021-11-28 01:17:15 +01:00
fetch("/config")
.then(response => response.json())
.then(data => {
rawConfig = data;
configForm.style.display = "block";
processChildren(configForm, data);
var submitButton = document.createElement("button");
submitButton.type = "submit";
submitButton.innerHTML = "update";
configForm.appendChild(submitButton);
configForm.addEventListener("submit", (event) => {
event.preventDefault();
console.log(rawConfig);
const response = fetch("/config", {
method: "POST",
credentials: "same-origin",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(rawConfig)
}).then(response => response.json())
.then((data) => console.log(data));
2021-11-27 16:50:05 +01:00
});
});
var regenerateButton = document.getElementById("config-regenerate");
regenerateButton.addEventListener("click", (event) => {
event.preventDefault();
regenerateButton.style.cursor = "progress";
regenerateButton.disabled = true;
fetch("/regenerate")
.then(response => response.json())
.then(data => () =>
{
regenerateButton.style.cursor = "pointer";
regenerateButton.disabled = false;
console.log(data);
});
})
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]) && !Array.isArray(data[key])) {
child.className+=" config-title";
processChildren(child, data[key]);
} else {
child.appendChild(createValueNode(data, key));
}
element.appendChild(child);
}
2021-11-28 00:00:47 +01:00
}
2021-11-28 01:17:15 +01:00
function makeElementEditable(original, data, key) {
original.addEventListener("click", () => {
2021-11-28 00:00:47 +01:00
var inputNewText = document.createElement("input");
inputNewText.type = "text";
2021-11-28 01:17:15 +01:00
inputNewText.className = "config-element-edit";
inputNewText.value = original.textContent;
fixInputOnFocusOut(inputNewText, data, key);
original.parentNode.replaceChild(inputNewText, original);
inputNewText.focus();
});
}
function createValueNode(data, key) {
var valueElement = document.createElement("span");
valueElement.className = "config-element-value";
valueElement.textContent = !data[key] ? emptyValueDefault : data[key];
makeElementEditable(valueElement, data, key);
return valueElement;
}
2021-11-28 01:17:15 +01:00
function fixInputOnFocusOut(original, data, key) {
original.addEventListener("blur", () => {
data[key] = (!!data[key] && original.value != emptyValueDefault) ? original.value : "";
original.parentNode.replaceChild(createValueNode(data, key), original);
2021-11-28 00:00:47 +01:00
})
2021-11-28 01:17:15 +01:00
}