-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.js
165 lines (141 loc) · 4.58 KB
/
web.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
155
156
157
158
159
160
161
162
163
164
165
(function(root, factory) { /* global define */
if (typeof define === 'function' && define.amd) {
define([], factory)
} else if (typeof module === 'object' && module.exports) {
module.exports = factory()
} else
root.NftsDialog = factory()
}(typeof self !== 'undefined' ? self : this, function() {
'use strict';
class Dialog {
constructor(conf, post_href, author_href, tag_href) {
this.parent = document.querySelector(conf.parent_container)
let btn = document.querySelector(conf.dialog_toggle_btn)
if (!(this.parent && btn)) throw new Error('init')
btn.style.visibility = 'visible'
btn.onclick = evt => { this.toggle(); evt.preventDefault() }
this.conf = conf
this.id = 'nfts__dialog'
Object.assign(this, {post_href, author_href, tag_href})
}
is_hidden() { return this.node.classList.contains('nfts__dialog--hidden') }
focus() { if (!this.is_hidden()) this.input.focus() }
toggle() {
if (!this.node)
this.init()
else
this.node.classList.toggle('nfts__dialog--hidden')
this.focus()
}
init() {
console.log('creating', this.id)
this.node = document.createElement('div')
this.node.id = this.id
this.parent.prepend(this.node)
this.node.innerHTML= '<input type="search" placeholder="Search..."><div></div>'
this.input = this.node.querySelector('input')
this.result = this.node.querySelector('div')
this.input.oninput = debounce(this.search, this.conf.debounce || 500).bind(this)
let style = document.createElement('style')
style.innerHTML = `
.nfts__dialog--hidden { display: none; }
#${this.id} table td { word-wrap: break-word; }
`
document.body.appendChild(style)
}
search() {
if (/^\s*$/.test(this.input.value)) return
this.result.innerText = 'Fetching results...'
this.send(this.input.value)
.then(this.print_results.bind(this))
.catch(this.search_error.bind(this))
}
send(query) {
let url = `${this.conf.server}/?q=${encodeURIComponent(query)}`
return fetch_json(url)
}
search_error(e) {
this.result.innerText = e.res && e.res.status === 412 ? 'Invalid query' : e.message
}
print_results(r) {
if (!r.length) { this.result.innerText = 'No matches'; return }
let tbl = ['<table style="table-layout: fixed; width: 100%">',
'<thead><tr>',
'<th style="width: 7em">Air Date</th>',
'<th>Snippet</th>',
'</tr></thead><tbody>']
tbl = tbl.concat(r.map( e => this.entry_fmt(e)))
tbl.push('</tbody></table>')
this.result.innerHTML = tbl.join("\n") + `<p>Total: ${r.length}</p>`
}
entry_fmt(e) {
let link = (href, name) => href === name ? esc(name) : `<a href='${esc(href)}'>${esc(name)}</a>`
let post_href = this.post_href || (v => v)
let author_href = this.author_href || (v => v)
let tag_href = this.tag_href || (v => v)
return [
'<tr><td style="vertical-align: top">',
Dialog.date_fmt(e.date * 1000),
'</td><td>', [
link(post_href(e.file), e.subject),
e.snippet, // unescaped but must be already safe from the db
'A: ' + e.authors.map( v => link(author_href(v), v)).join(', '),
'T: ' + e.tags.map( v => link(tag_href(v), v)).join(', '),
].join('<br>'),
'</td></tr>'].join('')
}
static date_fmt(t) {
let d = new Date(t); if (isNaN(d)) return t
return [d.getFullYear(), d.getMonth()+1, d.getDate()].join('/')
}
}
function fetch_json(url, opt) {
let err = r => {
if (r.ok) return r
let e = new Error(r.statusText); e.res = r
throw e
}
return fetch(url, opt).then(err).then( r => r.json())
}
function esc(s) {
if (s == null) return ''
return s.toString().replace(/[<>&'"]/g, ch => {
switch (ch) {
case '<': return '<'
case '>': return '>'
case '&': return '&'
case '\'': return '''
case '"': return '"'
}
})
}
// from underscore.js 1.8.3
function debounce(func, wait, immediate) {
var timeout, args, context, timestamp, result;
var later = function() {
var last = Date.now() - timestamp;
if (last < wait && last >= 0) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
if (!timeout) context = args = null;
}
}
};
return function() {
context = this;
args = arguments;
timestamp = Date.now();
var callNow = immediate && !timeout;
if (!timeout) timeout = setTimeout(later, wait);
if (callNow) {
result = func.apply(context, args);
context = args = null;
}
return result;
};
}
return Dialog
}));