{"version":3,"names":["Object","defineProperty","exports","value","OebbAutocomplete","input","list","this","useFirstItemOnEnter","showPanelOnFocus","isPanelAlwaysOpen","scrollSpaceTop","isListOpen","hasFocus","activeElementIndex","randId","Math","random","toString","substr","data","initDom","initListeners","render","prototype","setAttribute","template","defaultTemplate","activeCallback","defaultActive","selectCallback","defaultSelect","element","document","createElement","classList","add","style","position","parentNode","insertBefore","appendChild","display","addAriaAttributesOnInput","addAriaAttributesOnList","id","trim","console","warn","inputLabel","label","querySelector","refreshAriaAttributesOnItems","items","length","i","refreshAriaAttributesOnInput","toggleAriaAttributeSelected","selected","getAttribute","_this","addEventListener","handleFocus","handleBlur","e","handleKeydown","handleKeyup","preventDefault","handleItemClick","withTemplate","withFirstItemOnEnter","withPanelOnFocus","withPanelAlwaysOpen","activate","withScrollSpaceTop","onKeyup","keyupCallback","onKeydown","keydownCallback","onItemActive","onItemSelect","setData","getData","setValue","addValue","valueParts","split","pop","push","join","getValue","getOriginalValue","originalValue","getItems","getActiveElementIndex","lastKeyCode","hideList","keyCode","tabIndex","stopPropagation","remove","scrollTop","isItemInListView","y","offsetTop","clientHeight","target","item","closest","Array","slice","call","querySelectorAll","index","indexOf","focus","rectList","getBoundingClientRect","rectItem","top","height","removeAttribute","listHtml","innerHTML","forEach","FilterMode","valueLast","toLowerCase","substring","textContent","createOebbAutocompleteFromStringArray","rawData","filterMode","StartsWith","Contains","filter","option","createOebbAutocompleteFromRemoteStringArray","url","options","paramKey","suggestionsKey","debounceTiming","origin","debounceTimer","clearTimeout","setTimeout","fetch","encodeURI","then","response","json","catch","error","createOebbAutocompleteFromRemoteStringArrayWithTopResults","suggestionsLabel","topResultsKey","topResultsLabel","typeMapping","group","indexGroup","groupHtml","indexItem","title","resultData","itemIndex","itemElement","dataset","window","location","href","addOebbautocompleteDefaultStyle","tint","head"],"sources":["node_modules/@oebb-web-team/oebb-web-components/node_modules/@oebb-web-team/oebb-web-library/dist/autocomplete/OebbAutocomplete.js"],"sourcesContent":["\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nvar OebbAutocomplete = /** @class */ (function () {\n function OebbAutocomplete(input, list) {\n this.useFirstItemOnEnter = false;\n this.showPanelOnFocus = false;\n this.isPanelAlwaysOpen = false;\n this.scrollSpaceTop = 0;\n this.isListOpen = false;\n this.hasFocus = false;\n this.activeElementIndex = -1;\n this.randId = Math.random().toString(36).substr(2, 6);\n this.data = [];\n this.input = input;\n this.list = list;\n this.initDom();\n this.initListeners();\n if (this.isPanelAlwaysOpen) {\n this.render();\n }\n }\n OebbAutocomplete.prototype.initDom = function () {\n this.input.setAttribute('autocomplete', 'off');\n this.template = defaultTemplate;\n this.activeCallback = defaultActive;\n this.selectCallback = defaultSelect;\n this.element = document.createElement('div');\n this.element.classList.add('oebb-autocomplete-wrapper');\n this.element.style.position = 'relative';\n this.input.parentNode.insertBefore(this.element, this.input);\n this.element.appendChild(this.input);\n if (!this.list) {\n this.list = document.createElement('ul');\n this.element.appendChild(this.list);\n }\n this.list.classList.add('oebb-autocomplete-list');\n this.list.classList.add('js-oebb-autocomplete-list');\n this.list.style.display = 'none';\n this.addAriaAttributesOnInput();\n this.addAriaAttributesOnList();\n };\n OebbAutocomplete.prototype.addAriaAttributesOnInput = function () {\n if (!this.input.id || this.input.id.trim() == '')\n console.warn('No attribute \"id\" on autocomplete input found.');\n this.input.setAttribute('role', 'combobox');\n this.input.setAttribute('aria-autocomplete', 'list');\n this.input.setAttribute('aria-controls', 'oebb-autocomplete-list-' + this.randId);\n this.input.setAttribute('aria-expanded', 'false');\n this.input.setAttribute('aria-activedescendant', '');\n };\n OebbAutocomplete.prototype.addAriaAttributesOnList = function (inputLabel) {\n var label = (inputLabel !== null && inputLabel !== void 0 ? inputLabel : document.querySelector(\"[for=\\\"\" + this.input.id + \"\\\"]\"));\n if (!label)\n return;\n label.setAttribute('id', 'oebb-autocomplete-label-' + this.randId);\n this.list.setAttribute('id', 'oebb-autocomplete-list-' + this.randId);\n this.list.setAttribute('role', 'listbox');\n this.list.setAttribute('aria-labelledby', 'oebb-autocomplete-label-' + this.randId);\n };\n OebbAutocomplete.prototype.refreshAriaAttributesOnItems = function () {\n if (this.items.length == 0)\n return;\n for (var i = 0; i < this.items.length; i++) {\n this.items[i].setAttribute('id', 'oebb-autocomplete-item-' + this.randId + '-' + i);\n this.items[i].setAttribute('role', 'option');\n this.items[i].setAttribute('aria-selected', 'false');\n }\n };\n OebbAutocomplete.prototype.refreshAriaAttributesOnInput = function () {\n if (this.isListOpen) {\n this.input.setAttribute('aria-expanded', 'true');\n }\n else {\n this.input.setAttribute('aria-expanded', 'false');\n }\n if (this.activeElementIndex > -1) {\n this.input.setAttribute('aria-activedescendant', 'oebb-autocomplete-item-' + this.randId + '-' + this.activeElementIndex);\n }\n else {\n this.input.setAttribute('aria-activedescendant', '');\n }\n };\n OebbAutocomplete.prototype.toggleAriaAttributeSelected = function (i) {\n if (i < 0)\n return;\n this.input.setAttribute('aria-activedescendant', 'oebb-autocomplete-item-' + this.randId + '-' + i);\n var selected = this.items[i].getAttribute('aria-selected');\n if (selected === null || selected === 'false') {\n this.items[i].setAttribute('aria-selected', 'true');\n }\n else {\n this.items[i].setAttribute('aria-selected', 'false');\n }\n };\n OebbAutocomplete.prototype.initListeners = function () {\n var _this = this;\n this.input.addEventListener('focus', function () { return _this.handleFocus(); }, false);\n this.input.addEventListener('blur', function () { return _this.handleBlur(); }, false);\n this.input.addEventListener('keydown', function (e) { return _this.handleKeydown(e); }, false);\n this.input.addEventListener('keyup', function (e) { return _this.handleKeyup(e); }, false);\n this.list.addEventListener('mousedown', function (e) { return e.preventDefault(); }, false); // Prevent list from closing on item click because of \"blur\" event\n this.list.addEventListener('click', function (e) { return _this.handleItemClick(e); }, false);\n };\n OebbAutocomplete.prototype.withTemplate = function (template) {\n this.template = template;\n return this;\n };\n OebbAutocomplete.prototype.withFirstItemOnEnter = function () {\n this.useFirstItemOnEnter = true;\n return this;\n };\n OebbAutocomplete.prototype.withPanelOnFocus = function () {\n this.showPanelOnFocus = true;\n return this;\n };\n OebbAutocomplete.prototype.withPanelAlwaysOpen = function (activate) {\n if (activate === void 0) { activate = true; }\n if (activate) {\n this.isPanelAlwaysOpen = true;\n }\n else {\n this.isPanelAlwaysOpen = false;\n }\n return this;\n };\n OebbAutocomplete.prototype.withScrollSpaceTop = function (scrollSpaceTop) {\n this.scrollSpaceTop = scrollSpaceTop;\n return this;\n };\n OebbAutocomplete.prototype.onKeyup = function (keyupCallback) {\n this.keyupCallback = keyupCallback;\n return this;\n };\n OebbAutocomplete.prototype.onKeydown = function (keydownCallback) {\n this.keydownCallback = keydownCallback;\n return this;\n };\n OebbAutocomplete.prototype.onItemActive = function (activeCallback) {\n this.activeCallback = activeCallback;\n return this;\n };\n OebbAutocomplete.prototype.onItemSelect = function (selectCallback) {\n this.selectCallback = selectCallback;\n return this;\n };\n OebbAutocomplete.prototype.setData = function (data) {\n this.data = data;\n };\n OebbAutocomplete.prototype.getData = function () {\n return this.data;\n };\n OebbAutocomplete.prototype.setValue = function (value) {\n this.value = value;\n this.input.value = value;\n };\n OebbAutocomplete.prototype.addValue = function (value) {\n var valueParts = this.input.value.split(/\\s+/);\n valueParts.pop();\n valueParts.push(value);\n this.value = valueParts.join(' ');\n ;\n this.input.value = this.value;\n };\n OebbAutocomplete.prototype.getValue = function () {\n return this.input.value;\n };\n OebbAutocomplete.prototype.getOriginalValue = function () {\n return this.originalValue;\n };\n OebbAutocomplete.prototype.getItems = function () {\n return this.items;\n };\n OebbAutocomplete.prototype.getActiveElementIndex = function () {\n return this.activeElementIndex;\n };\n OebbAutocomplete.prototype.handleFocus = function () {\n this.hasFocus = true;\n this.lastKeyCode = null;\n if (this.showPanelOnFocus)\n this.render();\n };\n OebbAutocomplete.prototype.handleBlur = function () {\n this.hasFocus = false;\n this.lastKeyCode = null;\n this.hideList();\n };\n OebbAutocomplete.prototype.handleKeydown = function (e) {\n this.toggleAriaAttributeSelected(this.activeElementIndex);\n if (this.activeElementIndex == -1)\n this.originalValue = this.getValue();\n switch (e.keyCode) {\n case 9:\n this.list.tabIndex = -1;\n return;\n case 13: // Enter\n if (this.isListOpen)\n e.preventDefault(); // Prevent form submit\n if (this.activeElementIndex > -1)\n this.selectCallback(this.activeElementIndex, e);\n if (this.useFirstItemOnEnter && this.isListOpen && this.data.length > 0 && this.activeElementIndex == -1)\n this.selectCallback(0, e);\n this.hideList();\n break;\n case 27: // ESC\n if (this.activeElementIndex > -1)\n e.stopPropagation();\n this.hideList();\n this.setValue(this.originalValue);\n break;\n case 38: // Up\n if (this.activeElementIndex > -1)\n e.preventDefault();\n // Any element\n if (this.activeElementIndex > -1) {\n this.items[this.activeElementIndex].classList.remove('oebb-autocomplete-item--active');\n this.activeElementIndex--;\n }\n // Any element, still not first element\n if (this.activeElementIndex > -1)\n this.items[this.activeElementIndex].classList.add('oebb-autocomplete-item--active');\n // Scroll to top after first element, usefull if groups are used\n if (this.activeElementIndex == -1)\n this.list.scrollTop = 0;\n // If on element, fire activeCallback\n if (this.activeElementIndex == -1)\n this.setValue(this.originalValue);\n if (this.activeElementIndex > -1)\n this.activeCallback(this.activeElementIndex);\n break;\n case 40: // Down\n // List not open, no data\n if (this.data.length == 0)\n return;\n // List not open\n if (!this.isListOpen && this.data.length > 0) {\n this.activeElementIndex = -1;\n this.render();\n break;\n }\n // Last element\n if ((this.activeElementIndex + 1) == this.items.length)\n break;\n // Any element\n if (this.activeElementIndex > -1)\n this.items[this.activeElementIndex].classList.remove('oebb-autocomplete-item--active');\n this.activeElementIndex++;\n this.items[this.activeElementIndex].classList.add('oebb-autocomplete-item--active');\n // If on element, fire activeCallback\n if (this.activeElementIndex == -1)\n this.setValue(this.originalValue);\n if (this.activeElementIndex > -1)\n this.activeCallback(this.activeElementIndex);\n break;\n }\n this.toggleAriaAttributeSelected(this.activeElementIndex);\n this.refreshAriaAttributesOnInput();\n if (this.keydownCallback)\n this.keydownCallback(e);\n // No element\n if (this.activeElementIndex == -1)\n return;\n if (e.keyCode == 38 && this.isListOpen && !this.isItemInListView()) {\n var y = this.items[this.activeElementIndex].offsetTop;\n this.list.scrollTop = y - this.scrollSpaceTop;\n }\n if (e.keyCode == 40 && this.isListOpen && !this.isItemInListView()) {\n var y = this.items[this.activeElementIndex].offsetTop - this.list.clientHeight + this.items[this.activeElementIndex].clientHeight;\n this.list.scrollTop = y;\n }\n };\n OebbAutocomplete.prototype.handleKeyup = function (e) {\n this.value = this.input.value;\n this.lastKeyCode = e.keyCode;\n if (e.keyCode === 13\n || e.keyCode === 27\n || e.keyCode === 38\n || e.keyCode === 40)\n return;\n this.keyupCallback(e);\n this.render();\n };\n OebbAutocomplete.prototype.handleItemClick = function (e) {\n e.stopPropagation();\n var target = e.target;\n var item = target.closest('li');\n var items = Array.prototype.slice.call(this.list.querySelectorAll('.js-oebb-autocomplete-item'));\n var index = items.indexOf(item);\n this.selectCallback(index, e);\n this.hideList();\n this.input.focus();\n };\n OebbAutocomplete.prototype.isItemInListView = function () {\n var rectList = this.list.getBoundingClientRect();\n var rectItem = this.items[this.activeElementIndex].getBoundingClientRect();\n return (rectItem.top >= rectList.top + this.scrollSpaceTop && rectItem.top <= rectList.top + rectList.height - rectItem.height); // top && bottom\n };\n OebbAutocomplete.prototype.hideList = function () {\n if (this.activeElementIndex > -1) {\n this.items[this.activeElementIndex].classList.remove('oebb-autocomplete-item--active');\n this.activeElementIndex = -1;\n }\n if (this.isPanelAlwaysOpen)\n return;\n if (!this.isListOpen)\n return;\n this.list.removeAttribute('tabindex');\n this.list.scrollTop = 0;\n this.list.style.display = 'none';\n this.isListOpen = false;\n this.refreshAriaAttributesOnInput();\n };\n OebbAutocomplete.prototype.render = function () {\n var _this = this;\n this.activeElementIndex = -1;\n this.list.removeAttribute('tabindex');\n this.list.scrollTop = 0;\n if (!this.isPanelAlwaysOpen) {\n if (!this.hasFocus)\n return this.hideList();\n if (this.data.length == 0)\n return this.hideList();\n if (!this.showPanelOnFocus && (!this.value || this.value == ''))\n return this.hideList();\n if (this.lastKeyCode === 13)\n return; // Prevents panel after submitting form because of suggestions via async request\n }\n var listHtml = \"\";\n this.list.innerHTML = \"\";\n this.data.forEach(function (item, i) {\n listHtml += _this.template(item, i);\n });\n this.list.innerHTML = listHtml;\n this.list.style.display = 'block';\n this.isListOpen = true;\n this.items = this.list.querySelectorAll('.js-oebb-autocomplete-item');\n // this.refreshItemListener();\n this.refreshAriaAttributesOnInput();\n this.refreshAriaAttributesOnItems();\n };\n return OebbAutocomplete;\n}());\nexports.OebbAutocomplete = OebbAutocomplete;\nvar FilterMode;\n(function (FilterMode) {\n FilterMode[\"StartsWith\"] = \"STARTS_WITH\";\n FilterMode[\"Contains\"] = \"CONTAINS\";\n})(FilterMode || (FilterMode = {}));\n;\nvar defaultTemplate = function (item) {\n var valueParts = this.input.value.split(/\\s+/);\n var valueLast = valueParts.pop().toString();\n if (item.toLowerCase().indexOf(valueLast.toLowerCase()) === 0) {\n return \"