Jake Vanderwerf
5 hours ago 56a9a1ccf764ff7a6af8f8a2292cb07443cb4aa7
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
document.addEventListener("DOMContentLoaded", function(e) {
    // Tabs functionality for settings pages
    let tabs = document.querySelectorAll('.jvb-settings-tab');
 
    tabs.forEach(tab => {
        tab.addEventListener('click', (e) => {
            removeActiveTab(tabs);
            tab.classList.add('active');
            setActiveSection(tab);
        });
    });
 
    // Check for hash in URL and activate corresponding tab
    if (window.location.hash) {
        var hash = window.location.hash.substring(1);
        document.querySelector('.jvb-settings-tab[data-tab="' + hash + '"]')?.click();
    } else {
        // Activate first tab by default
        document.querySelector('.jvb-settings-tab')?.click();
    }
 
    let confirm = document.querySelector('.jvb-confirm-action');
    if (confirm) {
        confirm.addEventListener('click', (e) => {
            if (!window.confirm(confirm.dataset.confirm || 'Are you sure?')) {
                e.preventDefault();
                return false;
            }
        });
    }
 
    // Initialize admin actions
    initAdminActions();
    initCacheActions();
    initIconActions();
});
 
function removeActiveTab(tabs) {
    let active = document.querySelectorAll('.active');
    active.forEach(tab => {
        tab.classList.remove('active');
        if (tab.dataset.tab) {
            setActiveSection(tab, false);
            window.location.hash = tab.dataset.tab;
        }
    });
}
 
function setActiveSection(tab, set = true) {
    let id = tab.dataset.tab;
    if (!id) return;
 
    let section = document.querySelector('#' + id);
    if (!section) return;
 
    if (set) {
        section.classList.add('active');
    } else {
        section.classList.remove('active');
    }
}
 
/**
 * Initialize admin action buttons (dashboard quick actions)
 */
