{ "version": 3, "sources": ["../../../Vettvangur.Frontend/src/scripts/components/search.ts"], "sourcesContent": ["import axios from 'axios'\nimport { debounce } from 'throttle-debounce'\nimport gsap from 'gsap'\n\nconst search = {\n el: {\n search: {\n background: document.querySelector('.search__background'),\n body: document.querySelector('.search__body'),\n closeButton: document.querySelector('.search__close-button'),\n head: document.querySelector('.search__head'),\n input: document.querySelector('.search__input input'),\n modal: document.querySelector('.search__modal'),\n resultsButton: document.querySelector('.search__results-button'),\n root: document.querySelector('.search'),\n },\n trigger: {\n desktop: document.querySelector('.header__search-trigger'),\n mobile: document.querySelector('.mobile-header__search-trigger'),\n },\n tabs: {\n buttons: document.querySelectorAll('.search .tabs__button'),\n content: document.querySelectorAll('.search .tabs-content'),\n },\n },\n\n init(): void {\n ;[this.el.search.background, this.el.search.closeButton, this.el.trigger.desktop, this.el.trigger.mobile].forEach((trigger) => {\n trigger.addEventListener('click', this.toggleSearchVisibility.bind(this))\n })\n\n this.el.search.resultsButton.addEventListener('mouseover', this.handleResultsButtonHover.bind(this))\n this.el.search.resultsButton.addEventListener('mouseout', this.handleResultsButtonHover.bind(this))\n\n this.el.search.input.addEventListener(\n 'keyup',\n debounce(66, (event) => {\n event.preventDefault()\n\n if (event.target.value.length >= 3) {\n this.getSearchResults(event.target.value).then((response) => {\n this.renderSearchResults(response.data)\n })\n }\n\n this.toggleSearchResultsVisibility(event)\n })\n )\n },\n\n areResultsVisible(): boolean {\n return this.el.search.root.classList.contains('search--results-visible')\n },\n\n closeOnEscPress(event): void {\n if (event.key.match('Escape') && this.el.search.root.classList.contains('search--active')) {\n this.toggleSearchVisibility(null)\n }\n },\n\n async getSearchResults(input: string): Promise {\n const host = window.location.origin\n const query = '/umbraco/api/search/search?query='\n const url = host.concat(query, input)\n\n return await axios\n .get(url)\n .then((response) => {\n return response\n })\n .catch((err) => console.log(err))\n },\n\n handleResultsButtonHover(): void {\n this.el.search.modal.classList.toggle('search__modal--results-button-hovered')\n },\n\n renderSearchResults(data): void {\n const results = [data.treatmentResults, data.cureResults, data.productResults, data.otherResults]\n const total = data.totalCures + data.totalProducts + data.totalTreatments + data.totalOther\n\n // Rendering amount of results in each tab button\n this.el.tabs.buttons.forEach((button, index) => {\n if (results[index].length > 0) {\n button.classList.add('tabs__button--count')\n button.querySelector('.tabs__button-count').innerHTML = results[index].length\n } else {\n button.classList.remove('tabs__button--count')\n button.querySelector('.tabs__button-count').innerHTML = '0'\n }\n })\n\n // Rendering results\n this.el.tabs.content.forEach((content, index) => {\n const list = content.firstElementChild\n const noResultsText = this.el.search.root.querySelector('[name=\"no-results-text\"]').value\n\n list.innerHTML = ''\n\n if (results[index].length > 0) {\n results[index].forEach((result) => {\n list.insertAdjacentHTML(\n 'beforeend',\n `\n
  • \n \n

    ${result.name}

    \n ${result.summary ? `

    ${result.summary}

    ` : ''}\n
    \n
  • \n `\n )\n })\n } else {\n list.innerHTML = `
  • ${noResultsText}
  • `\n }\n })\n\n // Jumping to the first tab with results\n let jumped = false\n\n this.el.tabs.buttons.forEach((button, index) => {\n if (results[index].length > 0 && !jumped) {\n button.click()\n jumped = true\n }\n })\n\n // Updating the redirect link for all results to be displayed\n this.el.search.resultsButton.setAttribute('href', `${this.el.search.head.getAttribute('action')}?q=${this.el.search.input.value}`)\n\n // Rendering total amount of results in the results button\n if (total > 0) {\n this.el.search.resultsButton.classList.remove('search__results-button--hidden')\n this.el.search.resultsButton.querySelector('.button__text span').innerHTML = total.toString()\n } else {\n this.el.search.resultsButton.classList.add('search__results-button--hidden')\n }\n },\n\n toggleSearchVisibility(event): void {\n event && event.preventDefault()\n\n if (this.el.search.root.classList.contains('search--active')) {\n this.el.search.root.classList.remove('search--active')\n this.el.trigger.desktop.ariaExpanded = 'false'\n this.el.trigger.mobile.ariaExpanded = 'false'\n window.removeEventListener('keydown', this.closeOnEscPress.bind(this))\n setTimeout(() => {\n this.el.search.input.value = ''\n this.el.search.root.classList.remove('search--results-visible')\n }, 345)\n } else {\n this.el.search.root.classList.add('search--active')\n this.el.trigger.desktop.ariaExpanded = 'true'\n this.el.trigger.mobile.ariaExpanded = 'true'\n window.addEventListener('keydown', this.closeOnEscPress.bind(this))\n setTimeout(() => {\n this.el.search.input.focus()\n }, 678)\n }\n },\n\n toggleSearchResultsVisibility(event): void {\n const runAnimation = (action) => {\n const props = {\n clearProps: 'all',\n duration: 0.543,\n ease: 'power3.inOut',\n }\n\n if (gsap.isTweening(this.el.search.body)) {\n return\n }\n\n if (!this.areResultsVisible() && action === 'open') {\n gsap.fromTo(\n this.el.search.body,\n {\n height: 0,\n },\n {\n clearProps: 'all',\n delay: 0.06, // Tiny delay\n height: 'auto',\n onStart: () => {\n this.el.search.root.classList.add('search--results-visible')\n },\n ...props,\n }\n )\n }\n\n if (this.areResultsVisible() && action === 'close') {\n gsap.fromTo(\n this.el.search.body,\n {\n height: this.el.search.body.offsetHeight,\n },\n {\n clearProps: 'all',\n height: 0,\n // onComplete: () => { // This results in jerky transition between opened modal and search bar.\n // this.el.search.root.classList.remove('search--results-visible')\n // },\n onStart: () => {\n // Delay from start gives smoother transition when going from opened modal to a search bar.\n gsap.delayedCall(0.343, () => {\n this.el.search.root.classList.remove('search--results-visible')\n })\n },\n ...props,\n }\n )\n }\n }\n\n runAnimation(event.target.value.length >= 3 ? 'open' : 'close')\n },\n}\n\nexport default search\n"], "mappings": "gLAIA,IAAMA,EAAS,CACb,GAAI,CACF,OAAQ,CACN,WAAY,SAAS,cAA2B,qBAAqB,EACrE,KAAM,SAAS,cAA2B,eAAe,EACzD,YAAa,SAAS,cAA2B,uBAAuB,EACxE,KAAM,SAAS,cAA2B,eAAe,EACzD,MAAO,SAAS,cAAgC,sBAAsB,EACtE,MAAO,SAAS,cAA2B,gBAAgB,EAC3D,cAAe,SAAS,cAA2B,yBAAyB,EAC5E,KAAM,SAAS,cAA2B,SAAS,CACrD,EACA,QAAS,CACP,QAAS,SAAS,cAA2B,yBAAyB,EACtE,OAAQ,SAAS,cAA2B,gCAAgC,CAC9E,EACA,KAA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CE,EAAK,OACH,KAAK,GAAG,OAAO,KACf,CACE,OAAQ,KAAK,GAAG,OAAO,KAAK,YAC9B,EACAC,EAAA,CACE,WAAY,MACZ,OAAQ,EAIR,QAAS,IAAM,CAEbD,EAAK,YAAY,KAAO,IAAM,CAC5B,KAAK,GAAG,OAAO,KAAK,UAAU,OAAO,yBAAyB,CAChE,CAAC,CACH,GACGD,EAEP,EAEJ,GAEajB,EAAM,OAAO,MAAM,QAAU,EAAI,OAAS,OAAO,CAChE,CACF,EAEOoB,EAAQvB", "names": ["search", "trigger", "debounce", "event", "response", "input", "url", "axios_default", "err", "data", "results", "total", "button", "index", "content", "list", "noResultsText", "result", "jumped", "action", "props", "gsapWithCSS", "__spreadValues", "search_default"] }