mirror of
https://github.com/werkraum-media/watchlist.git
synced 2024-11-24 08:16:09 +01:00
Daniel Siepmann
ca9d22298e
Fix broken cookie path within middleware. For some reason we used the `/typo3/` path while storing cookies server side. But we used `/` in JavaScript. That didn't play together and was fixed to always be `/` for now, but it should be configurable in general. The fix revealed that the detection of whether to store a cookie was broken, which was fixed within the corresponding service. Furthermore the dates how long the cookie should be stored was different. We now always use 7 days.
112 lines
3.6 KiB
JavaScript
112 lines
3.6 KiB
JavaScript
(function () {
|
|
|
|
// From: https://plainjs.com/javascript/utilities/set-cookie-get-cookie-and-delete-cookie-5/
|
|
const cookie = {
|
|
name: 'watchlist',
|
|
days: 7,
|
|
get: function() {
|
|
let v = document.cookie.match('(^|;) ?' + cookie.name + '=([^;]*)(;|$)');
|
|
return v ? v[2] : null;
|
|
},
|
|
set: function(value) {
|
|
let d = new Date;
|
|
d.setTime(d.getTime() + 24 * 60 * 60 * 1000 * cookie.days);
|
|
document.cookie = cookie.name + "=" + value + ";path=/;expires=" + d.toGMTString();
|
|
},
|
|
delete: function() {
|
|
let d = new Date;
|
|
d.setTime(d.getTime() -1);
|
|
document.cookie = cookie.name + "=;path=/;expires=" + d.toGMTString();
|
|
}
|
|
};
|
|
|
|
const watchlist = {
|
|
get: function() {
|
|
const cookieValue = cookie.get();
|
|
if (cookieValue === null) {
|
|
return [];
|
|
}
|
|
|
|
return decodeURIComponent(cookieValue).split(',');
|
|
},
|
|
save: function(items) {
|
|
var cookieValue = items.join(',');
|
|
|
|
if (cookieValue == '') {
|
|
cookie.delete();
|
|
} else {
|
|
cookie.set(encodeURIComponent(items.join(',')));
|
|
}
|
|
},
|
|
toggleItem: function(identifier) {
|
|
let items = watchlist.get();
|
|
const position = items.indexOf(identifier);
|
|
|
|
if (position === -1) {
|
|
items = items.concat(identifier);
|
|
} else {
|
|
items.splice(position, 1);
|
|
}
|
|
|
|
watchlist.save(items);
|
|
watchlist.triggerUpdateEvent(identifier);
|
|
},
|
|
triggerUpdateEvent: function(identifier) {
|
|
const event = new CustomEvent('WatchlistUpdate', {
|
|
detail: {
|
|
watchlistItem: identifier
|
|
}
|
|
});
|
|
document.dispatchEvent(event);
|
|
}
|
|
};
|
|
|
|
const watchlistHtml = {
|
|
selectors: {
|
|
items: '[data-watchlist-item]',
|
|
counter: '[data-watchlist-counter]'
|
|
},
|
|
getItems: function() {
|
|
return document.querySelectorAll(watchlistHtml.selectors.items);
|
|
},
|
|
getCounters: function() {
|
|
return document.querySelectorAll(watchlistHtml.selectors.counter);
|
|
},
|
|
update: function() {
|
|
watchlistHtml.updateCounter();
|
|
watchlistHtml.updateItems();
|
|
},
|
|
updateCounter: function() {
|
|
const count = watchlist.get().length;
|
|
watchlistHtml.getCounters().forEach(function (element) {
|
|
element.innerText = count;
|
|
});
|
|
},
|
|
updateItems: function() {
|
|
const items = watchlist.get();
|
|
watchlistHtml.getItems().forEach(function (element) {
|
|
if (items.indexOf(element.dataset.watchlistItem) === -1) {
|
|
element.classList.add('watchlist-inactive');
|
|
element.classList.remove('watchlist-active');
|
|
return;
|
|
}
|
|
|
|
element.classList.remove('watchlist-inactive');
|
|
element.classList.add('watchlist-active');
|
|
});
|
|
}
|
|
};
|
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
watchlistHtml.update();
|
|
|
|
watchlistHtml.getItems().forEach(function (element) {
|
|
element.addEventListener('click', function(event) {
|
|
watchlist.toggleItem(event.currentTarget.dataset.watchlistItem);
|
|
});
|
|
});
|
|
});
|
|
document.addEventListener('WatchlistUpdate', function(event) {
|
|
watchlistHtml.update();
|
|
});
|
|
})();
|