1 | import { Directive, EventEmitter, Input, KeyValueDiffers, NgZone, Output } from '@angular/core';
|
2 | import { LeafletUtil } from '../../core/leaflet.util';
|
3 | import { LeafletDirective } from '../../core/leaflet.directive';
|
4 | import { LeafletDirectiveWrapper } from '../../core/leaflet.directive.wrapper';
|
5 | import { LeafletControlLayersWrapper } from '../control/leaflet-control-layers.wrapper';
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var LeafletBaseLayersDirective = (function () {
|
18 | function LeafletBaseLayersDirective(leafletDirective, differs, zone) {
|
19 | this.differs = differs;
|
20 | this.zone = zone;
|
21 |
|
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 |
|
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 |
|
46 | this.leafletDirective.init();
|
47 |
|
48 | this.zone.runOutsideAngular(function () {
|
49 |
|
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 |
|
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 |
|
79 | map.eachLayer(function (l) {
|
80 | foundLayer = layers.find(function (bl) { return (l === bl); });
|
81 | });
|
82 |
|
83 | if (null != foundLayer) {
|
84 |
|
85 | this.baseLayer = foundLayer;
|
86 | }
|
87 | else {
|
88 |
|
89 | if (layers.length > 0) {
|
90 | this.baseLayer = layers[0];
|
91 |
|
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 | }());
|
115 | export { LeafletBaseLayersDirective };
|
116 |
|
\ | No newline at end of file |