|
2 | 2 |
|
3 | 3 | (function ($, lunr, database) {
|
4 | 4 | var resultsContainer = $('#search-results')
|
5 |
| - var navigationContainer = $('#instructions') |
6 |
| - var nothingFound = $('<li>Nothing found.</li>') |
| 5 | + var instructionsContainer = $('#instructions') |
| 6 | + var nothingFound = $('#nothing-found') |
7 | 7 | var searchQuery = $('#search-input')
|
8 | 8 | database = database || {}
|
9 | 9 |
|
|
12 | 12 | */
|
13 | 13 | function createSearchStore(data) {
|
14 | 14 | var searchStore = lunr(function () {
|
15 |
| - var self = this |
| 15 | + var lunrBuilder = this |
16 | 16 |
|
17 |
| - self.pipeline.remove(lunr.stemmer); |
18 |
| - self.searchPipeline.remove(lunr.stemmer); |
| 17 | + lunrBuilder.pipeline.remove(lunr.stemmer); |
| 18 | + lunrBuilder.searchPipeline.remove(lunr.stemmer); |
19 | 19 |
|
20 | 20 | let leftAnchoredSearch = function (token) {
|
21 | 21 | token.update(function () { return token.toString() + '*' });
|
22 | 22 | return token;
|
23 | 23 | };
|
| 24 | + |
24 | 25 | lunr.Pipeline.registerFunction(leftAnchoredSearch, 'las');
|
25 |
| - self.searchPipeline.add(leftAnchoredSearch); |
| 26 | + lunrBuilder.searchPipeline.add(leftAnchoredSearch); |
| 27 | + |
| 28 | + lunrBuilder.field('id'); |
| 29 | + lunrBuilder.field('vendor'); |
| 30 | + lunrBuilder.field('model', { boost: 20 }); |
| 31 | + lunrBuilder.field('title'); |
| 32 | + lunrBuilder.field('zigbeemodel'); |
26 | 33 |
|
27 |
| - self.field('id'); |
28 |
| - self.field('vendor'); |
29 |
| - self.field('model', { boost: 20 }); |
30 |
| - self.field('title'); |
31 |
| - self.field('zigbeemodel'); |
| 34 | + Object.entries(data).forEach(function ([key, entry]) { |
| 35 | + var zigbeeModel = entry.zigbeemodel || ''; |
| 36 | + zigbeeModel = Array.isArray(zigbeeModel) ? zigbeeModel.join(' ') : String(zigbeeModel); |
32 | 37 |
|
33 |
| - Object.keys(data).forEach(function (key) { |
34 |
| - self.add({ |
| 38 | + lunrBuilder.add({ |
35 | 39 | id: key,
|
36 |
| - title: data[key].title, |
37 |
| - model: data[key].model, |
38 |
| - vendor: data[key].vendor, |
39 |
| - zigbeemodel: data[key].zigbeemodel, |
| 40 | + title: entry.title, |
| 41 | + model: entry.model, |
| 42 | + vendor: entry.vendor, |
| 43 | + zigbeemodel: zigbeeModel, |
40 | 44 | });
|
41 | 45 | })
|
42 | 46 |
|
|
45 | 49 | return searchStore
|
46 | 50 | }
|
47 | 51 |
|
| 52 | + |
48 | 53 | /**
|
49 | 54 | * @param {DatabaseEntry} result
|
50 | 55 | */
|
51 | 56 | function resultEntry(result) {
|
52 |
| - var searchEntry = $('<li />') |
53 |
| - var searchLink = $('<a />') |
54 |
| - |
55 |
| - var categoryPath = result.url.split('/') |
56 |
| - categoryPath.shift() |
57 |
| - categoryPath.pop() |
58 |
| - |
59 |
| - searchEntry |
60 |
| - .append(searchLink) |
61 |
| - |
62 |
| - searchLink.attr('href', result.href) |
63 |
| - |
64 |
| - searchLink |
65 |
| - .text(result.vendor) |
66 |
| - .append(" ") |
67 |
| - .append(result.title) |
68 |
| - .append(" ") |
69 |
| - .append(result.model) |
70 |
| - .append("  ZigbeeID: ") |
71 |
| - .append(result.zigbeemodel); |
| 57 | + var imgSrc = `/assets/images/devices${result.href.replace('.html', '')}.webp`; |
| 58 | + var compatible = Array.isArray(result.compatible) ? result.compatible : []; |
| 59 | + |
| 60 | + var searchEntry = $(`<tr> |
| 61 | + <td class="td-first"> |
| 62 | + <img src="${imgSrc}" height="75" alt="${result.vendor} ${result.model}" loading="lazy" /> |
| 63 | + </td> |
| 64 | + <td class="td-second"> |
| 65 | + <b> |
| 66 | + <a class="menu" href="${result.href}"> |
| 67 | + ${result.vendor} ${result.title} |
| 68 | + </a> |
| 69 | + </b> |
| 70 | + </td> |
| 71 | + <td> |
| 72 | + ${result.model.length >= 18 ? result.model.substring(0, 15) + '...' : result.model} |
| 73 | + </td> |
| 74 | +
|
| 75 | + <td class="td-compat"> |
| 76 | + ${compatible.includes('zha') ? '<img alt="zha" title="Zigbee Home Automation for Home Assistant" src="/assets/images/zha-icon.png" />' : ''} |
| 77 | + </td> |
| 78 | + <td class="td-compat"> |
| 79 | + ${compatible.includes('tasmota') || result.category == 'light' || result.category == 'dimmer' ? '<img alt="Tasmota" title="Tasmota" src="/assets/images/tasmota-icon.png" />' : ''} |
| 80 | + </td> |
| 81 | + <td class="td-compat"> |
| 82 | + ${compatible.includes('z2m') ? '<img alt="Zigbee2MQTT" title="Zigbee2MQTT" src="/assets/images/z2m-icon.png" />' : ''} |
| 83 | + </td> |
| 84 | + <td class="td-compat"> |
| 85 | + ${compatible.includes('deconz') ? '<img alt="deCONZ" title="deCONZ" src="/assets/images/deconz-icon.png" />' : ''} |
| 86 | + </td> |
| 87 | + <td class="td-compat"> |
| 88 | + ${compatible.includes('z4d') ? '<img alt="Zigbee for Domoticz" title="Zigbee for Domoticz" src="/assets/images/z4d-icon.png" />' : ''} |
| 89 | + </td> |
| 90 | + <td class="td-compat"> |
| 91 | + ${compatible.includes('iob') || compatible.includes('z2m') ? '<img alt="ioBroker.zigbee" title="ioBroker.zigbee" src="/assets/images/iobroker-icon.png" />' : ''} |
| 92 | + </td> |
| 93 | + </tr>` |
| 94 | + ); |
72 | 95 |
|
73 | 96 | return searchEntry
|
74 | 97 | }
|
|
78 | 101 | */
|
79 | 102 | function displayResults(results) {
|
80 | 103 | resultsContainer.empty()
|
81 |
| - var fragment = $(document.createDocumentFragment()); |
| 104 | + instructionsContainer.hide() |
82 | 105 |
|
83 |
| - if (results.length > 0) { |
84 |
| - results.map(function(entry) { |
85 |
| - resultsContainer.append(resultEntry(entry)) |
86 |
| - }) |
87 |
| - } else { |
88 |
| - resultsContainer.append(nothingFound) |
| 106 | + if (!results.length) { |
| 107 | + nothingFound.show(); |
| 108 | + return; |
89 | 109 | }
|
90 | 110 |
|
| 111 | + var fragment = $(document.createDocumentFragment()); |
| 112 | + results.map(function(entry) { |
| 113 | + fragment.append(resultEntry(entry)) |
| 114 | + }) |
| 115 | + |
91 | 116 | resultsContainer.append(fragment);
|
92 |
| - navigationContainer.hide() |
93 | 117 | resultsContainer.show()
|
94 | 118 | }
|
95 | 119 |
|
96 | 120 | function hideResults() {
|
97 | 121 | resultsContainer.hide()
|
98 |
| - navigationContainer.show() |
| 122 | + nothingFound.hide(); |
| 123 | + instructionsContainer.show() |
99 | 124 | }
|
100 | 125 |
|
101 | 126 | /**
|
|
0 commit comments