Changeset 5675
- Timestamp:
- 01/07/08 18:28:25 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
sandbox/tschaub/history/examples/navigation-history.html
r5672 r5675 24 24 map.zoomToMaxExtent(); 25 25 26 nav = new OpenLayers.Control.NavigationHistory(); 27 map.addControl(nav); 28 29 prev = new OpenLayers.Control({ 30 type: OpenLayers.Control.BUTTON, 31 trigger: function() {nav.previous();}, 32 }); 33 nav.onPreviousChange = function(state, length) { 34 if(state) { 35 prev.activate(); 26 // set any application specific behavior here 27 var previousOptions = { 28 onActivate: function() { 36 29 document.getElementById("previous").disabled = false; 37 } else {38 prev.deactivate();30 }, 31 onDeactivate: function() { 39 32 document.getElementById("previous").disabled = true; 40 33 } 41 OpenLayers.Console.log( 42 length + " items remaining in previous history" 43 ); 44 } 45 map.addControl(prev); 46 47 next = new OpenLayers.Control({ 48 type: OpenLayers.Control.BUTTON, 49 trigger: function() {nav.next();} 50 }); 51 nav.onNextChange = function(state, length) { 52 if(state) { 53 next.activate(); 34 }; 35 var nextOptions = { 36 onActivate: function() { 54 37 document.getElementById("next").disabled = false; 55 } else {56 next.deactivate();38 }, 39 onDeactivate: function() { 57 40 document.getElementById("next").disabled = true; 58 41 } 59 OpenLayers.Console.log( 60 length + " items remaining in next history" 61 ); 62 } 63 map.addControl(next); 42 }; 43 var options = { 44 previousOptions: previousOptions, 45 nextOptions: nextOptions 46 }; 47 nav = new OpenLayers.Control.NavigationHistory(options); 48 49 map.addControl(nav); 64 50 65 51 } … … 77 63 78 64 <div id="map"></div> 79 zoom to <button onclick="prev.trigger();" id="previous" disabled="disabled">previous</button> 80 <button onclick="next.trigger();" id="next" disabled="disabled">next</button><br /> 81 65 zoom to 66 <button onclick="nav.previous.trigger();" 67 id="previous" 68 disabled="disabled"> 69 previous 70 </button> 71 <button onclick="nav.next.trigger();" 72 id="next" 73 disabled="disabled"> 74 next 75 </button> 76 82 77 <div id="docs"></div> 83 78 </body> sandbox/tschaub/history/lib/OpenLayers/Control/NavigationHistory.js
r5672 r5675 9 9 /** 10 10 * Class: OpenLayers.Control.NavigationHistory 11 * A navigation history control. 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. 12 17 * 13 18 * Inherits from: … … 17 22 18 23 /** 19 * APIProperty: type 20 * OpenLayers.Control.TYPE_BUTTON 21 */ 22 type: OpenLayers.Control.TYPE_BUTTON, 24 * APIProperty: previous 25 * {OpenLayers.Control} A button type control whose trigger method restores 26 * the previous state managed by this control. 27 */ 28 previous: null, 29 30 /** 31 * APIProperty: previousOptions 32 * {Object} Set this property on the options argument of the constructor 33 * to set optional properties on the <previous> control. 34 */ 35 previousOptions: null, 36 37 /** 38 * APIProperty: next 39 * {OpenLayers.Control} A button type control whose trigger method restores 40 * the next state managed by this control. 41 */ 42 next: null, 43 44 /** 45 * APIProperty: nextOptions 46 * {Object} Set this property on the options argument of the constructor 47 * to set optional properties on the <next> control. 48 */ 49 nextOptions: null, 23 50 24 51 /** … … 28 55 */ 29 56 limit: 50, 30 31 /**32 * APIProperty: backwards33 * {Boolean} Navigate backwards through history by default. This is34 * relevant when the <trigger> method is called. If true, the35 * <trigger> method will call <previous>. If false, the <trigger>36 * method will call <next>. Default is true.37 */38 backwards: true,39 57 40 58 /** … … 111 129 OpenLayers.Control.prototype.initialize.apply(this, [options]); 112 130 this.clear(); 113 }, 114 115 /** 116 * APIMethod: onPreviousChange 117 * Called when the previous history stack changes. This function will 118 * be called with two arguments, the state ({Object}) that will be 119 * restored if previous is called and the number ({Integer}) of items 120 * remaining in the previous history. 121 */ 122 onPreviousChange: function(state, length) {}, 123 124 /** 125 * APIMethod: onNextChange 126 * Called when the next history stack changes. This function will 127 * be called with two arguments, the state ({Object}) that will be 128 * restored if next is called and the number ({Integer}) of items 129 * remaining in the next history. 130 */ 131 onNextChange: function(state, length) {}, 131 132 var previousOptions = { 133 type: OpenLayers.Control.BUTTON, 134 trigger: OpenLayers.Function.bind(this.previousTrigger, this), 135 displayClass: this.displayClass + "Previous", 136 onActivate: function() {}, 137 onDeactivate: function() {} 138 }; 139 if(options) { 140 OpenLayers.Util.extend(previousOptions, options.previousOptions); 141 } 142 this.previous = new OpenLayers.Control(previousOptions); 143 144 var nextOptions = { 145 type: OpenLayers.Control.BUTTON, 146 trigger: OpenLayers.Function.bind(this.nextTrigger, this), 147 displayClass: this.displayClass + "Next", 148 onActivate: function() {}, 149 onDeactivate: function() {} 150 }; 151 if(options) { 152 OpenLayers.Util.extend(nextOptions, options.nextOptions); 153 } 154 this.next = new OpenLayers.Control(nextOptions); 155 156 }, 157 158 /** 159 * Method: onPreviousChange 160 * Called when the previous history stack changes. 161 * 162 * Parameters: 163 * state - {Object} An object representing the state to be restored 164 * if previous is triggered again or null if no previous states remain. 165 * length - {Integer} The number of remaining previous states that can 166 * be restored. 167 */ 168 onPreviousChange: function(state, length) { 169 if(state && !this.previous.active) { 170 this.previous.activate(); 171 this.previous.onActivate(); 172 } else if(!state && this.previous.active) { 173 this.previous.deactivate(); 174 this.previous.onDeactivate(); 175 } 176 }, 177 178 /** 179 * Method: onNextChange 180 * Called when the next history stack changes. 181 * 182 * Parameters: 183 * state - {Object} An object representing the state to be restored 184 * if next is triggered again or null if no next states remain. 185 * length - {Integer} The number of remaining next states that can 186 * be restored. 187 */ 188 onNextChange: function(state, length) { 189 if(state && !this.next.active) { 190 this.next.activate(); 191 this.next.onActivate(); 192 } else if(!state && this.next.active) { 193 this.next.deactivate(); 194 this.next.onDeactivate(); 195 } 196 }, 132 197 133 198 /** … … 137 202 destroy: function() { 138 203 OpenLayers.Control.prototype.destroy.apply(this); 204 this.previous.destroy(); 205 this.next.destroy(); 139 206 this.deactivate(); 140 207 for(var prop in this) { … … 143 210 }, 144 211 212 /** 213 * Method: setMap 214 * Set the map property for the control and <previous> and <next> child 215 * controls. 216 * 217 * Parameters: 218 * map - {<OpenLayers.Map>} 219 */ 220 setMap: function(map) { 221 this.map = map; 222 this.next.setMap(map); 223 this.previous.setMap(map); 224 }, 225 145 226 /** 146 227 * Method: draw … … 149 230 draw: function() { 150 231 OpenLayers.Control.prototype.draw.apply(this, arguments); 232 this.next.draw(); 233 this.previous.draw(); 151 234 if(this.activateOnDraw) { 152 235 this.activate(); … … 155 238 156 239 /** 157 * APIMethod: trigger 158 * Trigger the default action on the control. If <backwards> is true, this 159 * will call <previous>. If <backwards> is false, this will call 160 * <next>. 161 * 162 * Parameters: 163 * options - {Object} Optional object to modify trigger behavior. An 164 * opposite property on the options object will cause previous to be 165 * called instead of next. 166 */ 167 trigger: function(options) { 168 var backwards = (options.opposite) ? !this.backwards : this.backwards; 169 if(backwards) { 170 this.previous(); 171 } else { 172 this.next(); 173 } 174 }, 175 176 /** 177 * APIMethod: previous 240 * Method: previousTrigger 178 241 * Restore the previous state. If no items are in the previous history 179 242 * stack, this has no effect. … … 183 246 * items are in the previous history stack. 184 247 */ 185 previous : function() {248 previousTrigger: function() { 186 249 var current = this.previousStack.shift(); 187 250 var state = this.previousStack.shift(); … … 203 266 204 267 /** 205 * APIMethod: next 268 * APIMethod: nextTrigger 206 269 * Restore the next state. If no items are in the next history 207 270 * stack, this has no effect. The next history stack is populated … … 212 275 * items are in the next history stack. 213 276 */ 214 next : function() {277 nextTrigger: function() { 215 278 var state = this.nextStack.shift(); 216 279 if(state != undefined) {
