| | 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. */ |
|---|
| | 4 | |
|---|
| | 5 | /** |
|---|
| | 6 | * @requires OpenLayers/Control.js |
|---|
| | 7 | */ |
|---|
| | 8 | |
|---|
| | 9 | /** |
|---|
| | 10 | * Class: OpenLayers.Control.NavigationHistory |
|---|
| | 11 | * A navigation history control. This is a meta-control, that creates two |
|---|
| | 12 | * dependent controls: <previous> and <next>. Call the trigger method |
|---|
| | 13 | * on the <previous> and <next> controls to restore previous and next |
|---|
| | 14 | * history states. The previous and next controls will become active |
|---|
| | 15 | * when there are available states to restore and will become deactive |
|---|
| | 16 | * when there are no states to restore. |
|---|
| | 17 | * |
|---|
| | 18 | * Inherits from: |
|---|
| | 19 | * - <OpenLayers.Control.Control> |
|---|
| | 20 | */ |
|---|
| | 21 | OpenLayers.Control.NavigationHistory = OpenLayers.Class(OpenLayers.Control, { |
|---|
| | 22 | |
|---|
| | 23 | /** |
|---|
| | 24 | * Property: type |
|---|
| | 25 | * {String} Note that this control is not intended to be added directly |
|---|
| | 26 | * to a control panel. Instead, add the sub-controls previous and |
|---|
| | 27 | * next. These sub-controls are button type controls that activate |
|---|
| | 28 | * and deactivate themselves. If this parent control is added to |
|---|
| | 29 | * a panel, it will act as a toggle. |
|---|
| | 30 | */ |
|---|
| | 31 | type: OpenLayers.Control.TYPE_TOGGLE, |
|---|
| | 32 | |
|---|
| | 33 | /** |
|---|
| | 34 | * APIProperty: previous |
|---|
| | 35 | * {OpenLayers.Control} A button type control whose trigger method restores |
|---|
| | 36 | * the previous state managed by this control. |
|---|
| | 37 | */ |
|---|
| | 38 | previous: null, |
|---|
| | 39 | |
|---|
| | 40 | /** |
|---|
| | 41 | * APIProperty: previousOptions |
|---|
| | 42 | * {Object} Set this property on the options argument of the constructor |
|---|
| | 43 | * to set optional properties on the <previous> control. |
|---|
| | 44 | */ |
|---|
| | 45 | previousOptions: null, |
|---|
| | 46 | |
|---|
| | 47 | /** |
|---|
| | 48 | * APIProperty: next |
|---|
| | 49 | * {OpenLayers.Control} A button type control whose trigger method restores |
|---|
| | 50 | * the next state managed by this control. |
|---|
| | 51 | */ |
|---|
| | 52 | next: null, |
|---|
| | 53 | |
|---|
| | 54 | /** |
|---|
| | 55 | * APIProperty: nextOptions |
|---|
| | 56 | * {Object} Set this property on the options argument of the constructor |
|---|
| | 57 | * to set optional properties on the <next> control. |
|---|
| | 58 | */ |
|---|
| | 59 | nextOptions: null, |
|---|
| | 60 | |
|---|
| | 61 | /** |
|---|
| | 62 | * APIProperty: limit |
|---|
| | 63 | * {Integer} Optional limit on the number of history items to retain. If |
|---|
| | 64 | * null, there is no limit. Default is 50. |
|---|
| | 65 | */ |
|---|
| | 66 | limit: 50, |
|---|
| | 67 | |
|---|
| | 68 | /** |
|---|
| | 69 | * Property: activateOnDraw |
|---|
| | 70 | * {Boolean} Activate the control when it is first added to the map. |
|---|
| | 71 | * Default is true. |
|---|
| | 72 | */ |
|---|
| | 73 | activateOnDraw: true, |
|---|
| | 74 | |
|---|
| | 75 | /** |
|---|
| | 76 | * Property: clearOnDeactivate |
|---|
| | 77 | * {Boolean} Clear the history when the control is deactivated. Default |
|---|
| | 78 | * is false. |
|---|
| | 79 | */ |
|---|
| | 80 | clearOnDeactivate: false, |
|---|
| | 81 | |
|---|
| | 82 | /** |
|---|
| | 83 | * Property: events |
|---|
| | 84 | * {<OpenLayers.Events>} An events object that will be used for registering |
|---|
| | 85 | * listeners. Defaults to the map events for this control. |
|---|
| | 86 | */ |
|---|
| | 87 | events: null, |
|---|
| | 88 | |
|---|
| | 89 | /** |
|---|
| | 90 | * Property: registry |
|---|
| | 91 | * {Object} An object with keys corresponding to event types. Values |
|---|
| | 92 | * are functions that return an object representing the current state. |
|---|
| | 93 | */ |
|---|
| | 94 | registry: null, |
|---|
| | 95 | |
|---|
| | 96 | /** |
|---|
| | 97 | * Property: nextStack |
|---|
| | 98 | * {Array} Array of items in the history. |
|---|
| | 99 | */ |
|---|
| | 100 | nextStack: null, |
|---|
| | 101 | |
|---|
| | 102 | /** |
|---|
| | 103 | * Property: previousStack |
|---|
| | 104 | * {Array} List of items in the history. First item represents the current |
|---|
| | 105 | * state. |
|---|
| | 106 | */ |
|---|
| | 107 | previousStack: null, |
|---|
| | 108 | |
|---|
| | 109 | /** |
|---|
| | 110 | * Property: listeners |
|---|
| | 111 | * {Object} An object containing properties corresponding to event types. |
|---|
| | 112 | * This object is used to configure the control and is modified on |
|---|
| | 113 | * construction. |
|---|
| | 114 | */ |
|---|
| | 115 | listeners: null, |
|---|
| | 116 | |
|---|
| | 117 | /** |
|---|
| | 118 | * Property: restoring |
|---|
| | 119 | * {Boolean} Currently restoring a history state. This is set to true |
|---|
| | 120 | * before callilng restore and set to false after restore returns. |
|---|
| | 121 | */ |
|---|
| | 122 | restoring: false, |
|---|
| | 123 | |
|---|
| | 124 | /** |
|---|
| | 125 | * Constructor: OpenLayers.Control.NavigationHistory |
|---|
| | 126 | * |
|---|
| | 127 | * Parameters: |
|---|
| | 128 | * options - {Object} An optional object whose properties will be used |
|---|
| | 129 | * to extend the control. |
|---|
| | 130 | */ |
|---|
| | 131 | initialize: function(options) { |
|---|
| | 132 | OpenLayers.Control.prototype.initialize.apply(this, [options]); |
|---|
| | 133 | |
|---|
| | 134 | this.registry = OpenLayers.Util.extend({ |
|---|
| | 135 | "moveend": function() { |
|---|
| | 136 | return { |
|---|
| | 137 | center: this.map.getCenter(), |
|---|
| | 138 | resolution: this.map.getResolution() |
|---|
| | 139 | }; |
|---|
| | 140 | } |
|---|
| | 141 | }, this.registry); |
|---|
| | 142 | |
|---|
| | 143 | this.clear(); |
|---|
| | 144 | |
|---|
| | 145 | var previousOptions = { |
|---|
| | 146 | trigger: OpenLayers.Function.bind(this.previousTrigger, this), |
|---|
| | 147 | displayClass: this.displayClass + "Previous", |
|---|
| | 148 | onActivate: function() {}, |
|---|
| | 149 | onDeactivate: function() {} |
|---|
| | 150 | }; |
|---|
| | 151 | if(options) { |
|---|
| | 152 | OpenLayers.Util.extend(previousOptions, options.previousOptions); |
|---|
| | 153 | } |
|---|
| | 154 | this.previous = new OpenLayers.Control.Button(previousOptions); |
|---|
| | 155 | |
|---|
| | 156 | var nextOptions = { |
|---|
| | 157 | trigger: OpenLayers.Function.bind(this.nextTrigger, this), |
|---|
| | 158 | displayClass: this.displayClass + "Next", |
|---|
| | 159 | onActivate: function() {}, |
|---|
| | 160 | onDeactivate: function() {} |
|---|
| | 161 | }; |
|---|
| | 162 | if(options) { |
|---|
| | 163 | OpenLayers.Util.extend(nextOptions, options.nextOptions); |
|---|
| | 164 | } |
|---|
| | 165 | this.next = new OpenLayers.Control.Button(nextOptions); |
|---|
| | 166 | |
|---|
| | 167 | }, |
|---|
| | 168 | |
|---|
| | 169 | /** |
|---|
| | 170 | * Method: onPreviousChange |
|---|
| | 171 | * Called when the previous history stack changes. |
|---|
| | 172 | * |
|---|
| | 173 | * Parameters: |
|---|
| | 174 | * state - {Object} An object representing the state to be restored |
|---|
| | 175 | * if previous is triggered again or null if no previous states remain. |
|---|
| | 176 | * length - {Integer} The number of remaining previous states that can |
|---|
| | 177 | * be restored. |
|---|
| | 178 | */ |
|---|
| | 179 | onPreviousChange: function(state, length) { |
|---|
| | 180 | if(state && !this.previous.active) { |
|---|
| | 181 | this.previous.activate(); |
|---|
| | 182 | this.previous.onActivate(); |
|---|
| | 183 | } else if(!state && this.previous.active) { |
|---|
| | 184 | this.previous.deactivate(); |
|---|
| | 185 | this.previous.onDeactivate(); |
|---|
| | 186 | } |
|---|
| | 187 | }, |
|---|
| | 188 | |
|---|
| | 189 | /** |
|---|
| | 190 | * Method: onNextChange |
|---|
| | 191 | * Called when the next history stack changes. |
|---|
| | 192 | * |
|---|
| | 193 | * Parameters: |
|---|
| | 194 | * state - {Object} An object representing the state to be restored |
|---|
| | 195 | * if next is triggered again or null if no next states remain. |
|---|
| | 196 | * length - {Integer} The number of remaining next states that can |
|---|
| | 197 | * be restored. |
|---|
| | 198 | */ |
|---|
| | 199 | onNextChange: function(state, length) { |
|---|
| | 200 | if(state && !this.next.active) { |
|---|
| | 201 | this.next.activate(); |
|---|
| | 202 | this.next.onActivate(); |
|---|
| | 203 | } else if(!state && this.next.active) { |
|---|
| | 204 | this.next.deactivate(); |
|---|
| | 205 | this.next.onDeactivate(); |
|---|
| | 206 | } |
|---|
| | 207 | }, |
|---|
| | 208 | |
|---|
| | 209 | /** |
|---|
| | 210 | * APIMethod: destroy |
|---|
| | 211 | * Destroy the control. |
|---|
| | 212 | */ |
|---|
| | 213 | destroy: function() { |
|---|
| | 214 | OpenLayers.Control.prototype.destroy.apply(this); |
|---|
| | 215 | this.previous.destroy(); |
|---|
| | 216 | this.next.destroy(); |
|---|
| | 217 | this.deactivate(); |
|---|
| | 218 | for(var prop in this) { |
|---|
| | 219 | this[prop] = null; |
|---|
| | 220 | } |
|---|
| | 221 | }, |
|---|
| | 222 | |
|---|
| | 223 | /** |
|---|
| | 224 | * Method: setMap |
|---|
| | 225 | * Set the map property for the control and <previous> and <next> child |
|---|
| | 226 | * controls. |
|---|
| | 227 | * |
|---|
| | 228 | * Parameters: |
|---|
| | 229 | * map - {<OpenLayers.Map>} |
|---|
| | 230 | */ |
|---|
| | 231 | setMap: function(map) { |
|---|
| | 232 | this.map = map; |
|---|
| | 233 | this.next.setMap(map); |
|---|
| | 234 | this.previous.setMap(map); |
|---|
| | 235 | }, |
|---|
| | 236 | |
|---|
| | 237 | /** |
|---|
| | 238 | * Method: draw |
|---|
| | 239 | * Called when the control is added to the map. |
|---|
| | 240 | */ |
|---|
| | 241 | draw: function() { |
|---|
| | 242 | OpenLayers.Control.prototype.draw.apply(this, arguments); |
|---|
| | 243 | this.next.draw(); |
|---|
| | 244 | this.previous.draw(); |
|---|
| | 245 | if(this.activateOnDraw) { |
|---|
| | 246 | this.activate(); |
|---|
| | 247 | } |
|---|
| | 248 | }, |
|---|
| | 249 | |
|---|
| | 250 | /** |
|---|
| | 251 | * Method: previousTrigger |
|---|
| | 252 | * Restore the previous state. If no items are in the previous history |
|---|
| | 253 | * stack, this has no effect. |
|---|
| | 254 | * |
|---|
| | 255 | * Returns: |
|---|
| | 256 | * {Object} Item representing state that was restored. Undefined if no |
|---|
| | 257 | * items are in the previous history stack. |
|---|
| | 258 | */ |
|---|
| | 259 | previousTrigger: function() { |
|---|
| | 260 | var current = this.previousStack.shift(); |
|---|
| | 261 | var state = this.previousStack.shift(); |
|---|
| | 262 | if(state != undefined) { |
|---|
| | 263 | this.nextStack.unshift(current); |
|---|
| | 264 | this.previousStack.unshift(state); |
|---|
| | 265 | this.restoring = true; |
|---|
| | 266 | this.restore(state); |
|---|
| | 267 | this.restoring = false; |
|---|
| | 268 | this.onNextChange(this.nextStack[0], this.nextStack.length); |
|---|
| | 269 | this.onPreviousChange( |
|---|
| | 270 | this.previousStack[1], this.previousStack.length - 1 |
|---|
| | 271 | ); |
|---|
| | 272 | } else { |
|---|
| | 273 | this.previousStack.unshift(current); |
|---|
| | 274 | } |
|---|
| | 275 | return state; |
|---|
| | 276 | }, |
|---|
| | 277 | |
|---|
| | 278 | /** |
|---|
| | 279 | * APIMethod: nextTrigger |
|---|
| | 280 | * Restore the next state. If no items are in the next history |
|---|
| | 281 | * stack, this has no effect. The next history stack is populated |
|---|
| | 282 | * as states are restored from the previous history stack. |
|---|
| | 283 | * |
|---|
| | 284 | * Returns: |
|---|
| | 285 | * {Object} Item representing state that was restored. Undefined if no |
|---|
| | 286 | * items are in the next history stack. |
|---|
| | 287 | */ |
|---|
| | 288 | nextTrigger: function() { |
|---|
| | 289 | var state = this.nextStack.shift(); |
|---|
| | 290 | if(state != undefined) { |
|---|
| | 291 | this.previousStack.unshift(state); |
|---|
| | 292 | this.restoring = true; |
|---|
| | 293 | this.restore(state); |
|---|
| | 294 | this.restoring = false; |
|---|
| | 295 | this.onNextChange(this.nextStack[0], this.nextStack.length); |
|---|
| | 296 | this.onPreviousChange( |
|---|
| | 297 | this.previousStack[1], this.previousStack.length - 1 |
|---|
| | 298 | ); |
|---|
| | 299 | } |
|---|
| | 300 | return state; |
|---|
| | 301 | }, |
|---|
| | 302 | |
|---|
| | 303 | /** |
|---|
| | 304 | * APIMethod: clear |
|---|
| | 305 | * Clear history. |
|---|
| | 306 | */ |
|---|
| | 307 | clear: function() { |
|---|
| | 308 | this.previousStack = []; |
|---|
| | 309 | this.nextStack = []; |
|---|
| | 310 | }, |
|---|
| | 311 | |
|---|
| | 312 | /** |
|---|
| | 313 | * Method: restore |
|---|
| | 314 | * Update the state with the given object. |
|---|
| | 315 | * |
|---|
| | 316 | * Parameters: |
|---|
| | 317 | * state - {Object} An object representing the state to restore. |
|---|
| | 318 | */ |
|---|
| | 319 | restore: function(state) { |
|---|
| | 320 | var zoom = this.map.getZoomForResolution(state.resolution); |
|---|
| | 321 | this.map.setCenter(state.center, zoom); |
|---|
| | 322 | }, |
|---|
| | 323 | |
|---|
| | 324 | /** |
|---|
| | 325 | * Method: setListeners |
|---|
| | 326 | * Sets functions to be registered in the listeners object. |
|---|
| | 327 | */ |
|---|
| | 328 | setListeners: function() { |
|---|
| | 329 | this.listeners = {}; |
|---|
| | 330 | for(var type in this.registry) { |
|---|
| | 331 | this.listeners[type] = OpenLayers.Function.bind(function() { |
|---|
| | 332 | if(!this.restoring) { |
|---|
| | 333 | var state = this.registry[type].apply(this, arguments); |
|---|
| | 334 | this.previousStack.unshift(state); |
|---|
| | 335 | if(this.previousStack.length > 1) { |
|---|
| | 336 | this.onPreviousChange( |
|---|
| | 337 | this.previousStack[1], this.previousStack.length - 1 |
|---|
| | 338 | ); |
|---|
| | 339 | } |
|---|
| | 340 | if(this.previousStack.length > (this.limit + 1)) { |
|---|
| | 341 | this.previousStack.pop(); |
|---|
| | 342 | } |
|---|
| | 343 | if(this.nextStack.length > 0) { |
|---|
| | 344 | this.nextStack = []; |
|---|
| | 345 | this.onNextChange(null, 0); |
|---|
| | 346 | } |
|---|
| | 347 | } |
|---|
| | 348 | return true; |
|---|
| | 349 | }, this); |
|---|
| | 350 | } |
|---|
| | 351 | }, |
|---|
| | 352 | |
|---|
| | 353 | /** |
|---|
| | 354 | * APIMethod: activate |
|---|
| | 355 | * Activate the control. This registers any listeners. |
|---|
| | 356 | * |
|---|
| | 357 | * Returns: |
|---|
| | 358 | * {Boolean} Control successfully activated. |
|---|
| | 359 | */ |
|---|
| | 360 | activate: function() { |
|---|
| | 361 | var activated = false; |
|---|
| | 362 | if(this.map) { |
|---|
| | 363 | if(OpenLayers.Control.prototype.activate.apply(this)) { |
|---|
| | 364 | if(this.listeners == null) { |
|---|
| | 365 | this.setListeners(); |
|---|
| | 366 | } |
|---|
| | 367 | if(!this.events) { |
|---|
| | 368 | this.events = this.map.events; |
|---|
| | 369 | } |
|---|
| | 370 | for(var type in this.listeners) { |
|---|
| | 371 | this.events.register(type, this, this.listeners[type]); |
|---|
| | 372 | } |
|---|
| | 373 | activated = true; |
|---|
| | 374 | if(this.previousStack.length == 0) { |
|---|
| | 375 | this.initStack(); |
|---|
| | 376 | } |
|---|
| | 377 | } |
|---|
| | 378 | } |
|---|
| | 379 | return activated; |
|---|
| | 380 | }, |
|---|
| | 381 | |
|---|
| | 382 | /** |
|---|
| | 383 | * Method: initStack |
|---|
| | 384 | * Called after the control is activated if the previous history stack is |
|---|
| | 385 | * empty. |
|---|
| | 386 | */ |
|---|
| | 387 | initStack: function() { |
|---|
| | 388 | if(this.map.getCenter()) { |
|---|
| | 389 | this.listeners.moveend(); |
|---|
| | 390 | } |
|---|
| | 391 | }, |
|---|
| | 392 | |
|---|
| | 393 | /** |
|---|
| | 394 | * APIMethod: deactivate |
|---|
| | 395 | * Deactivate the control. This unregisters any listeners. |
|---|
| | 396 | * |
|---|
| | 397 | * Returns: |
|---|
| | 398 | * {Boolean} Control successfully deactivated. |
|---|
| | 399 | */ |
|---|
| | 400 | deactivate: function() { |
|---|
| | 401 | var deactivated = false; |
|---|
| | 402 | if(this.map) { |
|---|
| | 403 | if(OpenLayers.Control.prototype.deactivate.apply(this)) { |
|---|
| | 404 | for(var type in this.listeners) { |
|---|
| | 405 | this.events.unregister( |
|---|
| | 406 | type, this, this.listeners[type] |
|---|
| | 407 | ); |
|---|
| | 408 | } |
|---|
| | 409 | if(this.clearOnDeactivate) { |
|---|
| | 410 | this.clear(); |
|---|
| | 411 | } |
|---|
| | 412 | deactivated = true; |
|---|
| | 413 | } |
|---|
| | 414 | } |
|---|
| | 415 | return deactivated; |
|---|
| | 416 | }, |
|---|
| | 417 | |
|---|
| | 418 | CLASS_NAME: "OpenLayers.Control.NavigationHistory" |
|---|
| | 419 | }); |
|---|
| | 420 | |