From 235ce5716edc2f7cbe80fdccf26eac7269587839 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Mon, 08 Jun 2026 04:38:18 +0000
Subject: [PATCH] =FavouritesManager.php and FavouritesRoutes.php fixes. Moving all logic to FavouritesManager.php. Still some left to do

---
 assets/js/concise/TaxonomySelector.js |  108 +++++++++++++++++++++++++++++++-----------------------
 1 files changed, 62 insertions(+), 46 deletions(-)

diff --git a/assets/js/concise/TaxonomySelector.js b/assets/js/concise/TaxonomySelector.js
index 2f58c05..90f6076 100644
--- a/assets/js/concise/TaxonomySelector.js
+++ b/assets/js/concise/TaxonomySelector.js
@@ -184,7 +184,7 @@
 			},
 			favourites: '.favourite-terms',
 			field: {
-				toggle: 'button.taxonomy-toggle, [data-filter="taxonomy"]',
+				toggle: 'button.selector-toggle, [data-filter="taxonomy"]',
 				value: 'input[type="hidden"]',
 				selected: '.selected-items',
 				dropdown: {
@@ -239,6 +239,22 @@
 		const field = this.fields.get(fieldId);
 		if (!fieldId || !field) return;
 
+		if (this.creator) {
+			let button = window.targetCheck(e, this.selectors.create.button);
+			if (button) {
+				this.maybeCreateTerm(e).then(()=>{});
+			}
+		}
+
+		const removeButton = window.targetCheck(e, '.remove-term');
+		if (removeButton) {
+			const termId = removeButton.closest('[data-id]').dataset.id??false;
+			if (fieldId && termId) {
+				this.removeSelected(parseInt(termId), fieldId);
+			}
+			return;
+		}
+
 		const autocomplete = window.targetCheck(e, '.item.autocomplete');
 
 		if (autocomplete) {
@@ -259,14 +275,6 @@
 			return;
 		}
 
-		const removeButton = window.targetCheck(e, '.remove-term');
-		if (removeButton) {
-			const termId = removeButton.closest('[data-id]').dataset.id??false;
-			if (fieldId && termId) {
-				this.removeSelected(parseInt(termId), fieldId);
-			}
-			return;
-		}
 
 		if (e.target.matches('.modal-close')) {
 			this.updateFieldValue(fieldId);
@@ -320,14 +328,6 @@
 				this.ui.search.input.value = '';
 			}
 		}
-
-		if (this.creator) {
-			let button = window.targetCheck(e, this.selectors.create.button);
-			if (button) {
-				this.maybeCreateTerm(e).then(()=>{});
-			}
-		}
-
 	}
 	handleChange(e) {
 		if (!this.container.contains(e.target) && !e.target.closest('[data-type="selector"], [data-field-type="selector"]')) {
@@ -365,7 +365,7 @@
 		}
 
 		let query = e.target.value.trim();
-		this.setMessage(true, `Searching for "${query}" in ${field.plural??'items'}`);
+		this.setMessage(field,true, `Searching for "${query}" in ${field.plural??'items'}`);
 		window.debouncer.schedule(
 			`${fieldId}-search`,
 			async () => {
@@ -390,7 +390,7 @@
 			return;
 		}
 		this.activeField = fieldId;
-		this.setMessage(true, `Loading ${field.plural}...`);
+		this.setMessage(field,true, `Loading ${field.plural}...`);
 		this.resetFilters({taxonomy: field.taxonomy});
 	}
 
@@ -434,6 +434,8 @@
 		const field = this.fields.get(fieldId);
 		if (!field) return;
 		if (!field.hasAutocomplete || this.container.open) return;
+		if (e.target.closest('.remove-item')) return;
+
 		if (e.relatedTarget && field.ui.dropdown.wrapper?.contains(e.relatedTarget)) return;
 
 		this.scheduleHideDropdown(fieldId);
@@ -702,7 +704,7 @@
 
 		let selectors = this.selectors.field;
 		const isFilter = Object.hasOwn(element.dataset,'filter') && element.dataset.filter === 'taxonomy';
-		let button = (isFilter) ? element : element.querySelector('button.taxonomy-toggle');
+		let button = (isFilter) ? element : element.querySelector('button.selector-toggle');
 
 		if (Object.keys(options).length === 0){
 			if (!button) return;
@@ -899,7 +901,7 @@
 			if (this.store.filters.page??1 === 1) {
 				window.removeChildren(this.ui.terms.list);
 			}
-			this.setMessage(true, this.store.filters.search === ''
+			this.setMessage(field,true, this.store.filters.search === ''
 				? `No matching ${field.plural}.`
 				: `No ${field.plural} found.`,
 				false);
@@ -909,7 +911,7 @@
 			return;
 		}
 
-		this.setCreateButton(true);
+		this.setCreateButton(field,true);
 
 		if (this.ui.terms.sentinel) {
 			if (this.store.lastResponse?.has_more) {
@@ -930,7 +932,7 @@
 		).then(()=>{});
 
 		if (terms.length > 0) {
-			this.setMessage(false);
+			this.setMessage(field,false);
 		}
 	}
 	createTermElement(term) {
@@ -946,7 +948,7 @@
 
 		window.removeChildren(dropdown);
 		if (terms.length === 0) {
-			this.setMessage(true, `No ${field.plural} found.`, false);
+			this.setMessage(field,true, `No ${field.plural} found.`, false);
 		} else {
 			window.chunkIt(
 				terms,
@@ -954,9 +956,9 @@
 				(fragment) => dropdown.append(fragment)
 			).then(()=>{});
 
-			this.setMessage(false);
+			this.setMessage(field,false);
 		}
-		this.setCreateButton(true);
+		this.setCreateButton(field,true);
 
 		if (field.ui.dropdown.wrapper) {
 			field.ui.dropdown.wrapper.hidden = false;
@@ -1084,7 +1086,6 @@
 			handlers[event]?.(data);
 		} catch (error) {
 			console.error(`Error handling store event "${event}":`, error);
-			this.setMessage(true, 'An error occurred loading data', false);
 		}
 	}
 	handleDataLoaded() {
@@ -1103,11 +1104,9 @@
 			this.showResults(true);
 			return;
 		}
-		this.setMessage(false);
 	}
 
 	showResults(isAutoComplete = false) {
-		this.setMessage(false);
 		const terms = this.store.getFiltered();
 		const filters = this.store.filters;
 		const isSearch = filters.search && filters.search.length > 0;
@@ -1120,7 +1119,7 @@
 		if (!this.activeField && isAutoComplete) {
 			return;
 		}
-
+		this.setMessage(this.currentField(), false);
 		if (isAutoComplete) {
 			this.showAutocompleteTerms();
 		} else {
@@ -1136,11 +1135,31 @@
 
 	handleFetchError(error) {
 		const field = this.currentField();
-		const message = field
-			? `Failed to load ${field.plural}`
-			: 'Failed to load data';
+		this.setMessage(field, true, 'Something went wrong.', false);
 
-		this.setMessage(true, message, false);
+		const conf = this.container.open || field?.isFilter ? this.ui : field?.ui;
+		const p = conf?.message?.message;
+
+		if (p && !p.querySelector('.clear-cache-btn')) {
+			const btn = document.createElement('button');
+			btn.className = 'clear-cache-btn';
+			btn.type = 'button';
+			btn.textContent = 'Clear cache and try again';
+			btn.addEventListener('click', async () => {
+				btn.remove();
+				this.store.clearCache();
+				if (this.activeField && field) {
+					await this.store.setFilters({
+						taxonomy: field.taxonomy,
+						page: 1,
+						search: '',
+						parent: 0
+					});
+				}
+			});
+			p.appendChild(btn);
+		}
+
 		console.error('Store fetch error:', error);
 	}
 	async batchFetchTaxonomies() {
@@ -1171,14 +1190,14 @@
 	/**************************************************
 	 LOADING
 	**************************************************/
-	setCreateButton(show = true) {
-		const field = this.currentField();
-		if (!field || !field.canCreate || !this.creator) return;
+	setCreateButton(field, show = true) {
+		if (!field.canCreate || !this.creator) return;
 
 		const conf = (this.container.open) ? this.ui : field.ui;
 		if (!conf.create?.button || !conf.create?.span) return;
 
 		const createButton = conf.create.button;
+		createButton.hidden = !show;
 		const buttonSpan = conf.create.span;
 		const input = (this.container.open) ? conf.search.input : conf.search;
 		if (!input) return;
@@ -1212,8 +1231,8 @@
 		}
 
 		if (data.parent !== undefined && data.name) {
-			this.setMessage(true, `Creating "${data.name}"...`);
-			this.setCreateButton(false);
+			this.setMessage(field,true, `Creating "${data.name}"...`);
+			this.setCreateButton(field,false);
 
 			if (this.container.open) {
 				window.removeChildren(this.ui.terms.list);
@@ -1228,7 +1247,7 @@
 
 			if (term) {
 				// Stop any typeLoop animation and show success message WITHOUT typeLoop
-				this.setMessage(true, `"${term.name}" created!`, false);
+				this.setMessage(field,true, `"${term.name}" created!`, false);
 
 				this.addSelected(term.id, field.id);
 				this.updateFieldValue(field.id);
@@ -1242,10 +1261,10 @@
 					}
 				}
 				this.scheduleHideDropdown(field.id, 300);
-				this.setMessage(false);
+				this.setMessage(field,false);
 			} else {
 				// Creation failed - hide immediately
-				this.setMessage(false);
+				this.setMessage(field,false);
 				if (!this.container.open && field.ui.dropdown.wrapper) {
 					field.ui.dropdown.wrapper.hidden = true;
 				}
@@ -1257,10 +1276,7 @@
 			}
 		}
 	}
-	setMessage(show = true, message = '', type = true) {
-		const field = this.currentField();
-		if (!field) return;
-
+	setMessage(field, show = true, message = '', type = true) {
 		const conf = this.container.open||field.isFilter ? this.ui : (field.isFilter ? null : field.ui);
 		if (!conf?.message?.message) return;
 

--
Gitblit v1.10.0