UNPKG

3.84 kBJavaScriptView Raw
1import { Directive, Input, IterableDiffers, NgZone } from '@angular/core';
2import { LeafletDirective } from '../core/leaflet.directive';
3import { LeafletDirectiveWrapper } from '../core/leaflet.directive.wrapper';
4/**
5 * Layers directive
6 *
7 * This directive is used to directly control map layers. As changes are made to the input array of
8 * layers, the map is synched to the array. As layers are added or removed from the input array, they
9 * are also added or removed from the map. The input array is treated as immutable. To detect changes,
10 * you must change the array instance.
11 *
12 * Important Note: The input layers array is assumed to be immutable. This means you need to use an
13 * immutable array implementation or create a new copy of your array when you make changes, otherwise
14 * this directive won't detect the change. This is by design. It's for performance reasons. Change
15 * detection of mutable arrays requires diffing the state of the array on every DoCheck cycle, which
16 * is extremely expensive from a time complexity perspective.
17 *
18 */
19var LeafletLayersDirective = /** @class */ (function () {
20 function LeafletLayersDirective(leafletDirective, differs, zone) {
21 this.differs = differs;
22 this.zone = zone;
23 this.leafletDirective = new LeafletDirectiveWrapper(leafletDirective);
24 this.layersDiffer = this.differs.find([]).create();
25 }
26 Object.defineProperty(LeafletLayersDirective.prototype, "layers", {
27 get: function () {
28 return this.layersValue;
29 },
30 // Set/get the layers
31 set: function (v) {
32 this.layersValue = v;
33 // Now that we have a differ, do an immediate layer update
34 this.updateLayers();
35 },
36 enumerable: false,
37 configurable: true
38 });
39 LeafletLayersDirective.prototype.ngDoCheck = function () {
40 this.updateLayers();
41 };
42 LeafletLayersDirective.prototype.ngOnInit = function () {
43 // Init the map
44 this.leafletDirective.init();
45 // Update layers once the map is ready
46 this.updateLayers();
47 };
48 LeafletLayersDirective.prototype.ngOnDestroy = function () {
49 this.layers = [];
50 };
51 /**
52 * Update the state of the layers.
53 * We use an iterable differ to synchronize the map layers with the state of the bound layers array.
54 * This is important because it allows us to react to changes to the contents of the array as well
55 * as changes to the actual array instance.
56 */
57 LeafletLayersDirective.prototype.updateLayers = function () {
58 var map = this.leafletDirective.getMap();
59 if (null != map && null != this.layersDiffer) {
60 var changes_1 = this.layersDiffer.diff(this.layersValue);
61 if (null != changes_1) {
62 // Run outside angular to ensure layer events don't trigger change detection
63 this.zone.runOutsideAngular(function () {
64 changes_1.forEachRemovedItem(function (c) {
65 map.removeLayer(c.item);
66 });
67 changes_1.forEachAddedItem(function (c) {
68 map.addLayer(c.item);
69 });
70 });
71 }
72 }
73 };
74 LeafletLayersDirective.decorators = [
75 { type: Directive, args: [{
76 selector: '[leafletLayers]'
77 },] }
78 ];
79 LeafletLayersDirective.ctorParameters = function () { return [
80 { type: LeafletDirective },
81 { type: IterableDiffers },
82 { type: NgZone }
83 ]; };
84 LeafletLayersDirective.propDecorators = {
85 layers: [{ type: Input, args: ['leafletLayers',] }]
86 };
87 return LeafletLayersDirective;
88}());
89export { LeafletLayersDirective };
90//# sourceMappingURL=leaflet-layers.directive.js.map
\No newline at end of file