sway-ipc/static/javascript/application.js

59 lines
1.4 KiB
JavaScript

$(function () {
$('[data-toggle="tooltip"]').tooltip()
})
async function sendMessage() {
const [form, label, valid] = await validate("form");
if (!valid) return
const response = await request("/outputs", "POST");
if (response.ok) {
form.reset();
label.then(resp => {
resp.textContent = "resolution set";
resp.classList.add("text-success");
document.getElementById("form").appendChild(resp);
})
window.location.reload(true);
return
}
label.then(resp => {
resp.textContent = "unable to send";
resp.classList.add("text-danger");
document.getElementById("form").appendChild(resp);
window.location.reload(true);
})
}
async function request(url, method) {
let response = await fetch(url, {
method: method,
body: new FormData(form)
});
return response
}
async function validate(id) {
var label = message();
var form = document.getElementById(id);
if (form.checkValidity() == false) {
form.reportValidity();
return [form, label, false]
}
return [form, label, true]
}
async function message() {
let label = document.createElement("span");
label.id = "label";
label.classList.add("p-2");
remove("label");
return label
}
async function remove(id) {
elem = document.getElementById(id);
if (elem != null) {
elem.parentNode.removeChild(elem);
}
}