Jake Vanderwerf
2026-01-01 52733beffd7f1c48012b371d4ad8e7d937afd924
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/**
 * Simplified autosave approach for FormController
 * Leverages QueueManager for all server operations
 */
 
// In FormController class - replace the autosave-related methods with:
 
class FormController {
    // ... existing constructor and init code ...
 
    /**
     * Initialize form tracking for autosave
     */
    registerForm(formElement, options = {}) {
        const formId = formElement.dataset.formId || `form_${Date.now()}`;
        formElement.dataset.formId = formId;
 
        const formConfig = {
            element: formElement,
            id: formId,
            contentId: formElement.dataset.contentId || null, // For existing content
            contentType: formElement.dataset.contentType || null,
            options: {
                autoSave: true,
                saveDelay: this.autoSaveDefaults.delay,
                endpoint: formElement.dataset.save || formElement.action,
                ...options
            },
            lastSnapshot: {}, // Last saved state
            isDirty: false
        };
 
        // Take initial snapshot
        formConfig.lastSnapshot = this.collectFormData(formElement);
 
        // Initialize special fields
        this.initializeFormFields(formElement, formConfig);
 
        // Store form config
        this.forms.set(formId, formConfig);
 
        return formConfig;
    }
 
    /**
     * Simplified change handler
     */
    handleChange(event) {
        const target = event.target;
        const form = target.form || target.closest('form');
        if (!form) return;
 
        const formConfig = this.forms?.get(form.dataset.formId);
        if (!formConfig) return;
 
        // Check conditional fields (existing functionality)
        const dependencies = formConfig.dependencies?.get(target.name);
        if (dependencies) {
            dependencies.forEach(dep => {
                this.checkFieldDependency(form, dep.field, target.name, dep.requiredValue, dep.operator);
            });
        }
 
        // Schedule autosave if enabled
        if (formConfig.options.autoSave && !form.dataset.noautosave) {
            const delay = this.getDelayForField(target);
            this.scheduleSave(formConfig, delay);
        }
    }
 
    /**
     * Get appropriate delay based on field type and context
     */
    getDelayForField(field) {
        // Text fields get longer delay for typing
        if (field.type === 'text' || field.type === 'textarea') {
            return this.autoSaveDefaults.typingDelay;
        }
 
        // Checkboxes, radios, selects get shorter delay
        if (['checkbox', 'radio', 'select-one', 'select-multiple'].includes(field.type)) {
            return 1000;
        }
 
        // Default delay
        return this.autoSaveDefaults.delay;
    }
 
    /**
     * Simplified scheduleSave - just debounces the queue addition
     */
    scheduleSave(formConfig, delay = this.autoSaveDefaults.delay) {
        const saveKey = `autosave_${formConfig.id}`;
 
        this.debouncer.schedule(
            saveKey,
            () => this.queueAutosave(formConfig),
            delay
        );
    }
 
    /**
     * Queue the autosave operation
     */
    async queueAutosave(formConfig) {
        const currentData = this.collectFormData(formConfig.element);
        const changes = this.getChangedFields(formConfig.lastSnapshot, currentData);
 
        // No changes? Don't save
        if (Object.keys(changes).length === 0) return;
 
        // For bulk edit forms, handle specially
        if (formConfig.element.classList.contains('bulk-edit')) {
            this.queueBulkSave(formConfig, changes);
            return;
        }
 
        // Build the queue operation
        const operation = {
            endpoint: formConfig.options.endpoint || '/wp-json/jvb/v1/autosave',
            method: formConfig.contentId ? 'PUT' : 'POST',
            data: {
                form_id: formConfig.id,
                content_id: formConfig.contentId,
                content_type: formConfig.contentType,
                changes: changes,
                full_data: currentData
            },
            title: `Autosaving ${formConfig.contentType || 'form'}`,
            popup: null, // No popup for autosave
            headers: {
                'X-Autosave': 'true'
            },
            source: 'form',
            formId: formConfig.id,
            // For optimistic updates
            localUpdate: formConfig.contentId ? {
                action: 'update',
                id: formConfig.contentId,
                changes: changes
            } : null,
            dataStore: this.store
        };
 
        // Add to queue
        const queueItem = await this.queue.addToQueue(operation);
 
        if (queueItem) {
            // Update snapshot to prevent re-saving same changes
            formConfig.lastSnapshot = currentData;
            formConfig.isDirty = false;
 
            // Visual feedback
            this.showFormStatus(formConfig.element, 'queued');
 
            // Track the operation
            this.trackOperation(formConfig, queueItem.id);
        }
    }
 
    /**
     * Handle bulk edit forms specially
     */
    queueBulkSave(formConfig, changes) {
        const selectedItems = formConfig.element.querySelectorAll('.bulk-item input:checked');
        const itemIds = Array.from(selectedItems).map(cb => cb.value);
 
        if (itemIds.length === 0) return;
 
        const operation = {
            endpoint: formConfig.options.endpoint || '/wp-json/jvb/v1/bulk-update',
            method: 'PUT',
            data: {
                content_type: formConfig.contentType,
                item_ids: itemIds,
                changes: changes
            },
            title: `Updating ${itemIds.length} items`,
            popup: `${itemIds.length} items queued for update`,
            headers: {
                'X-Bulk-Operation': 'true'
            },
            source: 'form',
            formId: formConfig.id,
            // Optimistic update for each item
            localUpdate: {
                action: 'bulk-update',
                ids: itemIds,
                changes: changes
            },
            dataStore: this.store
        };
 
        // Add to queue
        const queueItem = this.queue.addToQueue(operation);
 
        if (queueItem) {
            formConfig.lastSnapshot = this.collectFormData(formConfig.element);
            formConfig.isDirty = false;
            this.showFormStatus(formConfig.element, 'queued');
        }
    }
 
