1
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3
* full text of the license. */
7
* @requires OpenLayers/Control/PanZoom.js
11
* Class: OpenLayers.Control.PanZoomBar
12
* The PanZoomBar is a visible control composed of a
13
* <OpenLayers.Control.PanPanel> and a <OpenLayers.Control.ZoomBar>.
14
* By default it is displayed in the upper left corner of the map as 4
15
* directional arrows above a vertical slider.
18
* - <OpenLayers.Control.PanZoom>
20
OpenLayers.Control.PanZoomBar = OpenLayers.Class(OpenLayers.Control.PanZoom, {
23
* APIProperty: zoomStopWidth
28
* APIProperty: zoomStopHeight
38
* Property: sliderEvents
39
* {<OpenLayers.Events>}
44
* Property: zoombarDiv
51
* {<OpenLayers.Events>}
56
* APIProperty: zoomWorldIcon
62
* APIProperty: forceFixedZoomLevel
63
* {Boolean} Force a fixed zoom level even though the map has
66
forceFixedZoomLevel: false,
69
* Property: mouseDragStart
70
* {<OpenLayers.Pixel>}
76
* {<OpenLayers.Pixel>}
81
* Constructor: OpenLayers.Control.PanZoomBar
83
initialize: function() {
84
OpenLayers.Control.PanZoom.prototype.initialize.apply(this, arguments);
92
this._removeZoomBar();
95
"changebaselayer": this.redraw,
99
OpenLayers.Control.PanZoom.prototype.destroy.apply(this, arguments);
101
delete this.mouseDragStart;
102
delete this.zoomStart;
109
* map - {<OpenLayers.Map>}
111
setMap: function(map) {
112
OpenLayers.Control.PanZoom.prototype.setMap.apply(this, arguments);
113
this.map.events.register("changebaselayer", this, this.redraw);
118
* clear the div and start over.
121
if (this.div != null) {
122
this.removeButtons();
123
this._removeZoomBar();
132
* px - {<OpenLayers.Pixel>}
135
// initialize our internal div
136
OpenLayers.Control.prototype.draw.apply(this, arguments);
137
px = this.position.clone();
139
// place the controls
142
var sz = new OpenLayers.Size(18,18);
143
var centered = new OpenLayers.Pixel(px.x+sz.w/2, px.y);
144
var wposition = sz.w;
146
if (this.zoomWorldIcon) {
147
centered = new OpenLayers.Pixel(px.x+sz.w, px.y);
150
this._addButton("panup", "north-mini.png", centered, sz);
151
px.y = centered.y+sz.h;
152
this._addButton("panleft", "west-mini.png", px, sz);
153
if (this.zoomWorldIcon) {
154
this._addButton("zoomworld", "zoom-world-mini.png", px.add(sz.w, 0), sz);
158
this._addButton("panright", "east-mini.png", px.add(wposition, 0), sz);
159
this._addButton("pandown", "south-mini.png", centered.add(0, sz.h*2), sz);
160
this._addButton("zoomin", "zoom-plus-mini.png", centered.add(0, sz.h*3+5), sz);
161
centered = this._addZoomBar(centered.add(0, sz.h*4 + 5));
162
this._addButton("zoomout", "zoom-minus-mini.png", centered, sz);
167
* Method: _addZoomBar
170
* location - {<OpenLayers.Pixel>} where zoombar drawing is to start.
172
_addZoomBar:function(centered) {
173
var imgLocation = OpenLayers.Util.getImagesLocation();
175
var id = this.id + "_" + this.map.id;
176
var zoomsToEnd = this.map.getNumZoomLevels() - 1 - this.map.getZoom();
177
var slider = OpenLayers.Util.createAlphaImageDiv(id,
178
centered.add(-1, zoomsToEnd * this.zoomStopHeight),
179
new OpenLayers.Size(20,9),
180
imgLocation+"slider.png",
182
this.slider = slider;
184
this.sliderEvents = new OpenLayers.Events(this, slider, null, true,
186
this.sliderEvents.on({
187
"mousedown": this.zoomBarDown,
188
"mousemove": this.zoomBarDrag,
189
"mouseup": this.zoomBarUp,
190
"dblclick": this.doubleClick,
191
"click": this.doubleClick
194
var sz = new OpenLayers.Size();
195
sz.h = this.zoomStopHeight * this.map.getNumZoomLevels();
196
sz.w = this.zoomStopWidth;
199
if (OpenLayers.Util.alphaHack()) {
200
var id = this.id + "_" + this.map.id;
201
div = OpenLayers.Util.createAlphaImageDiv(id, centered,
202
new OpenLayers.Size(sz.w,
203
this.zoomStopHeight),
204
imgLocation + "zoombar.png",
205
"absolute", null, "crop");
206
div.style.height = sz.h + "px";
208
div = OpenLayers.Util.createDiv(
209
'OpenLayers_Control_PanZoomBar_Zoombar' + this.map.id,
212
imgLocation+"zoombar.png");
215
this.zoombarDiv = div;
217
this.divEvents = new OpenLayers.Events(this, div, null, true,
220
"mousedown": this.divClick,
221
"mousemove": this.passEventToSlider,
222
"dblclick": this.doubleClick,
223
"click": this.doubleClick
226
this.div.appendChild(div);
228
this.startTop = parseInt(div.style.top);
229
this.div.appendChild(slider);
231
this.map.events.register("zoomend", this, this.moveZoomBar);
233
centered = centered.add(0,
234
this.zoomStopHeight * this.map.getNumZoomLevels());
239
* Method: _removeZoomBar
241
_removeZoomBar: function() {
242
this.sliderEvents.un({
243
"mousedown": this.zoomBarDown,
244
"mousemove": this.zoomBarDrag,
245
"mouseup": this.zoomBarUp,
246
"dblclick": this.doubleClick,
247
"click": this.doubleClick
249
this.sliderEvents.destroy();
252
"mousedown": this.divClick,
253
"mousemove": this.passEventToSlider,
254
"dblclick": this.doubleClick,
255
"click": this.doubleClick
257
this.divEvents.destroy();
259
this.div.removeChild(this.zoombarDiv);
260
this.zoombarDiv = null;
261
this.div.removeChild(this.slider);
264
this.map.events.unregister("zoomend", this, this.moveZoomBar);
268
* Method: passEventToSlider
269
* This function is used to pass events that happen on the div, or the map,
270
* through to the slider, which then does its moving thing.
273
* evt - {<OpenLayers.Event>}
275
passEventToSlider:function(evt) {
276
this.sliderEvents.handleBrowserEvent(evt);
281
* Picks up on clicks directly on the zoombar div
282
* and sets the zoom level appropriately.
284
divClick: function (evt) {
285
if (!OpenLayers.Event.isLeftClick(evt)) {
289
var top = OpenLayers.Util.pagePosition(evt.object)[1];
290
var levels = (y - top)/this.zoomStopHeight;
291
if(this.forceFixedZoomLevel || !this.map.fractionalZoom) {
292
levels = Math.floor(levels);
294
var zoom = (this.map.getNumZoomLevels() - 1) - levels;
295
zoom = Math.min(Math.max(zoom, 0), this.map.getNumZoomLevels() - 1);
296
this.map.zoomTo(zoom);
297
OpenLayers.Event.stop(evt);
301
* Method: zoomBarDown
302
* event listener for clicks on the slider
305
* evt - {<OpenLayers.Event>}
307
zoomBarDown:function(evt) {
308
if (!OpenLayers.Event.isLeftClick(evt)) {
312
"mousemove": this.passEventToSlider,
313
"mouseup": this.passEventToSlider,
316
this.mouseDragStart = evt.xy.clone();
317
this.zoomStart = evt.xy.clone();
318
this.div.style.cursor = "move";
319
// reset the div offsets just in case the div moved
320
this.zoombarDiv.offsets = null;
321
OpenLayers.Event.stop(evt);
325
* Method: zoomBarDrag
326
* This is what happens when a click has occurred, and the client is
327
* dragging. Here we must ensure that the slider doesn't go beyond the
328
* bottom/top of the zoombar div, as well as moving the slider to its new
332
* evt - {<OpenLayers.Event>}
334
zoomBarDrag:function(evt) {
335
if (this.mouseDragStart != null) {
336
var deltaY = this.mouseDragStart.y - evt.xy.y;
337
var offsets = OpenLayers.Util.pagePosition(this.zoombarDiv);
338
if ((evt.clientY - offsets[1]) > 0 &&
339
(evt.clientY - offsets[1]) < parseInt(this.zoombarDiv.style.height) - 2) {
340
var newTop = parseInt(this.slider.style.top) - deltaY;
341
this.slider.style.top = newTop+"px";
342
this.mouseDragStart = evt.xy.clone();
344
OpenLayers.Event.stop(evt);
350
* Perform cleanup when a mouseup event is received -- discover new zoom
351
* level and switch to it.
354
* evt - {<OpenLayers.Event>}
356
zoomBarUp:function(evt) {
357
if (!OpenLayers.Event.isLeftClick(evt)) {
360
if (this.mouseDragStart) {
361
this.div.style.cursor="";
363
"mouseup": this.passEventToSlider,
364
"mousemove": this.passEventToSlider,
367
var deltaY = this.zoomStart.y - evt.xy.y;
368
var zoomLevel = this.map.zoom;
369
if (!this.forceFixedZoomLevel && this.map.fractionalZoom) {
370
zoomLevel += deltaY/this.zoomStopHeight;
371
zoomLevel = Math.min(Math.max(zoomLevel, 0),
372
this.map.getNumZoomLevels() - 1);
374
zoomLevel += Math.round(deltaY/this.zoomStopHeight);
376
this.map.zoomTo(zoomLevel);
377
this.mouseDragStart = null;
378
this.zoomStart = null;
379
OpenLayers.Event.stop(evt);
384
* Method: moveZoomBar
385
* Change the location of the slider to match the current zoom level.
387
moveZoomBar:function() {
389
((this.map.getNumZoomLevels()-1) - this.map.getZoom()) *
390
this.zoomStopHeight + this.startTop + 1;
391
this.slider.style.top = newTop + "px";
394
CLASS_NAME: "OpenLayers.Control.PanZoomBar"