|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +document.addEventListener('DOMContentLoaded', () => { |
| 4 | + const onIntegrationPage = document.querySelector('.package-button'); |
| 5 | + if (! onIntegrationPage) { |
| 6 | + return; |
| 7 | + } |
| 8 | + |
| 9 | + loadIntegrationPage(); |
| 10 | +}); |
| 11 | + |
| 12 | +function loadIntegrationPage() |
| 13 | +{ |
| 14 | + document.querySelectorAll('.package-button.type, .package-button.category, .package-button.usage').forEach(button => { |
| 15 | + button.addEventListener('click', handleFilters); |
| 16 | + }) |
| 17 | + |
| 18 | + document.querySelectorAll('.package-button.keyword').forEach(button => { |
| 19 | + button.addEventListener('click', handleKeywords); |
| 20 | + }) |
| 21 | + |
| 22 | + document.querySelectorAll('.integration-filter').forEach(button => { |
| 23 | + button.addEventListener('click', removeKeyword); |
| 24 | + }) |
| 25 | + |
| 26 | + document.querySelectorAll('#integration-pagination a').forEach(a => { |
| 27 | + const url = new URL(a.href) |
| 28 | + for (let [k,v] of new URLSearchParams(window.location.search).entries()) { |
| 29 | + if (k === 'keywords[]' || k === 'q' || k === 'type' || k === 'category' || k === 'usage') { |
| 30 | + url.searchParams.set(k,v); |
| 31 | + } |
| 32 | + } |
| 33 | + a.href = url.toString(); |
| 34 | + }) |
| 35 | + |
| 36 | + document.querySelector('#clear-filters-button')?.addEventListener('click', function () { |
| 37 | + const url = new URL(window.location.href); |
| 38 | + |
| 39 | + for (let [k,v] of new URLSearchParams(window.location.search).entries()) { |
| 40 | + if (k !== 'page') { |
| 41 | + url.searchParams.delete(k); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + window.location.replace(url.toString()); |
| 46 | + }); |
| 47 | + |
| 48 | + document.querySelector('#integration-search-btn').addEventListener('click', function () { |
| 49 | + setSearchQuery(document.querySelector('#integration-search').value); |
| 50 | + }); |
| 51 | + |
| 52 | + document.querySelector('#integration-search').addEventListener('keypress', function (e) { |
| 53 | + const search = this.value; |
| 54 | + if (e.which === 13) { |
| 55 | + setSearchQuery(search); |
| 56 | + } |
| 57 | + }) |
| 58 | + |
| 59 | + function handleFilters() { |
| 60 | + for (const filter of ['type', 'category', 'usage']) { |
| 61 | + if (this.classList.contains(filter)) { |
| 62 | + handleParams(filter, this.dataset.value); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + function handleParams(filterKey, filterValue) { |
| 68 | + const url = new URL(window.location.href); |
| 69 | + const params = new URLSearchParams(url.search); |
| 70 | + |
| 71 | + if (filterValue === 'all' || params.get(filterKey) === filterValue) { |
| 72 | + url.searchParams.delete(filterKey); |
| 73 | + |
| 74 | + window.location.replace(url.toString()); |
| 75 | + } else if (! params.has(filterKey, filterValue)) { |
| 76 | + url.searchParams.set(filterKey, filterValue); |
| 77 | + |
| 78 | + window.location.replace(url.toString()); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + function handleKeywords() { |
| 83 | + const url = new URL(window.location.href); |
| 84 | + const params = new URLSearchParams(url.search); |
| 85 | + const keyword = this.dataset.value; |
| 86 | + |
| 87 | + if (! params.has("keywords[]", keyword)) { |
| 88 | + params.append("keywords[]", keyword); |
| 89 | + url.search = params.toString(); |
| 90 | + |
| 91 | + window.location.replace(url.toString()); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + function removeKeyword() { |
| 96 | + const url = new URL(window.location.href); |
| 97 | + const params = new URLSearchParams(url.search); |
| 98 | + const keyword = this.dataset.value; |
| 99 | + |
| 100 | + if (params.has("keywords[]", keyword)) { |
| 101 | + params.delete("keywords[]", keyword); |
| 102 | + url.search = params.toString(); |
| 103 | + |
| 104 | + window.location.replace(url.toString()); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + function setSearchQuery(search) { |
| 109 | + const url = new URL(window.location.href); |
| 110 | + |
| 111 | + url.searchParams.set('q', search); |
| 112 | + window.location.replace(url.toString()); |
| 113 | + } |
| 114 | +} |
0 commit comments