|
| 1 | +(function (global, doc) { |
| 2 | + const getClearedItem = (item) => { |
| 3 | + const REMOVED_PATTERN = '¶'; |
| 4 | + const hierarchy = Object.entries(item.hierarchy).reduce((output, [hierarchyIndex, hierarchy]) => { |
| 5 | + return { |
| 6 | + ...output, |
| 7 | + [hierarchyIndex]: hierarchy?.replace(REMOVED_PATTERN, '') ?? null, |
| 8 | + }; |
| 9 | + }, {}); |
| 10 | + const highlightResultHierarchy = Object.entries(item._highlightResult.hierarchy).reduce((output, [hierarchyIndex, hierarchy]) => { |
| 11 | + return { |
| 12 | + ...output, |
| 13 | + [hierarchyIndex]: { |
| 14 | + ...hierarchy, |
| 15 | + value: hierarchy?.value.replace(REMOVED_PATTERN, '') ?? null, |
| 16 | + }, |
| 17 | + }; |
| 18 | + }, {}); |
| 19 | + |
| 20 | + return { |
| 21 | + ...item, |
| 22 | + hierarchy, |
| 23 | + _highlightResult: { |
| 24 | + ...item._highlightResult, |
| 25 | + hierarchy: highlightResultHierarchy, |
| 26 | + }, |
| 27 | + } |
| 28 | + } |
| 29 | + const search_query = document.location.hash.match(/q=(.*)(&|$)/)[1] ?? ''; |
| 30 | + const parsed_search_query = decodeURI(search_query.replace('+', ' ')); |
| 31 | + const search_page = document.location.hash.match(/p=(\d*)(&|$)/)[1] ?? 1; |
| 32 | + const parsed_search_page = parseInt(search_page); |
| 33 | + const version = document.location.pathname.split('/')[2]; |
| 34 | + const search = instantsearch({ |
| 35 | + indexName: 'ezplatform', |
| 36 | + searchClient: algoliasearch('2DNYOU6YJZ', '21ce3e522455e18e7ee16cf7d66edb4b'), |
| 37 | + initialUiState: { |
| 38 | + ezplatform: { |
| 39 | + query: parsed_search_query, |
| 40 | + refinementList: {version: [version]}, |
| 41 | + page: parsed_search_page, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }); |
| 45 | + |
| 46 | + document.getElementById('searchbox').addEventListener('keyup', function (event) { |
| 47 | + window.location.hash = '#q=' + encodeURI(event.target.value) + '&p=1'; |
| 48 | + }) |
| 49 | + |
| 50 | + document.getElementById('pagination').addEventListener('click', function (event) { |
| 51 | + let page = document.getElementsByClassName('ais-Pagination-item--selected').length ? parseInt(document.getElementsByClassName('ais-Pagination-item--selected')[0].innerText) : 1 |
| 52 | + window.location.hash = window.location.hash.includes('p=') ? window.location.hash.replace(/p=\d*/, 'p=' + page) : window.location.hash + '&p=' + page; |
| 53 | + }) |
| 54 | + |
| 55 | + search.addWidgets([ |
| 56 | + instantsearch.widgets.configure({ |
| 57 | + hitsPerPage: 10, |
| 58 | + }), |
| 59 | + instantsearch.widgets.stats({ |
| 60 | + container: '#stats', |
| 61 | + templates: { |
| 62 | + text: `<h1> |
| 63 | + Search results ({{#helpers.formatNumber}}{{nbHits}}{{/helpers.formatNumber}}) |
| 64 | + </h1>`, |
| 65 | + }, |
| 66 | + }), |
| 67 | + instantsearch.widgets.searchBox({ |
| 68 | + container: '#searchbox', |
| 69 | + }), |
| 70 | + instantsearch.widgets.hits({ |
| 71 | + container: '#hits', |
| 72 | + transformItems(items) { |
| 73 | + const outputItemsMap = {}; |
| 74 | + |
| 75 | + items.forEach((item) => { |
| 76 | + const hierarchy = Object.entries(item.hierarchy).filter(([, value]) => value); |
| 77 | + const breadcrumbsKeys = hierarchy.map(([key]) => key); |
| 78 | + const groupNameKey = breadcrumbsKeys.shift(); |
| 79 | + const entryNameKey = breadcrumbsKeys.pop(); |
| 80 | + const newItem = { |
| 81 | + entryNameKey, |
| 82 | + breadcrumbsKeys, |
| 83 | + item: getClearedItem(item), |
| 84 | + }; |
| 85 | + const groupChildren = outputItemsMap[groupNameKey]?.children ?? []; |
| 86 | + |
| 87 | + outputItemsMap[groupNameKey] = { |
| 88 | + groupNameKey, |
| 89 | + children: [...groupChildren, newItem], |
| 90 | + }; |
| 91 | + }); |
| 92 | + |
| 93 | + return Object.values(outputItemsMap); |
| 94 | + }, |
| 95 | + templates: { |
| 96 | + item: (hit) => { |
| 97 | + const { groupNameKey, children } = hit; |
| 98 | + const groupItem = children[0].item; |
| 99 | + const groupHeaderHTML = `<h2 class="instantsearch__group-header"> |
| 100 | + ${instantsearch.highlight({ attribute: `hierarchy.${groupNameKey}`, highlightedTagName: 'mark', hit: groupItem })} |
| 101 | + (${children.length}) |
| 102 | + </h2>`; |
| 103 | + let groupContentHTML = ''; |
| 104 | + |
| 105 | + children.forEach((childHit) => { |
| 106 | + const { breadcrumbsKeys, entryNameKey, item: entryItem } = childHit; |
| 107 | + const headerHTML = `<h3 class="instantsearch__entry-header"> |
| 108 | + ${instantsearch.highlight({ attribute: `hierarchy.${entryNameKey}`, highlightedTagName: 'mark', hit: entryItem })} |
| 109 | + </h3>`; |
| 110 | + let breadcrumbsHTML = ''; |
| 111 | + let contentHTML = ''; |
| 112 | + |
| 113 | + if (entryItem.content) { |
| 114 | + contentHTML = `<div class="instantsearch__entry-content"> |
| 115 | + ${instantsearch.highlight({ attribute: `content`, highlightedTagName: 'mark', hit: entryItem })} |
| 116 | + </div>`; |
| 117 | + } |
| 118 | + |
| 119 | + breadcrumbsKeys?.forEach((breadcrumbKey) => { |
| 120 | + breadcrumbsHTML += `<span class="instantsearch__entry-breadcrumbs-item"> |
| 121 | + ${instantsearch.highlight({ attribute: `hierarchy.${breadcrumbKey}`, highlightedTagName: 'mark', hit: entryItem })} |
| 122 | + </span>` |
| 123 | + }); |
| 124 | + |
| 125 | + const childHTML = `<div class="instantsearch__entry"> |
| 126 | + ${headerHTML} |
| 127 | + ${contentHTML} |
| 128 | + <div class="instantsearch__entry-breadcrumbs"> |
| 129 | + ${breadcrumbsHTML} |
| 130 | + </div> |
| 131 | + </div>`; |
| 132 | + |
| 133 | + groupContentHTML += childHTML; |
| 134 | + }); |
| 135 | + |
| 136 | + return `<div class="instantsearch"> |
| 137 | + ${groupHeaderHTML} |
| 138 | + <div class="instantsearch__group"> |
| 139 | + ${groupContentHTML} |
| 140 | + </div> |
| 141 | + </div>`; |
| 142 | + }, |
| 143 | + }, |
| 144 | + }), |
| 145 | + instantsearch.widgets.pagination({ |
| 146 | + container: '#pagination', |
| 147 | + }), |
| 148 | + instantsearch.widgets.stats({ |
| 149 | + container: '#pagination-stats', |
| 150 | + templates: { |
| 151 | + text(data, {html}) { |
| 152 | + let page = 1 + data.page; |
| 153 | + return html`<span>${page} / ${data.nbPages}</span>`; |
| 154 | + }, |
| 155 | + }, |
| 156 | + }), |
| 157 | + instantsearch.widgets.refinementList({ |
| 158 | + container: document.querySelector('#version'), |
| 159 | + attribute: 'version', |
| 160 | + }), |
| 161 | + ]); |
| 162 | + |
| 163 | + search.start(); |
| 164 | +})(window, window.document); |
0 commit comments