Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 7 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
28 alex 1
/*
2
 *
3
 * Copyright (c) 2012, Kartena AB
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *
9
 * 1. Redistributions of source code must retain the above copyright notice, this
10
 *    list of conditions and the following disclaimer.
11
 * 2. Redistributions in binary form must reproduce the above copyright notice,
12
 *    this list of conditions and the following disclaimer in the documentation
13
 *    and/or other materials provided with the distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
 *
26
 */
27
 
7 delphine 28
L.Control.Zoomslider = (function(){
29
 
30
	var Knob = L.Draggable.extend({
31
		initialize: function (element, steps, stepHeight, knobHeight) {
32
			var sliderHeight = steps * stepHeight;
33
			L.Draggable.prototype.initialize.call(this, element, element);
34
 
35
			this._element = element;
36
			this._maxValue = steps - 1;
37
 
38
			// conversion parameters
39
			// the conversion is just a common linear function.
40
			this._k = -stepHeight;
41
			this._m = sliderHeight - (stepHeight + knobHeight) / 2;
42
 
43
			this.on('predrag', function() {
44
				this._newPos.x = 0;
45
				this._newPos.y = this._adjust(this._newPos.y);
46
			}, this);
47
		},
48
 
49
		_adjust: function (y) {
50
			var value = Math.round(this._toValue(y));
51
			value = Math.max(0, Math.min(this._maxValue, value));
52
			return this._toY(value);
53
		},
54
 
55
		// y = k*v + m
56
		_toY: function (value) {
57
			return this._k * value + this._m;
58
		},
59
		// v = (y - m) / k
60
		_toValue: function (y) {
61
			return (y - this._m) / this._k;
62
		},
63
 
64
		setPosition: function (y) {
65
			L.DomUtil.setPosition(this._element,
66
								  L.point(0, this._adjust(y)));
67
		},
68
 
69
		setValue: function (v) {
70
			this.setPosition(this._toY(v));
71
		},
72
 
73
		getValue: function () {
74
			return this._toValue(L.DomUtil.getPosition(this._element).y);
75
		}
76
	});
77
 
78
	var Zoomslider = L.Control.extend({
79
		options: {
80
			position: 'topleft',
81
			// Height of zoom-slider.png in px
82
			stepHeight: 9,
83
			// Height of the knob div in px
84
			knobHeight: 5,
85
			styleNS: 'leaflet-control-zoomslider'
86
		},
87
 
88
		onAdd: function (map) {
89
			var container = L.DomUtil.create('div', this.options.styleNS + ' leaflet-bar');
90
 
91
			L.DomEvent.disableClickPropagation(container);
92
 
93
			this._map = map;
94
 
95
			this._zoomInButton = this._createZoomButton(
96
				'in', 'top', container, this._zoomIn);
97
 
98
			this._sliderElem = L.DomUtil.create(
99
				'div',
100
				this.options.styleNS + "-slider leaflet-bar-part",
101
				container);
102
 
103
			this._zoomOutButton = this._createZoomButton(
104
				'out', 'bottom', container, this._zoomOut);
105
 
106
			map .on('layeradd layerremove', this._refresh, this)
107
				.on("zoomend", this._updateSlider, this)
108
				.on("zoomend", this._updateDisabled, this)
109
				.whenReady(this._createSlider, this)
110
				.whenReady(this._createKnob, this)
111
				.whenReady(this._updateSlider, this)
112
				.whenReady(this._updateDisabled, this);
113
 
114
			return container;
115
		},
116
 
117
		onRemove: function (map) {
118
			map .off("zoomend", this._updateSlider)
119
				.off("zoomend", this._updateDisabled)
120
				.off('layeradd layerremove', this._refresh);
121
		},
122
 
123
		_refresh: function () {
124
			// TODO: listen to zoomlevelschange-event instead in 0.6.x
125
			this._map
126
				.removeControl(this)
127
				.addControl(this);
128
		},
129
		_zoomLevels: function(){
130
			return this._map.getMaxZoom() - this._map.getMinZoom() + 1;
131
		},
132
 
133
		_createSlider: function () {
134
			var zoomLevels = this._zoomLevels();
135
 
136
			// No tilelayer probably
137
			if(zoomLevels == Infinity){
138
				return;
139
			}
140
 
141
			this._sliderBody = L.DomUtil.create('div',
142
												this.options.styleNS + '-slider-body',
143
												this._sliderElem);
144
			this._sliderBody.style.height
145
				= (this.options.stepHeight * zoomLevels) + "px";
146
			L.DomEvent.on(this._sliderBody, 'click', this._onSliderClick, this);
147
		},
148
 
149
		_createKnob: function () {
150
			var knobElem,
151
				zoomLevels = this._zoomLevels();
152
 
153
			// No tilelayer probably
154
			if(zoomLevels == Infinity) {
155
				return;
156
			}
157
 
158
			knobElem = L.DomUtil.create('div', this.options.styleNS + '-slider-knob',
159
										this._sliderBody);
160
			L.DomEvent.disableClickPropagation(knobElem);
161
 
162
			this._knob = new Knob(knobElem,
163
								  this._zoomLevels(),
164
								  this.options.stepHeight,
165
								  this.options.knobHeight)
166
				.on('dragend', this._updateZoom, this);
167
			this._knob.enable();
168
		},
169
 
170
		_onSliderClick: function (e) {
171
			var first = (e.touches && e.touches.length === 1 ? e.touches[0] : e);
172
			var y = L.DomEvent.getMousePosition(first).y
173
	  				- L.DomUtil.getViewportOffset(this._sliderBody).y; // Cache this?
174
			this._knob.setPosition(y);
175
			this._updateZoom();
176
		},
177
 
178
		_zoomIn: function (e) {
179
			this._map.zoomIn(e.shiftKey ? 3 : 1);
180
		},
181
		_zoomOut: function (e) {
182
			this._map.zoomOut(e.shiftKey ? 3 : 1);
183
		},
184
 
185
		_createZoomButton: function (zoomDir, end, container, fn) {
186
			var barPart = 'leaflet-bar-part',
187
				classDef = this.options.styleNS + '-' + zoomDir
188
					+ ' ' + barPart
189
					+ ' ' + barPart + '-' + end,
190
				title = 'Zoom ' + zoomDir,
191
				link = L.DomUtil.create('a', classDef, container);
192
			link.href = '#';
193
			link.title = title;
194
 
195
			L.DomEvent
196
				.on(link, 'click', L.DomEvent.preventDefault)
197
				.on(link, 'click', fn, this);
198
 
199
			return link;
200
		},
201
		_toZoomLevel: function (sliderValue) {
202
			return sliderValue + this._map.getMinZoom();
203
		},
204
		_toSliderValue: function (zoomLevel) {
205
			return zoomLevel - this._map.getMinZoom();
206
		},
207
 
208
		_updateZoom: function(){
209
			this._map.setZoom(this._toZoomLevel(this._knob.getValue()));
210
		},
211
		_updateSlider: function(){
212
			if(this._knob){
213
				this._knob.setValue(this._toSliderValue(this._map.getZoom()));
214
			}
215
		},
216
		_updateDisabled: function () {
217
			var map = this._map,
218
				className = this.options.styleNS + '-disabled';
219
 
220
			L.DomUtil.removeClass(this._zoomInButton, className);
221
			L.DomUtil.removeClass(this._zoomOutButton, className);
222
 
223
			if (map.getZoom() === map.getMinZoom()) {
224
				L.DomUtil.addClass(this._zoomOutButton, className);
225
			}
226
			if (map.getZoom() === map.getMaxZoom()) {
227
				L.DomUtil.addClass(this._zoomInButton, className);
228
			}
229
		}
230
	});
231
	return Zoomslider;
232
})();
233
 
234
L.Map.mergeOptions({
235
    zoomControl: false,
236
    zoomsliderControl: true
237
});
238
 
239
L.Map.addInitHook(function () {
240
    if (this.options.zoomsliderControl) {
241
		this.zoomsliderControl = new L.Control.Zoomslider();
242
		this.addControl(this.zoomsliderControl);
243
	}
244
});
245
 
246
L.control.zoomslider = function (options) {
247
    return new L.Control.Zoomslider(options);
248
};