Spaces:
Runtime error
Runtime error
File size: 11,785 Bytes
afc0068 | 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 | /**
* AutoComplete Manager Module
* Handles intelligent auto-suggestions and field auto-completion
*/
export class AutoCompleteManager {
constructor(apiClient, uiManager) {
this.apiClient = apiClient;
this.uiManager = uiManager;
this.searchTimeouts = {};
this.activeDropdowns = new Set();
this.selectedIndex = -1;
this.availableTreeCodes = [];
}
async initialize() {
try {
this.availableTreeCodes = await this.apiClient.loadTreeCodes();
this.setupAutocomplete('localName', 'tree-suggestions');
this.setupAutocomplete('scientificName', 'tree-suggestions');
this.setupAutocomplete('commonName', 'tree-suggestions');
this.setupAutocomplete('treeCode', 'tree-codes');
} catch (error) {
console.error('Error initializing auto-suggestions:', error);
}
}
setupAutocomplete(fieldId, apiType) {
const input = document.getElementById(fieldId);
if (!input) return;
this.createAutocompleteContainer(input, fieldId);
this.attachEventListeners(input, fieldId, apiType);
}
createAutocompleteContainer(input, fieldId) {
if (input.parentElement.classList.contains('autocomplete-container')) return;
const container = document.createElement('div');
container.className = 'autocomplete-container';
input.parentNode.insertBefore(container, input);
container.appendChild(input);
const dropdown = document.createElement('div');
dropdown.className = 'autocomplete-dropdown';
dropdown.id = `${fieldId}-dropdown`;
container.appendChild(dropdown);
}
attachEventListeners(input, fieldId, apiType) {
input.addEventListener('input', (e) => this.handleInputChange(e, apiType));
input.addEventListener('keydown', (e) => this.handleKeyDown(e, fieldId));
input.addEventListener('blur', (e) => this.handleInputBlur(e, fieldId));
input.addEventListener('focus', (e) => this.handleInputFocus(e, fieldId, apiType));
}
async handleInputChange(event, apiType) {
const input = event.target;
const query = input.value.trim();
const fieldId = input.id;
this.clearSearchTimeout(fieldId);
if (query.length < 2) {
this.hideDropdown(fieldId);
return;
}
this.showLoadingState(fieldId);
this.debounceSearch(fieldId, query, apiType);
}
clearSearchTimeout(fieldId) {
if (this.searchTimeouts[fieldId]) {
clearTimeout(this.searchTimeouts[fieldId]);
}
}
debounceSearch(fieldId, query, apiType) {
this.searchTimeouts[fieldId] = setTimeout(async () => {
try {
const suggestions = await this.fetchSuggestions(query, apiType, fieldId);
this.showSuggestions(fieldId, suggestions, query);
} catch (error) {
console.error('Error fetching suggestions:', error);
this.hideDropdown(fieldId);
}
}, 300);
}
async fetchSuggestions(query, apiType, fieldId) {
if (apiType === 'tree-codes') {
return this.filterTreeCodes(query);
} else {
return this.searchTreeSuggestions(query, fieldId);
}
}
filterTreeCodes(query) {
return this.availableTreeCodes
.filter(code => code.toLowerCase().includes(query.toLowerCase()))
.slice(0, 10)
.map(code => ({
primary: code,
secondary: 'Tree Reference Code',
type: 'code'
}));
}
async searchTreeSuggestions(query, fieldId) {
const suggestions = await this.apiClient.searchTreeSuggestions(query, 10);
return suggestions.map(suggestion => ({
primary: this.getPrimaryText(suggestion, fieldId),
secondary: this.getSecondaryText(suggestion, fieldId),
badges: this.getBadges(suggestion),
data: suggestion
}));
}
getPrimaryText(suggestion, fieldId) {
const fieldMap = {
'localName': suggestion.local_name,
'scientificName': suggestion.scientific_name,
'commonName': suggestion.common_name
};
return fieldMap[fieldId] ||
suggestion.local_name ||
suggestion.scientific_name ||
suggestion.common_name;
}
getSecondaryText(suggestion, fieldId) {
const parts = [];
if (fieldId !== 'localName' && suggestion.local_name) {
parts.push(`Local: ${suggestion.local_name}`);
}
if (fieldId !== 'scientificName' && suggestion.scientific_name) {
parts.push(`Scientific: ${suggestion.scientific_name}`);
}
if (fieldId !== 'commonName' && suggestion.common_name) {
parts.push(`Common: ${suggestion.common_name}`);
}
if (suggestion.tree_code) {
parts.push(`Code: ${suggestion.tree_code}`);
}
return parts.join(' • ');
}
getBadges(suggestion) {
const badges = [];
if (suggestion.tree_code) {
badges.push(suggestion.tree_code);
}
if (suggestion.fruiting_season) {
badges.push(`Season: ${suggestion.fruiting_season}`);
}
return badges;
}
showLoadingState(fieldId) {
const dropdown = document.getElementById(`${fieldId}-dropdown`);
if (!dropdown) return;
dropdown.innerHTML = '<div class="autocomplete-loading">Searching...</div>';
dropdown.style.display = 'block';
this.activeDropdowns.add(fieldId);
}
showSuggestions(fieldId, suggestions, query) {
const dropdown = document.getElementById(`${fieldId}-dropdown`);
if (!dropdown) return;
if (suggestions.length === 0) {
dropdown.innerHTML = '<div class="autocomplete-no-results">No matching suggestions found</div>';
dropdown.style.display = 'block';
this.activeDropdowns.add(fieldId);
return;
}
const html = this.buildSuggestionsHTML(suggestions, query, fieldId);
dropdown.innerHTML = html;
dropdown.style.display = 'block';
this.activeDropdowns.add(fieldId);
this.selectedIndex = -1;
this.attachSuggestionClickHandlers(dropdown, suggestions);
}
buildSuggestionsHTML(suggestions, query, fieldId) {
return suggestions.map((suggestion, index) => `
<div class="autocomplete-item" data-index="${index}" data-field="${fieldId}">
<div class="autocomplete-primary">${this.highlightMatch(suggestion.primary, query)}</div>
${suggestion.secondary ? `<div class="autocomplete-secondary">${suggestion.secondary}</div>` : ''}
${this.buildBadgesHTML(suggestion.badges)}
</div>
`).join('');
}
buildBadgesHTML(badges) {
if (!badges || badges.length === 0) return '';
const badgeElements = badges.map(badge => `<span class="autocomplete-badge">${badge}</span>`).join('');
return `<div>${badgeElements}</div>`;
}
highlightMatch(text, query) {
if (!query || !text) return text;
const regex = new RegExp(`(${query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`, 'gi');
return text.replace(regex, '<strong>$1</strong>');
}
attachSuggestionClickHandlers(dropdown, suggestions) {
dropdown.querySelectorAll('.autocomplete-item').forEach(item => {
item.addEventListener('mousedown', (e) => this.handleSuggestionClick(e, suggestions));
});
}
handleSuggestionClick(event, suggestions) {
event.preventDefault();
const item = event.target.closest('.autocomplete-item');
const index = parseInt(item.dataset.index);
const fieldId = item.dataset.field;
const suggestion = suggestions[index];
this.applySuggestion(fieldId, suggestion);
this.hideDropdown(fieldId);
}
applySuggestion(fieldId, suggestion) {
const input = document.getElementById(fieldId);
if (suggestion.type === 'code') {
input.value = suggestion.primary;
} else {
this.applySuggestionData(input, fieldId, suggestion);
this.autoFillRelatedFields(suggestion.data, fieldId);
}
input.dispatchEvent(new Event('input', { bubbles: true }));
}
applySuggestionData(input, fieldId, suggestion) {
const data = suggestion.data;
const fieldValueMap = {
'localName': data.local_name,
'scientificName': data.scientific_name,
'commonName': data.common_name
};
input.value = fieldValueMap[fieldId] || suggestion.primary;
}
autoFillRelatedFields(data, excludeFieldId) {
const fields = {
'localName': data.local_name,
'scientificName': data.scientific_name,
'commonName': data.common_name,
'treeCode': data.tree_code
};
Object.entries(fields).forEach(([fieldId, value]) => {
if (fieldId !== excludeFieldId && value) {
this.fillEmptyField(fieldId, value);
}
});
}
fillEmptyField(fieldId, value) {
const input = document.getElementById(fieldId);
if (input && !input.value.trim()) {
input.value = value;
this.uiManager.highlightAutoFilledField(fieldId);
}
}
handleKeyDown(event, fieldId) {
const dropdown = document.getElementById(`${fieldId}-dropdown`);
if (!dropdown || dropdown.style.display === 'none') return;
const items = dropdown.querySelectorAll('.autocomplete-item');
if (items.length === 0) return;
switch (event.key) {
case 'ArrowDown':
event.preventDefault();
this.selectedIndex = Math.min(this.selectedIndex + 1, items.length - 1);
this.updateHighlight(items);
break;
case 'ArrowUp':
event.preventDefault();
this.selectedIndex = Math.max(this.selectedIndex - 1, -1);
this.updateHighlight(items);
break;
case 'Enter':
event.preventDefault();
if (this.selectedIndex >= 0 && items[this.selectedIndex]) {
items[this.selectedIndex].click();
}
break;
case 'Escape':
event.preventDefault();
this.hideDropdown(fieldId);
break;
}
}
updateHighlight(items) {
items.forEach((item, index) => {
item.classList.toggle('highlighted', index === this.selectedIndex);
});
}
handleInputBlur(event, fieldId) {
setTimeout(() => this.hideDropdown(fieldId), 150);
}
handleInputFocus(event, fieldId, apiType) {
const input = event.target;
if (input.value.length >= 2) {
this.handleInputChange(event, apiType);
}
}
hideDropdown(fieldId) {
const dropdown = document.getElementById(`${fieldId}-dropdown`);
if (dropdown) {
dropdown.style.display = 'none';
dropdown.innerHTML = '';
this.activeDropdowns.delete(fieldId);
this.selectedIndex = -1;
}
}
hideAllDropdowns() {
this.activeDropdowns.forEach(fieldId => {
this.hideDropdown(fieldId);
});
}
}
|