PanZoomBar.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the Clear BSD license.
  3. * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/Control/PanZoom.js
  7. */
  8. /**
  9. * Class: OpenLayers.Control.PanZoomBar
  10. * The PanZoomBar is a visible control composed of a
  11. * <OpenLayers.Control.PanPanel> and a <OpenLayers.Control.ZoomBar>.
  12. * By default it is displayed in the upper left corner of the map as 4
  13. * directional arrows above a vertical slider.
  14. *
  15. * Inherits from:
  16. * - <OpenLayers.Control.PanZoom>
  17. */
  18. OpenLayers.Control.PanZoomBar = OpenLayers.Class(OpenLayers.Control.PanZoom, {
  19. /**
  20. * APIProperty: zoomStopWidth
  21. */
  22. zoomStopWidth: 18,
  23. /**
  24. * APIProperty: zoomStopHeight
  25. */
  26. zoomStopHeight: 11,
  27. /**
  28. * Property: slider
  29. */
  30. slider: null,
  31. /**
  32. * Property: sliderEvents
  33. * {<OpenLayers.Events>}
  34. */
  35. sliderEvents: null,
  36. /**
  37. * Property: zoombarDiv
  38. * {DOMElement}
  39. */
  40. zoombarDiv: null,
  41. /**
  42. * Property: divEvents
  43. * {<OpenLayers.Events>}
  44. */
  45. divEvents: null,
  46. /**
  47. * APIProperty: zoomWorldIcon
  48. * {Boolean}
  49. */
  50. zoomWorldIcon: false,
  51. /**
  52. * APIProperty: panIcons
  53. * {Boolean} Set this property to false not to display the pan icons. If
  54. * false the zoom world icon is placed under the zoom bar. Defaults to
  55. * true.
  56. */
  57. panIcons: true,
  58. /**
  59. * APIProperty: forceFixedZoomLevel
  60. * {Boolean} Force a fixed zoom level even though the map has
  61. * fractionalZoom
  62. */
  63. forceFixedZoomLevel: false,
  64. /**
  65. * Property: mouseDragStart
  66. * {<OpenLayers.Pixel>}
  67. */
  68. mouseDragStart: null,
  69. /**
  70. * Property: deltaY
  71. * {Number} The cumulative vertical pixel offset during a zoom bar drag.
  72. */
  73. deltaY: null,
  74. /**
  75. * Property: zoomStart
  76. * {<OpenLayers.Pixel>}
  77. */
  78. zoomStart: null,
  79. /**
  80. * Constructor: OpenLayers.Control.PanZoomBar
  81. */
  82. /**
  83. * APIMethod: destroy
  84. */
  85. destroy: function() {
  86. this._removeZoomBar();
  87. this.map.events.un({
  88. "changebaselayer": this.redraw,
  89. scope: this
  90. });
  91. OpenLayers.Control.PanZoom.prototype.destroy.apply(this, arguments);
  92. delete this.mouseDragStart;
  93. delete this.zoomStart;
  94. },
  95. /**
  96. * Method: setMap
  97. *
  98. * Parameters:
  99. * map - {<OpenLayers.Map>}
  100. */
  101. setMap: function(map) {
  102. OpenLayers.Control.PanZoom.prototype.setMap.apply(this, arguments);
  103. this.map.events.register("changebaselayer", this, this.redraw);
  104. },
  105. /**
  106. * Method: redraw
  107. * clear the div and start over.
  108. */
  109. redraw: function() {
  110. if (this.div != null) {
  111. this.removeButtons();
  112. this._removeZoomBar();
  113. }
  114. this.draw();
  115. },
  116. /**
  117. * Method: draw
  118. *
  119. * Parameters:
  120. * px - {<OpenLayers.Pixel>}
  121. */
  122. draw: function(px) {
  123. // initialize our internal div
  124. OpenLayers.Control.prototype.draw.apply(this, arguments);
  125. px = this.position.clone();
  126. // place the controls
  127. this.buttons = [];
  128. var sz = new OpenLayers.Size(18,18);
  129. if (this.panIcons) {
  130. var centered = new OpenLayers.Pixel(px.x+sz.w/2, px.y);
  131. var wposition = sz.w;
  132. if (this.zoomWorldIcon) {
  133. centered = new OpenLayers.Pixel(px.x+sz.w, px.y);
  134. }
  135. this._addButton("panup", "north-mini.png", centered, sz);
  136. px.y = centered.y+sz.h;
  137. this._addButton("panleft", "west-mini.png", px, sz);
  138. if (this.zoomWorldIcon) {
  139. this._addButton("zoomworld", "zoom-world-mini.png", px.add(sz.w, 0), sz);
  140. wposition *= 2;
  141. }
  142. this._addButton("panright", "east-mini.png", px.add(wposition, 0), sz);
  143. this._addButton("pandown", "south-mini.png", centered.add(0, sz.h*2), sz);
  144. this._addButton("zoomin", "zoom-plus-mini.png", centered.add(0, sz.h*3+5), sz);
  145. centered = this._addZoomBar(centered.add(0, sz.h*4 + 5));
  146. this._addButton("zoomout", "zoom-minus-mini.png", centered, sz);
  147. }
  148. else {
  149. this._addButton("zoomin", "zoom-plus-mini.png", px, sz);
  150. centered = this._addZoomBar(px.add(0, sz.h));
  151. this._addButton("zoomout", "zoom-minus-mini.png", centered, sz);
  152. if (this.zoomWorldIcon) {
  153. centered = centered.add(0, sz.h+3);
  154. this._addButton("zoomworld", "zoom-world-mini.png", centered, sz);
  155. }
  156. }
  157. return this.div;
  158. },
  159. /**
  160. * Method: _addZoomBar
  161. *
  162. * Parameters:
  163. * location - {<OpenLayers.Pixel>} where zoombar drawing is to start.
  164. */
  165. _addZoomBar:function(centered) {
  166. var imgLocation = OpenLayers.Util.getImagesLocation();
  167. var id = this.id + "_" + this.map.id;
  168. var zoomsToEnd = this.map.getNumZoomLevels() - 1 - this.map.getZoom();
  169. var slider = OpenLayers.Util.createAlphaImageDiv(id,
  170. centered.add(-1, zoomsToEnd * this.zoomStopHeight),
  171. new OpenLayers.Size(20,9),
  172. imgLocation+"slider.png",
  173. "absolute");
  174. slider.style.cursor = "move";
  175. this.slider = slider;
  176. this.sliderEvents = new OpenLayers.Events(this, slider, null, true,
  177. {includeXY: true});
  178. this.sliderEvents.on({
  179. "touchstart": this.zoomBarDown,
  180. "touchmove": this.zoomBarDrag,
  181. "touchend": this.zoomBarUp,
  182. "mousedown": this.zoomBarDown,
  183. "mousemove": this.zoomBarDrag,
  184. "mouseup": this.zoomBarUp,
  185. "dblclick": this.doubleClick,
  186. "click": this.doubleClick
  187. });
  188. var sz = new OpenLayers.Size();
  189. sz.h = this.zoomStopHeight * this.map.getNumZoomLevels();
  190. sz.w = this.zoomStopWidth;
  191. var div = null;
  192. if (OpenLayers.Util.alphaHack()) {
  193. var id = this.id + "_" + this.map.id;
  194. div = OpenLayers.Util.createAlphaImageDiv(id, centered,
  195. new OpenLayers.Size(sz.w,
  196. this.zoomStopHeight),
  197. imgLocation + "zoombar.png",
  198. "absolute", null, "crop");
  199. div.style.height = sz.h + "px";
  200. } else {
  201. div = OpenLayers.Util.createDiv(
  202. 'OpenLayers_Control_PanZoomBar_Zoombar' + this.map.id,
  203. centered,
  204. sz,
  205. imgLocation+"zoombar.png");
  206. }
  207. div.style.cursor = "pointer";
  208. this.zoombarDiv = div;
  209. this.divEvents = new OpenLayers.Events(this, div, null, true,
  210. {includeXY: true});
  211. this.divEvents.on({
  212. "touchmove": this.passEventToSlider,
  213. "mousedown": this.divClick,
  214. "mousemove": this.passEventToSlider,
  215. "dblclick": this.doubleClick,
  216. "click": this.doubleClick
  217. });
  218. this.div.appendChild(div);
  219. this.startTop = parseInt(div.style.top);
  220. this.div.appendChild(slider);
  221. this.map.events.register("zoomend", this, this.moveZoomBar);
  222. centered = centered.add(0,
  223. this.zoomStopHeight * this.map.getNumZoomLevels());
  224. return centered;
  225. },
  226. /**
  227. * Method: _removeZoomBar
  228. */
  229. _removeZoomBar: function() {
  230. this.sliderEvents.un({
  231. "touchmove": this.zoomBarDrag,
  232. "mousedown": this.zoomBarDown,
  233. "mousemove": this.zoomBarDrag,
  234. "mouseup": this.zoomBarUp,
  235. "dblclick": this.doubleClick,
  236. "click": this.doubleClick
  237. });
  238. this.sliderEvents.destroy();
  239. this.divEvents.un({
  240. "touchmove": this.passEventToSlider,
  241. "mousedown": this.divClick,
  242. "mousemove": this.passEventToSlider,
  243. "dblclick": this.doubleClick,
  244. "click": this.doubleClick
  245. });
  246. this.divEvents.destroy();
  247. this.div.removeChild(this.zoombarDiv);
  248. this.zoombarDiv = null;
  249. this.div.removeChild(this.slider);
  250. this.slider = null;
  251. this.map.events.unregister("zoomend", this, this.moveZoomBar);
  252. },
  253. /**
  254. * Method: passEventToSlider
  255. * This function is used to pass events that happen on the div, or the map,
  256. * through to the slider, which then does its moving thing.
  257. *
  258. * Parameters:
  259. * evt - {<OpenLayers.Event>}
  260. */
  261. passEventToSlider:function(evt) {
  262. this.sliderEvents.handleBrowserEvent(evt);
  263. },
  264. /**
  265. * Method: divClick
  266. * Picks up on clicks directly on the zoombar div
  267. * and sets the zoom level appropriately.
  268. */
  269. divClick: function (evt) {
  270. if (!OpenLayers.Event.isLeftClick(evt)) {
  271. return;
  272. }
  273. var levels = evt.xy.y / this.zoomStopHeight;
  274. if(this.forceFixedZoomLevel || !this.map.fractionalZoom) {
  275. levels = Math.floor(levels);
  276. }
  277. var zoom = (this.map.getNumZoomLevels() - 1) - levels;
  278. zoom = Math.min(Math.max(zoom, 0), this.map.getNumZoomLevels() - 1);
  279. this.map.zoomTo(zoom);
  280. OpenLayers.Event.stop(evt);
  281. },
  282. /*
  283. * Method: zoomBarDown
  284. * event listener for clicks on the slider
  285. *
  286. * Parameters:
  287. * evt - {<OpenLayers.Event>}
  288. */
  289. zoomBarDown:function(evt) {
  290. if (!OpenLayers.Event.isLeftClick(evt) && !OpenLayers.Event.isSingleTouch(evt)) {
  291. return;
  292. }
  293. this.map.events.on({
  294. "touchmove": this.passEventToSlider,
  295. "mousemove": this.passEventToSlider,
  296. "mouseup": this.passEventToSlider,
  297. scope: this
  298. });
  299. this.mouseDragStart = evt.xy.clone();
  300. this.zoomStart = evt.xy.clone();
  301. this.div.style.cursor = "move";
  302. // reset the div offsets just in case the div moved
  303. this.zoombarDiv.offsets = null;
  304. OpenLayers.Event.stop(evt);
  305. },
  306. /*
  307. * Method: zoomBarDrag
  308. * This is what happens when a click has occurred, and the client is
  309. * dragging. Here we must ensure that the slider doesn't go beyond the
  310. * bottom/top of the zoombar div, as well as moving the slider to its new
  311. * visual location
  312. *
  313. * Parameters:
  314. * evt - {<OpenLayers.Event>}
  315. */
  316. zoomBarDrag:function(evt) {
  317. if (this.mouseDragStart != null) {
  318. var deltaY = this.mouseDragStart.y - evt.xy.y;
  319. var offsets = OpenLayers.Util.pagePosition(this.zoombarDiv);
  320. if ((evt.clientY - offsets[1]) > 0 &&
  321. (evt.clientY - offsets[1]) < parseInt(this.zoombarDiv.style.height) - 2) {
  322. var newTop = parseInt(this.slider.style.top) - deltaY;
  323. this.slider.style.top = newTop+"px";
  324. this.mouseDragStart = evt.xy.clone();
  325. }
  326. // set cumulative displacement
  327. this.deltaY = this.zoomStart.y - evt.xy.y;
  328. OpenLayers.Event.stop(evt);
  329. }
  330. },
  331. /*
  332. * Method: zoomBarUp
  333. * Perform cleanup when a mouseup event is received -- discover new zoom
  334. * level and switch to it.
  335. *
  336. * Parameters:
  337. * evt - {<OpenLayers.Event>}
  338. */
  339. zoomBarUp:function(evt) {
  340. if (!OpenLayers.Event.isLeftClick(evt) && evt.type !== "touchend") {
  341. return;
  342. }
  343. if (this.mouseDragStart) {
  344. this.div.style.cursor="";
  345. this.map.events.un({
  346. "touchmove": this.passEventToSlider,
  347. "mouseup": this.passEventToSlider,
  348. "mousemove": this.passEventToSlider,
  349. scope: this
  350. });
  351. var zoomLevel = this.map.zoom;
  352. if (!this.forceFixedZoomLevel && this.map.fractionalZoom) {
  353. zoomLevel += this.deltaY/this.zoomStopHeight;
  354. zoomLevel = Math.min(Math.max(zoomLevel, 0),
  355. this.map.getNumZoomLevels() - 1);
  356. } else {
  357. zoomLevel += this.deltaY/this.zoomStopHeight;
  358. zoomLevel = Math.max(Math.round(zoomLevel), 0);
  359. }
  360. this.map.zoomTo(zoomLevel);
  361. this.mouseDragStart = null;
  362. this.zoomStart = null;
  363. this.deltaY = 0;
  364. OpenLayers.Event.stop(evt);
  365. }
  366. },
  367. /*
  368. * Method: moveZoomBar
  369. * Change the location of the slider to match the current zoom level.
  370. */
  371. moveZoomBar:function() {
  372. var newTop =
  373. ((this.map.getNumZoomLevels()-1) - this.map.getZoom()) *
  374. this.zoomStopHeight + this.startTop + 1;
  375. this.slider.style.top = newTop + "px";
  376. },
  377. CLASS_NAME: "OpenLayers.Control.PanZoomBar"
  378. });