Jake Vanderwerf
2025-09-30 12e5205559c8def89659f9922a91b8b628289664
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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);
}