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
14 this._createButton('Zoom in', className + '-in'
\r
15 , container, this._zoomIn , this);
\r
16 this._createSlider(className + '-slider', container, map);
\r
17 this._createButton('Zoom out', className + '-out'
\r
18 , container, this._zoomOut, this);
\r
22 this._map.on('zoomend', this._snapToSliderValue, this);
\r
24 this._snapToSliderValue();
\r
28 onRemove: function(map){
\r
29 map.off('zoomend', this._snapToSliderValue);
\r
32 _createSlider: function (className, container, map) {
\r
33 var zoomLevels = map.getMaxZoom() - map.getMinZoom();
\r
34 this._sliderHeight = this.options.stepHeight * zoomLevels;
\r
36 var wrapper = L.DomUtil.create('div', className + '-wrap', container);
\r
37 wrapper.style.height = (this._sliderHeight + 5) + "px";
\r
38 var slider = L.DomUtil.create('div', className, wrapper);
\r
39 this._knob = L.DomUtil.create('div', className + '-knob', slider);
\r
41 this._draggable = this._createDraggable();
\r
42 this._draggable.enable();
\r
45 .on(slider, 'click', L.DomEvent.stopPropagation)
\r
46 .on(slider, 'click', L.DomEvent.preventDefault)
\r
47 .on(slider, 'click', this._onSliderClick, this);
\r
52 _zoomIn: function (e) {
\r
53 this._map.zoomIn(e.shiftKey ? 3 : 1);
\r
56 _zoomOut: function (e) {
\r
57 this._map.zoomOut(e.shiftKey ? 3 : 1);
\r
60 _createButton: function (title, className, container, fn, context) {
\r
61 var link = L.DomUtil.create('a', className, container);
\r
66 .on(link, 'click', L.DomEvent.stopPropagation)
\r
67 .on(link, 'click', L.DomEvent.preventDefault)
\r
68 .on(link, 'click', fn, context);
\r
73 _createDraggable: function() {
\r
74 L.DomUtil.setPosition(this._knob, new L.Point(0, 0));
\r
78 , L.DomEvent.stopPropagation)
\r
79 .on(this._knob, 'click', L.DomEvent.stopPropagation);
\r
81 var bounds = new L.Bounds(
\r
83 new L.Point(0, this._sliderHeight)
\r
85 var draggable = new L.BoundedDraggable(this._knob,
\r
88 .on('drag', this._snap, this)
\r
89 .on('dragend', this._setZoom, this);
\r
95 this._snapToSliderValue(this._posToSliderValue());
\r
97 _setZoom: function() {
\r
98 this._map.setZoom(this._toZoomLevel(this._posToSliderValue()));
\r
101 _onSliderClick: function(e){
\r
102 var first = (e.touches && e.touches.length === 1 ? e.touches[0] : e);
\r
103 var offset = first.offsetY
\r
105 : L.DomEvent.getMousePosition(first).y
\r
106 - L.DomUtil.getViewportOffset(this._knob).y;
\r
107 var value = this._posToSliderValue(offset - this._knob.offsetHeight / 2);
\r
108 this._snapToSliderValue(value);
\r
109 this._map.setZoom(this._toZoomLevel(value));
\r
112 _posToSliderValue: function(pos) {
\r
114 ? L.DomUtil.getPosition(this._knob).y
\r
116 return Math.round( (this._sliderHeight - pos) / this.options.stepHeight);
\r
119 _snapToSliderValue: function(sliderValue) {
\r
121 sliderValue = isNaN(sliderValue)
\r
122 ? this._getSliderValue()
\r
124 var y = this._sliderHeight
\r
125 - (sliderValue * this.options.stepHeight);
\r
126 L.DomUtil.setPosition(this._knob, new L.Point(0, y));
\r
129 _toZoomLevel: function(sliderValue) {
\r
130 return sliderValue + this._map.getMinZoom();
\r
132 _toSliderValue: function(zoomLevel) {
\r
133 return zoomLevel - this._map.getMinZoom();
\r
135 _getSliderValue: function(){
\r
136 return this._toSliderValue(this._map.getZoom());
\r
140 L.Map.mergeOptions({
\r
141 zoomControl: false,
\r
142 zoomsliderControl: true
\r
145 L.Map.addInitHook(function () {
\r
146 if (this.options.zoomsliderControl) {
\r
147 this.zoomsliderControl = new L.Control.Zoomslider();
\r
148 this.addControl(this.zoomsliderControl);
\r
152 L.control.zoomslider = function (options) {
\r
153 return new L.Control.Zoomslider(options);
\r
157 L.BoundedDraggable = L.Draggable.extend({
\r
158 initialize: function(element, dragStartTarget, bounds) {
\r
159 L.Draggable.prototype.initialize.call(this, element, dragStartTarget);
\r
160 this._bounds = bounds;
\r
161 this.on('predrag', function() {
\r
162 if(!this._bounds.contains(this._newPos)){
\r
163 this._newPos = this._fitPoint(this._newPos);
\r
167 _fitPoint: function(point){
\r
168 var closest = new L.Point(
\r
169 Math.min(point.x, this._bounds.max.x),
\r
170 Math.min(point.y, this._bounds.max.y)
\r
172 closest.x = Math.max(closest.x, this._bounds.min.x);
\r
173 closest.y = Math.max(closest.y, this._bounds.min.y);
\r