class FrontendFavourites {
|
constructor() {
|
// Initialize DataStore for queue persistence
|
this.store = new window.jvbStore({
|
name: 'favourites',
|
endpoint: 'favourites',
|
useIndexedDB: true,
|
TTL: Infinity,
|
showLoading: false,
|
filters: {
|
user: jvbSettings.currentUser,
|
content: 'all',
|
order: 'desc',
|
orderby: 'date',
|
page: 1,
|
all: true,
|
}
|
});
|
|
this.store.subscribe((event, data) => {
|
switch (event) {
|
case 'data-fetched':
|
case 'data-cached':
|
// this.updateOperationsFromServer(data.data.items);
|
break;
|
case 'items-updated':
|
// this.updateOperationsFromServer(data.items);
|
break;
|
case 'item-stored':
|
// this.updateOperationsFromServer([data])
|
break;
|
}
|
|
});
|
|
this.store.fetch();
|
}
|
|
toggleFavourite(button) {
|
if (!jvbSettings.currentUser) {
|
window.location.href = jvbSettings.redirect + '&action=register&type=favourites';
|
return;
|
}
|
// Toggle UI immediately
|
button.classList.toggle('favourited');
|
const action = button.classList.contains('favourited') ? 'add' : 'remove';
|
const message = button.classList.contains('favourited')
|
? `Added ${button.dataset.type} to favourites.`
|
: `Removed ${button.dataset.type} from favourites.`;
|
|
window.jvbA11y.announce(message);
|
|
// Update button icon
|
button.innerHTML = jvbSettings.icons[button.classList.contains('favourited') ? 'heart-filled' : 'heart'];
|
|
this.store.setItem(button.dataset.id, {
|
target_id: button.dataset.id,
|
action: action,
|
type: button.dataset.type,
|
artist: button.dataset.artist,
|
});
|
}
|
|
isFavourited(content, id){
|
if(!jvbSettings.currentUser){
|
return false;
|
}
|
let item = this.store.getItem(id);
|
return (item) ? item.action === 'add' : false;
|
}
|
}
|
document.addEventListener('DOMContentLoaded', function() {
|
window.jvbFavourites = false;
|
if (jvbSettings.currentUser !== '') {
|
window.jvbFavourites = new FrontendFavourites();
|
}
|
});
|
|
|
window.toggleFavourite = function(button) {
|
if (!window.jvbFavourites()) {
|
console.log('No Favourites Loaded');
|
return;
|
}
|
window.jvbFavourites.toggleFavourite(button);
|
}
|
|
/**
|
* Tests whether item is favourited by current user
|
* @param content
|
* @param id
|
* @returns {*|boolean}
|
*/
|
window.isFavourited = function(content, id){
|
if (!window.jvbFavourites()) {
|
console.log('No Favourites Loaded');
|
return;
|
}
|
return window.jvbFavourites.isFavourited(content, id);
|
}
|