This repository has been archived by the owner on Aug 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.html
225 lines (184 loc) · 8.04 KB
/
index.html
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Autocomplete demo</title>
<script id="test" src="dist/bundle.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/milligram/1.4.1/milligram.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1/mark.min.js"
integrity="sha512-5CYOlHXGh6QpOFA/TeTylKLWfB3ftPsde7AnmhuitiTX4K5SqCLBeKro6sPS8ilsz1Q4NRx3v8Ko2IBiszzdww=="
crossorigin="anonymous"></script>
<style>
hr {
margin: 0em;
}
.result span+span:before {
content: " | ";
padding: 0 10px;
}
.secondary {
font-size: 0.8em;
}
mark {
background: none;
color: #606c76;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<div class="row" style="height: 5rem">
</div>
<div class="row">
<div class="column column-80 column-offset-10">
<input id="autocomplete" type="text" placeholder="Ask a question">
</div>
</div>
<div class="row">
<div class="column column-100" id="results">
</div>
</div>
</div>
<script>
window.addEventListener('load', (event) => {
const inputField = document.getElementById("autocomplete");
const resultsElement = document.getElementById("results");
let currentQuery = "";
const autocomplete = new TreeComplete([
"https://termen.opoi.org/nta",
"https://termen.opoi.org/vtmk",
"https://termen.opoi.org/cht",
"https://termen.opoi.org/rkdartists"
], 10);
function isPrimaryLabel(quad) {
const predicate = quad.predicate.value;
return predicate === "http://schema.org/name" || predicate === "http://www.w3.org/2004/02/skos/core#prefLabel";
}
function selectLabels(matches, allQuads) {
let primaryLabels = [];
let secondaryLabels = [];
for (const [quad, overlap] of matches) {
const predicate = quad.predicate.value;
const value = quad.object.value;
let destination;
if (isPrimaryLabel(quad)) {
destination = primaryLabels;
} else {
destination = secondaryLabels;
}
destination.push([value, overlap]);
}
secondaryLabels = secondaryLabels.sort();
// sort all matches that are primary labels
// if no matches are primary labels, fallback to a non-matching one
if (primaryLabels.length === 0) {
let candidates = [];
for (const quad of allQuads) {
if (isPrimaryLabel(quad)) {
candidates.push([quad.object.value, []]);
}
}
if (candidates.length > 0) {
// retain only the first one
candidates = candidates.sort();
primaryLabels = [candidates[0]];
} else {
primaryLabels = [secondaryLabels[0]];
secondaryLabels = secondaryLabels.slice(1);
}
} else {
primaryLabels = primaryLabels.sort();
}
return [primaryLabels, secondaryLabels]
}
function formatPrimaryLabel(value, overlap) {
const element = document.createElement('span');
element.setAttribute("class", "primary");
element.innerText = value;
return element;
}
function formatSecondaryLabel(value, overlap) {
const element = document.createElement('span');
element.setAttribute("class", "secondary");
element.innerText = value;
return element;
}
function showResult(element, matches, allQuads) {
while (element.lastElementChild) {
element.removeChild(element.lastElementChild);
}
const [primaryLabels, secondaryLabels] = selectLabels(matches, allQuads);
const primaryElements = primaryLabels.map((a) => formatPrimaryLabel(...a));
element.appendChild(...primaryElements);
if (secondaryLabels.length > 0) {
const secondaryElements = secondaryLabels.map((a) => formatSecondaryLabel(...a));
element.appendChild(...secondaryElements);
};
const marker = new Mark(element);
marker.mark(currentQuery, { ignorePunctuation: ":;.,-–—‒_(){}[]!'\"+=".split("") });
}
function reset() {
while (resultsElement.lastElementChild) {
resultsElement.removeChild(resultsElement.lastElementChild);
}
currentResults = new Map();
}
autocomplete.on("reset", (meta) => {
if (meta.query === currentQuery) {
reset();
}
});
let currentResults = new Map();
autocomplete.on("data", (quad, meta) => {
if (currentQuery !== meta.query) {
return;
}
const subject = quad.subject.value;
let dataColumn;
if (!currentResults.has(subject)) {
currentResults.set(subject, [[[quad, meta.overlap]], meta.quads]);
row = document.createElement('div');
row.setAttribute("class", "row result");
dataColumn = document.createElement('div');
dataColumn.setAttribute("class", "column column-80 column-offset-10");
dataColumn.id = subject;
row.appendChild(dataColumn);
linkRow = document.createElement('div');
linkRow.setAttribute("class", "row result");
linkColumn = document.createElement('div');
linkColumn.setAttribute("class", "column column-80 column-offset-10");
linkColumn.setAttribute("style", "text-align: right;");
linkRow.appendChild(linkColumn);
link = document.createElement('a');
link.setAttribute("href", subject);
link.textContent = subject;
linkColumn.appendChild(link);
resultsElement.appendChild(row);
resultsElement.appendChild(linkColumn);
resultsElement.appendChild(document.createElement('hr'));
} else {
dataColumn = document.getElementById(subject);
const [matches, _] = currentResults.get(subject);
matches.push([quad, meta.overlap])
}
const [matches, allQuads] = currentResults.get(subject);
showResult(dataColumn, matches, allQuads);
})
inputField.addEventListener("input", function (e) {
var a, b, i, val = this.value;
if (!val) {
reset();
currentQuery = "";
} else if (val.trim() !== currentQuery) {
reset();
currentQuery = val.trim();
autocomplete.query(val);
}
});
});
</script>
</body>
</html>