Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
164 lines (138 loc) · 4.28 KB
/
Copy pathmain.js
File metadata and controls
164 lines (138 loc) · 4.28 KB
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
155
156
157
158
159
160
161
162
163
164
/**
* @param {string} spec
* @param {(Node | string)[]} children
* @returns {Element}
*/
function $(spec, ...children) {
let elem;
for (const match of spec.matchAll(/(^[^.#]*)|([.#])([^.#]*)/g)) {
const [_, name, sign, content] = match;
if (name != null) elem = document.createElement(name);
if (sign === '#') elem.id = content;
else if (sign === '.') elem.classList.add(content);
}
elem.append(...children);
return elem;
}
function escapeRegExp(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
class Entry {
constructor(word, pos, definition, antonymDefinition, keywords, category, elem, hintE) {
this.word = word;
this.pos = pos;
this.definition = definition;
this.antonymDefinition = antonymDefinition;
this.keywords = keywords;
this.category = category;
this.elem = elem;
this.hintE = hintE;
}
}
const entries = [];
const searchInputE = document.querySelector('#search-input');
const mobileSearchInputE = document.querySelector('#mobile-search-input');
const themeButtonE = document.querySelector('#theme-button');
const categoryInputsBodyE = document.querySelector('#category-inputs-body');
const wordsE = document.querySelector('#words');
const data = await (await fetch('data.json')).json();
console.log(data);
for (const entry of data) {
const entryMainE = $('div.entry-main',
$('div.entry-term.entry-term-plain',
$('div.entry-title',
$('h3.entry-word', entry.word),
$('small.entry-pos', entry.pos)
),
$('p.entry-definition', entry.definition)
),
);
const entryHintE = $('small.entry-hint');
const entryE = $('div.entry', entryMainE, entryHintE);
if (entry.antonymDefinition) {
entryMainE.append(
$('div.entry-term.entry-term-antonym',
$('div.entry-title',
$('h3.entry-word', 'Vöt' + entry.word)
),
$('p.entry-definition', entry.antonymDefinition)
)
);
}
entries.push(new Entry(entry.word, entry.pos, entry.definition, entry.antonymDefinition, entry.keywords ?? [], entry.category, entryE, entryHintE));
}
const search = {
query: '',
categories: new Set(['official'])
};
function updateSearch({ query, categories }) {
const wordMatches = new Set();
const definitionWholeMatches = new Set();
const definitionMatches = new Set();
const keywordMatches = new Map();
const escaped = escapeRegExp(query).toLowerCase().replace('0', '[0ö]').replace('9', '[9ð]');
const queryRegex = new RegExp(escaped, 'i');
const queryWholeRegex = new RegExp(`\\b${escaped}\\b`, 'i');
for (const entry of entries) {
entry.hintE.innerHTML = '';
if (!categories.has(entry.category))
continue;
if (queryWholeRegex.test(entry.definition) || queryWholeRegex.test(entry.antonymDefinition)) {
definitionWholeMatches.add(entry);
continue;
}
if (queryRegex.test(entry.definition) || queryRegex.test(entry.antonymDefinition)) {
definitionMatches.add(entry);
continue;
}
if (queryRegex.test(entry.word)) {
wordMatches.add(entry);
continue;
}
for (const keyword of entry.keywords) {
if (!keyword.includes(query)) continue;
let keywords = keywordMatches.get(entry);
if (!keywords) {
keywords = [];
keywordMatches.set(entry, keywords);
}
keywords.push(keyword);
}
}
wordsE.innerHTML = '';
for (const entry of definitionWholeMatches) wordsE.append(entry.elem);
for (const entry of definitionMatches) wordsE.append(entry.elem);
for (const entry of wordMatches) wordsE.append(entry.elem);
for (const [entry, keywords] of keywordMatches.entries()) {
wordsE.append(entry.elem);
entry.hintE.append(
'matched ' + (keywords.length === 1 ? 'keyword' : 'keywords') + ' ',
$('br')
);
for (const [i, keyword] of keywords.entries()) {
if (i > 0) entry.hintE.append(', ');
entry.hintE.append($('strong', keyword));
}
}
}
updateSearch(search);
searchInputE.addEventListener('input', e => {
search.query = e.target.value;
updateSearch(search);
});
mobileSearchInputE.addEventListener('input', e => {
search.query = e.target.value;
updateSearch(search);
});
themeButtonE.addEventListener('click', () => {
document.documentElement.classList.toggle('dark');
});
categoryInputsBodyE.addEventListener('change', e => {
if (e.target.checked) {
search.categories.add(e.target.name);
} else {
search.categories.delete(e.target.name);
}
console.log(search);
updateSearch(search);
});