[PLUGINS] ~maj globale
[lhc/web/www.git] / www / plugins / gis / lib / leaflet / plugins / Marker.Rotate.js
1 /*
2 * Based on comments by @runanet and @coomsie
3 * https://github.com/CloudMade/Leaflet/issues/386
4 *
5 * Wrapping function is needed to preserve L.Marker.update function
6 */
7 (function () {
8 var _old__setPos = L.Marker.prototype._setPos;
9 L.Marker.include({
10 _updateImg: function (i, a, s) {
11 a = L.point(s).divideBy(2)._subtract(L.point(a));
12 var transform = '';
13 transform += ' translate(' + -a.x + 'px, ' + -a.y + 'px)';
14 transform += ' rotate(' + this.options.iconAngle + 'deg)';
15 transform += ' translate(' + a.x + 'px, ' + a.y + 'px)';
16 i.style[L.DomUtil.TRANSFORM] += transform;
17 },
18
19 _getShortestEndDegree: function (startDegrees, endDegrees) {
20 var turnAngle = Math.abs(endDegrees - startDegrees);
21 var turnAnglePositive = (endDegrees - startDegrees) >= 0;
22 if (turnAngle <= 180) return endDegrees;
23 var result = startDegrees + (360 - turnAngle) * (turnAnglePositive ? -1 : 1);
24 return result;
25 },
26
27 setIconAngle: function (iconAngle) {
28 // find shortest angle to turn over
29 this.options.iconAngle = this._getShortestEndDegree(this.options.iconAngle || 0, iconAngle);
30 if (this._map)
31 this.update();
32 },
33
34 _setPos: function (pos) {
35 if (this._icon)
36 this._icon.style[L.DomUtil.TRANSFORM] = '';
37 if (this._shadow)
38 this._shadow.style[L.DomUtil.TRANSFORM] = '';
39
40 _old__setPos.apply(this,[pos]);
41
42 if (this.options.iconAngle) {
43 var defaultIcon = new L.Icon.Default();
44 var a = this.options.icon.options.iconAnchor || defaultIcon.options.iconAnchor;
45 var s = this.options.icon.options.iconSize || defaultIcon.options.iconSize;
46 var i;
47 if (this._icon) {
48 i = this._icon;
49 this._updateImg(i, a, s);
50 }
51 if (this._shadow) {
52 if (this.options.icon.options.shadowAnchor)
53 a = this.options.icon.options.shadowAnchor;
54 s = this.options.icon.options.shadowSize;
55 i = this._shadow;
56 this._updateImg(i, a, s);
57 }
58 }
59 }
60 });
61 }());