forked from area17/twill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocationField.vue
executable file
·323 lines (288 loc) · 8.81 KB
/
LocationField.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<template>
<a17-inputframe :error="error" :note="note" :locale="locale" @localize="updateLocale" :label="label" :name="name" :required="required">
<div class="form__field" :class="textfieldClasses">
<input
type="search"
:placeholder="placeholder"
:name="name"
:id="name"
:disabled="disabled"
:required="required"
:readonly="readonly"
:autofocus="autofocus"
:autocomplete="autocomplete"
:value="address"
@focus="onFocus"
@blur="onBlur"
@input="onInput"
/>
<div v-if="showMap" class="form__field--showMap">
<a href="#" type="button" @click.prevent="toggleMap"><span v-svg symbol="location"></span><span v-html="mapMessage"></span></a>
</div>
<input type="hidden" :name="`${name}__lat`" :value="lat"/>
<input type="hidden" :name="`${name}__lng`" :value="lng"/>
</div>
<div class="form__mapContainer" v-if="showMap" v-show="isMapOpen"></div>
</a17-inputframe>
</template>
<script>
import isEqual from 'lodash/isEqual'
import InputMixin from '@/mixins/input'
import FormStoreMixin from '@/mixins/formStore'
import InputframeMixin from '@/mixins/inputFrame'
import LocaleMixin from '@/mixins/locale'
import { loadScript } from '@/utils/loader'
const MAPMESSAGE = {
show: 'Show map',
hide: 'Hide map'
}
const GOOGLEMAPURL = 'https://maps.googleapis.com/maps/api/js?libraries=places&key='
const APIKEY = window[process.env.VUE_APP_NAME].hasOwnProperty('APIKEYS') && window[process.env.VUE_APP_NAME].APIKEYS.hasOwnProperty('googleMapApi') ? window[process.env.VUE_APP_NAME].APIKEYS.googleMapApi : null
/* global google */
export default {
name: 'A17Locationfield',
mixins: [InputMixin, InputframeMixin, LocaleMixin, FormStoreMixin],
props: {
type: {
type: String,
default: 'text'
},
zoom: {
type: Number,
default: 15
},
showMap: {
type: Boolean,
default: true
},
openMap: {
type: Boolean,
default: false
},
initialLat: {
type: Number,
default: null
},
initialLng: {
type: Number,
default: null
}
},
data: function () {
return {
map: null,
autocompletePlace: null,
markers: [],
address: '',
beforeFocusAddress: '',
lat: this.initialLat,
lng: this.initialLng,
focused: false,
isMapOpen: this.openMap,
mapMessage: this.openMap ? MAPMESSAGE.hide : MAPMESSAGE.show
}
},
computed: {
value: {
get () {
return {
latlng: this.lat + '|' + this.lng,
address: this.address
}
},
set (value) {
const coord = value.latlng.split('|')
this.lat = parseFloat(coord[0])
this.lng = parseFloat(coord[coord.length - 1])
this.address = value.address
}
},
textfieldClasses: function () {
return {
's--focus': this.focused,
's--disabled': this.disabled
}
}
},
methods: {
updateFromStore: function (newValue) { // called from the formStore mixin
if (!isEqual(newValue, this.value)) {
this.value = newValue
this.clearMarkers()
if (this.address === '') {
this.lat = this.initialLat
this.lng = this.initialLng
}
if (this.lat && this.lng && this.map) {
const location = { lat: this.lat, lng: this.lng }
this.addMarker(location)
this.map.panTo(location)
}
}
},
onFocus: function (event) {
this.focused = true
this.beforeFocusAddress = this.address
this.$emit('focus')
},
onBlur: function (event) {
this.focused = false
if (this.address === '') {
this.clearMarkers()
this.lat = this.initialLat
this.lng = this.initialLng
}
// Only save into the store if something changed from the moment you focused the field
// see formStore mixin
if (this.beforeFocusAddress !== this.address) this.saveIntoStore()
this.$emit('blur')
},
onInput: function (event) {
const newValue = event.target.value
this.address = newValue
this.$emit('change', newValue)
},
onPlaceChanged: function () {
const place = this.autocompletePlace.getPlace()
this.clearMarkers()
this.clearLatLng()
if (place.geometry) {
const location = place.geometry.location
this.address = place.formatted_address
this.setLatLng(location)
if (this.map) {
this.addMarker(location)
this.map.panTo(location)
this.map.setZoom(this.zoom)
}
}
this.beforeFocusAddress = this.address
// see formStore mixin
this.saveIntoStore()
},
clearMarkers: function () {
for (let i = 0; i < this.markers.length; i++) {
if (this.markers[i]) {
this.markers[i].setMap(null)
}
}
this.markers = []
},
clearLatLng: function () {
this.lat = 0
this.lng = 0
},
addMarker: function (location) {
const marker = new google.maps.Marker({
position: location,
map: this.map
})
this.markers.push(marker)
},
setLatLng: function (latlng) {
this.lat = latlng.lat()
this.lng = latlng.lng()
},
toggleMap: function () {
this.isMapOpen = !this.isMapOpen
this.mapMessage = this.isMapOpen ? MAPMESSAGE.hide : MAPMESSAGE.show
if (!this.map && typeof google !== 'undefined') {
this.$nextTick(function () {
this.initMap()
})
}
},
initMap: function () {
const preset = this.lat + this.lng
const mapOptions = {
zoom: preset ? this.zoom : 1,
center: new google.maps.LatLng(this.lat, this.lng),
mapTypeControl: false,
panControl: false,
zoomControl: false,
streetViewControl: false
}
// Init google API here
const mapCanvas = document.createElement('div')
mapCanvas.className = 'form__map'
this.$el.querySelector('.form__mapContainer').appendChild(mapCanvas)
this.map = new google.maps.Map(mapCanvas, mapOptions)
if (preset) {
this.addMarker(new google.maps.LatLng(this.lat, this.lng))
}
},
initGeocoder: function () {
const self = this
// Create the autocomplete object and associate it with the UI input control.
this.autocompletePlace = new google.maps.places.Autocomplete(this.$el.querySelector('input[type="search"]'))
// When a place is selected
google.maps.event.addListener(this.autocompletePlace, 'place_changed', this.onPlaceChanged)
if (this.address === '' && this.lat && this.lng) {
const geocoder = new google.maps.Geocoder()
const location = { lat: this.lat, lng: this.lng }
// reverse geocoding
geocoder.geocode({
location: location
}, function (results, status) {
if (status === 'OK') {
if (results[1]) {
self.address = results[1].formatted_address
} else {
console.error('Geocoding - No results found')
}
} else {
console.error('Geocoding - Geocoder failed due to: ' + status)
}
})
}
},
initGoogleApi: function () {
this.initGeocoder()
if (this.showMap && this.isMapOpen) {
this.initMap()
}
}
},
mounted: function () {
if (typeof google !== 'undefined') {
this.initGoogleApi()
} else {
const id = 'google-map-api-script'
const src = GOOGLEMAPURL + APIKEY
loadScript(id, src, 'text/javascript')
.then(() => {
this.initGoogleApi()
})
}
},
beforeDestroy: function () {
if (typeof google !== 'undefined') google.maps.event.clearListeners(this.autocompletePlace, 'place_changed', this.onPlaceChanged)
}
}
</script>
<style lang="scss" scoped>
.form__field {
display: flex;
align-items: center;
padding: 0 15px;
input {
padding: 0;
}
.form__field--showMap {
a {
@include font-tiny();
display: flex;
align-items: center;
text-decoration: none;
color: $color__text--light;
transition: color 250ms;
&:hover {
color: $color__text--forms;
}
span {
margin-right: 5px;
}
}
}
}
</style>