273 lines
11 KiB
JavaScript
273 lines
11 KiB
JavaScript
/**
|
|
* Auswahlfeld fuer Lagerhaus-Gegenstaende: Suchfeld mit aufklappender Liste (Bild, Name,
|
|
* Kategorie, Bestand) statt Datalist oder Dropdown. Tastatur: Pfeile, Enter, Escape.
|
|
*
|
|
* Markup: <div class="item-picker" data-picker-items="<id eines JSON-Scripts>" data-picker-mode="name|id|submit">
|
|
* name - der Text selbst wird gesendet (freie Eingabe erlaubt, z.B. neue Gegenstaende)
|
|
* id - ein verstecktes Feld bekommt die ID; nur Eintraege aus der Liste sind gueltig
|
|
* submit - wie name, schickt nach der Auswahl das Formular ab (Suche im Verlauf)
|
|
* Optional data-picker-empty: Text, wenn nichts passt.
|
|
* Von aussen: element.itemPicker.set(idOderName) und element.itemPicker.refresh().
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
|
|
var MAX_SHOWN = 60;
|
|
|
|
function norm(value) {
|
|
return String(value || '').toLowerCase().normalize('NFD').replace(/[̀-ͯ]/g, '');
|
|
}
|
|
|
|
function formatQty(qty) {
|
|
return Number(qty).toLocaleString('de-DE');
|
|
}
|
|
|
|
// Name mit hervorgehobenem Suchbegriff (als Textknoten, kein innerHTML mit Benutzerdaten)
|
|
function highlighted(name, query) {
|
|
var span = document.createElement('span');
|
|
span.className = 'item-picker-name';
|
|
var at = query ? norm(name).indexOf(query) : -1;
|
|
if (at === -1) {
|
|
span.textContent = name;
|
|
return span;
|
|
}
|
|
var mark = document.createElement('mark');
|
|
mark.textContent = name.slice(at, at + query.length);
|
|
span.appendChild(document.createTextNode(name.slice(0, at)));
|
|
span.appendChild(mark);
|
|
span.appendChild(document.createTextNode(name.slice(at + query.length)));
|
|
return span;
|
|
}
|
|
|
|
function thumb(item) {
|
|
if (item.icon) {
|
|
var img = document.createElement('img');
|
|
img.className = 'item-picker-thumb';
|
|
img.src = item.icon;
|
|
img.alt = '';
|
|
img.loading = 'lazy';
|
|
return img;
|
|
}
|
|
var empty = document.createElement('span');
|
|
empty.className = 'item-picker-thumb item-picker-thumb-empty';
|
|
empty.textContent = item.name.charAt(0).toUpperCase();
|
|
return empty;
|
|
}
|
|
|
|
function ItemPicker(root) {
|
|
var source = document.getElementById(root.dataset.pickerItems);
|
|
var input = root.querySelector('.item-picker-input');
|
|
if (!source || !input) return;
|
|
|
|
var items = JSON.parse(source.textContent || '[]');
|
|
items.forEach(function (item) { item.key = norm(item.name); item.catKey = norm(item.category); });
|
|
|
|
var mode = root.dataset.pickerMode || 'name';
|
|
var hidden = root.querySelector('input[type="hidden"]');
|
|
var iconSlot = root.querySelector('.item-picker-icon');
|
|
var searchIcon = iconSlot ? iconSlot.innerHTML : '';
|
|
var emptyText = root.dataset.pickerEmpty || 'Kein Gegenstand gefunden.';
|
|
|
|
var list = document.createElement('ul');
|
|
list.className = 'item-picker-list';
|
|
list.id = (input.id || 'item-picker-' + Math.random().toString(36).slice(2)) + '-list';
|
|
list.setAttribute('role', 'listbox');
|
|
list.hidden = true;
|
|
document.body.appendChild(list);
|
|
|
|
input.setAttribute('role', 'combobox');
|
|
input.setAttribute('aria-autocomplete', 'list');
|
|
input.setAttribute('aria-expanded', 'false');
|
|
input.setAttribute('aria-controls', list.id);
|
|
input.setAttribute('autocomplete', 'off');
|
|
|
|
var matches = [];
|
|
var active = -1;
|
|
|
|
function findItem(value) {
|
|
var key = norm(String(value).trim());
|
|
for (var i = 0; i < items.length; i++) {
|
|
if (String(items[i].id) === String(value) || items[i].key === key) return items[i];
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function refresh() {
|
|
var item = findItem(input.value);
|
|
if (iconSlot) {
|
|
iconSlot.innerHTML = '';
|
|
if (item && item.icon) {
|
|
var img = document.createElement('img');
|
|
img.className = 'icon item-picker-preview';
|
|
img.src = item.icon;
|
|
img.alt = '';
|
|
iconSlot.appendChild(img);
|
|
} else {
|
|
iconSlot.innerHTML = searchIcon;
|
|
}
|
|
}
|
|
if (mode === 'id' && hidden) {
|
|
var id = item ? String(item.id) : '';
|
|
if (hidden.value !== id) {
|
|
hidden.value = id;
|
|
hidden.dataset.max = item && item.qty != null ? item.qty : '';
|
|
hidden.dispatchEvent(new Event('change'));
|
|
}
|
|
input.setCustomValidity(id || !input.required ? '' : 'Bitte einen Gegenstand aus der Liste wählen.');
|
|
}
|
|
}
|
|
|
|
function position() {
|
|
var rect = input.getBoundingClientRect();
|
|
var below = window.innerHeight - rect.bottom;
|
|
list.style.left = rect.left + 'px';
|
|
list.style.width = rect.width + 'px';
|
|
if (below < 240 && rect.top > below) {
|
|
list.style.top = 'auto';
|
|
list.style.bottom = (window.innerHeight - rect.top + 6) + 'px';
|
|
list.style.maxHeight = Math.min(340, rect.top - 12) + 'px';
|
|
} else {
|
|
list.style.bottom = 'auto';
|
|
list.style.top = (rect.bottom + 6) + 'px';
|
|
list.style.maxHeight = Math.min(340, below - 12) + 'px';
|
|
}
|
|
}
|
|
|
|
function render() {
|
|
var query = norm(input.value.trim());
|
|
var starts = [];
|
|
var contains = [];
|
|
items.forEach(function (item) {
|
|
if (!query) contains.push(item);
|
|
else if (item.key.indexOf(query) === 0) starts.push(item);
|
|
else if (item.key.indexOf(query) !== -1 || item.catKey.indexOf(query) !== -1) contains.push(item);
|
|
});
|
|
var all = starts.concat(contains);
|
|
matches = all.slice(0, MAX_SHOWN);
|
|
active = matches.length && query ? 0 : -1;
|
|
|
|
list.innerHTML = '';
|
|
matches.forEach(function (item, index) {
|
|
var li = document.createElement('li');
|
|
li.className = 'item-picker-option';
|
|
li.id = list.id + '-' + index;
|
|
li.setAttribute('role', 'option');
|
|
li.appendChild(thumb(item));
|
|
var text = document.createElement('span');
|
|
text.className = 'item-picker-text';
|
|
text.appendChild(highlighted(item.name, query));
|
|
var meta = document.createElement('span');
|
|
meta.className = 'item-picker-meta';
|
|
meta.textContent = item.category;
|
|
text.appendChild(meta);
|
|
li.appendChild(text);
|
|
if (item.qty != null) {
|
|
var qty = document.createElement('span');
|
|
qty.className = 'item-picker-qty';
|
|
qty.textContent = formatQty(item.qty) + ' auf Lager';
|
|
li.appendChild(qty);
|
|
}
|
|
li.addEventListener('mousedown', function (event) { event.preventDefault(); });
|
|
li.addEventListener('click', function () { choose(item); });
|
|
list.appendChild(li);
|
|
});
|
|
|
|
if (!matches.length) {
|
|
var none = document.createElement('li');
|
|
none.className = 'item-picker-note';
|
|
none.textContent = emptyText;
|
|
list.appendChild(none);
|
|
} else if (all.length > matches.length) {
|
|
var more = document.createElement('li');
|
|
more.className = 'item-picker-note';
|
|
more.textContent = '… und ' + (all.length - matches.length) + ' weitere - weiter tippen zum Eingrenzen';
|
|
list.appendChild(more);
|
|
}
|
|
highlight();
|
|
}
|
|
|
|
function highlight() {
|
|
Array.prototype.forEach.call(list.querySelectorAll('.item-picker-option'), function (li, index) {
|
|
var on = index === active;
|
|
li.classList.toggle('active', on);
|
|
li.setAttribute('aria-selected', String(on));
|
|
if (on) li.scrollIntoView({ block: 'nearest' });
|
|
});
|
|
if (active >= 0) input.setAttribute('aria-activedescendant', list.id + '-' + active);
|
|
else input.removeAttribute('aria-activedescendant');
|
|
}
|
|
|
|
function open() {
|
|
render();
|
|
list.hidden = false;
|
|
input.setAttribute('aria-expanded', 'true');
|
|
position();
|
|
window.addEventListener('scroll', position, true);
|
|
window.addEventListener('resize', position);
|
|
}
|
|
|
|
function close() {
|
|
list.hidden = true;
|
|
input.setAttribute('aria-expanded', 'false');
|
|
window.removeEventListener('scroll', position, true);
|
|
window.removeEventListener('resize', position);
|
|
}
|
|
|
|
function choose(item, silent) {
|
|
input.value = item.name;
|
|
refresh();
|
|
close();
|
|
input.dispatchEvent(new Event('change', { bubbles: true }));
|
|
if (!silent && mode === 'submit' && input.form) input.form.submit();
|
|
}
|
|
|
|
input.addEventListener('input', function () {
|
|
refresh();
|
|
open();
|
|
});
|
|
input.addEventListener('click', function () {
|
|
if (list.hidden) open();
|
|
});
|
|
input.addEventListener('change', refresh);
|
|
input.addEventListener('blur', close);
|
|
|
|
input.addEventListener('keydown', function (event) {
|
|
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
event.preventDefault();
|
|
if (list.hidden) { open(); return; }
|
|
if (!matches.length) return;
|
|
active = event.key === 'ArrowDown'
|
|
? (active + 1) % matches.length
|
|
: (active <= 0 ? matches.length - 1 : active - 1);
|
|
highlight();
|
|
} else if (event.key === 'Enter' && !list.hidden && active >= 0) {
|
|
event.preventDefault();
|
|
choose(matches[active]);
|
|
} else if (event.key === 'Escape' && !list.hidden) {
|
|
// Nur die Liste schliessen, nicht gleich den ganzen Dialog
|
|
event.stopPropagation();
|
|
close();
|
|
} else if (event.key === 'Tab') {
|
|
close();
|
|
}
|
|
});
|
|
|
|
// Vorbelegung (z.B. nach einem Fehler): ID im versteckten Feld -> Name anzeigen
|
|
if (mode === 'id' && hidden && hidden.value && !input.value) {
|
|
var initial = findItem(hidden.value);
|
|
if (initial) input.value = initial.name;
|
|
}
|
|
refresh();
|
|
|
|
root.itemPicker = {
|
|
set: function (value) {
|
|
var item = findItem(value);
|
|
if (item) choose(item, true);
|
|
else { input.value = value; refresh(); }
|
|
},
|
|
refresh: refresh
|
|
};
|
|
}
|
|
|
|
document.querySelectorAll('.item-picker').forEach(ItemPicker);
|
|
})();
|