    /**
     * Track operations for forms
     */
    trackOperation(formConfig, operationId) {
        if (!formConfig.operations) {
            formConfig.operations = new Set();
        }
        formConfig.operations.add(operationId);
 
        // Subscribe to queue updates for this operation
        const unsubscribe = this.queue.subscribe((event, data) => {
            if (data?.id !== operationId) return;
 
            switch(event) {
                case 'operation-status':
                    this.updateFormStatus(formConfig, data.status);
                    break;
 
                case 'operation-completed':
                    this.handleSaveSuccess(formConfig, data);
                    formConfig.operations.delete(operationId);
                    unsubscribe();
                    break;
 
                case 'operation-failed':
                    this.handleSaveFailure(formConfig, data);
                    formConfig.operations.delete(operationId);
                    unsubscribe();
                    break;
            }
        });
    }
 
    /**
     * Update form status based on queue status
     */
    updateFormStatus(formConfig, status) {
        const statusMap = {
            'queued': 'queued',
            'uploading': 'saving',
            'processing': 'saving',
            'pending': 'saving',
            'completed': 'saved',
            'failed': 'error',
            'failed_permanent': 'error'
        };
 
        this.showFormStatus(formConfig.element, statusMap[status] || status);
    }
 
    /**
     * Handle successful save from queue
     */
    handleSaveSuccess(formConfig, data) {
        // Update content ID if this was a create operation
        if (!formConfig.contentId && data.result?.id) {
            formConfig.contentId = data.result.id;
            formConfig.element.dataset.contentId = data.result.id;
        }
 
        // Reset dirty flag
        formConfig.isDirty = false;
 
        // Show success
        this.showFormStatus(formConfig.element, 'saved');
 
        // Notify subscribers
        this.notify('form-saved', {
            formId: formConfig.id,
            contentId: formConfig.contentId,
            data: data
        });
    }
 
    /**
     * Handle save failure from queue
     */
    handleSaveFailure(formConfig, data) {
        // Mark as dirty so it will retry
        formConfig.isDirty = true;
 
        // Show error
        this.showFormStatus(formConfig.element, 'error');
 
        // Notify subscribers
        this.notify('form-save-failed', {
            formId: formConfig.id,
            error: data.lastError
        });
    }
 
    /**
     * Manual save (for submit button)
     */
    async handleSubmit(event) {
        const form = event.target;
        if (!form.dataset.formId) return;
 
        event.preventDefault();
 
        const formConfig = this.forms.get(form.dataset.formId);
        if (!formConfig) return;
 
        // Force immediate save
        this.debouncer.cancel(`autosave_${formConfig.id}`);
 
        // Add to queue with higher priority
        const currentData = this.collectFormData(form);
 
        const operation = {
            endpoint: formConfig.options.endpoint,
            method: formConfig.contentId ? 'PUT' : 'POST',
            data: {
                content_id: formConfig.contentId,
                content_type: formConfig.contentType,
                ...currentData
            },
            title: `Saving ${formConfig.contentType || 'form'}`,
            popup: 'Saved successfully',
            priority: 'high', // Process before autosaves
            source: 'form',
            formId: formConfig.id,
            localUpdate: formConfig.contentId ? {
                action: 'update',
                id: formConfig.contentId,
                changes: currentData
            } : {
                action: 'create',
                data: currentData
            },
            dataStore: this.store
        };
 
        const queueItem = await this.queue.addToQueue(operation);
 
        if (queueItem) {
            formConfig.lastSnapshot = currentData;
            this.trackOperation(formConfig, queueItem.id);
            this.showFormStatus(form, 'saving');
        }
    }
 
    /**
     * Check if form has unsaved changes
     */
    hasUnsavedChanges(formId) {
        const formConfig = this.forms.get(formId);
        if (!formConfig) return false;
 
        // Check if there are pending operations
        if (formConfig.operations?.size > 0) return true;
 
        // Check if current data differs from snapshot
        const currentData = this.collectFormData(formConfig.element);
        const changes = this.getChangedFields(formConfig.lastSnapshot, currentData);
 
        return Object.keys(changes).length > 0;
    }
 
    /**
     * Cleanup when form is closed/destroyed
     */
    cleanupForm(formId) {
        const formConfig = this.forms.get(formId);
        if (!formConfig) return;
 
        // Cancel any pending debounced saves
        this.debouncer.cancel(`autosave_${formId}`);
 
        // Check for unsaved changes
        if (this.hasUnsavedChanges(formId)) {
            // Could show a warning or auto-save
            this.queueAutosave(formConfig);
        }
 
        // Clean up special fields
        this.cleanupSpecialFields();
 
        // Remove form config
        this.forms.delete(formId);
    }
}