Mercurial > gemma
annotate client/src/application/Search.vue @ 1230:957907eaaa72
implemented context sensitive box below search bar (see: issue224)
author | Markus Kottlaender <markus@intevation.de> |
---|---|
date | Tue, 20 Nov 2018 08:47:53 +0100 |
parents | ba8cd80d68b6 |
children | 315e618a29fd |
rev | line source |
---|---|
1196 | 1 <template> |
2 <div :class="searchbarContainerStyle"> | |
1204
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
3 <div class="input-group-prepend"> |
1196 | 4 <span @click="toggleSearchbar" :class="searchButtonStyle" for="search"> |
5 <i class="fa fa-search d-print-none"></i> | |
6 </span> | |
7 </div> | |
8 <div class="searchgroup flex-fill"> | |
9 <input | |
10 @keyup.enter="takeFirstSearchresult" | |
11 v-if="showSearchbar" | |
12 id="search" | |
13 v-model="searchQuery" | |
14 type="text" | |
1204
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
15 :class="searchInputStyle" |
1196 | 16 > |
1230
957907eaaa72
implemented context sensitive box below search bar (see: issue224)
Markus Kottlaender <markus@intevation.de>
parents:
1217
diff
changeset
|
17 <ul v-if="showSearchbar && searchResults !== null && !showInContextBox" class="list-group d-print-none"> |
1196 | 18 <li v-for="entry of searchResults" :key="entry.name" class="list-group-item"> |
19 <a href="#" @click.prevent="moveToSearchResult(entry)">{{entry.name}}</a> | |
20 </li> | |
21 </ul> | |
22 </div> | |
23 </div> | |
24 </template> | |
25 | |
26 <style lang="sass" scoped> | |
27 .searchcontainer | |
28 height: $icon-height | |
29 | |
30 .searchbar-expanded | |
1207
70116d392387
close bottleneck list: made searchbar collapse only if it was collapsed before
Markus Kottlaender <markus@intevation.de>
parents:
1206
diff
changeset
|
31 min-width: 600px |
1196 | 32 .searchbar |
33 border-top-left-radius: 0 !important | |
34 border-bottom-left-radius: 0 !important | |
35 | |
36 .searchbar-collapsed | |
37 width: $icon-width !important | |
38 transition: $transition-fast | |
39 | |
40 .searchbar | |
41 height: $icon-height !important | |
1204
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
42 box-shadow: none !important |
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
43 &.rounded-top-right |
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
44 border-radius: 0 !important |
1213
9d93968db2cd
replaced custom css with bootstrap classes
Markus Kottlaender <markus@intevation.de>
parents:
1207
diff
changeset
|
45 border-top-right-radius: $border-radius !important |
1196 | 46 |
47 .searchlabel | |
1204
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
48 &.rounded-top-left |
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
49 border-radius: 0 !important |
1213
9d93968db2cd
replaced custom css with bootstrap classes
Markus Kottlaender <markus@intevation.de>
parents:
1207
diff
changeset
|
50 border-top-left-radius: $border-radius !important |
1196 | 51 |
52 .input-group-text | |
53 height: $icon-height | |
54 width: $icon-width | |
55 | |
56 .list-group | |
57 pointer-events: auto | |
1204
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
58 max-height: 40vh |
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
59 overflow: auto |
1196 | 60 </style> |
61 | |
62 <script> | |
63 /* | |
64 * This is Free Software under GNU Affero General Public License v >= 3.0 | |
65 * without warranty, see README.md and license for details. | |
66 * | |
67 * SPDX-License-Identifier: AGPL-3.0-or-later | |
68 * License-Filename: LICENSES/AGPL-3.0.txt | |
69 * | |
70 * Copyright (C) 2018 by via donau | |
71 * – Österreichische Wasserstraßen-Gesellschaft mbH | |
72 * Software engineering by Intevation GmbH | |
73 * | |
74 * Author(s): | |
75 * Markus Kottländer <markus.kottlaender@intevation.de> | |
76 */ | |
77 import debounce from "lodash.debounce"; | |
78 import { mapState } from "vuex"; | |
79 | |
80 import { displayError } from "../application/lib/errors.js"; | |
81 import { HTTP } from "../application/lib/http"; | |
82 | |
83 const setFocus = () => document.querySelector("#search").focus(); | |
84 | |
85 export default { | |
86 name: "search", | |
87 data() { | |
88 return { | |
89 searchQueryIsDirty: false, | |
90 searchResults: null, | |
91 isSearching: false | |
92 }; | |
93 }, | |
94 computed: { | |
1230
957907eaaa72
implemented context sensitive box below search bar (see: issue224)
Markus Kottlaender <markus@intevation.de>
parents:
1217
diff
changeset
|
95 ...mapState("application", ["showSearchbar", "showInContextBox"]), |
1204
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
96 searchQuery: { |
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
97 get() { |
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
98 return this.$store.state.application.searchQuery; |
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
99 }, |
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
100 set(value) { |
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
101 this.$store.commit("application/searchQuery", value); |
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
102 } |
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
103 }, |
1196 | 104 searchIndicator: function() { |
105 if (this.isSearching) { | |
106 return "⟳"; | |
107 } else if (this.searchQueryIsDirty) { | |
108 return ""; | |
109 } else { | |
110 return "✓"; | |
111 } | |
112 }, | |
113 searchbarContainerStyle() { | |
114 return [ | |
1217
ba8cd80d68b6
made more use of bootstrap classes instead of custom css
Markus Kottlaender <markus@intevation.de>
parents:
1213
diff
changeset
|
115 "input-group searchcontainer ml-3 shadow", |
1196 | 116 { |
117 "searchbar-collapsed": !this.showSearchbar, | |
1204
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
118 "searchbar-expanded": this.showSearchbar, |
1230
957907eaaa72
implemented context sensitive box below search bar (see: issue224)
Markus Kottlaender <markus@intevation.de>
parents:
1217
diff
changeset
|
119 "d-flex": this.showInContextBox !== "imports", |
957907eaaa72
implemented context sensitive box below search bar (see: issue224)
Markus Kottlaender <markus@intevation.de>
parents:
1217
diff
changeset
|
120 "d-none": this.showInContextBox === "imports" |
1196 | 121 } |
122 ]; | |
123 }, | |
1204
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
124 searchInputStyle() { |
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
125 return [ |
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
126 "form-control ui-element search searchbar d-print-none border-0", |
1230
957907eaaa72
implemented context sensitive box below search bar (see: issue224)
Markus Kottlaender <markus@intevation.de>
parents:
1217
diff
changeset
|
127 { "rounded-top-right": this.showInContextBox } |
1204
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
128 ]; |
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
129 }, |
1196 | 130 searchButtonStyle() { |
131 return [ | |
1217
ba8cd80d68b6
made more use of bootstrap classes instead of custom css
Markus Kottlaender <markus@intevation.de>
parents:
1213
diff
changeset
|
132 "ui-element input-group-text p-0 d-flex border-0 justify-content-center searchlabel bg-white d-print-none", |
1196 | 133 { |
134 rounded: !this.showSearchbar, | |
1204
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
135 "rounded-left": this.showSearchbar, |
1230
957907eaaa72
implemented context sensitive box below search bar (see: issue224)
Markus Kottlaender <markus@intevation.de>
parents:
1217
diff
changeset
|
136 "rounded-top-left": this.showSearchbar && this.showInContextBox |
1196 | 137 } |
138 ]; | |
139 } | |
140 }, | |
141 watch: { | |
142 searchQuery: function() { | |
143 this.searchQueryIsDirty = true; | |
144 this.triggerSearch(); | |
145 } | |
146 }, | |
147 methods: { | |
148 takeFirstSearchresult() { | |
149 if (!this.searchResults || this.searchResults.length != 1) return; | |
150 this.moveToSearchResult(this.searchResults[0]); | |
151 }, | |
152 triggerSearch: debounce(function() { | |
153 this.doSearch(); | |
154 }, 500), | |
155 doSearch() { | |
156 this.isCalculating = true; | |
157 this.searchResults = null; | |
158 | |
159 if (this.searchQuery == "") { | |
160 return; | |
161 } | |
162 | |
163 HTTP.post( | |
164 "/search", | |
165 { string: this.searchQuery }, | |
166 { | |
167 headers: { | |
168 "X-Gemma-Auth": localStorage.getItem("token"), | |
169 "Content-type": "text/xml; charset=UTF-8" | |
170 } | |
171 } | |
172 ) | |
173 .then(response => { | |
174 // console.log("got:", response.data); | |
175 this.searchResults = response.data; | |
176 }) | |
177 .catch(error => { | |
178 const { status, data } = error.response; | |
179 displayError({ | |
180 title: "Backend Error", | |
181 message: `${status}: ${data.message || data}` | |
182 }); | |
183 }); | |
184 | |
185 this.isCalculating = false; | |
186 this.searchQueryIsDirty = false; | |
187 }, | |
188 moveToSearchResult(resultEntry) { | |
189 // DEBUG console.log("Moving to", resultEntry); | |
190 if (resultEntry.geom.type == "Point") { | |
191 let zoom = 11; | |
192 if (resultEntry.type === "bottleneck") zoom = 17; | |
193 if (resultEntry.type === "rhm") zoom = 15; | |
194 if (resultEntry.type === "city") zoom = 13; | |
195 | |
196 this.$store.commit("map/moveMap", { | |
197 coordinates: resultEntry.geom.coordinates, | |
198 zoom, | |
199 preventZoomOut: true | |
200 }); | |
201 } | |
202 // this.searchQuery = ""; // clear search query again | |
203 this.toggleSearchbar(); | |
204 }, | |
205 toggleSearchbar() { | |
1230
957907eaaa72
implemented context sensitive box below search bar (see: issue224)
Markus Kottlaender <markus@intevation.de>
parents:
1217
diff
changeset
|
206 if (!this.showInContextBox) { |
1204
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
207 if (!this.showSearchbar) { |
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
208 setTimeout(setFocus, 300); |
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
209 } |
ddfdf440da24
made searchbar contextual
Markus Kottlaender <markus@intevation.de>
parents:
1201
diff
changeset
|
210 this.$store.commit("application/showSearchbar", !this.showSearchbar); |
1196 | 211 } |
212 } | |
213 } | |
214 }; | |
215 </script> |