PanZoomBar.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the 2-clause BSD license.
  3. * See license.txt in the OpenLayers distribution or repository 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. * APIProperty: zoomWorldIcon
  43. * {Boolean}
  44. */
  45. zoomWorldIcon: false,
  46. /**
  47. * APIProperty: panIcons
  48. * {Boolean} Set this property to false not to display the pan icons. If
  49. * false the zoom world icon is placed under the zoom bar. Defaults to
  50. * true.
  51. */
  52. panIcons: true,
  53. /**
  54. * APIProperty: forceFixedZoomLevel
  55. * {Boolean} Force a fixed zoom level even though the map has
  56. * fractionalZoom
  57. */
  58. forceFixedZoomLevel: false,
  59. /**
  60. * Property: mouseDragStart
  61. * {<OpenLayers.Pixel>}
  62. */
  63. mouseDragStart: null,
  64. /**
  65. * Property: deltaY
  66. * {Number} The cumulative vertical pixel offset during a zoom bar drag.
  67. */
  68. deltaY: null,
  69. /**
  70. * Property: zoomStart
  71. * {<OpenLayers.Pixel>}
  72. */
  73. zoomStart: null,
  74. /**
  75. * Constructor: OpenLayers.Control.PanZoomBar
  76. */
  77. /**
  78. * APIMethod: destroy
  79. */
  80. destroy: function() {
  81. this._removeZoomBar();
  82. this.map.events.un({
  83. "changebaselayer": this.redraw,
  84. "updatesize": this.redraw,
  85. scope: this
  86. });
  87. OpenLayers.Control.PanZoom.prototype.destroy.apply(this, arguments);
  88. delete this.mouseDragStart;
  89. delete this.zoomStart;
  90. },
  91. /**
  92. * Method: setMap
  93. *
  94. * Parameters:
  95. * map - {<OpenLayers.Map>}
  96. */
  97. setMap: function(map) {
  98. OpenLayers.Control.PanZoom.prototype.setMap.apply(this, arguments);
  99. this.map.events.on({
  100. "changebaselayer": this.redraw,
  101. "updatesize": this.redraw,
  102. scope: this
  103. });
  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 = {w: 18, h: 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. * centered - {<OpenLayers.Pixel>} where zoombar drawing is to start.
  164. */
  165. _addZoomBar:function(centered) {
  166. var imgLocation = OpenLayers.Util.getImageLocation("slider.png");
  167. var id = this.id + "_" + this.map.id;
  168. var minZoom = this.map.getMinZoom();
  169. var zoomsToEnd = this.map.getNumZoomLevels() - 1 - this.map.getZoom();
  170. var slider = OpenLayers.Util.createAlphaImageDiv(id,
  171. centered.add(-1, zoomsToEnd * this.zoomStopHeight),
  172. {w: 20, h: 9},
  173. imgLocation,
  174. "absolute");
  175. slider.style.cursor = "move";
  176. this.slider = slider;
  177. this.sliderEvents = new OpenLayers.Events(this, slider, null, true,
  178. {includeXY: true});
  179. this.sliderEvents.on({
  180. "touchstart": this.zoomBarDown,
  181. "touchmove": this.zoomBarDrag,
  182. "touchend": this.zoomBarUp,
  183. "mousedown": this.zoomBarDown,
  184. "mousemove": this.zoomBarDrag,
  185. "mouseup": this.zoomBarUp
  186. });
  187. var sz = {
  188. w: this.zoomStopWidth,
  189. h: this.zoomStopHeight * (this.map.getNumZoomLevels() - minZoom)
  190. };
  191. var imgLocation = OpenLayers.Util.getImageLocation("zoombar.png");
  192. var div = null;
  193. if (OpenLayers.Util.alphaHack()) {
  194. var id = this.id + "_" + this.map.id;
  195. div = OpenLayers.Util.createAlphaImageDiv(id, centered,
  196. {w: sz.w, h: this.zoomStopHeight},
  197. imgLocation,
  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);
  206. }
  207. div.style.cursor = "pointer";
  208. div.className = "olButton";
  209. this.zoombarDiv = div;
  210. this.div.appendChild(div);
  211. this.startTop = parseInt(div.style.top);
  212. this.div.appendChild(slider);
  213. this.map.events.register("zoomend", this, this.moveZoomBar);
  214. centered = centered.add(0,
  215. this.zoomStopHeight * (this.map.getNumZoomLevels() - minZoom));
  216. return centered;
  217. },
  218. /**
  219. * Method: _removeZoomBar
  220. */
  221. _removeZoomBar: function() {
  222. this.sliderEvents.un({
  223. "touchstart": this.zoomBarDown,
  224. "touchmove": this.zoomBarDrag,
  225. "touchend": this.zoomBarUp,
  226. "mousedown": this.zoomBarDown,
  227. "mousemove": this.zoomBarDrag,
  228. "mouseup": this.zoomBarUp
  229. });
  230. this.sliderEvents.destroy();
  231. this.div.removeChild(this.zoombarDiv);
  232. this.zoombarDiv = null;
  233. this.div.removeChild(this.slider);
  234. this.slider = null;
  235. this.map.events.unregister("zoomend", this, this.moveZoomBar);
  236. },
  237. /**
  238. * Method: onButtonClick
  239. *
  240. * Parameters:
  241. * evt - {Event}
  242. */
  243. onButtonClick: function(evt) {
  244. OpenLayers.Control.PanZoom.prototype.onButtonClick.apply(this, arguments);
  245. if (evt.buttonElement === this.zoombarDiv) {
  246. var levels = evt.buttonXY.y / this.zoomStopHeight;
  247. if(this.forceFixedZoomLevel || !this.map.fractionalZoom) {
  248. levels = Math.floor(levels);
  249. }
  250. var zoom = (this.map.getNumZoomLevels() - 1) - levels;
  251. zoom = Math.min(Math.max(zoom, 0), this.map.getNumZoomLevels() - 1);
  252. this.map.zoomTo(zoom);
  253. }
  254. },
  255. /**
  256. * Method: passEventToSlider
  257. * This function is used to pass events that happen on the div, or the map,
  258. * through to the slider, which then does its moving thing.
  259. *
  260. * Parameters:
  261. * evt - {<OpenLayers.Event>}
  262. */
  263. passEventToSlider:function(evt) {
  264. this.sliderEvents.handleBrowserEvent(evt);
  265. },
  266. /*
  267. * Method: zoomBarDown
  268. * event listener for clicks on the slider
  269. *
  270. * Parameters:
  271. * evt - {<OpenLayers.Event>}
  272. */
  273. zoomBarDown:function(evt) {
  274. if (!OpenLayers.Event.isLeftClick(evt) && !OpenLayers.Event.isSingleTouch(evt)) {
  275. return;
  276. }
  277. this.map.events.on({
  278. "touchmove": this.passEventToSlider,
  279. "mousemove": this.passEventToSlider,
  280. "mouseup": this.passEventToSlider,
  281. scope: this
  282. });
  283. this.mouseDragStart = evt.xy.clone();
  284. this.zoomStart = evt.xy.clone();
  285. this.div.style.cursor = "move";
  286. // reset the div offsets just in case the div moved
  287. this.zoombarDiv.offsets = null;
  288. OpenLayers.Event.stop(evt);
  289. },
  290. /*
  291. * Method: zoomBarDrag
  292. * This is what happens when a click has occurred, and the client is
  293. * dragging. Here we must ensure that the slider doesn't go beyond the
  294. * bottom/top of the zoombar div, as well as moving the slider to its new
  295. * visual location
  296. *
  297. * Parameters:
  298. * evt - {<OpenLayers.Event>}
  299. */
  300. zoomBarDrag:function(evt) {
  301. if (this.mouseDragStart != null) {
  302. var deltaY = this.mouseDragStart.y - evt.xy.y;
  303. var offsets = OpenLayers.Util.pagePosition(this.zoombarDiv);
  304. if ((evt.clientY - offsets[1]) > 0 &&
  305. (evt.clientY - offsets[1]) < parseInt(this.zoombarDiv.style.height) - 2) {
  306. var newTop = parseInt(this.slider.style.top) - deltaY;
  307. this.slider.style.top = newTop+"px";
  308. this.mouseDragStart = evt.xy.clone();
  309. }
  310. // set cumulative displacement
  311. this.deltaY = this.zoomStart.y - evt.xy.y;
  312. OpenLayers.Event.stop(evt);
  313. }
  314. },
  315. /*
  316. * Method: zoomBarUp
  317. * Perform cleanup when a mouseup event is received -- discover new zoom
  318. * level and switch to it.
  319. *
  320. * Parameters:
  321. * evt - {<OpenLayers.Event>}
  322. */
  323. zoomBarUp:function(evt) {
  324. if (!OpenLayers.Event.isLeftClick(evt) && evt.type !== "touchend") {
  325. return;
  326. }
  327. if (this.mouseDragStart) {
  328. this.div.style.cursor="";
  329. this.map.events.un({
  330. "touchmove": this.passEventToSlider,
  331. "mouseup": this.passEventToSlider,
  332. "mousemove": this.passEventToSlider,
  333. scope: this
  334. });
  335. var zoomLevel = this.map.zoom;
  336. if (!this.forceFixedZoomLevel && this.map.fractionalZoom) {
  337. zoomLevel += this.deltaY/this.zoomStopHeight;
  338. zoomLevel = Math.min(Math.max(zoomLevel, 0),
  339. this.map.getNumZoomLevels() - 1);
  340. } else {
  341. zoomLevel += this.deltaY/this.zoomStopHeight;
  342. zoomLevel = Math.max(Math.round(zoomLevel), 0);
  343. }
  344. this.map.zoomTo(zoomLevel);
  345. this.mouseDragStart = null;
  346. this.zoomStart = null;
  347. this.deltaY = 0;
  348. OpenLayers.Event.stop(evt);
  349. }
  350. },
  351. /*
  352. * Method: moveZoomBar
  353. * Change the location of the slider to match the current zoom level.
  354. */
  355. moveZoomBar:function() {
  356. var newTop =
  357. ((this.map.getNumZoomLevels()-1) - this.map.getZoom()) *
  358. this.zoomStopHeight + this.startTop + 1;
  359. this.slider.style.top = newTop + "px";
  360. },
  361. CLASS_NAME: "OpenLayers.Control.PanZoomBar"
  362. });