bunteshaus.de/themes/buha/static/js/generator.js
2022-06-26 15:49:16 +02:00

409 lines
11 KiB
JavaScript

var zip = new JSZip();
var archive_name = "";
var fout = document.getElementById('added-files');
function isFileImage(file) {
return file && file['type'].split('/')[0] === 'image';
}
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 handleFile(f) {
var name = f.name;
var data = f.raw;
zip.file(name, data);
}
function Clear(name) {
var ele = document.getElementsByName(name);
for(var i=0;i<ele.length;i++)
ele[i].checked = false;
}
function rm_file(fname) {
var rmid = document.getElementById(fname);
rmid.remove(); // Removes the div with the 'div-02' id
zip.remove(fname);
}
function insert_file(fname) {
var arr = fname.split(";");
let t = arr[2];
let x = arr[1];
let target = arr[0];
if (!(x.startsWith("banner"))) {
var id = document.getElementById(target);
function typeInTextarea(newText, el = id) {
const [start, end] = [el.selectionStart, el.selectionEnd];
el.setRangeText(newText, start, end);
}
if (t == "image") {
var n = prompt("Name", "Wikipedia Logo");
var desc = prompt("Describe the Picture for the Blind", "A Globe composed of letters and puzzle pieces");
if (desc != null && n != null) {var out = "\n![" + n + "](" + x + " \"" + desc + "\"" + ")\n";}
} else if (t == "other") {
var n = prompt("Name", "Textfile");
var desc = prompt("Describe the File for the Blind", "A file with content related to the subject");
if (desc != null && n != null) {var out = "\n[" + n + "](" + x + " \"" + desc + "\"" + ")\n";}
}
if (out != null) {
typeInTextarea(out);
var end = id.value.length;
id.setSelectionRange(end, end);
id.focus({preventScroll: true});
}
}
}
function addfiles(str) {
var arr = str.split(";");
let x = arr[1];
let target = arr[0];
var input = document.createElement('input');
var type = "image";
input.type = 'file';
input.multiple = 'multiple';
input.id = 'inpic';
input.click();
input.onchange = e => {
var files = e.target.files;
for (var i = 0; i < files.length; i++) {
var name = files[i].name.trim();
if (isFileImage(files[i])) {
type = "image";
} else {
type = "other"
}
zip.file(name, files[i]);
fout.innerHTML += "<div id=\"" + name + "\" style=\"display: flex; flex-direction: row;\">" + "<button type=\"button\" onclick=\"insert_file('" + target + ";" + name + ";" + type + "')\" value=\"" + name + "\" id=\"" + name + "\" class=\"button\">Einfügen</button>" + "<button type=\"button\" onclick=\"rm_file('" + name + "')\" value=\"" + name + "\" id=\"" + name + "\" class=\"button\">Entfernen</button>" + name + "</div>";
}
}
}
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) {
var n = prompt("Name", "Wikipedia");
var desc = prompt("Describe the Link for the Blind", "The free Encyclopedia");
if (desc != null && n != null) {var out = "![" + n + "](" + url + " \"" + desc + "\"" + ") ";}
}
}
if (x == "picurl") {
let url = prompt("Please enter Link to an image", "https://de.wikipedia.org/static/images/project-logos/dewiki.png");
if (url != null) {
var n = prompt("Name", "Wikipedia Logo");
var desc = prompt("Describe the Picture for the Blind", "A Globe composed of letters and puzzle pieces"); if (desc != null && n != null) {var out = "\n![" + n + "](" + url + " \"" + desc + "\"" + ")\n";}
}
}
if (x == "headline") {
let headline = prompt("Headline", "Act One");
if (headline != null) {var out = "## " + headline + "\n\n";}
}
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 && x != "pic") {
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) {
var display = document.getElementById("result");
display.innerHTML = '';
e.preventDefault();
display.style.display = "flex";
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 datestring_shorter = d.getFullYear() + "-" + ("0"+(d.getMonth()+1)).slice(-2) + "-" + ("0" + d.getDate()).slice(-2);
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) {
var output = "---\n";
var output = output + "title: \"" + title.trim() + "\"\n";
if (lastmod) {
var output = output + "lastmod: " + lastmod.trim() + "\n";
} else {
var output = output + "date: " + datestring.trim() + "\n";
}
if (featured != '') {
var output = output + "featured: true\n";
}
if (about != '') {
var output = output + "about: true\n";
}
if (recurrent != '') {
var output = output + "recurrent: true\n";
}
if (when) {
var output = output + "when: \"" + when + "\"\n";
}
if (nnews) {
var output = output + "news: \[" + nnews + "]\n";
}
if (nevents) {
var output = output + "events: \[" + nevents + "]\n";
}
if (nfoundations) {
var output = output + "foundations: \[" + nfoundations + "]\n";
}
if (alltags) {
var output = output + "tags: \[" + alltags + "]\n";
}
if (covid) {
var output = output + "covid: \"" + covid + "\"\n";
}
if (price) {
var output = output + "price: \"" + price + "\"\n";
}
if (contact_mail) {
var output = output + "contact_mail: \"" + contact_mail + "\"\n";
}
if (insta) {
var output = output + "insta: \"" + insta + "\"\n";
}
var output = output + "---\n";
if (summary) {
var output = output + summary.trim() + "\n" + "<!--more-->" + "\n";
}
if (content) {
var output = output + content.trim();
}
// Add an top-level, arbitrary text file with contents
zip.file("index.de.md", output);
var pattern = /[^a-z\d\-_\s]+$/i;
var t = title.replace(/^["'](.+(?=["']$))["']$/, '$1').toLowerCase();
var t = t.trim();
var t = t.replaceAll(/\s+/g, '-');
var t = t.replace(/[^\w.-]+/g, "");
archive_name = datestring_shorter + "-" + t + ".zip";
var check_banner = false;
for(let [filename, file] of Object.entries(zip.files)) {
console.log(filename);
if (filename.startsWith("banner")) { check_banner = true; }
}
if (output) {
display.innerHTML += "<button style=\"background: #69c839;\" class=\"button\" onclick=\"dl_archive()\" type=\"button\" \">Save Archive</button><br>";
if (check_banner == false) {
display.innerHTML += "<br>\n<div>Banner (1920x850px) missing</div>"
}
if (!(summary)) {
display.innerHTML += "<br>\n<div>Summary missing</div>"
}
if (!(content)) {
display.innerHTML += "<br>\n<div>Content missing</div>"
}
}
}
}
function dl_archive() {
// Generate the zip file asynchronously
zip.generateAsync({type:"blob"})
.then(function(cont) {
// Force down of the Zip file
saveAs(cont, archive_name);
});
}