class FrontendVotes {
|
constructor() {
|
// Initialize DataStore for queue persistence
|
this.store = window.jvbStore.register('votes',
|
{
|
endpoint: 'votes',
|
useIndexedDB: true,
|
TTL: 6 * 60 * 1000,
|
showLoading: false
|
});
|
}
|
|
handleVote(button) {
|
if (!window.auth.getUser()) {
|
window.location.href = jvbSettings.redirect + '&action=register&type=vote';
|
return;
|
}
|
window.jvbQueue.handleVote(button);
|
|
const parent = button.closest('.vote');
|
const alreadyVoted = parent.querySelector('.voted');
|
|
// Handle previous vote if exists
|
if (alreadyVoted) {
|
const count = alreadyVoted.querySelector('.count');
|
if (alreadyVoted.classList.contains('up')) {
|
count.textContent = parseInt(count.textContent) - 1;
|
} else {
|
count.textContent = parseInt(count.textContent) + 1;
|
}
|
alreadyVoted.classList.remove('voted');
|
}
|
|
// Update current vote
|
button.classList.add('voted');
|
const count = button.querySelector('.count');
|
if (button.classList.contains('up')) {
|
count.textContent = parseInt(count.textContent) + 1;
|
} else {
|
count.textContent = parseInt(count.textContent) - 1;
|
}
|
}
|
|
isFavourited(content, id){
|
if(!window.auth.getUser()){
|
return false;
|
}
|
let item = this.store.getItem(id);
|
return (item) ? item.action === 'add' : false;
|
}
|
}
|
window.jvbVotes = false;
|
if (window.auth.getUser() !== '') {
|
window.jvbVotes = new FrontendFavourites();
|
}
|
|
window.toggleFavourite = function(button) {
|
window.jvbVotes.toggleVote(button);
|
}
|
|
|
window.handleVote = function(button) {
|
window.jvbVotes.handleVote(button);
|
};
|