« get me outta code hell

clientSearch.js « static « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/static/clientSearch.js
blob: 4d01cfd9008121453010ea5687773fdd2d527cde (plain)
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
/* eslint-env browser */

async function initSearch() {
  const FlexSearch = window.FlexSearch;

  // Copied directly from server search.js
  window.indexes = {
    albums: new FlexSearch.Document({
      id: "reference",
      index: ["name", "groups"],
    }),
    tracks: new FlexSearch.Document({
      id: "reference",
      index: ["track", "album", "artists", "directory", "additionalNames"],
    }),
    artists: new FlexSearch.Document({
      id: "reference",
      index: ["names"],
    })
  }

  let searchData = await fetch('/media/search_index.json').then(resp => resp.json())

  Object.entries(searchData).forEach(key_index_pair => {
    const [index_key, index_data] = key_index_pair
    Object.entries(index_data).forEach(key_value_pair => {
      const [key, value] = key_value_pair
      window.indexes[index_key].import(key, value);
    })
  })
}

function searchAll(query, options) {
  options = options || {}
  return Object.entries(window.indexes).reduce((a, pair) => {
    const [k, v] = pair
    a[k] = v.search(query, options)
    return a
  }, {})
}

document.addEventListener('DOMContentLoaded', initSearch);