-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
154 lines (131 loc) · 4.85 KB
/
search.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
class SearchForm extends HTMLFormElement {
/**
* @type {null|HTMLInputElement} Search element
*/
search = null
/**
* @type {HTMLDivElement|null}
*/
resultsContainer = null
static index = null
static lunrIndex = null
static packages = null
constructor() {
self = super();
this.self = self
}
/**
* Retrieve the search query from the URL
* @returns {null|string} The query
*/
static getSearchQuery() {
return new URLSearchParams(window.location.search).get("q")
}
connectedCallback() {
console.log("SearchForm connectedCallback", this)
window.requestAnimationFrame(() => {
this.updateQueryField()
})
}
updateQueryField() {
this.search = this.querySelector("input[type=search]")
if (this.search) {
this.search.value = SearchForm.getSearchQuery()
} else {
}
}
ensureIndexLoaded() {
if (SearchForm.index) {
return Promise.resolve()
}
return fetch(window.publicUrl + "/packages-index.json")
.then((response) => {
return response.json()
})
.then((index) => {
SearchForm.index = index
SearchForm.lunrIndex = lunr.Index.load(index)
return Promise.resolve()
})
.then(() => {
return fetch(window.publicUrl + "/packages.json")
})
.then((response) => {
return response.json()
})
.then((packages) => {
SearchForm.packages = packages;
return Promise.resolve()
})
.catch((error) => alert("Error while searching: " + error.toString()))
}
doSearch(query) {
this._clearResults()
document.getElementById("search-results-busy").style.display = "block";
let self = this;
this.ensureIndexLoaded()
.then(() => {
let matches = SearchForm.lunrIndex.search(query)
let results = SearchForm.packages
.map(pkg => {
pkg.match = matches.find(m => m.ref === pkg.name)
return pkg
})
.filter(pkg => pkg.match)
.sort((l, r) => r.match.score - l.match.score)
console.log(results)
self.updateResults(results)
})
}
_clearResults() {
if (!this.resultsContainer) this.resultsContainer = document.getElementById("search-results")
while (this.resultsContainer.firstChild) {
this.resultsContainer.removeChild(this.resultsContainer.firstChild)
}
document.getElementById("search-results-empty").style.display = "none";
document.getElementById("search-results-busy").style.display = "none";
document.getElementById("search-results-no-query").style.display = "none";
}
/**
* Updates the UI to show the search results
* @param {Object[]} results The search results to show
*/
updateResults(results) {
let resultsContainer = document.getElementById("search-results")
this._clearResults()
document.getElementById("search-results-busy").style.display = "none";
if (results.length === 0) {
document.getElementById("search-results-empty").style.display = "block";
return;
}
/**
* @type {HTMLTemplateElement}
*/
let resultTemplate = document.getElementById("search-result")
for (const result of results) {
let resultInstance = resultTemplate.content.cloneNode(true)
let link = resultInstance.querySelector("a.pkg-title");
link.textContent = result.title;
link.href = result.url;
let iconLink = resultInstance.querySelector("a.pkg-icon");
iconLink.href = result.url;
let pkgIcon = resultInstance.querySelector("picture.pkg-icon > source:first-child")
pkgIcon.srcset = result.icon;
resultInstance.querySelector(".pkg-summary").textContent = result.summary;
let version = resultInstance.querySelector(".pkg-version")
version.textContent = result.version_short;
version.title = result.version;
resultsContainer.appendChild(resultInstance);
}
}
}
customElements.define("search-form", SearchForm, {extends: "form"})
document.addEventListener("DOMContentLoaded", () => {
let mainSearchForm = document.getElementById("main-search-form")
let searchQuery = SearchForm.getSearchQuery()
if (mainSearchForm && searchQuery) {
mainSearchForm.doSearch(searchQuery)
} else {
document.getElementById("search-results-no-query").style.display = "block";
}
})