spinn_mit/js/functions.js
2022-11-15 14:59:24 +01:00

470 lines
11 KiB
JavaScript

/* ideeSpeichern();
function ideeSpeichern(titel, inhalt) {
let url = "system/ideeSpeichern.php";
let formData = new FormData();
formData.append('titel', titel);
formData.append('inhalt', inhalt);
fetch(url,
{
body: formData,
method: "post",
})
.then((response) => {
return response.json();
})
.then((markerData) => {
}
.catch(function(error) {
console.log("error: " + error.message);
});
}
*/
//LocalLike Objekt erstellen
let localLikes = {}
$(document).ready(function(){
// submit kommentar nicht seite neu laden
$("#kommentarform").submit(function(e) {
console.log("Submitted")
e.preventDefault(); // avoid to execute the actual submit of the form.
});
//LocalStorage erstellen oder auslesen
if(localStorage.getItem('localLikes')){
//likes auslesen
localLikes = JSON.parse(localStorage.getItem('localLikes'))
console.log("Stored local Likes:")
console.log(localLikes)
}else{
//LocalLikes leer
localLikes = {
ideen: [],
kommentare: []
}
console.log("New Local Likes created")
console.log(localLikes)
localStorage.setItem('localLikes',JSON.stringify(localLikes));
}
})
//LocalStorage Anpassen (Likes hinzufügen oder entfernen)
function chageLocalStorrage(typ, id, addRemove){
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));
}
// IDEE ANZEIGEN
function ideeAnzeigen(data) {
document.getElementById("idee").innerHTML = "";
document.querySelectorAll('.loeschen').forEach(e => e.remove());
let titelErstellen = document.createElement("h1");
let titelInhalt = document.createTextNode(data.titel);
titelErstellen.appendChild(titelInhalt);
titelErstellen.classList.add("loeschen");
document.getElementById('idee').appendChild(titelErstellen);
let inhaltErstellen = document.createElement("p");
let inhaltInhalt = document.createTextNode(data.inhalt);
inhaltErstellen.appendChild(inhaltInhalt);
inhaltErstellen.classList.add("loeschen");
document.getElementById('idee').appendChild(inhaltErstellen);
let likesErstellen = document.createElement("p");
let likes = document.createTextNode(data.likes + " Likes");
likesErstellen.appendChild(likes);
likesErstellen.classList.add("likeDisplay");
document.getElementById('idee').appendChild(likesErstellen);
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);
let ideeLike = document.createElement("img");
ideeLike.classList.add("herz");
//check if user has liked Idea before,load proper image and set class
if(localLikes.ideen.includes(data.id)){
ideeLike.src= "../images/herz-1.png";
ideeLike.classList.add("liked");
}else{
ideeLike.src= "../images/herz-0.png";
ideeLike.classList.add("unliked");
}
document.getElementById('idee').appendChild(ideeLike);
ideeLike.addEventListener('click', function() {
if(localLikes.ideen.includes(data.id)){
ideeLike.src= "../images/herz-0.png";
ideeLike.classList.add("unliked");
ideeLike.classList.remove("liked");
removeLike("idee", data.id)
}else{
ideeLike.src= "../images/herz-1.png";
ideeLike.classList.add("liked");
ideeLike.classList.remove("unliked");
addLike("idee", data.id)
}
});
kommentarErstellen(data.id);
let kommentare_container = document.createElement("div")
kommentare_container.setAttribute("id", "kommentare_container");
document.getElementById('idee').appendChild(kommentare_container);
kommentareAnzeigen(data.id);
};
//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){
//neue Likes anzeigen
$("p.likeDisplay").html(response + " Likes")
});
//add like in LocalStorage
chageLocalStorrage("idee", id, "add")
}else if(typ == "kommentar"){
$.post('system/addLike.php/?typ=kommentar&id=' + id, {}).done(function(response){
element.prev().html(response + " Likes")
});
//add like in LocalStorage
chageLocalStorrage("kommentar", id, "add")
}
}
function removeLike(typ, id, element){
//decrement likes in DB
if(typ == "idee"){
$.post('system/removeLike.php/?typ=idee&id=' + id, {}).done(function(response){
$("p.likeDisplay").html(response + " Likes")
});
chageLocalStorrage("idee", id, "remove")
}else if(typ == "kommentar"){
$.post('system/removeLike.php/?typ=kommentar&id=' + id, {}).done(function(response){
element.prev().html(response + " Likes")
});
chageLocalStorrage("kommentar", id, "remove")
}
}
// kommentar formular likesErstellen
function kommentarErstellen(idee_id) {
console.log(idee_id);
let formular = document.createElement("form");
formular.classList.add("formular");
formular.setAttribute("action", "../system/kommentarSpeichern.php");
formular.setAttribute("method", "post");
formular.setAttribute("id", "kommentarform");
document.getElementById("idee").appendChild(formular);
let text_label = document.createElement("label");
text_label.setAttribute("for", "text");
let text_input = document.createElement("input");
text_input.setAttribute("type", "text");
text_input.setAttribute("name", "text")
let submit_input = document.createElement("input");
submit_input.setAttribute("type", "submit");
submit_input.setAttribute("value", "Kommentar posten");
let id = document.createElement("input");
id.setAttribute("type", "hidden");
id.setAttribute("name", "idee_id")
id.value = idee_id;
document.getElementById("kommentarform").appendChild(text_label);
document.getElementById("kommentarform").appendChild(text_input);
document.getElementById("kommentarform").appendChild(submit_input);
document.getElementById("kommentarform").appendChild(id);
$("#kommentarform").submit(function(e){
let form = $(this);
$.ajax({
type: "POST",
url: "/system/kommentarSpeichern.php",
data: form.serialize(),
})
e.preventDefault();
document.getElementById("kommentare_container").innerHTML = "";
kommentareAnzeigen(idee_id);
});
};
// Idee formular erstellen
function formularErstellen(koordianten) {
document.getElementById("idee").innerHTML = "";
let formular = document.createElement("form");
formular.classList.add("formular");
formular.setAttribute("action", "../system/ideeSpeichern.php");
formular.setAttribute("method", "post");
formular.setAttribute("id", "form");
document.getElementById("idee").appendChild(formular);
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;
let titel_label = document.createElement("label");
titel_label.setAttribute("for", "titel");
titel_label.innerHTML = "Titel:";
let titel_input = document.createElement("input");
titel_input.setAttribute("type", "text");
titel_input.setAttribute("name", "titel")
let text_label = document.createElement("label");
text_label.setAttribute("for", "text");
text_label.innerHTML = "Text:";
let text_input = document.createElement("input");
text_input.setAttribute("type", "text");
text_input.setAttribute("name", "text")
let submit_input = document.createElement("input");
submit_input.setAttribute("type", "submit");
submit_input.setAttribute("value", "Formular Absenden");
document.getElementById("form").appendChild(titel_label);
document.getElementById("form").appendChild(titel_input);
document.getElementById("form").appendChild(text_label);
document.getElementById("form").appendChild(text_input);
document.getElementById("form").appendChild(submit_input);
document.getElementById("form").appendChild(koordinaten_lat);
document.getElementById("form").appendChild(koordinaten_lng);
};
function kommentareAnzeigen(idee_id){
$.post("./system/alleKommentareHolen.php/?id=" + idee_id, {}).done(function(comments){
let k = document.createElement("h3");
let k_text = document.createTextNode("Kommentare:");
k.appendChild(k_text);
document.getElementById('kommentare_container').appendChild(k);
let commentArray = JSON.parse(comments);
for (var i = 0; i < commentArray.length; i++) {
let thisComment = commentArray[i];
let kommentarContainer = $("<div>").addClass("comment");
let kommentarText = $("<p>").addClass("commentText");
kommentarText.text(thisComment.k_text);
kommentarText.appendTo(kommentarContainer);
let kommentarLikeNumber = $("<p>").text(thisComment.k_likes + " Likes").addClass("commentLikeNumber");
kommentarLikeNumber.appendTo(kommentarContainer);
let kommentarLikeButton = $("<img>").addClass("unliked commentLike")
if(localLikes.kommentare.includes(thisComment.k_id)){
kommentarLikeButton.attr("src", "../images/herz-1.png");
kommentarLikeButton.addClass("liked");
}else{
kommentarLikeButton.attr("src", "../images/herz-0.png");
kommentarLikeButton.addClass("unliked");
}
kommentarLikeButton.appendTo(kommentarContainer);
kommentarContainer.appendTo($("#kommentare_container"));
kommentarLikeButton.click( function() {
if(localLikes.kommentare.includes(thisComment.k_id)){
kommentarLikeButton.attr("src", "../images/herz-0.png");
kommentarLikeButton.addClass("unliked");
kommentarLikeButton.removeClass("liked");
removeLike("kommentar", thisComment.k_id, $(this))
}else{
kommentarLikeButton.attr("src", "../images/herz-1.png");
kommentarLikeButton.addClass("liked");
kommentarLikeButton.removeClass("unliked");
addLike("kommentar", thisComment.k_id, $(this))
}
});
/*
let kommentarErstellen = document.createElement("p");
let kommentar = document.createTextNode(commentArray[i].k_text);
kommentarErstellen.appendChild(kommentar);
kommentarErstellen.classList.add("comment");
document.getElementById('kommentare_container').appendChild(kommentarErstellen);
*/
}
});
}