This repository has been archived by the owner on May 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchpage.tag
156 lines (115 loc) · 3.44 KB
/
searchpage.tag
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
<searchpage>
<form>
<div class="row">
<div class="col-sm-3">
<h1>Hoover</h1>
</div>
<div class="col-sm-8">
<div class="form-group">
<input name="q" value={q} class="form-control"
placeholder="query ...">
</div>
<div class="form-inline">
<div class="form-group">
<label for="search-size">Results per page</label>
<select class="form-control" id="search-size" name="size">
<option
each={size in sizeOptions}
selected={size==this.parent.size}
>{size}</option>
</select>
</div>
<button type="submit" class="btn btn-primary btn-sm">search</button>
<p class="pull-sm-right">
<a href="/batch.html">batch search</a>
</p>
</div>
</div>
<div class="col-sm-1">
<navbar></navbar>
</div>
</div>
<div class="row">
<div id="collections-box" class="col-sm-3">
<p if={!collections}>loading collections ...</p>
<div each={collections} class="checkbox">
<label>
<input type="checkbox" value="{name}"
checked={selectedCollections.indexOf(name) > -1}
onchange={onSelectCollection}>
{title}
</label>
</div>
<em if={!collections.length}>none available</em>
<input id="collections-input" type="hidden">
</div>
<div class="col-sm-9">
<search query={query} collections={collections}></search>
</div>
</div>
</form>
<script>
function getCollections(callback) {
$.ajax({
url: '/collections',
success: callback,
})
}
function saveCollections() {
var checkboxes = $('#collections-box input[type=checkbox]')
var collectionsInput = $('#collections-input')
if(checkboxes.filter(':not(:checked)').length > 0) {
var selected = ''+(
checkboxes
.filter(':checked')
.get()
.map(function(c) { return c.value })
.join(' ')
)
collectionsInput.attr('name', 'collections').val(selected)
}
else {
collectionsInput.attr('name', null).val('')
}
}
function parseQuery(url) {
var rv = {}
if(url.indexOf('?') > -1) {
url.match(/\?(.*)/)[1].split('&').forEach(function(pair) {
var kv = pair.split('=').map(decodeURIComponent)
var k = kv[0], v = kv[1]
if(! rv[k]) { rv[k] = [] }
rv[k].push(v)
})
}
return rv
}
onSelectCollection(evt) {
saveCollections()
}
var args = parseQuery(window.location.href)
this.q = args.q ? ("" + args.q).replace(/\+/g, ' ') : ""
this.sizeOptions = [10, 50, 200, 1000]
this.size = args.size ? +args.size : 10
getCollections(function(resp) {
this.collections = resp
if(args.collections) {
var sel = ''+args.collections
this.selectedCollections = sel ? sel.split('+') : []
}
else {
this.selectedCollections = resp.map(function(c) { return c.name })
}
if(this.q) {
this.query = {
q: this.q,
collections: this.selectedCollections,
page: args.p ? +args.p : 1,
size: this.size,
}
}
this.update()
saveCollections()
}.bind(this))
</script>
</searchpage>