Subversion Repositories eFlore/Applications.moissonnage

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
7 delphine 1
L.Control.Zoomslider = (function(){
2
 
3
	var Knob = L.Draggable.extend({
4
		initialize: function (element, steps, stepHeight, knobHeight) {
5
			var sliderHeight = steps * stepHeight;
6
			L.Draggable.prototype.initialize.call(this, element, element);
7
 
8
			this._element = element;
9
			this._maxValue = steps - 1;
10
 
11
			// conversion parameters
12
			// the conversion is just a common linear function.
13
			this._k = -stepHeight;
14
			this._m = sliderHeight - (stepHeight + knobHeight) / 2;
15
 
16
			this.on('predrag', function() {
17
				this._newPos.x = 0;
18
				this._newPos.y = this._adjust(this._newPos.y);
19
			}, this);
20
		},
21
 
22
		_adjust: function (y) {
23
			var value = Math.round(this._toValue(y));
24
			value = Math.max(0, Math.min(this._maxValue, value));
25
			return this._toY(value);
26
		},
27
 
28
		// y = k*v + m
29
		_toY: function (value) {
30
			return this._k * value + this._m;
31
		},
32
		// v = (y - m) / k
33
		_toValue: function (y) {
34
			return (y - this._m) / this._k;
35
		},
36
 
37
		setPosition: function (y) {
38
			L.DomUtil.setPosition(this._element,
39
								  L.point(0, this._adjust(y)));
40
		},
41
 
42
		setValue: function (v) {
43
			this.setPosition(this._toY(v));
44
		},
45
 
46
		getValue: function () {
47
			return this._toValue(L.DomUtil.getPosition(this._element).y);
48
		}
49
	});
50
 
51
	var Zoomslider = L.Control.extend({
52
		options: {
53
			position: 'topleft',
54
			// Height of zoom-slider.png in px
55
			stepHeight: 9,
56
			// Height of the knob div in px
57
			knobHeight: 5,
58
			styleNS: 'leaflet-control-zoomslider'
59
		},
60
 
61
		onAdd: function (map) {
62
			var container = L.DomUtil.create('div', this.options.styleNS + ' leaflet-bar');
63
 
64
			L.DomEvent.disableClickPropagation(container);
65
 
66
			this._map = map;
67
 
68
			this._zoomInButton = this._createZoomButton(
69
				'in', 'top', container, this._zoomIn);
70
 
71
			this._sliderElem = L.DomUtil.create(
72
				'div',
73
				this.options.styleNS + "-slider leaflet-bar-part",
74
				container);
75
 
76
			this._zoomOutButton = this._createZoomButton(
77
				'out', 'bottom', container, this._zoomOut);
78
 
79
			map .on('layeradd layerremove', this._refresh, this)
80
				.on("zoomend", this._updateSlider, this)
81
				.on("zoomend", this._updateDisabled, this)
82
				.whenReady(this._createSlider, this)
83
				.whenReady(this._createKnob, this)
84
				.whenReady(this._updateSlider, this)
85
				.whenReady(this._updateDisabled, this);
86
 
87
			return container;
88
		},
89
 
90
		onRemove: function (map) {
91
			map .off("zoomend", this._updateSlider)
92
				.off("zoomend", this._updateDisabled)
93
				.off('layeradd layerremove', this._refresh);
94
		},
95
 
96
		_refresh: function () {
97
			// TODO: listen to zoomlevelschange-event instead in 0.6.x
98
			this._map
99
				.removeControl(this)
100
				.addControl(this);
101
		},
102
		_zoomLevels: function(){
103
			return this._map.getMaxZoom() - this._map.getMinZoom() + 1;
104
		},
105
 
106
		_createSlider: function () {
107
			var zoomLevels = this._zoomLevels();
108
 
109
			// No tilelayer probably
110
			if(zoomLevels == Infinity){
111
				return;
112
			}
113
 
114
			this._sliderBody = L.DomUtil.create('div',
115
												this.options.styleNS + '-slider-body',
116
												this._sliderElem);
117
			this._sliderBody.style.height
118
				= (this.options.stepHeight * zoomLevels) + "px";
119
			L.DomEvent.on(this._sliderBody, 'click', this._onSliderClick, this);
120
		},
121
 
122
		_createKnob: function () {
123
			var knobElem,
124
				zoomLevels = this._zoomLevels();
125
 
126
			// No tilelayer probably
127
			if(zoomLevels == Infinity) {
128
				return;
129
			}
130
 
131
			knobElem = L.DomUtil.create('div', this.options.styleNS + '-slider-knob',
132
										this._sliderBody);
133
			L.DomEvent.disableClickPropagation(knobElem);
134
 
135
			this._knob = new Knob(knobElem,
136
								  this._zoomLevels(),
137
								  this.options.stepHeight,
138
								  this.options.knobHeight)
139
				.on('dragend', this._updateZoom, this);
140
			this._knob.enable();
141
		},
142
 
143
		_onSliderClick: function (e) {
144
			var first = (e.touches && e.touches.length === 1 ? e.touches[0] : e);
145
			var y = L.DomEvent.getMousePosition(first).y
146
	  				- L.DomUtil.getViewportOffset(this._sliderBody).y; // Cache this?
147
			this._knob.setPosition(y);
148
			this._updateZoom();
149
		},
150
 
151
		_zoomIn: function (e) {
152
			this._map.zoomIn(e.shiftKey ? 3 : 1);
153
		},
154
		_zoomOut: function (e) {
155
			this._map.zoomOut(e.shiftKey ? 3 : 1);
156
		},
157
 
158
		_createZoomButton: function (zoomDir, end, container, fn) {
159
			var barPart = 'leaflet-bar-part',
160
				classDef = this.options.styleNS + '-' + zoomDir
161
					+ ' ' + barPart
162
					+ ' ' + barPart + '-' + end,
163
				title = 'Zoom ' + zoomDir,
164
				link = L.DomUtil.create('a', classDef, container);
165
			link.href = '#';
166
			link.title = title;
167
 
168
			L.DomEvent
169
				.on(link, 'click', L.DomEvent.preventDefault)
170
				.on(link, 'click', fn, this);
171
 
172
			return link;
173
		},
174
		_toZoomLevel: function (sliderValue) {
175
			return sliderValue + this._map.getMinZoom();
176
		},
177
		_toSliderValue: function (zoomLevel) {
178
			return zoomLevel - this._map.getMinZoom();
179
		},
180
 
181
		_updateZoom: function(){
182
			this._map.setZoom(this._toZoomLevel(this._knob.getValue()));
183
		},
184
		_updateSlider: function(){
185
			if(this._knob){
186
				this._knob.setValue(this._toSliderValue(this._map.getZoom()));
187
			}
188
		},
189
		_updateDisabled: function () {
190
			var map = this._map,
191
				className = this.options.styleNS + '-disabled';
192
 
193
			L.DomUtil.removeClass(this._zoomInButton, className);
194
			L.DomUtil.removeClass(this._zoomOutButton, className);
195
 
196
			if (map.getZoom() === map.getMinZoom()) {
197
				L.DomUtil.addClass(this._zoomOutButton, className);
198
			}
199
			if (map.getZoom() === map.getMaxZoom()) {
200
				L.DomUtil.addClass(this._zoomInButton, className);
201
			}
202
		}
203
	});
204
	return Zoomslider;
205
})();
206
 
207
L.Map.mergeOptions({
208
    zoomControl: false,
209
    zoomsliderControl: true
210
});
211
 
212
L.Map.addInitHook(function () {
213
    if (this.options.zoomsliderControl) {
214
		this.zoomsliderControl = new L.Control.Zoomslider();
215
		this.addControl(this.zoomsliderControl);
216
	}
217
});
218
 
219
L.control.zoomslider = function (options) {
220
    return new L.Control.Zoomslider(options);
221
};