-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsearch.js
More file actions
128 lines (109 loc) · 4.58 KB
/
Copy pathsearch.js
File metadata and controls
128 lines (109 loc) · 4.58 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
const mediaElements = {
'video': (mURI, mRes) => {
const nv = document.createElement('video');
nv.src = mURI;
nv.type = mRes.headers.get('content-type');
nv.controls = true;
nv.muted = true;
return nv;
},
'image': (mURI) => {
const img = document.createElement('img');
img.src = mURI;
return img;
}
};
async function keydownHandler(searchIn, e) {
if (e.code === 'Enter') {
searchIn.disabled = true;
document.getElementById('search_res_info').textContent = `Searching...`;
const srEle = document.getElementById('search_res');
while (srEle.firstChild) {
srEle.removeChild(srEle.firstChild);
}
for (const beEnt of Object.entries(backends)) {
const [_, beSpec] = beEnt;
const startTs = Date.now();
const res = await hlteFetch('/search', beSpec[1], undefined, {
q: searchIn.value,
l: Number.parseInt(document.getElementById('limit_in').value),
d: document.getElementById('newest_first_in').checked
});
if (res.ok) {
try {
const rJson = await res.json();
if (!rJson) {
document.getElementById('search_res_info').textContent = 'No results found!';
continue;
}
const rowTmpl = document.getElementById('search_res_row_tmpl');
const foundMedia = Object.keys(mediaElements).reduce((a, k) => ({ [k]: 0, ...a }), {});
for (let row of rJson) {
const newRow = rowTmpl.cloneNode(true);
const nrC = (cid) => findChildByDataId(cid, newRow);
newRow.removeAttribute('id');
nrC('srr_ts').textContent = new Date(Number.parseInt(row.timestamp) / 1e6).toLocaleString();
const pURL = new URL(row.primaryURI);
const pLink = document.createElement('a');
pLink.href = row.primaryURI;
pLink.textContent = pURL.hostname;
pLink.target = '_blank';
nrC('srr_uris').appendChild(pLink);
if (row.secondaryURI && row.secondaryURI.length) {
const sURL = new URL(row.secondaryURI);
const sLink = document.createElement('a');
sLink.href = row.secondaryURI;
sLink.textContent = `(${sURL.hostname})`;
sLink.target = '_blank';
nrC('srr_uris').appendChild(sLink);
try {
const mRes = await fetch(row.primaryURI);
if (mRes && mRes.ok && mRes.headers.has('content-type')) {
const ct = mRes.headers.get('content-type').split('/')[0];
if (mediaElements[ct]) {
newRow.appendChild(mediaElements[ct](row.primaryURI, mRes));
++foundMedia[ct];
} else {
console.log(`Unhandled media type: ${row.primaryURI} -> ${mRes.headers.get('content-type')}`);
}
}
else {
logger.error(`failed to fetch media ${row.primaryURI}`);
console.dir(mRes);
}
} catch (err) {
logger.error(`failed to fetch media ${row.primaryURI} EXECPTION -> ${err}`);
console.dir(err);
}
}
nrC('srr_hilite').textContent = row.hilite;
if (row.annotation && row.annotation.length) {
nrC('srr_ann').textContent = row.annotation;
}
srEle.appendChild(newRow);
}
document.getElementById('search_res_info').textContent = `${rJson.length} results ` +
` -- ${Object.entries(foundMedia).map(([m, c]) => `${c} ${m}${c > 1 ? 's' : ''}`).join(', ')}` +
` -- ${Date.now() - startTs}ms`;
} catch (err) {
logger.error(`failed to parse query result: ${err}`);
}
}
}
searchIn.disabled = false;
}
}
async function onContentLoaded(_) {
const searchIn = document.getElementById('search_in');
searchIn.addEventListener('keydown', keydownHandler.bind(null, searchIn));
const limitIn = document.getElementById('limit_in');
limitIn.dataset.lastValue = limitIn.value = config.search.defaultLimit;
document.getElementById('limit_in').addEventListener('input', (e) => {
const numVal = Math.max(1, Math.min(config.search.maxLimit, Number.parseInt(e.target.value)));
limitIn.dataset.lastValue = limitIn.value = Number.isNaN(numVal) ? limitIn.dataset.lastValue : numVal;
e.preventDefault();
e.stopPropagation();
return false;
});
}
document.addEventListener('DOMContentLoaded', sharedOnDOMContentLoaded.bind(null, onContentLoaded));