Jake Vanderwerf
2025-09-30 2143fc670fa245750ac7f774fcb38ea8cfbf7edb
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
/**
 * This separates out all create logic from the base TaxonomySelector.js, so that we only enqueue create logic if it's creatable
 * Updated to work with the refactored centralized TaxonomySelector
 */
 
class TaxonomyCreator {
 
    constructor(selector) {
        this.selector = selector;
 
        // Get taxonomy from current active field config
        this.taxonomy = selector.currentConfig?.taxonomy;
        if (!this.taxonomy) {
            console.error('TaxonomyCreator: No active field or taxonomy found');
            return;
        }
 
        this.createNew = selector.modal.querySelector('.create-new-term');
        this.toggle = selector.modal.querySelector('.new-term-toggle');
        this.form = this.createNew.querySelector('.create-new-term-section');
 
        this.initListeners();
        this.initTermCreation();
    }
 
    initListeners() {
        document.addEventListener('click', this.handleClick.bind(this));
    }
 
    handleClick(e) {
        if (window.targetCheck(e, '.create-new-term summary')) {
            if (this.createNew.open) {
                this.createNew.querySelector('input[name="term_name"]').focus();
            }
            this.resetParentOptions();
        }
 
        if (window.targetCheck(e, '.submit-term')) {
            this.handleTermCreation(e);
        }
    }
 
    async handleTermCreation(e) {
        const termName = this.form.querySelector('input[name="term_name"]').value.trim();
        const parentId = this.form.querySelector('input#select_parent')?.value;
 
        try {
            this.form.querySelector('button').disabled = true;
            const response = await this.createTerm(termName, parentId);
 
            if (response.success) {
                let term = response.term;
 
                // Close the create new section
                this.createNew.open = false;
 
                // Add to the terms list UI
                this.selector.createTermElement({
                    id: parseInt(term.id),
                    name: term.name,
                    hasChildren: term.hasChildren || false,
                    path: term.path || term.name,
                    show: false
                });
 
                // Add to current modal selection
                this.selector.addSelectedTermToModal(term.id, term.name, term.path);
 
                // Clear the form
                this.form.querySelector('input[name="term_name"]').value = '';
            }
        } catch (error) {
            console.error('Error creating term:', error);
            this.selector.showError?.('Failed to create term') ||
            console.error('Failed to create term');
        } finally {
            this.form.querySelector('button').disabled = false;
        }
    }
 
    initTermCreation() {
        if (!this.form) {
            return;
        }
 
        this.form.addEventListener('change', (e) => {
            e.preventDefault();
            e.stopPropagation();
        });
    }
 
    resetParentOptions() {
        let select = this.createNew.querySelector('#select_parent');
        if (!select) return;
 
        let defaultOption = select.querySelector('option');
        if (!defaultOption) return;
 
        // Clear existing options
        window.removeChildren(select);
        select.append(defaultOption.cloneNode(true));
 
        // Add current parent if we're in a sub-category
        if (this.selector.currentParentName !== '') {
            let parentOption = defaultOption.cloneNode(true);
            parentOption.value = this.selector.currentParent;
            parentOption.textContent = this.selector.currentParentName;
            select.append(parentOption);
        }
 
        // Add terms from current taxonomy cache
        const taxonomyTerms = this.selector.currentTerms;
        if (taxonomyTerms && taxonomyTerms.length > 0) {
            taxonomyTerms.forEach(term => {
                let option = defaultOption.cloneNode(true);
                option.id = `select-parent-${term.id}`;
                option.value = term.id;
                option.textContent = '  — ' + term.name;
                select.append(option);
            });
        }
    }
 
