]> git.openstreetmap.org Git - rails.git/blob - vendor/assets/leaflet/leaflet.zoom.js
6ce96757cefa5654c2ace0d0ff01804948a87ee6
[rails.git] / vendor / assets / leaflet / leaflet.zoom.js
1 L.Control.Zoomslider = L.Control.extend({\r
2         options: {\r
3                 position: 'topleft',\r
4                 // height in px of zoom-slider.png\r
5                 stepHeight: 9\r
6         },\r
7 \r
8         onAdd: function (map) {\r
9                 var className = 'leaflet-control-zoomslider',\r
10                                 container = L.DomUtil.create('div', className);\r
11 \r
12                 L.DomEvent\r
13                         .on(container, 'click', L.DomEvent.stopPropagation)\r
14                         .on(container, 'mousedown', L.DomEvent.stopPropagation)\r
15                         .on(container, 'dblclick', L.DomEvent.stopPropagation);\r
16                 \r
17                 this._map = map;\r
18 \r
19                 this._zoomInButton = this._createButton('+', 'Zoom in', className + '-in'\r
20                                                                                                 , container, this._zoomIn , this);\r
21                 this._createSlider(className + '-slider', container, map);\r
22                 this._zoomOutButton = this._createButton('-', 'Zoom out', className + '-out'\r
23                                                                                                  , container, this._zoomOut, this);\r
24                 \r
25                 map.on('layeradd layerremove', this._refresh, this);\r
26 \r
27                 map.whenReady(function(){\r
28                         this._snapToSliderValue();\r
29                         map.on('zoomend', this._snapToSliderValue, this);\r
30                 }, this);\r
31 \r
32                 return container;\r
33         },\r
34 \r
35         onRemove: function(map){\r
36                 map.off('zoomend', this._snapToSliderValue);\r
37                 map.off('layeradd layerremove', this._refresh);\r
38         },\r
39 \r
40         _refresh: function(){\r
41                 this._map\r
42                         .removeControl(this)\r
43                         .addControl(this);\r
44         },\r
45 \r
46         _createSlider: function (className, container, map) {\r
47                 var zoomLevels = map.getMaxZoom() - map.getMinZoom();\r
48                 this._sliderHeight = this.options.stepHeight * zoomLevels;\r
49 \r
50                 var wrapper =  L.DomUtil.create('div', className + '-wrap', container);\r
51                 wrapper.style.height = (this._sliderHeight + 5) + "px";\r
52                 var slider = L.DomUtil.create('div', className, wrapper);\r
53                 this._knob = L.DomUtil.create('div', className + '-knob', slider);\r
54 \r
55                 this._draggable = this._createDraggable();\r
56                 this._draggable.enable();\r
57 \r
58                 L.DomEvent.on(slider, 'click', this._onSliderClick, this);\r
59 \r
60                 return slider;\r
61         },\r
62 \r
63         _zoomIn: function (e) {\r
64             this._map.zoomIn(e.shiftKey ? 3 : 1);\r
65         },\r
66 \r
67         _zoomOut: function (e) {\r
68             this._map.zoomOut(e.shiftKey ? 3 : 1);\r
69         },\r
70 \r
71         _createButton: function (html, title, className, container, fn, context) {\r
72                 var link = L.DomUtil.create('a', className, container);\r
73                 link.innerHTML = html;\r
74                 link.href = '#';\r
75                 link.title = title;\r
76 \r
77                 L.DomEvent\r
78                     .on(link, 'click', L.DomEvent.preventDefault)\r
79                     .on(link, 'click', fn, context);\r
80 \r
81                 return link;\r
82         },\r
83 \r
84         _createDraggable: function() {\r
85                 L.DomUtil.setPosition(this._knob, new L.Point(0, 0));\r
86                 L.DomEvent\r
87                         .on(this._knob\r
88                                 , L.Draggable.START\r
89                                 , L.DomEvent.stopPropagation)\r
90                         .on(this._knob, 'click', L.DomEvent.stopPropagation);\r
91 \r
92                 var bounds = new L.Bounds(\r
93                         new L.Point(0, 0),\r
94                         new L.Point(0, this._sliderHeight)\r
95                 );\r
96                 var draggable = new L.BoundedDraggable(this._knob,\r
97                                                                                            this._knob,\r
98                                                                                            bounds)\r
99                         .on('drag', this._snap, this)\r
100                         .on('dragend', this._setZoom, this);\r
101 \r
102                 return draggable;\r
103         },\r
104 \r
105         _snap : function(){\r
106                 this._snapToSliderValue(this._posToSliderValue());\r
107         },\r
108         _setZoom: function() {\r
109                 this._map.setZoom(this._toZoomLevel(this._posToSliderValue()));\r
110         },\r
111 \r
112         _onSliderClick: function(e){\r
113                 var first = (e.touches && e.touches.length === 1 ? e.touches[0] : e);\r
114             var offset = first.offsetY\r
115                         ? first.offsetY\r
116                         : L.DomEvent.getMousePosition(first).y\r
117                         - L.DomUtil.getViewportOffset(this._knob).y;\r
118                 var value = this._posToSliderValue(offset - this._knob.offsetHeight / 2);\r
119                 this._snapToSliderValue(value);\r
120                 this._map.setZoom(this._toZoomLevel(value));\r
121         },\r
122 \r
123         _posToSliderValue: function(pos) {\r
124                 pos = isNaN(pos)\r
125                         ? L.DomUtil.getPosition(this._knob).y\r
126                         : pos;\r
127                 return Math.round( (this._sliderHeight - pos) / this.options.stepHeight);\r
128         },\r
129 \r
130         _snapToSliderValue: function(sliderValue) {\r
131                 this._updateDisabled();\r
132                 if(this._knob) {\r
133                         sliderValue = isNaN(sliderValue)\r
134                                 ? this._getSliderValue()\r
135                                 : sliderValue;\r
136                         var y = this._sliderHeight\r
137                                 - (sliderValue * this.options.stepHeight);\r
138                         L.DomUtil.setPosition(this._knob, new L.Point(0, y));\r
139                 }\r
140         },\r
141         _toZoomLevel: function(sliderValue) {\r
142                 return sliderValue + this._map.getMinZoom();\r
143         },\r
144         _toSliderValue: function(zoomLevel) {\r
145                 return zoomLevel - this._map.getMinZoom();\r
146         },\r
147         _getSliderValue: function(){\r
148                 return this._toSliderValue(this._map.getZoom());\r
149         },\r
150 \r
151         _updateDisabled: function () {\r
152                 var map = this._map,\r
153                         className = 'leaflet-control-zoomslider-disabled';\r
154 \r
155                 L.DomUtil.removeClass(this._zoomInButton, className);\r
156                 L.DomUtil.removeClass(this._zoomOutButton, className);\r
157 \r
158                 if (map.getZoom() === map.getMinZoom()) {\r
159                         L.DomUtil.addClass(this._zoomOutButton, className);\r
160                 }\r
161                 if (map.getZoom() === map.getMaxZoom()) {\r
162                         L.DomUtil.addClass(this._zoomInButton, className);\r
163                 }\r
164         }\r
165 });\r
166 \r
167 L.Map.mergeOptions({\r
168     zoomControl: false,\r
169     zoomsliderControl: true\r
170 });\r
171 \r
172 L.Map.addInitHook(function () {\r
173     if (this.options.zoomsliderControl) {\r
174                 L.control.zoomslider().addTo(this);\r
175         }\r
176 });\r
177 \r
178 L.control.zoomslider = function (options) {\r
179     return new L.Control.Zoomslider(options);\r
180 };\r
181 \r
182 \r
183 L.BoundedDraggable = L.Draggable.extend({\r
184         initialize: function(element, dragStartTarget, bounds) {\r
185                 L.Draggable.prototype.initialize.call(this, element, dragStartTarget);\r
186                 this._bounds = bounds;\r
187                 this.on('predrag', function() {\r
188                         if(!this._bounds.contains(this._newPos)){\r
189                                 this._newPos = this._fitPoint(this._newPos);\r
190                         }\r
191                 }, this);\r
192         },\r
193         _fitPoint: function(point){\r
194                 var closest = new L.Point(\r
195                         Math.min(point.x, this._bounds.max.x),\r
196                         Math.min(point.y, this._bounds.max.y)\r
197                 );\r
198                 closest.x = Math.max(closest.x, this._bounds.min.x);\r
199                 closest.y = Math.max(closest.y, this._bounds.min.y);\r
200                 return closest;\r
201         }\r
202 });\r