summaryrefslogtreecommitdiff
path: root/searx/static/themes/simple/js/infinite_scroll.min.js.map
blob: f839056b095f9f7221a59ed94e2e2067e0f377b7 (plain)
1
{"version":3,"file":"infinite_scroll.min.js","names":["resultsElement: HTMLElement | null","onlyImages: boolean","intersectionObserveOptions: IntersectionObserverInit","observer: IntersectionObserver","initialObservedElement: HTMLElement | null"],"sources":["../../../../../client/simple/src/js/main/infinite_scroll.ts"],"sourcesContent":["// SPDX-License-Identifier: AGPL-3.0-or-later\n\nimport { assertElement, http, settings } from \"../core/toolkit.ts\";\n\nconst newLoadSpinner = (): HTMLDivElement => {\n  return Object.assign(document.createElement(\"div\"), {\n    className: \"loader\"\n  });\n};\n\nconst loadNextPage = async (onlyImages: boolean, callback: () => void): Promise<void> => {\n  const searchForm = document.querySelector<HTMLFormElement>(\"#search\");\n  assertElement(searchForm);\n\n  const form = document.querySelector<HTMLFormElement>(\"#pagination form.next_page\");\n  assertElement(form);\n\n  const action = searchForm.getAttribute(\"action\");\n  if (!action) {\n    throw new Error(\"Form action not defined\");\n  }\n\n  const paginationElement = document.querySelector<HTMLElement>(\"#pagination\");\n  assertElement(paginationElement);\n\n  paginationElement.replaceChildren(newLoadSpinner());\n\n  try {\n    const res = await http(\"POST\", action, { body: new FormData(form) });\n    const nextPage = await res.text();\n    if (!nextPage) return;\n\n    const nextPageDoc = new DOMParser().parseFromString(nextPage, \"text/html\");\n    const articleList = nextPageDoc.querySelectorAll<HTMLElement>(\"#urls article\");\n    const nextPaginationElement = nextPageDoc.querySelector<HTMLElement>(\"#pagination\");\n\n    document.querySelector(\"#pagination\")?.remove();\n\n    const urlsElement = document.querySelector<HTMLElement>(\"#urls\");\n    if (!urlsElement) {\n      throw new Error(\"URLs element not found\");\n    }\n\n    if (articleList.length > 0 && !onlyImages) {\n      // do not add <hr> element when there are only images\n      urlsElement.appendChild(document.createElement(\"hr\"));\n    }\n\n    urlsElement.append(...Array.from(articleList));\n\n    if (nextPaginationElement) {\n      const results = document.querySelector<HTMLElement>(\"#results\");\n      results?.appendChild(nextPaginationElement);\n      callback();\n    }\n  } catch (error) {\n    console.error(\"Error loading next page:\", error);\n\n    const errorElement = Object.assign(document.createElement(\"div\"), {\n      textContent: settings.translations?.error_loading_next_page ?? \"Error loading next page\",\n      className: \"dialog-error\"\n    });\n    errorElement.setAttribute(\"role\", \"alert\");\n    document.querySelector(\"#pagination\")?.replaceChildren(errorElement);\n  }\n};\n\nconst resultsElement: HTMLElement | null = document.getElementById(\"results\");\nif (!resultsElement) {\n  throw new Error(\"Results element not found\");\n}\n\nconst onlyImages: boolean = resultsElement.classList.contains(\"only_template_images\");\nconst observedSelector = \"article.result:last-child\";\n\nconst intersectionObserveOptions: IntersectionObserverInit = {\n  rootMargin: \"320px\"\n};\n\nconst observer: IntersectionObserver = new IntersectionObserver((entries: IntersectionObserverEntry[]) => {\n  const [paginationEntry] = entries;\n\n  if (paginationEntry?.isIntersecting) {\n    observer.unobserve(paginationEntry.target);\n\n    loadNextPage(onlyImages, () => {\n      const nextObservedElement = document.querySelector<HTMLElement>(observedSelector);\n      if (nextObservedElement) {\n        observer.observe(nextObservedElement);\n      }\n    }).then(() => {\n      // wait until promise is resolved\n    });\n  }\n}, intersectionObserveOptions);\n\nconst initialObservedElement: HTMLElement | null = document.querySelector<HTMLElement>(observedSelector);\nif (initialObservedElement) {\n  observer.observe(initialObservedElement);\n}\n"],"mappings":"wDAIA,MAAM,MACG,OAAO,OAAO,SAAS,cAAc,OAAQ,CAClD,UAAW,SACZ,EAGG,EAAe,MAAO,EAAqB,IAAwC,CACvF,IAAM,EAAa,SAAS,cAA+B,WAC3D,EAAc,GAEd,IAAM,EAAO,SAAS,cAA+B,8BACrD,EAAc,GAEd,IAAM,EAAS,EAAW,aAAa,UACvC,GAAI,CAAC,EACH,MAAU,MAAM,2BAGlB,IAAM,EAAoB,SAAS,cAA2B,eAC9D,EAAc,GAEd,EAAkB,gBAAgB,KAElC,GAAI,CACF,IAAM,EAAM,MAAM,EAAK,OAA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