-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchBox.vue
216 lines (202 loc) · 5.19 KB
/
SearchBox.vue
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
<template>
<div role="combobox"
aria-label="search for anything on Risteys"
aria-haspopup="grid"
aria-owns="search-results"
:aria-expanded="ariaExpanded() ? 'true' : 'false'"
class="outshadow">
<input type="text"
v-on:keydown.enter.prevent="acceptResult"
v-on:keydown.down.prevent="nextResult"
v-on:keydown.up.prevent="previousResult"
v-on:keyup="kbdSearch"
v-bind:value="searchvalue"
v-on:input="searchvalue = $event.target.value"
role="searchbox"
aria-multiline="false"
aria-autocomplete="list"
aria-controls="search-results"
:aria-activedescendant="getItemIdSelected()"
placeholder="click or type 's' to search for disease, ICD code, endpoint"
id="search-input"
autocomplete="off">
<div id="search-results" class="results" role="grid">
<div class="category" v-for="(category, idx_category) in results">
<div class="category-name">{{ category[idx_name] }}</div>
<div :id="genItemId(idx_category, idx_item)"
:class="'item ' + classSelected(idx_category, idx_item)"
v-for="(item, idx_item) in category[idx_items]"
role="row">
<span class="font-mono" role="rowcell">
<a :href="item.url" v-html="item.phenocode"></a>
</span>
<span v-html="item.content"
class="pr-2 inline-block"
role="rowcell">
</span>
</div>
</div>
</div>
</div>
</template>
<style>
div[role="combobox"] {
color: black;
@apply text-left;
}
.outshadow:focus-within {
box-shadow: 0 0 30px rgba(0, 0, 0, 0.15);
}
div[role="combobox"] > input {
box-shadow: inset 0 0 15px rgba(0, 0, 0, 0.1);
@apply p-3;
@apply w-full;
@apply border-2;
@apply border-grey;
}
div[role="combobox"] > input:focus {
box-shadow: none;
@apply border-blue-dark;
}
.results {
position: relative;
@apply bg-grey-lightest;
}
.results .category {
@apply p-2;
}
.results .category:nth-child(n + 2) .category-name {
@apply mt-4;
}
.results .item {
@apply py-2;
display: grid;
grid-template-columns: 250px auto;
}
.results .item > span:nth-child(1) {
@apply pr-2;
overflow-wrap: break-word;
}
.results .item.selected {
@apply bg-grey-light;
}
.results .category-name {
@apply uppercase;
@apply bg-grey-lighter;
@apply py-2;
}
</style>
<script>
import { search_channel } from "./socket";
export default {
data () {
return {
searchvalue: "",
selected: {
category: null,
item: null,
},
// Array indexes in the categories
idx_name: 0,
idx_items: 1,
}
},
props: {
results: {
type: Array,
default: function () {
return []
}
},
example: {
type: String,
default: ""
}
},
watch: {
example (value) {
this.searchvalue = value;
this.search();
}
},
methods: {
acceptResult (event) {
let result_url = this.results[this.selected.category][this.idx_items][this.selected.item].url;
window.location = result_url;
},
ariaExpanded (event) {
return this.results.length > 0;
},
classSelected (idx_category, idx_item) {
let res = "";
if (idx_category === this.selected.category && idx_item === this.selected.item) {
res = "selected";
}
return res
},
genItemId (idx_category, idx_item) {
return `item__${idx_category}_${idx_item}`
},
getItemIdSelected () {
return this.genItemId(this.selected.category, this.selected.item)
},
nextResult (event) {
if (this.results.length > 0) {
let ncategories = this.results.length;
if (this.selected.category === null) {
// If nothing selected yet, then select first element
this.selected.category = 0;
this.selected.item = 0;
} else {
// Already a selection, just go down.
let nitems = this.results[this.selected.category][this.idx_items].length;
if (this.selected.item === nitems - 1) { // on last item
if (this.selected.category < ncategories - 1) { // there are more categories
this.selected.category += 1;
this.selected.item = 0;
}
} else {
this.selected.item += 1;
}
}
}
},
previousResult (event) {
if (this.results.length > 0) { // there are some results
let ncategories = this.results.length;
if (this.selected.category === null) { // no selection yet
let category = ncategories - 1;
this.selected.category = category;
let nitems = this.results[category][this.idx_items].length;
this.selected.item = nitems - 1;
} else { // already a selection
if (this.selected.item === 0) { // first item of the category
if (this.selected.category > 0) { // can go one category up
let category = this.selected.category - 1;
this.selected.category = category;
let nitems = this.results[category][this.idx_items].length;
this.selected.item = nitems - 1; // last item of the category
}
} else { // not the first item of the category
this.selected.item -= 1;
}
}
}
},
kbdSearch (event) {
switch (event.key) {
case "ArrowDown":
case "ArrowUp":
break;
default:
this.search();
}
},
search () {
search_channel.push("query", {body: this.searchvalue});
this.selected.category = 0;
this.selected.item = 0;
},
},
}
</script>