1 L.Control.Zoomslider = L.Control.extend({
\r
4 // height in px of zoom-slider.png
\r
8 onAdd: function (map) {
\r
9 var className = 'leaflet-control-zoomslider',
\r
10 container = L.DomUtil.create('div', className);
\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
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
25 map.on('layeradd layerremove', this._refresh, this);
\r
27 map.whenReady(function(){
\r
28 this._snapToSliderValue();
\r
29 map.on('zoomend', this._snapToSliderValue, this);
\r
35 onRemove: function(map){
\r
36 map.off('zoomend', this._snapToSliderValue);
\r
37 map.off('layeradd layerremove', this._refresh);
\r
40 _refresh: function(){
\r
42 .removeControl(this)
\r
46 _createSlider: function (className, container, map) {
\r
47 var zoomLevels = map.getMaxZoom() - map.getMinZoom();
\r
48 this._sliderHeight = this.options.stepHeight * zoomLevels;
\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
55 this._draggable = this._createDraggable();
\r
56 this._draggable.enable();
\r
58 L.DomEvent.on(slider, 'click', this._onSliderClick, this);
\r
63 _zoomIn: function (e) {
\r
64 this._map.zoomIn(e.shiftKey ? 3 : 1);
\r
67 _zoomOut: function (e) {
\r
68 this._map.zoomOut(e.shiftKey ? 3 : 1);
\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
78 .on(link, 'click', L.DomEvent.preventDefault)
\r
79 .on(link, 'click', fn, context);
\r
84 _createDraggable: function() {
\r
85 L.DomUtil.setPosition(this._knob, new L.Point(0, 0));
\r
89 , L.DomEvent.stopPropagation)
\r
90 .on(this._knob, 'click', L.DomEvent.stopPropagation);
\r
92 var bounds = new L.Bounds(
\r
94 new L.Point(0, this._sliderHeight)
\r
96 var draggable = new L.BoundedDraggable(this._knob,
\r
99 .on('drag', this._snap, this)
\r
100 .on('dragend', this._setZoom, this);
\r
105 _snap : function(){
\r
106 this._snapToSliderValue(this._posToSliderValue());
\r
108 _setZoom: function() {
\r
109 this._map.setZoom(this._toZoomLevel(this._posToSliderValue()));
\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
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
123 _posToSliderValue: function(pos) {
\r
125 ? L.DomUtil.getPosition(this._knob).y
\r
127 return Math.round( (this._sliderHeight - pos) / this.options.stepHeight);
\r
130 _snapToSliderValue: function(sliderValue) {
\r
131 this._updateDisabled();
\r
133 sliderValue = isNaN(sliderValue)
\r
134 ? this._getSliderValue()
\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
141 _toZoomLevel: function(sliderValue) {
\r
142 return sliderValue + this._map.getMinZoom();
\r
144 _toSliderValue: function(zoomLevel) {
\r
145 return zoomLevel - this._map.getMinZoom();
\r
147 _getSliderValue: function(){
\r
148 return this._toSliderValue(this._map.getZoom());
\r
151 _updateDisabled: function () {
\r
152 var map = this._map,
\r
153 className = 'leaflet-control-zoomslider-disabled';
\r
155 L.DomUtil.removeClass(this._zoomInButton, className);
\r
156 L.DomUtil.removeClass(this._zoomOutButton, className);
\r
158 if (map.getZoom() === map.getMinZoom()) {
\r
159 L.DomUtil.addClass(this._zoomOutButton, className);
\r
161 if (map.getZoom() === map.getMaxZoom()) {
\r
162 L.DomUtil.addClass(this._zoomInButton, className);
\r
167 L.Map.mergeOptions({
\r
168 zoomControl: false,
\r
169 zoomsliderControl: true
\r
172 L.Map.addInitHook(function () {
\r
173 if (this.options.zoomsliderControl) {
\r
174 L.control.zoomslider().addTo(this);
\r
178 L.control.zoomslider = function (options) {
\r
179 return new L.Control.Zoomslider(options);
\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
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
198 closest.x = Math.max(closest.x, this._bounds.min.x);
\r
199 closest.y = Math.max(closest.y, this._bounds.min.y);
\r