Archived
1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
SOD/public/javascripts/index.js

53 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2020-09-27 20:43:11 +02:00
const orders = document.getElementById("orders");
const sandwich = document.getElementById("sandwich");
const day = document.getElementById("day");
2020-08-18 16:48:09 +02:00
const locals = {
2020-09-27 20:43:11 +02:00
sandwich: document.querySelector("#oderCreator label").innerHTML,
2020-09-28 15:10:36 +02:00
day: document.querySelector("label[for=day]").innerText
2020-08-18 16:48:09 +02:00
};
2020-08-18 17:54:00 +02:00
function lastOrderId() {
2020-09-27 20:43:11 +02:00
if (orders.lastChild)
return parseInt(orders.lastChild.id.replace("order", ""));
return 0;
}
2020-09-27 20:43:11 +02:00
document.getElementById("addOrder").addEventListener("click", () => {
if (!sandwich.value || !day.value)
return;
2020-08-18 17:54:00 +02:00
let id = lastOrderId() + 1;
2020-09-27 20:43:11 +02:00
orders.insertAdjacentHTML("beforeend", `<div id="order${id}" class="row">
<div class="input-field col s6">
<input id="sandwich${id}" type="text" name="sandwiches[${id}]" value="${sandwich.value}" readonly required>
2020-08-18 16:48:09 +02:00
<label for="sandwich${id}">${locals.sandwich}</label>
</div>
2020-09-27 20:43:11 +02:00
<div class="input-field col s6">
<input id="date${id}" type="date" name="dates[${id}]" value="${day.value}" readonly required>
<label for="date${id}">${locals.day}</label>
</div>
2020-09-27 20:43:11 +02:00
<a class="btn-floating btn-large waves-effect waves-light red"><i class="material-icons">remove</i></a>
</div>`);
2020-09-27 20:43:11 +02:00
2020-09-30 11:39:46 +02:00
M.updateTextFields();
2020-09-27 20:43:11 +02:00
sandwich.selectedIndex = 0;
day.value = "";
let order = document.getElementById("order"+id);
order.querySelector("a").addEventListener("click", () => {
order.remove();
if (lastOrderId() === 0) {
sandwich.required = true;
day.required = true;
}
});
document.getElementById("order"+id).scrollIntoView({behavior: "smooth"});
});
2020-09-27 20:43:11 +02:00
document.querySelector("form").addEventListener("submit", () => {
return lastOrderId();
});