JO
2024-06-28 359cb1603348374b9ef46b72ece66bcdca6cd0e0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* global instantsearch, algoliasearch, CONFIG, pjax */
 
document.addEventListener('DOMContentLoaded', () => {
  const { indexName, appID, apiKey, hits } = CONFIG.algolia;
 
  const search = instantsearch({
    indexName,
    searchClient  : algoliasearch(appID, apiKey),
    searchFunction: helper => {
      if (document.querySelector('.search-input').value) {
        helper.search();
      }
    }
  });
 
  if (typeof pjax === 'object') {
    search.on('render', () => {
      pjax.refresh(document.querySelector('.algolia-hits'));
    });
  }
 
  // Registering Widgets
  search.addWidgets([
    instantsearch.widgets.configure({
      hitsPerPage: hits.per_page || 10
    }),
 
    instantsearch.widgets.searchBox({
      container           : '.search-input-container',
      placeholder         : CONFIG.i18n.placeholder,
      // Hide default icons of algolia search
      showReset           : false,
      showSubmit          : false,
      showLoadingIndicator: false,
      cssClasses          : {
        input: 'search-input'
      }
    }),
 
    instantsearch.widgets.stats({
      container: '.algolia-stats',
      templates: {
        text: data => {
          const stats = CONFIG.i18n.hits_time
            .replace('${hits}', data.nbHits)
            .replace('${time}', data.processingTimeMS);
          return `<span>${stats}</span>
            <img src="${CONFIG.images}/logo-algolia-nebula-blue-full.svg" alt="Algolia">`;
        }
      },
      cssClasses: {
        text: 'search-stats'
      }
    }),
 
    instantsearch.widgets.hits({
      container : '.algolia-hits',
      escapeHTML: false,
      templates : {
        item: data => {
          const { title, excerpt, excerptStrip, contentStripTruncate } = data._highlightResult;
          let result = `<a href="${data.permalink}" class="search-result-title">${title.value}</a>`;
          const content = excerpt || excerptStrip || contentStripTruncate;
          if (content && content.value) {
            const div = document.createElement('div');
            div.innerHTML = content.value;
            result += `<a href="${data.permalink}"><p class="search-result">${div.textContent.substring(0, 100)}...</p></a>`;
          }
          return result;
        },
        empty: data => {
          return `<div class="algolia-hits-empty">
              ${CONFIG.i18n.empty.replace('${query}', data.query)}
            </div>`;
        }
      },
      cssClasses: {
        list: 'search-result-list'
      }
    }),
 
    instantsearch.widgets.pagination({
      container: '.algolia-pagination',
      scrollTo : false,
      showFirst: false,
      showLast : false,
      templates: {
        first   : '<i class="fa fa-angle-double-left"></i>',
        last    : '<i class="fa fa-angle-double-right"></i>',
        previous: '<i class="fa fa-angle-left"></i>',
        next    : '<i class="fa fa-angle-right"></i>'
      },
      cssClasses: {
        list        : ['pagination', 'algolia-pagination'],
        item        : 'pagination-item',
        link        : 'page-number',
        selectedItem: 'current',
        disabledItem: 'disabled-item'
      }
    })
  ]);
 
  search.start();
 
  // Handle and trigger popup window
  document.querySelectorAll('.popup-trigger').forEach(element => {
    element.addEventListener('click', () => {
      document.body.classList.add('search-active');
      setTimeout(() => document.querySelector('.search-input').focus(), 500);
    });
  });
 
  // Monitor main search box
  const onPopupClose = () => {
    document.body.classList.remove('search-active');
  };
 
  document.querySelector('.search-pop-overlay').addEventListener('click', event => {
    if (event.target === document.querySelector('.search-pop-overlay')) {
      onPopupClose();
    }
  });
  document.querySelector('.popup-btn-close').addEventListener('click', onPopupClose);
  document.addEventListener('pjax:success', onPopupClose);
  window.addEventListener('keyup', event => {
    if (event.key === 'Escape') {
      onPopupClose();
    }
  });
});