spinn_mit/js/functions.js

619 lines
15 KiB
JavaScript
Raw Normal View History

2022-11-15 14:59:24 +01:00
//LocalLike Objekt erstellen
let localLikes = {}
2022-11-18 15:11:18 +01:00
UPLOADCARE_LOCALE_TRANSLATIONS = {
buttons: {
cancel: '',
remove: '',
choose: {
files: {
one: '',
other: ''
},
images: {
one: '',
other: ''
}
}
}
};
2022-11-16 10:32:30 +01:00
2022-11-14 16:27:53 +01:00
$(document).ready(function(){
2022-11-15 14:59:24 +01:00
2022-11-18 15:11:18 +01:00
window.scrollTo(0,1)
2022-11-15 14:59:24 +01:00
// submit kommentar nicht seite neu laden
2022-11-14 16:27:53 +01:00
$("#kommentarform").submit(function(e) {
console.log("Submitted")
2022-11-18 15:11:18 +01:00
2022-11-14 16:27:53 +01:00
e.preventDefault(); // avoid to execute the actual submit of the form.
});
2022-11-15 14:59:24 +01:00
2022-11-18 15:11:18 +01:00
if(!localStorage.getItem('firstVisit')){
console.log("loadStartscreen");
localStorage.setItem('firstVisit', 'false');
loadStartScreen();
} else {
console.log("not load Startscreen ");
$(".mapControls").removeClass("hideElement")
$("#logo").removeClass("hideElement")
2022-11-15 14:59:24 +01:00
2022-11-18 15:11:18 +01:00
}
2022-11-15 14:59:24 +01:00
//LocalStorage erstellen oder auslesen
if(localStorage.getItem('localLikes')){
//likes auslesen
2022-11-15 15:11:14 +01:00
2022-11-15 14:59:24 +01:00
localLikes = JSON.parse(localStorage.getItem('localLikes'))
console.log("Stored local Likes:")
console.log(localLikes)
2022-11-15 15:11:14 +01:00
2022-11-15 14:59:24 +01:00
}else{
2022-11-15 15:11:14 +01:00
//LocalLikes leer
2022-11-15 14:59:24 +01:00
localLikes = {
ideen: [],
kommentare: []
}
console.log("New Local Likes created")
console.log(localLikes)
2022-11-15 15:11:14 +01:00
2022-11-15 14:59:24 +01:00
localStorage.setItem('localLikes',JSON.stringify(localLikes));
}
2022-11-15 16:37:33 +01:00
2022-11-16 10:56:02 +01:00
2022-11-15 16:37:33 +01:00
2022-11-14 16:27:53 +01:00
})
2022-11-15 14:59:24 +01:00
2022-11-18 15:11:18 +01:00
function loadStartScreen(){
$("#startScreen").removeClass("hideElement")
$('#startScreen').click(() => {
$(".mapControls").removeClass("hideElement")
$("#logo").removeClass("hideElement")
$("#startScreen").addClass("hideElement")
})
setTimeout(() => {
$(".mapControls").removeClass("hideElement")
$("#logo").removeClass("hideElement")
$("#startScreen").addClass("hideElement")
}, 10000);
//onclick
//$(".mapControls").removeClass("hideElement")
//$("#logo").removeClass("hideElement")
}
2022-11-15 16:37:33 +01:00
function openOverlay(){
$("#overlay").removeClass("hidden").addClass("show")
2022-11-18 15:11:18 +01:00
$("#currentLocation").addClass("hideButton")
2022-11-15 16:37:33 +01:00
$("#cancel").click(function() {
$("#overlay").removeClass("show").addClass("hidden")
2022-11-18 15:11:18 +01:00
$("#currentLocation").removeClass("hideButton")
setTimeout(() => {
document.getElementById("overlay_container").innerHTML = "";
}, 300);
2022-11-16 10:56:02 +01:00
2022-11-15 16:37:33 +01:00
})
}
2022-11-15 14:59:24 +01:00
//LocalStorage Anpassen (Likes hinzufügen oder entfernen)
function chageLocalStorrage(typ, id, addRemove){
2022-11-15 15:11:14 +01:00
2022-11-15 14:59:24 +01:00
if(typ =="idee"){
if(addRemove == "add" && !localLikes.ideen.includes(id)){
localLikes.ideen.push(id);
console.log("like added");
console.log(localLikes);
}else if(addRemove == "remove" && localLikes.ideen.includes(id)){
let index = localLikes.ideen.indexOf(id);
localLikes.ideen.splice(index, 1);
console.log("like removed");
console.log(localLikes);
}
}else if(typ == "kommentar"){
if(addRemove == "add"){
localLikes.kommentare.push(id);
}else if(addRemove == "remove"){
let index = localLikes.kommentare.indexOf(id);
localLikes.kommentare.splice(index, 1);
}
}
localStorage.setItem('localLikes',JSON.stringify(localLikes));
2022-11-15 15:11:14 +01:00
2022-11-15 14:59:24 +01:00
}
// IDEE ANZEIGEN
2022-11-14 16:27:53 +01:00
2022-11-15 13:18:14 +01:00
function ideeAnzeigen(data) {
2022-11-14 16:27:53 +01:00
2022-11-16 10:32:30 +01:00
document.getElementById("overlay_container").innerHTML = "";
2022-11-18 15:11:18 +01:00
let inhaltContainer = document.createElement("div");
inhaltContainer.setAttribute("id","inhaltContainer")
let likesErstellen = document.createElement("p");
let likes = document.createTextNode("Gefällt " + data.likes + " Mal");
likesErstellen.appendChild(likes);
likesErstellen.classList.add("likeDisplay");
let titelErstellen = document.createElement("h1");
let titelInhalt = document.createTextNode(data.titel);
titelErstellen.appendChild(titelInhalt);
2022-11-18 15:11:18 +01:00
titelErstellen.classList.add("ideeTitel");
let inhaltErstellen = document.createElement("p");
let inhaltInhalt = document.createTextNode(data.inhalt);
inhaltErstellen.appendChild(inhaltInhalt);
2022-11-18 15:11:18 +01:00
inhaltErstellen.classList.add("ideeInhalt");
2022-11-18 15:11:18 +01:00
if(data.bild != null && data.bild != ""){
let bildContainer = $("<div>").addClass("ImgContainer");
let imgUrlArr = JSON.parse(data.bild);
for (let i = 0; i < imgUrlArr.length; i++) {
2022-11-18 15:11:18 +01:00
//let img = $("<img>").addClass("previewImage");
//img.attr("src", imgUrlArr[i]);
//img.appendTo(bildContainer);
bildContainer.css("background-image","url(" + imgUrlArr[i] + ")")
2022-11-16 10:56:02 +01:00
}
2022-11-16 10:56:02 +01:00
2022-11-16 10:32:30 +01:00
bildContainer.prependTo($("#overlay_container"))
2022-11-18 15:11:18 +01:00
} else {
inhaltContainer.classList.add("class","topMargin")
}
2022-11-16 10:56:02 +01:00
/*
let bild = document.createElement("img");
bild.src = data.bild;
bild.classList.add("ideeBilder");
bild.classList.add("loeschen");
document.getElementById('idee').appendChild(bild);
console.log(data);
*/
2022-11-15 14:59:24 +01:00
let ideeLike = document.createElement("img");
ideeLike.classList.add("herz");
2022-11-14 16:27:53 +01:00
2022-11-15 14:59:24 +01:00
//check if user has liked Idea before,load proper image and set class
2022-11-15 14:59:24 +01:00
if(localLikes.ideen.includes(data.id)){
2022-11-18 15:11:18 +01:00
ideeLike.src= "../images/herz-1.svg";
2022-11-15 14:59:24 +01:00
ideeLike.classList.add("liked");
}else{
2022-11-18 15:11:18 +01:00
ideeLike.src= "../images/herz-0.svg";
2022-11-15 14:59:24 +01:00
ideeLike.classList.add("unliked");
}
2022-11-18 15:11:18 +01:00
document.getElementById('overlay_container').appendChild(inhaltContainer);
document.getElementById('inhaltContainer').appendChild(likesErstellen);
document.getElementById('inhaltContainer').appendChild(ideeLike);
document.getElementById('inhaltContainer').appendChild(titelErstellen);
document.getElementById('inhaltContainer').appendChild(inhaltErstellen);
2022-11-14 16:27:53 +01:00
2022-11-15 14:59:24 +01:00
ideeLike.addEventListener('click', function() {
if(localLikes.ideen.includes(data.id)){
2022-11-18 15:11:18 +01:00
ideeLike.src= "../images/herz-0.svg";
2022-11-15 14:59:24 +01:00
ideeLike.classList.add("unliked");
ideeLike.classList.remove("liked");
removeLike("idee", data.id)
}else{
2022-11-15 15:11:14 +01:00
2022-11-18 15:11:18 +01:00
ideeLike.src= "../images/herz-1.svg";
2022-11-15 14:59:24 +01:00
ideeLike.classList.add("liked");
ideeLike.classList.remove("unliked");
addLike("idee", data.id)
}
2022-11-15 14:59:24 +01:00
});
2022-11-14 16:27:53 +01:00
kommentarErstellen(data.id);
let kommentare_container = document.createElement("div")
kommentare_container.setAttribute("id", "kommentare_container");
2022-11-18 15:11:18 +01:00
document.getElementById('inhaltContainer').appendChild(kommentare_container);
2022-11-14 16:27:53 +01:00
kommentareAnzeigen(data.id);
};
2022-11-15 14:59:24 +01:00
//Like Hinzufügen
function addLike(typ, id, element){
if(typ == "idee"){
//increment likes in DB
$.post('system/addLike.php/?typ=idee&id=' + id, {}).done(function(response){
2022-11-15 15:11:14 +01:00
2022-11-15 14:59:24 +01:00
//neue Likes anzeigen
2022-11-15 15:11:14 +01:00
2022-11-18 15:11:18 +01:00
$("p.likeDisplay").html("Gefällt " + response + " Mal")
2022-11-15 15:11:14 +01:00
2022-11-15 14:59:24 +01:00
});
//add like in LocalStorage
chageLocalStorrage("idee", id, "add")
2022-11-15 15:11:14 +01:00
2022-11-15 14:59:24 +01:00
}else if(typ == "kommentar"){
$.post('system/addLike.php/?typ=kommentar&id=' + id, {}).done(function(response){
2022-11-15 15:11:14 +01:00
2022-11-18 15:11:18 +01:00
element.parent().prev().children().first().html("Gefällt " + response + " Mal")
2022-11-15 15:11:14 +01:00
2022-11-15 14:59:24 +01:00
});
//add like in LocalStorage
chageLocalStorrage("kommentar", id, "add")
2022-11-15 15:11:14 +01:00
}
2022-11-15 14:59:24 +01:00
}
function removeLike(typ, id, element){
//decrement likes in DB
if(typ == "idee"){
$.post('system/removeLike.php/?typ=idee&id=' + id, {}).done(function(response){
2022-11-18 15:11:18 +01:00
$("p.likeDisplay").html("Gefällt " + response + " Mal")
2022-11-15 15:11:14 +01:00
2022-11-15 14:59:24 +01:00
});
chageLocalStorrage("idee", id, "remove")
}else if(typ == "kommentar"){
$.post('system/removeLike.php/?typ=kommentar&id=' + id, {}).done(function(response){
2022-11-15 15:11:14 +01:00
2022-11-18 15:11:18 +01:00
element.parent().prev().children().first().html("Gefällt " + response + " Mal")
2022-11-15 15:11:14 +01:00
2022-11-15 14:59:24 +01:00
});
chageLocalStorrage("kommentar", id, "remove")
2022-11-15 15:11:14 +01:00
}
2022-11-15 14:59:24 +01:00
}
2022-11-14 16:27:53 +01:00
// kommentar formular likesErstellen
function kommentarErstellen(idee_id) {
console.log(idee_id);
let formular = document.createElement("form");
2022-11-18 15:11:18 +01:00
formular.classList.add("kommentarFormular");
2022-11-15 14:59:24 +01:00
formular.setAttribute("action", "../system/kommentarSpeichern.php");
2022-11-14 16:27:53 +01:00
formular.setAttribute("method", "post");
formular.setAttribute("id", "kommentarform");
2022-11-18 15:11:18 +01:00
document.getElementById("inhaltContainer").appendChild(formular);
2022-11-14 16:27:53 +01:00
2022-11-18 15:11:18 +01:00
let text_input = document.createElement("textarea");
text_input.classList.add("kommentarText");
text_input.setAttribute("rows", "3");
2022-11-14 16:27:53 +01:00
text_input.setAttribute("type", "text");
2022-11-18 15:11:18 +01:00
text_input.setAttribute("placeholder", "Schreibe einen Kommentar...");
text_input.setAttribute("name", "text");
2022-11-14 16:27:53 +01:00
let submit_input = document.createElement("input");
2022-11-18 15:11:18 +01:00
submit_input.classList.add("kommentarSubmit");
2022-11-14 16:27:53 +01:00
submit_input.setAttribute("type", "submit");
2022-11-18 15:11:18 +01:00
submit_input.setAttribute("value", "");
2022-11-14 16:27:53 +01:00
let id = document.createElement("input");
id.setAttribute("type", "hidden");
id.setAttribute("name", "idee_id")
id.value = idee_id;
2022-11-18 15:11:18 +01:00
2022-11-14 16:27:53 +01:00
document.getElementById("kommentarform").appendChild(text_input);
document.getElementById("kommentarform").appendChild(submit_input);
document.getElementById("kommentarform").appendChild(id);
2022-11-14 16:27:53 +01:00
$("#kommentarform").submit(function(e){
2022-11-18 15:11:18 +01:00
2022-11-14 16:27:53 +01:00
let form = $(this);
$.ajax({
type: "POST",
2022-11-15 14:59:24 +01:00
url: "/system/kommentarSpeichern.php",
2022-11-14 16:27:53 +01:00
data: form.serialize(),
})
2022-11-18 15:11:18 +01:00
2022-11-14 16:27:53 +01:00
e.preventDefault();
2022-11-18 15:11:18 +01:00
$("textarea.kommentarText").val("")
2022-11-14 16:27:53 +01:00
document.getElementById("kommentare_container").innerHTML = "";
kommentareAnzeigen(idee_id);
});
};
2022-11-15 14:59:24 +01:00
2022-11-14 16:27:53 +01:00
// Idee formular erstellen
function formularErstellen(koordianten) {
2022-11-16 10:32:30 +01:00
document.getElementById("overlay_container").innerHTML = "";
2022-11-14 16:27:53 +01:00
let formular = document.createElement("form");
formular.classList.add("formular");
2022-11-15 14:59:24 +01:00
formular.setAttribute("action", "../system/ideeSpeichern.php");
2022-11-14 16:27:53 +01:00
formular.setAttribute("method", "post");
formular.setAttribute("id", "form");
2022-11-16 10:32:30 +01:00
document.getElementById("overlay_container").appendChild(formular);
2022-11-14 16:27:53 +01:00
let koordinaten_lng = document.createElement("input");
koordinaten_lng.setAttribute("type", "hidden");
koordinaten_lng.setAttribute("name", "lng")
koordinaten_lng.value = koordianten.toJSON().lng;
let koordinaten_lat = document.createElement("input");
koordinaten_lat.setAttribute("type", "hidden");
koordinaten_lat.setAttribute("name", "lat")
koordinaten_lat.value = koordianten.toJSON().lat;
2022-11-16 10:32:30 +01:00
2022-11-14 16:27:53 +01:00
let titel_input = document.createElement("input");
titel_input.setAttribute("type", "text");
2022-11-18 15:11:18 +01:00
titel_input.classList.add("titel");
2022-11-16 10:32:30 +01:00
titel_input.setAttribute("name", "titel");
2022-11-18 15:11:18 +01:00
titel_input.setAttribute("placeholder", "TITEL");
2022-11-16 10:56:02 +01:00
2022-11-18 15:11:18 +01:00
2022-11-14 16:27:53 +01:00
2022-11-18 15:11:18 +01:00
let text_input = $("<textarea>");
text_input.attr("rows", "10");
text_input.attr("name", "text");
text_input.attr("placeholder", "Deine Idee...");
2022-11-14 16:27:53 +01:00
let image_links = document.createElement("input");
image_links.setAttribute("type", "hidden");
image_links.setAttribute("name", "bilder");
2022-11-18 15:11:18 +01:00
let submit_input = document.createElement("button");
2022-11-14 16:27:53 +01:00
submit_input.setAttribute("type", "submit");
2022-11-18 15:11:18 +01:00
submit_input.classList.add("submit");
let textAndImage = $("<div>").addClass("textAndImage")
2022-11-14 16:27:53 +01:00
2022-11-16 10:56:02 +01:00
document.getElementById("form").appendChild(titel_input);
2022-11-18 15:11:18 +01:00
textAndImage.appendTo("#form");
text_input.appendTo(textAndImage);
2022-11-14 16:27:53 +01:00
document.getElementById("form").appendChild(submit_input);
document.getElementById("form").appendChild(koordinaten_lat);
document.getElementById("form").appendChild(koordinaten_lng);
document.getElementById("form").appendChild(image_links);
2022-11-15 13:18:14 +01:00
2022-11-16 10:32:30 +01:00
let imageUpload = $('<div class="image-preview" id="image-upload">')
2022-11-18 15:11:18 +01:00
imageUpload.html('<input type="hidden" role="uploadcare-uploader" data-public-key="3a3c37e85850c6cfe077" data-multiple="false" data-images-only data-public- data-clearable data-tabs="file camera" data-image-shrink: "1024x1024" data-system-dialog="true"/>')
imageUpload.appendTo(textAndImage);
2022-11-16 10:32:30 +01:00
2022-11-16 10:56:02 +01:00
2022-11-15 13:18:14 +01:00
// get a widget reference
2022-11-16 10:56:02 +01:00
const widget = uploadcare.Widget("[role=uploadcare-uploader]", { multiple: false});
2022-11-15 13:18:14 +01:00
// listen to the "upload completed" event
2022-11-16 10:56:02 +01:00
widget.onUploadComplete(fileInfo => {
2022-11-15 13:18:14 +01:00
// get a information about uploaded group
// check https://uploadcare.com/docs/file-uploader-api/file-groups/#file-group-info
let linkArr = [];
let ImgUrl;
$("<div>").addClass("ImgPrevContainer").appendTo($("div.image-preview"));
2022-11-16 10:56:02 +01:00
ImgUrl = "https://ucarecdn.com/" + fileInfo.uuid + "/-/preview/-/quality/smart/-/format/auto/";
console.log(fileInfo);
linkArr.push(ImgUrl);
2022-11-16 10:56:02 +01:00
createImgPrev(ImgUrl + "-/resize/x100/"); //img Preview 100 pixels high
let linkArrString = JSON.stringify(linkArr);
console.log(linkArrString);
image_links.value = linkArrString;
console.log(image_links.value);
2022-11-18 15:11:18 +01:00
$(".uploadcare--widget__button_type_remove").click(() => {
console.log("delete");
$("div.ImgPrevContainer").remove()
image_links.value = "";
})
2022-11-15 13:18:14 +01:00
});
2022-11-18 15:11:18 +01:00
console.log($(".uploadcare--widget__button_type_open needsclick"));
$(".uploadcare--widget__button_type_open needsclick").addClass("test")
2022-11-14 16:27:53 +01:00
};
2022-11-18 15:11:18 +01:00
function createImgPrev(imgURL){
2022-11-18 15:11:18 +01:00
//let prevImg = $("<img>").addClass("prevIMG");
//prevImg.attr('src', imgURL);
//prevImg.appendTo($("div.ImgPrevContainer"));
$("div.ImgPrevContainer").css('background-image', 'url(' + imgURL + ')');
}
2022-11-14 16:27:53 +01:00
function kommentareAnzeigen(idee_id){
2022-11-15 14:59:24 +01:00
$.post("./system/alleKommentareHolen.php/?id=" + idee_id, {}).done(function(comments){
2022-11-15 15:11:14 +01:00
2022-11-18 15:11:18 +01:00
2022-11-15 14:59:24 +01:00
2022-11-15 15:11:14 +01:00
2022-11-15 14:59:24 +01:00
let commentArray = JSON.parse(comments);
2022-11-15 15:11:14 +01:00
2022-11-15 14:59:24 +01:00
for (var i = 0; i < commentArray.length; i++) {
let thisComment = commentArray[i];
let kommentarContainer = $("<div>").addClass("comment");
2022-11-18 15:11:18 +01:00
let kommentarLinks = $("<div>").addClass("commentLeft")
let kommentarRechts = $("<div>").addClass("commentRight")
2022-11-15 14:59:24 +01:00
2022-11-18 15:11:18 +01:00
let kommentarLikeNumber = $("<p>").text("Gefällt " + thisComment.k_likes + " Mal").addClass("commentLikeNumber");
kommentarLikeNumber.appendTo(kommentarLinks);
2022-11-15 14:59:24 +01:00
let kommentarText = $("<p>").addClass("commentText");
kommentarText.text(thisComment.k_text);
2022-11-18 15:11:18 +01:00
kommentarText.appendTo(kommentarLinks);
2022-11-15 14:59:24 +01:00
let kommentarLikeButton = $("<img>").addClass("unliked commentLike")
2022-11-15 15:11:14 +01:00
2022-11-15 14:59:24 +01:00
if(localLikes.kommentare.includes(thisComment.k_id)){
2022-11-18 15:11:18 +01:00
kommentarLikeButton.attr("src", "../images/herz-1.svg");
2022-11-15 14:59:24 +01:00
kommentarLikeButton.addClass("liked");
}else{
2022-11-18 15:11:18 +01:00
kommentarLikeButton.attr("src", "../images/herz-0.svg");
2022-11-15 14:59:24 +01:00
kommentarLikeButton.addClass("unliked");
}
2022-11-18 15:11:18 +01:00
kommentarLikeButton.appendTo(kommentarRechts);
kommentarLinks.appendTo(kommentarContainer)
kommentarRechts.appendTo(kommentarContainer)
2022-11-15 14:59:24 +01:00
kommentarContainer.appendTo($("#kommentare_container"));
kommentarLikeButton.click( function() {
if(localLikes.kommentare.includes(thisComment.k_id)){
2022-11-18 15:11:18 +01:00
kommentarLikeButton.attr("src", "../images/herz-0.svg");
2022-11-15 14:59:24 +01:00
kommentarLikeButton.addClass("unliked");
kommentarLikeButton.removeClass("liked");
removeLike("kommentar", thisComment.k_id, $(this))
}else{
2022-11-15 15:11:14 +01:00
2022-11-18 15:11:18 +01:00
kommentarLikeButton.attr("src", "../images/herz-1.svg");
2022-11-15 14:59:24 +01:00
kommentarLikeButton.addClass("liked");
kommentarLikeButton.removeClass("unliked");
addLike("kommentar", thisComment.k_id, $(this))
2022-11-15 15:11:14 +01:00
2022-11-14 16:27:53 +01:00
}
2022-11-15 15:11:14 +01:00
2022-11-15 14:59:24 +01:00
});
2022-11-15 15:11:14 +01:00
2022-11-15 14:59:24 +01:00
2022-11-16 10:56:02 +01:00
2022-11-15 14:59:24 +01:00
}
2022-11-15 15:11:14 +01:00
2022-11-15 14:59:24 +01:00
});
}