/
ArrowSVG.js
129 lines (98 loc) · 3.71 KB
/
ArrowSVG.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
DG.Entrance.Arrow.SVG = DG.SVG.extend({
getEvents: function () {
var events = {
move: this._update
};
if (this._zoomAnimated) {
events.zoomanim = this._animateZoom;
}
if (DG.Browser.ie) {
events.moveend = events.mousemove = events.zoomend = this._refresh; //JSAPI-3379
}
return events;
},
_initMarkers: function (layer) {
var marker, markerStyle,
optionsByZoom = layer.options.byZoom,
id = layer._markerId = 'arrow-marker-' + DG.Util.stamp(layer);
Object.keys(optionsByZoom).map(function (zoom) {
marker = DG.SVG.create('marker');
markerStyle = optionsByZoom[zoom].marker;
Object.keys(markerStyle)
.filter(function (key) {
return key !== 'polygon' && key !== 'path';
})
.forEach(function (key) {
marker.setAttribute(key, markerStyle[key]);
});
marker.id = id + '-' + zoom;
marker.setAttribute('orient', 'auto');
marker.setAttribute('markerUnits', 'userSpaceOnUse');
marker.setAttribute('stroke-width', '0');
if (markerStyle.path) {
marker.appendChild(this._getMarkerChild('path', markerStyle.path, layer));
}
if (markerStyle.polygon) {
marker.appendChild(this._getMarkerChild('polygon', markerStyle.polygon, layer));
}
layer._markers.push(marker);
this._getDefs().appendChild(marker);
}, this);
this._updateMarker(layer);
},
_getMarkerChild: function (type, options, layer) {
var markerPath = DG.SVG.create('path'),
vector = (type === 'path') ? 'd' : 'points';
markerPath.setAttribute(vector, options[vector]);
markerPath.setAttribute('fill', options.color ? options.color : layer.options.color);
return markerPath;
},
_getDefs: function () {
this._defs = this._defs || DG.SVG.create('defs');
if (!this._defs.parentNode) {
this._container.appendChild(this._defs);
}
return this._defs;
},
_updateMarker: function (layer) {
var zoom = layer._map.getZoom(),
bound = layer._map.getBounds(),
lastPoint = layer._latlngs[layer._latlngs.length - 1],
url = (zoom >= DG.Entrance.SHOW_FROM_ZOOM && bound.contains(lastPoint)) ? layer._markerId + '-' + zoom : '';
layer._path.setAttribute('marker-end', 'url(#' + url + ')');
},
_removeMarkers: function (layer) {
var defs = this._getDefs(),
markers = layer._markers;
if (!defs && !markers) { return; }
markers.forEach(function (marker) {
defs.removeChild(marker);
});
markers.length = 0;
},
_refresh: function () {
this._container.parentNode.insertBefore(this._container, this._container);
},
_updateStyle: function (layer) {
var path = layer._path,
options = layer.options;
DG.SVG.prototype._updateStyle.call(this, layer);
path.setAttribute('visibility', options.visibility);
layer._markers.forEach(function (marker) {
marker.setAttribute('fill-opacity', options.opacity);
});
this._updateMarker(layer);
}
});
L.Map.include({
getArrowRenderer: function () {
var renderer = this._arrowRenderer;
if (!renderer) {
renderer = this._arrowRenderer = new DG.Entrance.Arrow.SVG();
}
if (!this.hasLayer(renderer)) {
this.addLayer(renderer);
}
return renderer;
}
});