    async createTerm(name, parent = 0) {
        let loadingMessage = this.createNew.querySelector('.loading-message.create-term');
        let text = loadingMessage?.querySelector('span');
 
        try {
            if (loadingMessage) {
                loadingMessage.hidden = false;
            }
 
            if (text && window.typeText) {
                window.typeText(text, 'Checking term...');
            } else if (text) {
                text.textContent = 'Checking term...';
            }
 
            // Check if term already exists by searching
            const originalSearchQuery = this.selector.searchQuery;
            const originalFetchSpecific = this.selector.fetchSpecificTerms;
 
            this.selector.searchQuery = name;
            this.selector.fetchSpecificTerms = false; // We want to search, not fetch specific IDs
 
            const existingTerms = await this.selector.fetchTerms(
                this.selector.activeField,
                false,
                true // isSearch = true
            );
 
            // Restore original search state
            this.selector.searchQuery = originalSearchQuery;
            this.selector.fetchSpecificTerms = originalFetchSpecific;
 
            // Check if any existing terms match exactly
            const exactMatches = existingTerms.filter(term =>
                term.name.toLowerCase() === name.toLowerCase()
            );
 
            if (exactMatches.length > 0) {
                this.showTermSuggestions(exactMatches);
                return { success: false, reason: 'exists' };
            }
 
            // Show similar terms if found
            if (existingTerms.length > 0) {
                this.showTermSuggestions(existingTerms);
                return { success: false, reason: 'similar' };
            }
 
            // Term doesn't exist, create it
            if (text) {
                text.textContent = 'Creating term...';
            }
 
            const response = await fetch(`${jvbSettings.api}terms`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-WP-Nonce': jvbSettings.nonce
                },
                body: JSON.stringify({
                    taxonomy: this.taxonomy,
                    name: name,
                    parent: parent
                })
            });
 
            if (!response.ok) {
                throw new Error(`Server error: ${response.status}`);
            }
 
            const result = await response.json();
            return result;
 
        } catch (error) {
            console.error('Error creating term:', error);
            throw error;
        } finally {
            this.form.querySelector('button').disabled = false;
            if (loadingMessage) {
                loadingMessage.hidden = true;
            }
        }
    }
 
    // Helper method to show term suggestions when similar terms exist
    showTermSuggestions(suggestions) {
        const suggestionContainer = this.createNew.querySelector('.term-suggestions') ||
            this.createSuggestionContainer();
 
        // Clear existing suggestions
        window.removeChildren(suggestionContainer);
 
        // Add heading
        const heading = document.createElement('h4');
        heading.textContent = 'Similar terms already exist:';
        suggestionContainer.appendChild(heading);
 
        // Create list of suggestions
        const list = document.createElement('ul');
        list.className = 'term-suggestion-list';
 
        suggestions.forEach(term => {
            const item = document.createElement('li');
 
            // Create term path display if available
            let termDisplay = term.path || term.name;
 
            const button = document.createElement('button');
            button.type = 'button';
            button.className = 'use-existing-term';
            button.setAttribute('data-id', term.id);
            button.textContent = termDisplay;
 
            button.addEventListener('click', () => {
                // Add this term to modal selection
                this.selector.addSelectedTermToModal(term.id, term.name, term.path);
 
                // Close the create new section
                this.createNew.open = false;
 
                // Clear suggestions
                suggestionContainer.hidden = true;
 
                // Clear the form
                this.form.querySelector('input[name="term_name"]').value = '';
            });
 
            item.appendChild(button);
            list.appendChild(item);
        });
 
        suggestionContainer.appendChild(list);
        suggestionContainer.hidden = false;
    }
 
    // Create container for term suggestions if it doesn't exist
    createSuggestionContainer() {
        const container = document.createElement('div');
        container.className = 'term-suggestions';
        container.hidden = true;
 
        // Insert after the form
        this.createNew.querySelector('form').after(container);
        return container;
    }
 
    /**
     * Clean up when modal closes
     */
    destroy() {
        // Remove event listeners if needed
        // Clear any pending operations
        const loadingMessage = this.createNew?.querySelector('.loading-message.create-term');
        if (loadingMessage) {
            loadingMessage.hidden = true;
        }
 
        // Clear suggestions
        const suggestionContainer = this.createNew?.querySelector('.term-suggestions');
        if (suggestionContainer) {
            suggestionContainer.hidden = true;
        }
    }
}
 
window.jvbTaxCreator = TaxonomyCreator;