forked from hdelva/termennetwerk_demo
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.html
408 lines (337 loc) · 16.3 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<!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">
<link rel="stylesheet" href="index.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1/mark.min.js"
integrity="sha512-5CYOlHXGh6QpOFA/TeTylKLWfB3ftPsde7AnmhuitiTX4K5SqCLBeKro6sPS8ilsz1Q4NRx3v8Ko2IBiszzdww=="
crossorigin="anonymous"></script>
<!-- Jquery for datasource selection -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<!-- Chosen -->
<link href='dist/assets/chosen.min.css' rel='stylesheet' type='text/css'>
<script src='dist/assets/chosen.jquery.min.js' type='text/javascript'></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>
<main>
<div class="row">
<nav class="navigation">
<p><a class="navpageheader" href="https://tree.linkeddatafragments.org">TREE</a></p>
<ul>
<li class="navpage"><a href="https://tree.linkeddatafragments.org/spec">Specs</a></li>
<!--<li class="navpage"><a href="/pages">Use it for…</a></li>
<li class="navpage"><a href="/datasets">Datasets</a></li>-->
<li class="navpage"><a href="https://tree.linkeddatafragments.org/software">Software</a></li>
<li class="navpage"><a href="https://tree.linkeddatafragments.org/publications">Publications</a></li>
<li class="navpage"><a href="https://tree.linkeddatafragments.org/team">Team</a></li>
<li class="navpage"><a href="https://tree.linkeddatafragments.org/demo">Try live</a></li>
<li class="navpage new"><a href="https://github.com/treecg">Github</a></li>
</ul>
</nav>
</div>
<div class="maincontent">
<p style="width: 100%;">
<h1 style="text-align: center;">
Autocomplete over TREE structured fragmentations
</h1>
</p>
<fieldset class="ldf-client">
<div class="row">
<div class="column column-80 column-offset-10">
<label>Choose TREE fragmentation to query over</label>
<span class="details-toggle" title="Select data source"></span>
<select id="datasourcesSelect" multiple data-placeholder="Select TREE fragmentations to query over..." name="datasets[]">
</select>
</div>
</div>
</fieldset>
<div>
<div class="row">
<div class="column column-80 column-offset-10">
<input id="autocomplete" type="text" placeholder="Give a search term" autocomplete="off">
</div>
</div>
<div class="row">
<div class="column column-100" id="results">
</div>
</div>
</div>
</div>
<footer>
<p>
© the Linked Data Fragments collaborators.
<strong><a href="mailto:[email protected]?subject=TREE%20question">Contact us</a>.</strong>
</p>
</footer>
</main>
<script>
window.addEventListener('load', (event) => {
let currentQuery = "";
let currentDatasetFieldValue = "";
const inputField = document.getElementById("autocomplete");
const resultsElement = document.getElementById("results");
var datasources = [];
// Process URL information.
const url = new URL(window.location.href);
let startingDatasets = url.searchParams.get('datasets[]')
let startingSearchString = url.searchParams.get('query')
startingDatasets = startingDatasets ? decodeURIComponent(startingDatasets).split(',') : [];
startingSearchString = startingSearchString ? decodeURIComponent(startingSearchString) : "";
const autocomplete = new TreeComplete.AutoCompleteWorker(10, []);
// Initialize chosen lib for datasources selection
$("#datasourcesSelect").chosen({no_results_text: `Press <b>Spacebar</b> to add datasource: ${currentDatasetFieldValue}`})
$(".chosen-search-input").on('keyup',function(e) {
currentDatasetFieldValue = e.target.value;
e.stopPropagation();
});
$(".chosen-search-input").on('keydown',function(e) {
e.stopPropagation();
if(e.which == 32) {
// Pressed Enter
e.stopPropagation();
addDataset(currentDatasetFieldValue);
}
e.stopPropagation();
});
$("#datasourcesSelect").on('change', function(evt, params) {
const selectedDatasets = getSelectedValues()
// update internal state of datasets for webworkers
updateSources(selectedDatasets);
// update url
const url = new URL(window.location.href);
const baseurl = new URL(url.origin + url.pathname)
// set url dataset parameters
baseurl.searchParams.append('datasets[]', encodeURIComponent(selectedDatasets.join(',')))
// Add query string
if (currentQuery) baseurl.searchParams.append('query', encodeURIComponent(currentQuery))
// push updated url to history
window.history.pushState("", "", baseurl.href);
});
function createCustomDataSource(URI) {
return {url: URI, description: `Custom datasource: ${URI}`}
}
function initialize() {
datasources = TreeComplete.config.datasources || []
// add custom datasources from URI
let datasourceURIs = datasources.map(s => s.url)
for (let dataSourceURI of startingDatasets) {
if (datasourceURIs.indexOf(dataSourceURI) === -1) {
// Add custom datasource
datasources.push(createCustomDataSource(dataSourceURI))
}
}
// Add data to datasource list
datasources.forEach(source => {
$("#datasourcesSelect").append(`<option value="${source.url}">${source.description}</option>`)
})
// Set starting values based on URL
$("#datasourcesSelect").val(startingDatasets)
// This updates the view (but does not trigger the on change?)
$("#datasourcesSelect").trigger("chosen:updated");
// This handles the on change /demo?datasets%5B%5D=https%253A%252F%252Ftree.linkeddatafragments.org%252Fdata%252Faddressregister%252Fstreetnames%252FStraatnaam%252F%252Chttps%253A%252F%252Ftree.linkeddatafragments.org%252Fdata%252Faddressregister%252Fmunicipalities%252FGemeente%252Ffunctionality for the initial dataset values
updateSources(startingDatasets)
// Set initial search value
if(startingSearchString) {
inputField.value = startingSearchString;
// Fire query for initial value
decideQuery(inputField, startingSearchString)
}
}
function addDataset(datasetURI) {
if(!datasetURI) return;
let datasetObject = createCustomDataSource(datasetURI)
datasources.push(datasetObject);
// Add data to datasource list
$("#datasourcesSelect").append(`<option value="${datasetObject.url}">${datasetObject.description}</option>`)
let selectedDatasets = getSelectedValues();
var updatedDatasets = selectedDatasets.concat(datasetObject.url)
// Set starting values based on URL
$("#datasourcesSelect").val(updatedDatasets)
// This updates the view (but does not trigger the on change?)
$("#datasourcesSelect").trigger("chosen:updated");
$("#datasourcesSelect").trigger("change");
}
function updateSources(sources) {
autocomplete.updateSources(sources)
}
function isPrimaryLabel(quad) {
const predicate = quad[1];
return TreeComplete.config.primaryLabels.indexOf(predicate) !== -1;
}
function selectLabels(matches, allQuads) {
let primaryLabels = [];
let secondaryLabels = [];
for (const quad of matches) {
const value = quad[2];
let destination;
if (isPrimaryLabel(quad)) {
destination = primaryLabels;
} else {
destination = secondaryLabels;
}
destination.push(value);
}
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[2]);
}
}
if (candidates.length > 0) {
// retain only the first one
candidates = candidates.sort();
primaryLabels = [candidates];
} else {
primaryLabels = [secondaryLabels[0]];
secondaryLabels = secondaryLabels.slice(1);
}
} else {
primaryLabels = primaryLabels.sort();
}
return [primaryLabels, secondaryLabels]
}
function formatPrimaryLabel(value) {
const element = document.createElement('span');
element.setAttribute("class", "primary");
element.innerText = value;
return element;
}
function formatSecondaryLabel(value) {
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();
}
let currentResults = new Map();
function createRow(subject, time) {
row = document.createElement('div');
row.setAttribute("class", "row result");
dataColumn = document.createElement('div');
dataColumn.setAttribute("class", "column column-80");
dataColumn.id = subject;
row.appendChild(dataColumn);
linkRow = document.createElement('div');
linkRow.setAttribute("class", "row result");
timeColumn = document.createElement('div');
timeColumn.setAttribute("class", "column column-10");
timeColumn.setAttribute("style", "text-align: left; font-size: 0.75em;");
timeColumn.textContent = `~ ${time} ms`;
linkRow.appendChild(timeColumn);
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);
resultItem = document.createElement('div')
resultItem.setAttribute("class", "resultItem column column-80 column-offset-10");
resultItem.appendChild(row);
resultItem.appendChild(linkRow);
resultsElement.appendChild(resultItem);
resultsElement.appendChild(document.createElement('hr'));
}
let timeout;
let iterator = null;
const debounce = (func, wait) => {
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
};
function fireQuery() {
if (iterator) {
iterator.removeAllListeners();
iterator.close();
}
if (getSelectedValues().length > 0) {
autocomplete.query(currentQuery);
} else {
window.alert('Please select one or more datasources to query over!')
inputField.value = "";
}
}
autocomplete.on('data', (results) => {
reset();
for (const result of results) {
createRow(result.subject, result.time);
showResult(dataColumn, result.matchingQuads, result.quads);
}
})
function decideQuery(field, e) {
var a, b, i, val = field.value;
const url = new URL(window.location.href);
if (!val) {
reset();
currentQuery = "";
// set url query parameter
url.searchParams.set('query', "")
} else if (val.trim() !== currentQuery) {
currentQuery = val.trim();
debounce(fireQuery, 100)();
url.searchParams.set('query', encodeURIComponent(val))
}
window.history.pushState("", "", url.href);
}
inputField.addEventListener("input", function (e) {
decideQuery(inputField, e)
});
function getSelectedValues() {
return $("#datasourcesSelect").val()
}
initialize();
});
</script>
</body>
</html>