bunteshaus.de/themes/buha/static/js/generator.js
2022-03-27 20:40:45 +02:00

107 lines
2.5 KiB
JavaScript

function check_checkboxes(name) {
const checkboxes = document.querySelectorAll(`input[name="${name}"]:checked`);
let values = [];
checkboxes.forEach((checkbox) => {
values.push("\"" + checkbox.value + "\"");
});
if (values) {
return values;
} else {
return null;
}
}
function testor(input) {
let v1 = [];
input.forEach((input) => {
v1.push("\"" + input.trim() + "\"");
});
if (v1) {
return v1;
} else {
return null;
}
}
function showInput(e) {
document.getElementById("display").innerHTML = '';
e.preventDefault();
var output_info = document.getElementById("myform");
var display = document.getElementById("display");
var d = new Date();
var datestring = d.getFullYear() + "-" + ("0"+(d.getMonth()+1)).slice(-2) + "-" + ("0" + d.getDate()).slice(-2) + "T" + ("0" + d.getHours()).slice(-2) + ":" + ("0" + d.getMinutes()).slice(-2) + ":00+02:00\"";
var form = document.querySelector('form[id="myform"]');
var title = form.elements['title'].value;
var content = form.elements['content'].value;
var event = form.elements['event'].value;
var cats = check_checkboxes('category');
var newcat = testor(form.elements['newcat'].value.split(','));
var tags = check_checkboxes('tags');
var newtag = testor(form.elements['newtag'].value.split(','));
var featured = check_checkboxes('featured');
if (newcat != '""') {
var ncats = newcat;
}
if (cats != '') {
var ncats = cats;
}
if (cats != '' && newcat != '""') {
var ncats = cats + "," + newcat;
}
if (newtag != '""') {
var ntags = newtag;
}
if (tags != '') {
var ntags = tags;
}
if (tags != '' && newtag != '""') {
var ntags = tags + "," + newtag;
}
if (!title) {
display.innerHTML += "Title is missing..<br>"
}
if (!content) {
display.innerHTML += "Content is missing..<br>"
}
if (title && content) {
display.innerHTML += "---<br>";
display.innerHTML += "title: \"" + title + "\"<br>";
display.innerHTML += "date: \"" + datestring + "<br>";
if (featured != '') {
display.innerHTML += "featured: true<br>";
}
if (event) {
display.innerHTML += "when: \"" + event + "\"<br>";
}
if (ncats) {
display.innerHTML += "categories: \[" + ncats + "]<br>";
}
if (ntags) {
display.innerHTML += "tags: \[" + ntags + "]<br>";
}
display.innerHTML += "---<br>";
if (content) {
display.innerHTML += content;
}
}
}