StaticLabel.js
1.9 KB
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
function StaticLabel(mvtFeature, ctx, latLng, style) {
var self = this;
this.mvtFeature = mvtFeature;
this.map = mvtFeature.map;
this.zoom = ctx.zoom;
this.latLng = latLng;
this.selected = false;
if (mvtFeature.linkedFeature) {
var linkedFeature = mvtFeature.linkedFeature();
if (linkedFeature && linkedFeature.selected) {
self.selected = true;
}
}
init(self, mvtFeature, ctx, latLng, style)
}
function init(self, mvtFeature, ctx, latLng, style) {
var ajaxData = mvtFeature.ajaxData;
var sty = self.style = style.staticLabel(mvtFeature, ajaxData);
var icon = self.icon = L.divIcon({
className: sty.cssClass || 'label-icon-text',
html: sty.html,
iconSize: sty.iconSize || [50,50]
});
self.marker = L.marker(latLng, {icon: icon}).addTo(self.map);
if (self.selected) {
self.marker._icon.classList.add(self.style.cssSelectedClass || 'label-icon-text-selected');
}
self.marker.on('click', function(e) {
self.toggle();
});
self.map.on('zoomend', function(e) {
var newZoom = e.target.getZoom();
if (self.zoom !== newZoom) {
self.map.removeLayer(self.marker);
}
});
}
StaticLabel.prototype.toggle = function() {
if (this.selected) {
this.deselect();
} else {
this.select();
}
};
StaticLabel.prototype.select = function() {
this.selected = true;
this.marker._icon.classList.add(this.style.cssSelectedClass || 'label-icon-text-selected');
var linkedFeature = this.mvtFeature.linkedFeature();
if (!linkedFeature.selected) linkedFeature.select();
};
StaticLabel.prototype.deselect = function() {
this.selected = false;
this.marker._icon.classList.remove(this.style.cssSelectedClass || 'label-icon-text-selected');
var linkedFeature = this.mvtFeature.linkedFeature();
if (linkedFeature.selected) linkedFeature.deselect();
};
StaticLabel.prototype.remove = function() {
if (!this.map || !this.marker) return;
this.map.removeLayer(this.marker);
};