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
12 L.DomEvent.disableClickPropagation(container);
\r
16 this._zoomInButton = this._createButton('Zoom in', className + '-in'
\r
17 , container, this._zoomIn , this);
\r
18 this._createSlider(className + '-slider', container, map);
\r
19 this._zoomOutButton = this._createButton('Zoom out', className + '-out'
\r
20 , container, this._zoomOut, this);
\r
22 map.on('layeradd layerremove', this._refresh, this);
\r
24 map.whenReady(function(){
\r
25 this._snapToSliderValue();
\r
26 map.on('zoomend', this._snapToSliderValue, this);
\r
32 onRemove: function(map){
\r
33 map.off('zoomend', this._snapToSliderValue);
\r
34 map.off('layeradd layerremove', this._refresh);
\r
37 _refresh: function(){
\r
39 .removeControl(this)
\r
43 _createSlider: function (className, container, map) {
\r
44 var zoomLevels = map.getMaxZoom() - map.getMinZoom();
\r
45 // This means we have no tilelayers (or that they are setup in a strange way).
\r
46 // Either way we don't want to add a slider here.
\r
47 if(zoomLevels == Infinity){
\r
50 this._sliderHeight = this.options.stepHeight * zoomLevels;
\r
52 var wrapper = L.DomUtil.create('div', className + '-wrap', container);
\r
53 wrapper.style.height = (this._sliderHeight + 5) + "px";
\r
54 var slider = L.DomUtil.create('div', className, wrapper);
\r
55 this._knob = L.DomUtil.create('div', className + '-knob', slider);
\r
57 this._draggable = this._createDraggable();
\r
58 this._draggable.enable();
\r
60 L.DomEvent.on(slider, 'click', this._onSliderClick, this);
\r
65 _zoomIn: function (e) {
\r
66 this._map.zoomIn(e.shiftKey ? 3 : 1);
\r
69 _zoomOut: function (e) {
\r
70 this._map.zoomOut(e.shiftKey ? 3 : 1);
\r
73 _createButton: function (title, className, container, fn, context) {
\r
74 var link = L.DomUtil.create('a', className, container);
\r
79 .on(link, 'click', L.DomEvent.preventDefault)
\r
80 .on(link, 'click', fn, context);
\r
85 _createDraggable: function() {
\r
86 L.DomUtil.setPosition(this._knob, L.point(0, 0));
\r
87 L.DomEvent.disableClickPropagation(this._knob);
\r
89 var bounds = new L.Bounds(
\r
91 L.point(0, this._sliderHeight)
\r
93 var draggable = new L.BoundedDraggable(this._knob,
\r
96 .on('drag', this._snap, this)
\r
97 .on('dragend', this._setZoom, this);
\r
102 _snap : function(){
\r
103 this._snapToSliderValue(this._posToSliderValue());
\r
105 _setZoom: function() {
\r
106 this._map.setZoom(this._toZoomLevel(this._posToSliderValue()));
\r
109 _onSliderClick: function(e){
\r
110 var first = (e.touches && e.touches.length === 1 ? e.touches[0] : e);
\r
111 var offset = first.offsetY
\r
113 : L.DomEvent.getMousePosition(first).y
\r
114 - L.DomUtil.getViewportOffset(this._knob).y;
\r
115 var value = this._posToSliderValue(offset - this._knob.offsetHeight / 2);
\r
116 this._snapToSliderValue(value);
\r
117 this._map.setZoom(this._toZoomLevel(value));
\r
120 _posToSliderValue: function(pos) {
\r
122 ? L.DomUtil.getPosition(this._knob).y
\r
124 return Math.round( (this._sliderHeight - pos) / this.options.stepHeight);
\r
127 _snapToSliderValue: function(sliderValue) {
\r
128 this._updateDisabled();
\r
130 sliderValue = isNaN(sliderValue)
\r
131 ? this._getSliderValue()
\r
133 var y = this._sliderHeight
\r
134 - (sliderValue * this.options.stepHeight);
\r
135 L.DomUtil.setPosition(this._knob, L.point(0, y));
\r
138 _toZoomLevel: function(sliderValue) {
\r
139 return sliderValue + this._map.getMinZoom();
\r
141 _toSliderValue: function(zoomLevel) {
\r
142 return zoomLevel - this._map.getMinZoom();
\r
144 _getSliderValue: function(){
\r
145 return this._toSliderValue(this._map.getZoom());
\r
148 _updateDisabled: function () {
\r
149 var map = this._map,
\r
150 className = 'leaflet-control-zoomslider-disabled';
\r
152 L.DomUtil.removeClass(this._zoomInButton, className);
\r
153 L.DomUtil.removeClass(this._zoomOutButton, className);
\r
155 if (map.getZoom() === map.getMinZoom()) {
\r
156 L.DomUtil.addClass(this._zoomOutButton, className);
\r
158 if (map.getZoom() === map.getMaxZoom()) {
\r
159 L.DomUtil.addClass(this._zoomInButton, className);
\r
164 L.Map.mergeOptions({
\r
165 zoomControl: false,
\r
166 zoomsliderControl: true
\r
169 L.Map.addInitHook(function () {
\r
170 if (this.options.zoomsliderControl) {
\r
171 L.control.zoomslider().addTo(this);
\r
175 L.control.zoomslider = function (options) {
\r
176 return new L.Control.Zoomslider(options);
\r
180 L.BoundedDraggable = L.Draggable.extend({
\r
181 initialize: function(element, dragStartTarget, bounds) {
\r
182 L.Draggable.prototype.initialize.call(this, element, dragStartTarget);
\r
183 this._bounds = bounds;
\r
184 this.on('predrag', function() {
\r
185 if(!this._bounds.contains(this._newPos)){
\r
186 this._newPos = this._fitPoint(this._newPos);
\r
190 _fitPoint: function(point){
\r
191 var closest = L.point(
\r
192 Math.min(point.x, this._bounds.max.x),
\r
193 Math.min(point.y, this._bounds.max.y)
\r
195 closest.x = Math.max(closest.x, this._bounds.min.x);
\r
196 closest.y = Math.max(closest.y, this._bounds.min.y);
\r