Candle/warenkorb.js

154 lines
5.0 KiB
JavaScript

var parsed = [];
var addItemButton = document.getElementsByClassName("binPicture");
if (window.location.pathname.includes("/warenkorb.html")) {
document.addEventListener("DOMContentLoaded", function () {
var addItemButton = document.getElementById("jetztReservieren");
addItemButton.addEventListener('click', parseAttributes)
deleteItem()
});
}
if (window.location.pathname.includes("/meinebuecher.html")) {
document.addEventListener("DOMContentLoaded", function () {
warenkorby = localStorage.getItem("meinebuecher") || [];
const parsedArray = JSON.parse(warenkorby)
addingMeinBuecher(parsedArray)
}
);}
function deleteItem()
{
updateArticle()
//Dieser Code Block gehört zur Warenkorb Datei
for(var i = 0;i<addItemButton.length;i++)
{
var button = addItemButton[i]
button.addEventListener(('click'), function(event)
{
var buttonClicked = event.target
var diving = buttonClicked.parentElement.parentElement.parentElement;
// Set counter to Zero
var title = diving.getElementsByTagName('h3')[0].innerHTML
var getValue= localStorage.getItem(title) || 0;
diving.getElementsByClassName('input-group-field')[0].value = getValue;
localStorage.setItem(title, 0)
var title = diving.getElementsByTagName('h3')[0].innerHTML
var warenkorby = localStorage.getItem("items") || [];
var parsedArray = JSON.parse(warenkorby)
for(var j = 0;j<parsedArray.length;j++)
{
const parser = new DOMParser();
const parsedDocument = parser.parseFromString(parsedArray[j], "text/html");
var titling =parsedDocument.getElementsByTagName('h3')[0].innerHTML
var classred = parsedDocument.getElementsByTagName('a')[0].innerHTML
if(titling.includes(title) ||classred.includes("red") ) {
delete warenkorb[j];
const jsonArray = JSON.stringify(warenkorb);
localStorage.setItem('items', jsonArray);
break;
}
}
buttonClicked.parentElement.parentElement.parentElement.remove()
delete addItemButton[i]
var getArticleNumber = document.getElementById("ArtikelWarenkorb").innerHTML = addItemButton.length + " Artikel";
}
)
}
}
function updateArticle()
{
var getArticleNumber = document.getElementById("ArtikelWarenkorb").innerHTML = addItemButton.length + " Artikel";
}
function parseAttributes()
{
var warenkorby = localStorage.getItem("items") || [];
var parsedArray = JSON.parse(warenkorby)
for(var j =0 ;j<parsedArray.length;j++)
{
const parser = new DOMParser();
const parsedDocument = parser.parseFromString(parsedArray[j], "text/html");
var imaging =parsedDocument.getElementsByTagName('img')[0].src
var discription = parsedDocument.getElementsByTagName('p')[0].innerHTML
var titling = parsedDocument.getElementsByTagName('h3')[0].innerHTML
// var cartItems = document.getElementsByClassName('containerofbuecher')[0]
var cartRowConetent = `<div class="items" >
<img src="${imaging}" width="125" height="200"/>
<h3 class="headerinItem">
${titling}</h3>
<p class="discinItem">${discription}</p>
<a class="greenbutton" href="">Sofort Abholbereit</a>
<a class="binPicture" href=""><img src="pictures/bin.png" width="25" height="25"></a>
<p id="message">Diese Reservierung verfällt, wenn das Buch nicht bis zum 24.04.2023 abgeholt wurde.</p>
<hr style="width: 90%;">
</div>
`
parsed.push(cartRowConetent)
}
// convert array to JSON string using JSON.stringify()
const jsonArray = JSON.stringify(parsed);
// save to localStorage using "array" as the key and jsonArray as the value
localStorage.setItem('meinebuecher', jsonArray);
localStorage.removeItem('items');
}
function addingMeinBuecher(wat) {
// var cartItems = document.getElementsByClassName('containerOfAll')[0];
for (var i = 0; i < wat.length; i++) {
/* var cartRow = document.createElement('div');
cartRow.insertAdjacentHTML('beforeend', wat[i]);
cartItems.appendChild(cartRow);
*/
insertHTMLStringToContainer(wat[i],"containerofmeinebuecher")
}
}
function insertHTMLStringToContainer(htmlString, containerId) {
var container = document.getElementsByClassName(containerId)[0];
container.insertAdjacentHTML('beforeend', htmlString);
}
function addingAll(wat) {
// var cartItems = document.getElementsByClassName('containerOfAll')[0];
for (var i = 0; i < wat.length; i++) {
/* var cartRow = document.createElement('div');
cartRow.insertAdjacentHTML('beforeend', wat[i]);
cartItems.appendChild(cartRow);
*/
insertHTMLStringToContainer(wat[i],"containerOfAll")
}
}