bunteshaus.de/themes/buha/static/js/generator.js
2022-06-24 22:24:02 +02:00

313 lines
7.6 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 clearThis(target) {
var id = document.getElementById(target);
id.value = "";
}
function Input(str) {
var arr = str.split(";");
let x = arr[1];
let target = arr[0];
var id = document.getElementById(target);
var value = id.value;
function typeInInputfield(newText, el = document.getElementById(target)) {
const [start, end] = [el.selectionStart, el.selectionEnd];
el.setRangeText(newText, start, end);
}
var out = x;
if ( value ) {
var out = "," + x;
}
if (out != null) {
typeInInputfield(out);
var end = id.value.length;
id.setSelectionRange(end, end);
id.focus({preventScroll: true});
}
}
function Clear(name)
{
clearRadioGroup(name);
}
function clearRadioGroup(name)
{
var ele = document.getElementsByName(name);
for(var i=0;i<ele.length;i++)
ele[i].checked = false;
}
function button(str) {
var arr = str.split(";");
let x = arr[1];
let target = arr[0];
var id = document.getElementById(target);
function typeInTextarea(newText, el = document.getElementById(target)) {
const [start, end] = [el.selectionStart, el.selectionEnd];
el.setRangeText(newText, start, end);
}
if (x == "url") {
let url = prompt("Please enter URL", "https://wikipedia.org");
if (url != null) {
let desc = prompt("Description", "The free Encyclopedia");
if (desc != null) {var out = "[" + desc + "](" + url + ") ";}
}
}
if (x == "headline") {
let headline = prompt("Headline", "Act One");
if (headline != null) {var out = "## " + headline + "\n\n";}
}
if (x == "pic") {
let desc = prompt("Description", "Wikipedia logo");
if (desc != null) {
let pic = prompt("Please enter URL or Number", "https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png");
if (pic != null) {var out = "![" + desc + "](" + pic + ") ";}
}
}
if (x == "bold") {
let bold = prompt("Bold", "");
if (bold != null) {var out = "\*\*" + bold + "\*\* ";}
}
if (x == "strike") {
let strike = prompt("Strike", "");
if (strike != null) {var out = "\~\~" + strike + "\~\~ ";}
}
if (x == "paragraph") {
var out = "\n\n";
}
if (x == "newline") {
var out = " \n";
}
if (x == "more") {
var out = "\n\n<!--more-->\n\n";
}
if (out != null) {
typeInTextarea(out);
var end = id.value.length;
id.setSelectionRange(end, end);
id.focus({preventScroll: true});
}
}
function testor(input) {
let v1 = [];
input.forEach((input) => {
v1.push("\"" + input.trim() + "\"");
});
if (v1) {
return v1;
} else {
return null;
}
}
function showInput(e) {
document.getElementById("result").innerHTML = '';
e.preventDefault();
document.getElementById("result").style.display = "flex";
var display = document.getElementById("result");
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="generatorform"]');
var title = form.elements['title'].value;
var summary = form.elements['summary'].value;
var content = form.elements['content'].value;
var when = form.elements['when'].value;
var covid = form.elements['covid'].value;
var price = form.elements['price'].value;
var contact_mail = form.elements['contact_mail'].value;
var insta = form.elements['insta'].value;
var events = check_checkboxes('events');
if (form.elements['events']) { var newevents = testor(form.elements['events'].value.split(',')); }
var news = check_checkboxes('news');
if (form.elements['news']) { var newnews = testor(form.elements['news'].value.split(',')); }
var tags = check_checkboxes('tags');
if (form.elements['tags']) { var newtags = testor(form.elements['tags'].value.split(',')); }
var featured = check_checkboxes('featured');
var about = check_checkboxes('about');
var recurrent = check_checkboxes('recurrent');
var foundations = check_checkboxes('foundations');
if (form.elements['foundations']) { var newfoundations = testor(form.elements['foundations'].value.split(',')); }
var lastmod = form.elements['lastmod'].value;
if (lastmod) {
var dl = new Date(lastmod);
var lastmod = dl.getFullYear() + "-" + ("0"+(dl.getMonth()+1)).slice(-2) + "-" + ("0" + dl.getDate()).slice(-2) + "T" + "14:" + "00:00+02:00";
}
if (newtags != '""') {
var ntags = newtags;
}
if (tags != '') {
var ntags = tags;
}
if (tags != '' && newtags != '""') {
var ntags = tags + "," + newtags;
}
if (newnews != '""') {
var nnews = newnews;
}
if (news != '' ) {
var nnews = news;
}
if (news != '' && newnews != '""') {
var nnews = news + "," + newnews;
}
if (newevents != '""') {
var nevents = newevents;
}
if (events != '') {
var nevents = events;
}
if (events != '' && newevents != '""') {
var nevents = events + "," + newevents;
}
if (nnews) {
if (alltags) {
var alltags = alltags + "," + nnews;
} else {
var alltags = nnews;
}
}
if (nevents) {
if (alltags) {
var alltags = alltags + "," + nevents;
} else {
var alltags = nevents;
}
}
if (ntags) {
if (alltags) {
var alltags = alltags + "," + ntags;
} else {
var alltags = ntags;
}
}
if (newfoundations != '""') {
var nfoundations = newfoundations;
}
if (foundations != '') {
var nfoundations = foundations;
}
if (foundations != '' && newfoundations != '""') {
var nfoundations = foundations + "," + newfoundations;
}
if (!title) {
display.innerHTML += "Title is missing..<br>"
}
if (title) {
display.innerHTML += "---<br>";
display.innerHTML += "title: \"" + title.trim() + "\"<br>";
if (lastmod) {
display.innerHTML += "lastmod: " + lastmod.trim() + "<br>";
} else {
display.innerHTML += "date: " + datestring.trim() + "<br>";
}
if (featured != '') {
display.innerHTML += "featured: true<br>";
}
if (about != '') {
display.innerHTML += "about: true<br>";
}
if (recurrent != '') {
display.innerHTML += "recurrent: true<br>";
}
if (when) {
display.innerHTML += "when: \"" + when + "\"<br>";
}
if (nnews) {
display.innerHTML += "news: \[" + nnews + "]<br>";
}
if (nevents) {
display.innerHTML += "events: \[" + nevents + "]<br>";
}
if (nfoundations) {
display.innerHTML += "foundations: \[" + nfoundations + "]<br>";
}
if (alltags) {
display.innerHTML += "tags: \[" + alltags + "]<br>";
}
if (covid) {
display.innerHTML += "covid: \"" + covid + "\"<br>";
}
if (price) {
display.innerHTML += "price: \"" + price + "\"<br>";
}
if (contact_mail) {
display.innerHTML += "contact_mail: \"" + contact_mail + "\"<br>";
}
if (insta) {
display.innerHTML += "insta: \"" + insta + "\"<br>";
}
display.innerHTML += "---<br>";
if (summary) {
display.innerHTML += summary.trim() + "\n" + "&lt;!--more--&gt;" + "\n";
}
if (content) {
display.innerHTML += content.trim();
}
}
}