{ "version": 3, "sources": ["../../../Vettvangur.Frontend/src/scripts/components/searchPopup.ts"], "sourcesContent": ["import TagManager from \"helpers/tag-manager\";\r\nimport { debounce } from \"utility/helpers\";\r\n\r\nconst SearchPopup = {\r\n\tel: {\r\n\t\troot: document.querySelector(\".search-popup\"),\r\n\t},\r\n\r\n\tinit() {\r\n\t\tif (this.el.root) {\r\n\t\t\tconst el = {\r\n\t\t\t\theaderSearchButton: document.querySelector(\".header__links--search\"),\r\n\t\t\t\tsearchPopup: document.querySelector(\".search-popup\"),\r\n\t\t\t\tpopupForm: document.querySelector(\r\n\t\t\t\t\t\".search-popup__form\",\r\n\t\t\t\t),\r\n\t\t\t\tsearchPopupInput: document.querySelector(\r\n\t\t\t\t\t\".search-popup__input input\",\r\n\t\t\t\t) as HTMLInputElement,\r\n\t\t\t\tsuggestionsContainer: document.querySelector(\r\n\t\t\t\t\t\".search-popup__suggestions-container\",\r\n\t\t\t\t),\r\n\t\t\t\tsuggestionsPopupResults: document.querySelector(\r\n\t\t\t\t\t\".search-popup__suggestions\",\r\n\t\t\t\t),\r\n\t\t\t\tsuggestionsPopupData: document.querySelector(\r\n\t\t\t\t\t\".search-popup-suggestion-data\",\r\n\t\t\t\t),\r\n\t\t\t\tlinkElement: document.querySelector(\r\n\t\t\t\t\t\".search-popup__link\",\r\n\t\t\t\t),\r\n\t\t\t\tlintTextElement: document.querySelector(\r\n\t\t\t\t\t\".search-popup-suggestion-data-total\",\r\n\t\t\t\t),\r\n\t\t\t};\r\n\r\n\t\t\tconst searchPagePath = el.linkElement\r\n\t\t\t\t.getAttribute(\"data-searchPageUrl\")\r\n\t\t\t\t.replaceAll(\"/\", \"\");\r\n\t\t\tconst currentPath = window.location.pathname.replaceAll(\"/\", \"\");\r\n\r\n\t\t\tif (searchPagePath === currentPath) {\r\n\t\t\t\treturn;\r\n\t\t\t}\r\n\r\n\t\t\tif (el.searchPopupInput) {\r\n\t\t\t\tthis.debounceInput(el);\r\n\r\n\t\t\t\tel.searchPopupInput.addEventListener(\"keyup\", () => {\r\n\t\t\t\t\t//* wait to call the submit after the user has finished typing\r\n\t\t\t\t\tthis.debounceInput(el);\r\n\t\t\t\t});\r\n\t\t\t}\r\n\r\n\t\t\tlet searchPopupIsOpen = false;\r\n\t\t\tlet isDesktop = false;\r\n\r\n\t\t\tif (window.innerWidth >= 1200) {\r\n\t\t\t\tisDesktop = true;\r\n\t\t\t}\r\n\r\n\t\t\twindow.addEventListener(\"resize\", () => {\r\n\t\t\t\tif (window.innerWidth >= 1200) {\r\n\t\t\t\t\tisDesktop = true;\r\n\t\t\t\t} else {\r\n\t\t\t\t\tisDesktop = false;\r\n\t\t\t\t\tsearchPopupIsOpen = this.onClose(el);\r\n\t\t\t\t}\r\n\t\t\t});\r\n\r\n\t\t\tel.searchPopup.addEventListener(\"keydown\", (e) => {\r\n\t\t\t\tconst { key } = e;\r\n\t\t\t\tconst searchPagePath =\r\n\t\t\t\t\tel.linkElement.getAttribute(\"data-searchPageUrl\");\r\n\r\n\t\t\t\tif (key === \"Escape\") {\r\n\t\t\t\t\tsearchPopupIsOpen = this.onClose(el);\r\n\t\t\t\t}\r\n\r\n\t\t\t\tif (key === \"Enter\") {\r\n\t\t\t\t\te.preventDefault();\r\n\t\t\t\t\tif (el.searchPopupInput.value.length >= 2) {\r\n\t\t\t\t\t\twindow.location.href = `${searchPagePath}?q=${el.searchPopupInput.value}`;\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t});\r\n\r\n\t\t\tdocument.addEventListener(\"click\", (event) => {\r\n\t\t\t\tconst withinSearchPopupBoundaries = event\r\n\t\t\t\t\t.composedPath()\r\n\t\t\t\t\t.includes(el.searchPopup);\r\n\t\t\t\tconst withinheaderSearchButtonBoundaries = event\r\n\t\t\t\t\t.composedPath()\r\n\t\t\t\t\t.includes(el.headerSearchButton);\r\n\r\n\t\t\t\tif (!isDesktop && withinheaderSearchButtonBoundaries) {\r\n\t\t\t\t\twindow.location.href =\r\n\t\t\t\t\t\tel.linkElement.getAttribute(\"data-searchPageUrl\");\r\n\t\t\t\t} else if (\r\n\t\t\t\t\tisDesktop &&\r\n\t\t\t\t\tsearchPopupIsOpen &&\r\n\t\t\t\t\t(!withinSearchPopupBoundaries || withinheaderSearchButtonBoundaries)\r\n\t\t\t\t) {\r\n\t\t\t\t\tsearchPopupIsOpen = this.onClose(el);\r\n\t\t\t\t} else if (\r\n\t\t\t\t\tisDesktop &&\r\n\t\t\t\t\t!searchPopupIsOpen &&\r\n\t\t\t\t\twithinheaderSearchButtonBoundaries\r\n\t\t\t\t) {\r\n\t\t\t\t\tsearchPopupIsOpen = this.onOpen(el);\r\n\t\t\t\t}\r\n\t\t\t});\r\n\r\n\t\t\tthis.submit(el);\r\n\t\t}\r\n\t},\r\n\r\n\tonOpen: (el) => {\r\n\t\tel.searchPopup.classList.add(\"search-popup--open\");\r\n\t\tdocument.body.classList.add(\"h-disable-scroll\");\r\n\t\tel.searchPopupInput.focus();\r\n\r\n\t\treturn true;\r\n\t},\r\n\r\n\tonClose: (el) => {\r\n\t\tel.searchPopup.classList.remove(\"search-popup--open\");\r\n\t\tdocument.body.classList.remove(\"h-disable-scroll\");\r\n\r\n\t\tel.searchPopupInput.value = \"\";\r\n\t\tSearchPopup.closeSuggestions(el);\r\n\r\n\t\treturn false;\r\n\t},\r\n\r\n\ttogglePopup: (el) => {\r\n\t\tif (el.searchPopup.classList.contains(\"search-popup--visible\")) {\r\n\t\t\tSearchPopup.onClose(el);\r\n\t\t\treturn;\r\n\t\t}\r\n\t\tSearchPopup.onOpen(el);\r\n\t},\r\n\r\n\tsendData: async (el) => {\r\n\t\tel.suggestionsPopupResults.innerHTML = \"\";\r\n\t\tconst action = el.popupForm.getAttribute(\"action\");\r\n\t\tconst lang = el.popupForm.getAttribute(\"data-lang\");\r\n\r\n\t\ttry {\r\n\t\t\t//* fetching search data\r\n\t\t\tconst response = await fetch(\r\n\t\t\t\t`${action}?q=${el.searchPopupInput.value}&culture=${lang}`,\r\n\t\t\t\t{\r\n\t\t\t\t\tmethod: el.popupForm.getAttribute(\"method\"),\r\n\t\t\t\t},\r\n\t\t\t);\r\n\r\n\t\t\tif (!response.ok) {\r\n\t\t\t\tconsole.error(\"Error fetching search data\");\r\n\t\t\t\treturn;\r\n\t\t\t}\r\n\r\n\t\t\tconst result = await response.json();\r\n\r\n\t\t\tif (result.length < 1) {\r\n\t\t\t\tSearchPopup.closeSuggestions(el);\r\n\t\t\t} else if (result && result.length > 0) {\r\n\t\t\t\tSearchPopup.openSuggestions(el, result);\r\n\t\t\t\tTagManager.fireSearchEvent(el.searchPopupInput.value);\r\n\t\t\t}\r\n\t\t} catch (el) {\r\n\t\t\tSearchPopup.closeSuggestions(el);\r\n\t\t}\r\n\t},\r\n\r\n\topenSuggestions: (el, result) => {\r\n\t\tSearchPopup.renderResultsDataLink(el, result.length);\r\n\t\tSearchPopup.renderSuggestions(el, result);\r\n\r\n\t\tif (el.suggestionsContainer) {\r\n\t\t\tel.suggestionsContainer.classList.add(\r\n\t\t\t\t\"search-popup__suggestions-container--open\",\r\n\t\t\t);\r\n\t\t}\r\n\t},\r\n\r\n\tcloseSuggestions: (el) => {\r\n\t\tel.suggestionsPopupResults.innerHTML = \"\";\r\n\r\n\t\tif (el.suggestionsContainer) {\r\n\t\t\tel.suggestionsContainer.classList.remove(\r\n\t\t\t\t\"search-popup__suggestions-container--open\",\r\n\t\t\t);\r\n\t\t}\r\n\t},\r\n\r\n\trenderResultsDataLink: (el, resultTotal) => {\r\n\t\tconst linkText = `${el.linkElement.getAttribute(\"data-searchPageUrl\")}?q=${el.searchPopupInput.value}`;\r\n\r\n\t\tel.linkElement.href = linkText;\r\n\t\tel.lintTextElement.innerHTML = resultTotal;\r\n\t},\r\n\r\n\trenderSuggestions: (el, result) => {\r\n\t\tconst suggestions = result.slice(0, 4);\r\n\r\n\t\tsuggestions.forEach((suggestion) => {\r\n\t\t\tel.suggestionsPopupResults.insertAdjacentHTML(\r\n\t\t\t\t\"beforeend\",\r\n\t\t\t\tSearchPopup.renderSuggestionItem(suggestion),\r\n\t\t\t);\r\n\t\t});\r\n\t},\r\n\r\n\trenderSuggestionItem: (suggestion) => {\r\n\t\tlet url = suggestion.url;\r\n\r\n\t\tif (suggestion.docAlias === \"employee\") {\r\n\t\t\turl = url.split(\"/\").slice(0, -2).join(\"/\");\r\n\t\t}\r\n\r\n\t\tif (suggestion.docAlias === \"faqItem\") {\r\n\t\t\tconst parts = url.split(\"/\").filter((part) => part);\r\n\t\t\tconst firstPart = parts[0] || null;\r\n\t\t\tconst allExceptLast = parts.slice(0, -1);\r\n\t\t\tconst resultWithoutLast = `/${allExceptLast.join(\"/\")}`;\r\n\r\n\t\t\turl =\r\n\t\t\t\tfirstPart === \"spurt-og-svarad\"\r\n\t\t\t\t\t? `${firstPart}#${suggestion.id}`\r\n\t\t\t\t\t: `${resultWithoutLast}#${suggestion.id}`;\r\n\t\t}\r\n\r\n\t\treturn `${suggestion.title}`;\r\n\t},\r\n\r\n\tdebounceInput: (el) => {\r\n\t\tconst debounceSubmit = debounce(() => SearchPopup.sendData(el));\r\n\t\tconst debounceClose = debounce(() => SearchPopup.closeSuggestions(el));\r\n\r\n\t\tif (el.searchPopupInput.value.length >= 2) {\r\n\t\t\tdebounceSubmit();\r\n\t\t} else {\r\n\t\t\tdebounceClose();\r\n\t\t}\r\n\t},\r\n\r\n\tsubmit(el) {\r\n\t\tel.popupForm.addEventListener(\r\n\t\t\t\"submit\",\r\n\t\t\t(e: Event & { target: HTMLInputElement }) => {\r\n\t\t\t\tTagManager.fireSearchEvent(e.target.value);\r\n\t\t\t},\r\n\t\t);\r\n\t},\r\n};\r\n\r\nexport default SearchPopup;\r\n"], "mappings": "wHAGA,IAAMA,EAAc,CACnB,GAAI,CACH,KAAM,SAAS,cAAc,eAAe,CAC7C,EAEA,MAAO,CACN,GAAI,KAAK,GAAG,KAAM,CACjB,IAAMC,EAAK,CACV,mBAAoB,SAAS,cAAc,wBAAwB,EACnE,YAAa,SA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AAC,EAEjEA,EAAG,iBAAiB,MAAM,QAAU,EACvCuB,EAAe,EAEfE,EAAc,CAEhB,EAEA,OAAOzB,EAAI,CACVA,EAAG,UAAU,iBACZ,SACCK,GAA4C,CAC5CS,EAAW,gBAAgBT,EAAE,OAAO,KAAK,CAC1C,CACD,CACD,CACD,EAEOqB,EAAQ3B", "names": ["SearchPopup", "el", "searchPagePath", "currentPath", "searchPopupIsOpen", "isDesktop", "e", "key", "event", "withinSearchPopupBoundaries", "withinheaderSearchButtonBoundaries", "action", "lang", "response", "result", "tag_manager_default", "resultTotal", "linkText", "suggestion", "url", "parts", "part", "firstPart", "resultWithoutLast", "debounceSubmit", "debounce", "debounceClose", "searchPopup_default"] }