This repository has been archived by the owner on Feb 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
311 lines (258 loc) · 8.3 KB
/
index.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
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
const Nanocomponent = require('nanocomponent')
const L = require('leaflet')
require('leaflet.markercluster')
require('leaflet.locatecontrol')
require('./lib/leaflet.zoomhome')(L)
const onIdle = require('on-idle')
const html = require('nanohtml')
const compare = require('nanocomponent/compare')
const markersLayer = L.markerClusterGroup()
class Carto extends Nanocomponent {
constructor (id, state, emit) {
super(id)
this.id = id
this.state = state
this.emit = emit
this._coords = [50.850340, 4.351710]
this._zoom = 15
this._map = null
this._scrollWheelZoom = false
this.items = [] // data items used to create markers and popups
this.selectedIndex = 0
this.mapbox = {
accessToken: '',
background: 'light'
}
this.markers = null
this._createMap = this._createMap.bind(this)
this._addMarkers = this._addMarkers.bind(this)
this._updateMap = this._updateMap.bind(this)
this._addControlPlaceholders = this._addControlPlaceholders.bind(this)
this.zoomtoselected = this.zoomtoselected.bind(this)
}
zoomtoselected (item = {}) {
const { _id } = item // get objectid
if (!_id) return
const selected = this.markers.find((o) => o.item._id === _id)
markersLayer.zoomToShowLayer(selected.marker, () => {
selected.marker.openPopup()
})
}
createElement (props) {
this._coords = props.coords
this._zoom = props.zoom
this.tiles = props.tiles
this.items = props.items
this.tilesAttribution = props.tilesAttribution
this.icons = props.icons
this.popupTemplate = props.popupTemplate
this.selectedIndex = props.selectedIndex
if (!this._map) {
this._element = html`<div id="map"></div>`
if (this._hasWindow) {
this._createMap()
this._addMarkers()
}
} else {
onIdle(() => {
this._updateMap()
})
}
return this._element
}
update (props) {
return props.coords[0] !== this._coords[0] ||
props.coords[1] !== this._coords[1] ||
compare(this.items, props.items)
}
unload () {
this._map.remove()
this._element = null
this._coords = null
this._map = null
}
_addMarkers () {
markersLayer.clearLayers()
const items = this.items
const { background = 'light' } = this.mapbox
const colorInvert = background === 'light' ? 'dark' : 'light'
const customOptions = {
'maxWidth': '240',
'className': 'custom'
}
const defaultIcon = L.divIcon({
className: 'default-marker-icon',
html: `
<svg viewBox="0 0 16 16" class="icon icon--lg icon-${colorInvert} icon-marker">
<use xlink:href="#icon-marker" />
</svg>
`,
iconSize: [36, 36],
iconAnchor: [18, 36],
popupAnchor: [0, -36]
})
const featuredIcon = L.divIcon({
className: 'featured-marker-icon',
html: `
<svg viewBox="0 0 16 16" class="icon icon--lg icon-${colorInvert} icon-marker">
<use xlink:href="#icon-marker-star" />
</svg>
`,
iconSize: [36, 36],
iconAnchor: [18, 36],
popupAnchor: [0, -36]
})
const markers = items.map((item) => {
const { lat, lng } = item.address.location
const marker = L.marker([lat, lng], { icon: item.featured ? featuredIcon : defaultIcon })
marker.bindPopup(this._customPopup(item), customOptions)
markersLayer.addLayer(marker)
return {
item,
marker
}
})
this.markers = markers
return markers
}
load () {
this._map.invalidateSize()
}
_addControlPlaceholders (map) {
const corners = map._controlCorners
const l = 'leaflet-'
const container = map._controlContainer
const createCorner = (vSide, hSide) => {
const className = l + vSide + ' ' + l + hSide
corners[vSide + hSide] = L.DomUtil.create('div', className, container)
}
createCorner('verticalcenter', 'left')
createCorner('verticalcenter', 'right')
}
_customPopup (item) {
const { url, title, cover } = item
const { streetName, streetNumber, zip, city } = item.address
const template = `
<a href=${url} target="_blank" rel="noopener" class="external">
<div class="cover">
<div class="image" style="background: url(${cover.src}) center center/cover no-repeat #333"></div>
</div>
<div class="title">
${title}
<svg viewBox="0 0 16 16" class="icon icon--sm icon-arrow-north-east">
<use xlink:href="#icon-arrow-north-east" />
</svg>
</div>
<div class="address">
${streetName}, ${streetNumber} ${zip} ${city}
</div>
</a>
`
return template
}
_createMap () {
const { background = 'light', accessToken } = this.mapbox
const defaultTiles = `https://api.mapbox.com/styles/v1/mapbox/${background}-v9/tiles/256/{z}/{x}/{y}?access_token=${accessToken}`
const defaultTilesAttribution = '© <a href="https://www.mapbox.com/map-feedback/">Mapbox</a>'
const tiles = this.tiles || defaultTiles
const tilesAttribution = this.tilesAttribution || defaultTilesAttribution
const mapboxFeedback = '<strong><a href="https://www.mapbox.com/map-feedback/" target="_blank" rel="noopener noreferrer">Improve this map</a></strong>'
const map = L.map(this._element, {
center: this._coords,
zoom: this._zoom,
zoomControl: false,
scrollWheelZoom: this._scrollWheelZoom
})
const tileLayer = L.tileLayer(tiles, {
attribution: `
${tilesAttribution} ©
<a href="https://www.openstreetmap.org/copyright">
OpenStreetMap
</a>
${!this.tiles ? mapboxFeedback : ''}
`,
minZoom: 0,
maxZoom: 20,
ext: 'png'
})
tileLayer.addTo(map)
map.on('zoomhome', (e) => this._updateMap())
/**
* Enable/disable scrollWheelZoom
*/
map.once('focus', () => map.scrollWheelZoom.enable())
map.on('click', () => {
if (map.scrollWheelZoom.enabled()) {
map.scrollWheelZoom.disable()
} else {
map.scrollWheelZoom.enable()
}
})
/**
* Init Leaflet.markercluster
* @link https://github.com/Leaflet/Leaflet.markercluster
*/
markersLayer.addTo(map)
/**
* How to locate leaflet zoom control in a desired position
* @link https://stackoverflow.com/questions/33614912/how-to-locate-leaflet-zoom-control-in-a-desired-position
*/
this._addControlPlaceholders(map) // How to locate leaflet zoom control in a desired position
L.control.scale({ position: 'verticalcenterright' }).addTo(map)
// Add locate.control
L.control.locate({
position: 'verticalcenterright',
setView: false,
icon: 'icon icon-marker',
iconLoading: 'icon icon-marker icon-marker--loading',
locateOptions: {
maxZoom: 10
}
}).addTo(map)
map.on('locationfound', e => {
const {latitude: lat, longitude: lng} = e
this.emit('locationfound', {lat, lng})
map.stopLocate()
})
/**
* Center leaflet popup AND marker to the map
* @link https://stackoverflow.com/questions/22538473/leaflet-center-popup-and-marker-to-the-map
*/
map.on('popupopen', e => {
const { popup } = e
const index = popup._source._index
const items = this.items
const selectedIndex = this.selectedIndex
if (index !== selectedIndex) {
this.emit('select', {
coords: this._coords,
item: items[index]
})
}
// find the pixel location on the map where the popup anchor is
const px = map.project(popup._latlng)
// find the height of the popup container,
// and divide by 2, subtract from the Y axis of marker location
px.y -= popup._container.clientHeight / 2
map.panTo(map.unproject(px), {animate: true}) //
})
const zoomHome = new L.Control.ZoomHome({
zoomHomeText: `
<svg viewBox="0 0 16 16" class="icon icon--xs icon-home">
<use xlink:href="#icon-home" />
</svg>
`
})
zoomHome.addTo(map)
this._map = map
}
_updateMap () {
const items = this.items
const selectedIndex = this.selectedIndex
this._addMarkers()
this._map.invalidateSize()
// this._map.setView(this._coords)
this.zoomtoselected(items[selectedIndex])
}
}
module.exports = Carto