-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathpx-map-behavior-control.es6.js
750 lines (649 loc) · 21.7 KB
/
px-map-behavior-control.es6.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/**
* @license
* Copyright (c) 2018, General Electric
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function() {
'use strict';
/****************************************************************************
* BEHAVIORS
****************************************************************************/
/* Ensures the behavior namespace is created */
window.PxMapBehavior = (window.PxMapBehavior || {});
/**
*
* @polymerBehavior PxMapBehavior.Control
*/
PxMapBehavior.ControlImpl = {
properties: {
/**
* Positions the control in one of the map corners. Choose from 'topright',
* 'topleft', 'bottomright', or 'bottomleft'.
*
* @type {String}
*/
position: {
type: String,
value: 'bottomright',
observer: 'shouldUpdateInst'
}
},
addInst(parent) {
this.elementInst.addTo(parent);
},
removeInst(parent) {
this.elementInst.remove();
},
getInstOptions() {
return {
position: this._getValidPosition()
}
},
_getValidPosition() {
const positionIsValid = (/^(topright|topleft|bottomright|bottomleft)$/.test(this.position));
if (!positionIsValid) {
console.log(`PX-MAP CONFIGURATION ERROR:
You entered an invalid \`position\` attribute '${this.position}' for ${this.is}.
Position must be a string with one of the following values:
- 'topright'
- 'topleft'
- 'bottomright'
- 'bottomleft'
Defaulting to 'bottomright'.`);
return 'bottomright';
}
return this.position;
}
};
/* Bind Control behavior */
/** @polymerBehavior */
PxMapBehavior.Control = [
PxMapBehavior.Layer,
PxMapBehavior.ControlImpl
];
/**
*
* @polymerBehavior PxMapBehavior.ZoomControl
*/
PxMapBehavior.ZoomControlImpl = {
properties: {
/**
* A valid IETF language tag as a string that `app-localize-behavior` will
* use to localize this component (see https://en.wikipedia.org/wiki/IETF_language_tag)
* for a list of valid tags.
*
* Examples:
* - 'en' (English)
* - 'es' (Spanish)
* - 'zh-cn' (Simplified Chinese)
*
* See https://github.com/PolymerElements/app-localize-behavior for API
* documentation and more information.
*
* @type {String}
*/
language: {
type: String,
value: 'en'
},
/**
* Object providing localized strings that `app-localize-behavior` will use
* to localize this component. The first key should be a valid IETF language
* tag, followed by key/value pairs for each string you need to localize.
* Settings can also be loaded from a locales.json file at the app level.
*
* For this component, the following keys can be localized:
* - 'Zoom in' - [en default] 'Zoom in'
* - 'Zoom out' - [en default] 'Zoom out'
*
* See https://github.com/PolymerElements/app-localize-behavior for API
* documentation and more information.
*
* @type {Object}
*/
resources: {
type: Object,
value: function() {
return {
'en': {'Zoom in': 'Zoom in', 'Zoom out': 'Zoom out'}
};
}
}
},
createInst(options) {
return new PxMap.ZoomControl(options);
},
updateInst(lastOptions, nextOptions) {
if (lastOptions.position !== nextOptions.position) {
this.elementInst.setPosition(nextOptions.position);
}
},
getInstOptions() {
const options = PxMapBehavior.ControlImpl.getInstOptions.call(this);
options.position = this.position;
options.zoomInText = '<px-icon icon="px-utl:add"></px-icon>'
options.zoomOutText = '<px-icon icon="px-utl:remove"></px-icon>'
// @TODO: An import order issue with the `AppLocalizeBehavior` mixin can
// cause the zoom control not to draw. Check if this.localize exists
// and can be called before doing so.
if (typeof this.localize === 'function') {
options.zoomInTitle = this.localize('Zoom in');
options.zoomOutTitle = this.localize('Zoom out');
}
return options;
}
};
/* Bind ZoomControl behavior */
/** @polymerBehavior */
PxMapBehavior.ZoomControl = [
Polymer.AppLocalizeBehavior,
PxMapBehavior.Control,
PxMapBehavior.ZoomControlImpl
];
/**
*
* @polymerBehavior PxMapBehavior.ScaleControl
*/
PxMapBehavior.ScaleControlImpl = {
properties: {
/**
* Shows an imperial unit scale (ft/mi) line if enabled. Multiple unit scales
* can be enabled to show multiple scales. If no units are enabled,
* the scale cannot be drawn.
*
* @type {Boolean}
*/
imperialUnits: {
type: Boolean,
value: false,
observer: 'shouldUpdateInst'
},
/**
* Shows a metric unit scale (m/km) line if enabled. Multiple unit scales
* can be enabled to show multiple scales. If no units are enabled,
* the scale cannot be drawn.
*
* @type {Boolean}
*/
metricUnits: {
type: Boolean,
value: false,
observer: 'shouldUpdateInst'
},
/**
* Enable this property to reverse the scale's colors, making it
* easier to read against a dark tile layer.
*
* @type {Boolean}
*/
reverseColors: {
type: Boolean,
value: false,
observer: 'shouldUpdateInst'
}
},
createInst(options) {
return new PxMap.ScaleControl(options);
},
updateInst(lastOptions, nextOptions) {
if (lastOptions.position !== nextOptions.position) {
this.elementInst.setPosition(nextOptions.position);
}
if (lastOptions.reverseColors !== nextOptions.reverseColors) {
this.elementInst.setReverseColors(nextOptions.reverseColors);
}
if (lastOptions.metric !== nextOptions.metric) {
this.elementInst.showMetric(nextOptions.metric);
}
if (lastOptions.imperial !== nextOptions.imperial) {
this.elementInst.showImperial(nextOptions.imperial);
}
},
getInstOptions() {
const options = PxMapBehavior.ControlImpl.getInstOptions.call(this);
options.imperial = this.imperialUnits;
options.metric = this.metricUnits;
options.reverseColors = this.reverseColors;
return options;
}
};
/* Bind ScaleControl behavior */
/** @polymerBehavior */
PxMapBehavior.ScaleControl = [
PxMapBehavior.Control,
PxMapBehavior.ScaleControlImpl
];
/**
*
* @polymerBehavior PxMapBehavior.LocateControl
*/
PxMapBehavior.LocateControlImpl = {
properties: {
/**
* A title for the locate button. Will be used to inform users with
* screen reading devices what the button does.
*
* @type {String}
*/
locateTitle: {
type: String,
value: 'Zoom to your location',
observer: 'shouldUpdateInst'
},
/**
* If enabled, the map will set its view center to the user's current
* location after a successful locate browser API call.
*
* @type {Boolean}
*/
moveToLocation: {
type: Boolean,
value: false
},
/**
* The maximum zoom level to set when the map moves to the user's location.
* The `moveToLocation` attribute must also be set for the map to move to the
* user's location after a location event.
*
* @type {Number}
*/
moveMaxZoom: {
type: Number,
observer: 'shouldUpdateInst'
},
/**
* After the user's location is successfully found, the result will be
* placed here. The object will have the following keys:
*
* - {Number} `lat` - User's found latitude
* - {Number} `lng` - User's found longitude
* - {Number} `timestamp` - Timestamp (UNIX format) for the location event
* - {Number} `accuracy` - The accuracy margin of error in meters from the centerpoint
* - {L.LatLngBouds} `bounds` - A bounding rectangle detailing the accuracy of the location
*
* @type {Object}
*/
lastFoundLocation: {
type: Object,
value: function(){ return {}; },
notify: true
}
},
addInst(parent) {
PxMapBehavior.ControlImpl.addInst.call(this, parent);
// Bind custom events for this control. Events will be unbound automatically.
const foundFn = this._handleLocationFound.bind(this);
const errorFn = this._handleLocationError.bind(this);
const tapFn = this._handleLocationTap.bind(this);
this.bindEvents({
'locationfound' : foundFn,
'locationerror' : errorFn,
'controlclick' : tapFn
});
},
removeInst(parent) {
PxMapBehavior.ControlImpl.removeInst.call(this, parent);
// Unbind events if necessary...
},
createInst(options) {
return new PxMap.LocateControl(options);
},
updateInst(lastOptions, nextOptions) {
if (lastOptions.position !== nextOptions.position) {
this.elementInst.setPosition(nextOptions.position);
}
if (lastOptions.moveMaxZoom !== nextOptions.moveMaxZoom) {
this.elementInst.setMoveMaxZoom(nextOptions.moveMaxZoom);
}
},
getInstOptions() {
return {
position: this.position,
locateIcon: '<px-icon icon="px-vis:crosshair"></px-icon>',
locateTitle: this.locateTitle,
moveToLocation: this.moveToLocation,
moveMaxZoom: this.moveMaxZoom
};
},
/**
* Bound to the control instance's `locationfound` event. Called when that
* event is fired. Parses the event details and fires an event from this
* component that the developer can handle. Also sets the `lastFoundLocation`
* property and notifies it.
*/
_handleLocationFound(evt) {
if (!evt) return;
const detail = {
lat: evt.latitude || null,
lng: evt.longitude || null,
timestamp: evt.timestamp || null,
bounds: evt.bounds || null
};
// Attempt to calculate the distance in meters from the center
// of the bounds to its NorthWest extent. This is our accuracy.
detail.accuracy = (evt.bounds.getCenter() && evt.bounds.getNorthWest()) ? evt.bounds.getCenter().distanceTo(evt.bounds.getNorthEast()) : null;
this.fire('px-map-control-locate-succeeded', detail);
this.set('lastFoundLocation', detail);
this.notifyPath('lastFoundLocation.*');
},
/**
* Fired after the user's location is successfully found.
*
* @event px-map-control-locate-succeeded
* @param {Object} detail
* @param {Number} detail.lat - The user's found latitude
* @param {Number} detail.lng - The user's found longitude
* @param {Number} detail.timestamp - The UNIX formatted timestamp detailing when the location was found
* @param {L.LatLngBouds} [detail.bounds] - A custom Leaflet object describing the visible bounds of the map after moving to the user's location, if available
* @param {Number} [detail.accuracy] - The margin of error of the accuracy in meters from its centerpoint to its maximum extent
*/
/**
* Bound to the control instance's `locationerror` event. Called when that
* event is fired. Parses the event details and fires an event from this
* component that the developer can handle.
*/
_handleLocationError(evt) {
if (!evt) return;
const detail = {
message: evt.message || null
};
this.fire('px-map-control-locate-failed', detail);
},
/**
* Fired after the control fails to find the user's location.
*
* @event px-map-control-locate-failed
* @param {Object} detail
* @param {String} detail.message - A message describing the reason for the failure
*/
/**
* Bound to the control instance's `controlclick` event. Called when that
* event is fired, but only handles the event if its `evt.action` is 'locate'.
* Fires an event from this component that the developer can handle.
*/
_handleLocationTap(evt) {
if (!evt || evt.action !== 'locate') return;
this.fire('px-map-locate-button-tapped');
}
/**
* Fired when the user clicks the locate button and initiates a location search.
*
* @event px-map-locate-button-tapped
*/
};
/* Bind LocateControl behavior */
/** @polymerBehavior */
PxMapBehavior.LocateControl = [
PxMapBehavior.Control,
PxMapBehavior.LocateControlImpl
];
/****************************************************************************
* KLASSES
****************************************************************************/
/* Ensures the klass namespace is created */
window.PxMap = (window.PxMap || {});
/**
*
* @class PxMap.ScaleControl
*/
class ScaleControl extends L.Control.Scale {
initialize(options) {
super.initialize(options);
}
onAdd(map) {
// Call default `onAdd` for scale to get the container
this.__scaleContainer = super.onAdd(map);
// Determine if we should add the reverse modifier CSS class
if (this.options.reverseColors === true) {
L.DomUtil.addClass(this.__scaleContainer, 'leaflet-control-scale--reverse');
}
return this.__scaleContainer;
}
onRemove(map) {
super.onRemove(map);
// Clean up scaleContainer reference
this.__scaleContainer = null;
}
/**
* Updates the `reverseColors` setting for the scale control. If the
* `shouldReverse` param doesn't match the current classes on the
* scale, updates the scale with the necessary classes.
*
* @param {Boolean} shouldReverse - If `true`, scale should be reversed. If `false`, it should not be.
*/
setReverseColors(shouldReverse) {
if (!this.__scaleContainer) return;
if (shouldReverse && !this.options.reverseColors) {
this.options.reverseColors = true;
L.DomUtil.addClass(this.__scaleContainer, 'leaflet-control-scale--reverse');
}
if (!shouldReverse && this.options.reverseColors) {
this.options.reverseColors = false;
L.DomUtil.removeClass(this.__scaleContainer, 'leaflet-control-scale--reverse');
}
}
/**
* Shows or hides the imperial unit scale.
*
* @param {Boolean} shouldShowImperial - If `true`, ensures imperial unit scale is visible.
*/
showImperial(shouldShowImperial) {
if (!this.__scaleContainer) return;
// No imperial scale exists, create one
if (shouldShowImperial && !this.options.imperial && !this._iScale) {
this._iScale = L.DomUtil.create('div', 'leaflet-control-scale-line', this.__scaleContainer);
this.options.imperial = true;
}
// We should remove the existing imperial scale
if (!shouldShowImperial && this.options.imperial && this._iScale) {
this.options.imperial = false;
L.DomUtil.remove(this._iScale);
this._iScale = null;
}
// Update the scale
this._update();
}
/**
* Shows or hides the metric unit scale.
*
* @param {Boolean} shouldShowMetric - If `true`, ensures metric unit scale is visible.
*/
showMetric(shouldShowMetric) {
if (!this.__scaleContainer) return;
// No metric scale exists, create one
if (shouldShowMetric && !this.options.metric && !this._mScale) {
this._mScale = L.DomUtil.create('div', 'leaflet-control-scale-line', this.__scaleContainer);
this.options.metric = true;
}
// We should remove the existing metric scale
if (!shouldShowMetric && this.options.metric && this._mScale) {
this.options.metric = false;
L.DomUtil.remove(this._mScale);
this._mScale = null;
}
// Update the scale
this._update();
}
};
/* Bind ScaleControl klass */
PxMap.ScaleControl = ScaleControl;
/**
*
* @class PxMap.ZoomControl
*/
class ZoomControl extends L.Control.Zoom {
_zoomIn(e) {
super._zoomIn(e);
if (this._map && this._map.fire) {
this._map.fire('controlclick', {
src: this,
action: 'zoomin'
})
}
}
_zoomOut(e) {
super._zoomOut(e);
if (this._map && this._map.fire) {
this._map.fire('controlclick', {
src: this,
action: 'zoomout'
});
}
}
_fireZoomClickEvt(evt) {
}
};
/* Bind ZoomControl klass */
PxMap.ZoomControl = ZoomControl;
/**
*
* @class PxMap.LocateControl
*/
class LocateControl extends L.Control {
initialize(options={}) {
const defaultOptions = {
position: 'bottomright',
className: '',
locateIcon: '<px-icon icon="px-utl:location"></px-icon>',
locateTitle: 'Zoom to your location',
locateTimeout: 10000,
moveToLocation: true,
moveMaxZoom: null
};
const composedOptions = Object.assign(defaultOptions, options);
L.Util.setOptions(this, composedOptions);
}
onAdd(map) {
const locateName = 'leaflet-control-locate';
this.__container = L.DomUtil.create('div', `${locateName} leaflet-bar ${this.options.className}`);
this.__locateButton = this._createButton(this.options.locateIcon, this.options.locateTitle, 'leaflet-control-locate-button', this.__container);
/* Bind map events */
L.DomEvent.on(map, 'locationfound', this._locationFound, this);
L.DomEvent.on(map, 'locationerror', this._locationError, this);
/* Bind button events */
L.DomEvent.disableClickPropagation(this.__locateButton);
L.DomEvent.on(this.__locateButton, 'click', L.DomEvent.stop);
L.DomEvent.on(this.__locateButton, 'click', this._locate, this);
L.DomEvent.on(this.__locateButton, 'click', this._refocusOnMap, this);
return this.__container;
}
onRemove(map) {
/* Unbind map events */
L.DomEvent.off(map, 'locationfound', this._locationFound, this);
L.DomEvent.off(map, 'locationerror', this._locationError, this);
/* Unbind button events */
L.DomEvent.off(this.__locateButton, 'click', L.DomEvent.stop);
L.DomEvent.off(this.__locateButton, 'click', this._locate, this);
L.DomEvent.off(this.__locateButton, 'click', this._refocusOnMap, this);
}
/**
* Sets the max zoom level after location is found
*
* @param {Number|String} zoom
*/
setMoveMaxZoom(zoom) {
if (typeof zoom === 'string' && zoom.length && !isNaN(zoom)) {
zoom = parseInt(zoom);
}
if (typeof zoom === 'number' && this.options.moveMaxZoom !== zoom) {
this.options.moveMaxZoom = zoom;
}
}
on(...args) {
if (!this._map) {
return undefined;
}
return this._map.on(...args);
}
off(...args) {
if (!this._map) {
return undefined;
}
return this._map.off(...args);
}
/**
* Internal method that calls the public `locate` method and fires an event
* to notify that the button has been clicked.
*/
_locate(evt) {
this._map.fire('controlclick', {
src: this,
action: 'locate'
});
this.locate(evt);
}
locate() {
this.__locating = true;
this._map.locate({
setView: this.options.moveToLocation,
maxZoom: this.options.moveMaxZoom,
timeout: this.options.locateTimeout
});
this._setLocatingState();
}
reset() {
this._setReadyState();
}
isDisabled() {
return this.__disabled || false;
}
_createButton(html, title, className, container, clickFn) {
const buttonEl = L.DomUtil.create('a', className, container);
buttonEl.innerHTML = html;
buttonEl.href = '#';
buttonEl.title = title;
// Tells screen readers to treat this as a button and read its title
buttonEl.setAttribute('role', 'button');
buttonEl.setAttribute('aria-label', title);
return buttonEl;
}
_locationFound(evt) {
if (this.__locating) {
this.__locating = false;
this._setReadyState();
}
}
_locationError(evt) {
if (this.__locating) {
this.__locating = false;
this._setReadyState();
}
}
_setLocatingState() {
if (!this.__locateButton || !this.__locating) return;
L.DomUtil.addClass(this.__locateButton, 'leaflet-control-locate-button--locating');
this.__disabled = true;
this._updateDisabled();
}
_setReadyState() {
if (!this.__locateButton || this.__locating) return;
this.__locateButton.innerHTML = this.options.locateIcon;
L.DomUtil.removeClass(this.__locateButton, 'leaflet-control-locate-button--locating');
L.DomUtil.removeClass(this.__locateButton, 'leaflet-control-locate-button--error');
this.__disabled = false;
this._updateDisabled();
}
_updateDisabled() {
if (!this.__locateButton) return;
if (this.__disabled) {
L.DomUtil.addClass(this.__locateButton, 'leaflet-control-locate-button--disabled');
}
if (!this.__disabled) {
L.DomUtil.removeClass(this.__locateButton, 'leaflet-control-locate-button--disabled');
}
}
};
/* Bind LocateControl klass */
PxMap.LocateControl = LocateControl;
})();