function initAdminActions() {
    document.querySelectorAll('a[data-action]').forEach(action => {
        action.addEventListener('click', (e) => {
            e.preventDefault();
            let loader = action.querySelector('.loader');
            loader.classList.add('loading');
 
            let a = action.dataset.action;
 
            makeAdminRequest('admin-action', { action: a })
                .then(data => {
                    if (data.success === true) {
                        loader.classList.remove('loading');
                        loader.classList.add('loaded');
                        setTimeout(() => {
                            loader.classList.remove('loaded');
                        }, 3000);
                    } else {
                        throw new Error(data.message || 'Action failed');
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    loader.classList.remove('loading');
                    alert('Error: ' + error.message);
                });
        });
    });
}
 
/**
 * Initialize cache management actions
 */
function initCacheActions() {
    const flushAllBtn = document.querySelector('[data-cache-action="flush-all"]');
    if (flushAllBtn) {
        flushAllBtn.addEventListener('click', function() {
            const originalText = this.innerHTML;
            this.disabled = true;
            this.innerHTML = 'Flushing...';
 
            makeAdminRequest('admin-cache', { action: 'flush-all' })
                .then(data => handleActionResponse(data, this, originalText))
                .catch(error => handleActionError(error, this, originalText));
        });
    }
 
    document.querySelectorAll('[data-cache-action="flush-cache"]').forEach(btn => {
        btn.addEventListener('click', function() {
            const group = this.getAttribute('data-group');
            const originalText = this.innerHTML;
            this.disabled = true;
            this.innerHTML = 'Flushing...';
 
            makeAdminRequest('admin-cache', { action: 'flush-cache', group: group })
                .then(data => handleActionResponse(data, this, originalText))
                .catch(error => handleActionError(error, this, originalText));
        });
    });
}
 
/**
 * Initialize icon management actions
 */
function initIconActions() {
    const currentSource = document.getElementById('icon-source-select')?.value || 'icons';
 
    // Select all checkbox
    const selectAll = document.getElementById('select-all-versions');
    if (selectAll) {
        selectAll.addEventListener('change', function() {
            document.querySelectorAll('.version-checkbox').forEach(checkbox => {
                checkbox.checked = this.checked;
                checkbox.dispatchEvent(new Event('change'));
            });
        });
    }
 
    // Enable/disable merge button based on selection
    document.querySelectorAll('.version-checkbox').forEach(checkbox => {
        checkbox.addEventListener('change', updateMergeButtonState);
    });
 
    // Toggle icon list view
    document.querySelectorAll('.view-icon-list-btn').forEach(btn => {
        btn.addEventListener('click', function() {
            const timestamp = this.getAttribute('data-timestamp');
            const row = document.getElementById('icon-list-' + timestamp);
            if (row) {
                row.style.display = row.style.display === 'none' ? '' : 'none';
                this.textContent = row.style.display === 'none' ? '(view)' : '(hide)';
            }
        });
    });
 
    // Force refresh button
    const refreshBtn = document.querySelector('[data-icon-action="refresh-icons"]');
    if (refreshBtn) {
        refreshBtn.addEventListener('click', function() {
            const originalText = this.innerHTML;
            this.disabled = true;
            this.innerHTML = 'Regenerating...';
 
            const source = this.getAttribute('data-source') || currentSource;
            makeAdminRequest('admin-icons', { action: 'refresh-icons', source: source })
                .then(data => {
                    handleActionResponse(data, this, originalText);
                })
                .catch(error => {
                    handleActionError(error, this, originalText);
                });
 
        });
    }
 
    // Merge versions button
    const mergeBtn = document.getElementById('merge-versions-btn');
    if (mergeBtn) {
        mergeBtn.addEventListener('click', function() {
            const checkboxes = document.querySelectorAll('.version-checkbox:checked');
            const timestamps = Array.from(checkboxes).map(cb => parseInt(cb.value));
 
            if (timestamps.length < 2) {
                alert('Please select at least 2 versions to merge');
                return;
            }
 
            if (confirm(`Merge ${timestamps.length} versions? This will create a new CSS file with all unique icons.`)) {
                const originalText = this.innerHTML;
                this.disabled = true;
                this.innerHTML = 'Merging...';
 
                const source = this.getAttribute('data-source') || currentSource;
                makeAdminRequest('admin-icons', {
                    action: 'merge-icon-versions',
                    source: source,
                    timestamps: timestamps
                })
                    .then(data => {
                        handleActionResponse(data, this, originalText);
                    })
                    .catch(error => {
                        handleActionError(error, this, originalText);
                    });
            }
        });
    }
 
    // Restore version buttons
    document.querySelectorAll('[data-icon-action="restore-icon-version"]').forEach(btn => {
        btn.addEventListener('click', function() {
            const timestamp = parseInt(this.getAttribute('data-timestamp'));
            const source = this.getAttribute('data-source') || currentSource;
 
            if (confirm('Restore this icon version? This will reload the page.')) {
                const originalText = this.innerHTML;
                this.disabled = true;
                this.innerHTML = 'Restoring...';
 
                makeAdminRequest('admin-icons', {
                    action: 'restore-icon-version',
                    source: source,
                    timestamp: timestamp
                })
                    .then(data => {
                        handleActionResponse(data, this, originalText);
                    })
                    .catch(error => {
                        handleActionError(error, this, originalText);
                    });
            }
        });
    });
}
 
/**
 * Update merge button state based on selected checkboxes
 */
function updateMergeButtonState() {
    const checkedCount = document.querySelectorAll('.version-checkbox:checked').length;
    const mergeBtn = document.getElementById('merge-versions-btn');
    if (mergeBtn) {
        mergeBtn.disabled = checkedCount < 2;
    }
}
 
/**
 * Make an admin API request
 * @param {string} endpoint - The API endpoint (without 'jvb/v1/')
 * @param {object} data - The data to send
 * @returns {Promise}
 */
function makeAdminRequest(endpoint, data = {}) {
    if (typeof jvbSettings === 'undefined') {
        console.error('jvbSettings is not defined');
        return Promise.reject(new Error('jvbSettings is not defined. Scripts may not be loaded correctly.'));
    }
 
    console.log('Making request to:', jvbSettings.api + endpoint, 'with data:', data);
 
    return fetch(jvbSettings.api + endpoint, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-WP-Nonce': jvbSettings.nonce,
            'X-Action-Nonce': jvbSettings.action
        },
        body: JSON.stringify(data)
    })
        .then(response => {
            console.log('Response status:', response.status);
            if (!response.ok) {
                return response.json().then(err => {
                    throw new Error(err.message || 'Network response was not ok');
                });
            }
            return response.json();
        })
        .then(data => {
            console.log('Response data:', data);
            return data;
        });
}
 
/**
 * Handle successful action response
 */
function handleActionResponse(data, button = null, originalText = null) {
    if (!data.success) {
        throw new Error(data.message || 'Unknown error');
    }
 
    if (button && originalText) {
        button.innerHTML = '✓ Success!';
        setTimeout(() => {
            button.disabled = false;
            button.innerHTML = originalText;
        }, 1500);
    }
}
 
/**
 * Handle action error
 */
function handleActionError(error, button = null, originalText = null) {
    console.error('Error:', error);
 
    if (button && originalText) {
        button.innerHTML = '✗ ' + (error.message || 'Error');
        setTimeout(() => {
            button.disabled = false;
            button.innerHTML = originalText;
        }, 2000);
    }
}