UNPKG

5.2 kBJavaScriptView Raw
1import { Directive, EventEmitter, Input, KeyValueDiffers, NgZone, Output } from '@angular/core';
2import { LeafletUtil } from '../../core/leaflet.util';
3import { LeafletDirective } from '../../core/leaflet.directive';
4import { LeafletDirectiveWrapper } from '../../core/leaflet.directive.wrapper';
5import { LeafletControlLayersWrapper } from '../control/leaflet-control-layers.wrapper';
6/**
7 * Baselayers directive
8 *
9 * This directive is provided as a convenient way to add baselayers to the map. The input accepts
10 * a key-value map of layer name -> layer. Mutable changed are detected. On changes, a differ is
11 * used to determine what changed so that layers are appropriately added or removed. This directive
12 * will also add the layers control so users can switch between available base layers.
13 *
14 * To specify which layer to show as the 'active' baselayer, you will want to add it to the map
15 * using the layers directive. Otherwise, the plugin will use the last one it sees.
16 */
17var LeafletBaseLayersDirective = /** @class */ (function () {
18 function LeafletBaseLayersDirective(leafletDirective, differs, zone) {
19 this.differs = differs;
20 this.zone = zone;
21 // Output for once the layers control is ready
22 this.layersControlReady = new EventEmitter();
23 this.leafletDirective = new LeafletDirectiveWrapper(leafletDirective);
24 this.controlLayers = new LeafletControlLayersWrapper(this.zone, this.layersControlReady);
25 this.baseLayersDiffer = this.differs.find({}).create();
26 }
27 Object.defineProperty(LeafletBaseLayersDirective.prototype, "baseLayers", {
28 get: function () {
29 return this.baseLayersValue;
30 },
31 // Set/get baseLayers
32 set: function (v) {
33 this.baseLayersValue = v;
34 this.updateBaseLayers();
35 },
36 enumerable: false,
37 configurable: true
38 });
39 LeafletBaseLayersDirective.prototype.ngOnDestroy = function () {
40 this.baseLayers = {};
41 this.controlLayers.getLayersControl().remove();
42 };
43 LeafletBaseLayersDirective.prototype.ngOnInit = function () {
44 var _this = this;
45 // Init the map
46 this.leafletDirective.init();
47 // Create the control outside angular to prevent events from triggering chnage detection
48 this.zone.runOutsideAngular(function () {
49 // Initially configure the controlLayers
50 _this.controlLayers
51 .init({}, _this.layersControlOptions)
52 .addTo(_this.leafletDirective.getMap());
53 });
54 this.updateBaseLayers();
55 };
56 LeafletBaseLayersDirective.prototype.ngDoCheck = function () {
57 this.updateBaseLayers();
58 };
59 LeafletBaseLayersDirective.prototype.updateBaseLayers = function () {
60 var map = this.leafletDirective.getMap();
61 var layersControl = this.controlLayers.getLayersControl();
62 if (null != map && null != layersControl && null != this.baseLayersDiffer) {
63 var changes = this.baseLayersDiffer.diff(this.baseLayersValue);
64 var results = this.controlLayers.applyBaseLayerChanges(changes);
65 if (results.changed()) {
66 this.syncBaseLayer();
67 }
68 }
69 };
70 /**
71 * Check the current base layer and change it to the new one if necessary
72 */
73 LeafletBaseLayersDirective.prototype.syncBaseLayer = function () {
74 var _this = this;
75 var map = this.leafletDirective.getMap();
76 var layers = LeafletUtil.mapToArray(this.baseLayers);
77 var foundLayer;
78 // Search all the layers in the map to see if we can find them in the baselayer array
79 map.eachLayer(function (l) {
80 foundLayer = layers.find(function (bl) { return (l === bl); });
81 });
82 // Did we find the layer?
83 if (null != foundLayer) {
84 // Yes - set the baselayer to the one we found
85 this.baseLayer = foundLayer;
86 }
87 else {
88 // No - set the baselayer to the first in the array and add it to the map
89 if (layers.length > 0) {
90 this.baseLayer = layers[0];
91 // Add layers outside of angular to prevent events from triggering change detection
92 this.zone.runOutsideAngular(function () {
93 _this.baseLayer.addTo(map);
94 });
95 }
96 }
97 };
98 LeafletBaseLayersDirective.decorators = [
99 { type: Directive, args: [{
100 selector: '[leafletBaseLayers]'
101 },] }
102 ];
103 LeafletBaseLayersDirective.ctorParameters = function () { return [
104 { type: LeafletDirective },
105 { type: KeyValueDiffers },
106 { type: NgZone }
107 ]; };
108 LeafletBaseLayersDirective.propDecorators = {
109 baseLayers: [{ type: Input, args: ['leafletBaseLayers',] }],
110 layersControlOptions: [{ type: Input, args: ['leafletLayersControlOptions',] }],
111 layersControlReady: [{ type: Output, args: ['leafletLayersControlReady',] }]
112 };
113 return LeafletBaseLayersDirective;
114}());
115export { LeafletBaseLayersDirective };
116//# sourceMappingURL=leaflet-baselayers.directive.js.map
\No newline at end of file