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. */
6 * @requires OpenLayers/Strategy.js
10 * Class: OpenLayers.Strategy.Paging
11 * Strategy for vector feature paging
14 * - <OpenLayers.Strategy>
16 OpenLayers.Strategy.Paging = OpenLayers.Class(OpenLayers.Strategy, {
20 * {Array(<OpenLayers.Feature.Vector>)} Cached features.
26 * {Integer} Number of features per page. Default is 10.
32 * {Integer} The currently displayed page number.
38 * {Boolean} The strategy is currently changing pages.
43 * Constructor: OpenLayers.Strategy.Paging
44 * Create a new paging strategy.
47 * options - {Object} Optional object whose properties will be set on the
50 initialize: function(options) {
51 OpenLayers.Strategy.prototype.initialize.apply(this, [options]);
56 * Activate the strategy. Register any listeners, do appropriate setup.
59 * {Boolean} The strategy was successfully activated.
61 activate: function() {
62 var activated = OpenLayers.Strategy.prototype.activate.call(this);
64 this.layer.events.on({
65 "beforefeaturesadded": this.cacheFeatures,
73 * APIMethod: deactivate
74 * Deactivate the strategy. Unregister any listeners, do appropriate
78 * {Boolean} The strategy was successfully deactivated.
80 deactivate: function() {
81 var deactivated = OpenLayers.Strategy.prototype.deactivate.call(this);
84 this.layer.events.un({
85 "beforefeaturesadded": this.cacheFeatures,
93 * Method: cacheFeatures
94 * Cache features before they are added to the layer.
97 * event - {Object} The event that this was listening for. This will come
98 * with a batch of features to be paged.
100 cacheFeatures: function(event) {
103 this.features = event.features;
104 this.pageNext(event);
110 * Clear out the cached features. This destroys features, assuming
111 * nothing else has a reference.
113 clearCache: function() {
115 for(var i=0; i<this.features.length; ++i) {
116 this.features[i].destroy();
119 this.features = null;
124 * APIMethod: pageCount
125 * Get the total count of pages given the current cache of features.
128 * {Integer} The page count.
130 pageCount: function() {
131 var numFeatures = this.features ? this.features.length : 0;
132 return Math.ceil(numFeatures / this.length);
137 * Get the zero based page number.
140 * {Integer} The current page number being displayed.
142 pageNum: function() {
147 * APIMethod: pageLength
148 * Gets or sets page length.
151 * newLength: {Integer} Optional length to be set.
154 * {Integer} The length of a page (number of features per page).
156 pageLength: function(newLength) {
157 if(newLength && newLength > 0) {
158 this.length = newLength;
164 * APIMethod: pageNext
165 * Display the next page of features.
168 * {Boolean} A new page was displayed.
170 pageNext: function(event) {
173 if(this.num === null) {
176 var start = (this.num + 1) * this.length;
177 changed = this.page(start, event);
183 * APIMethod: pagePrevious
184 * Display the previous page of features.
187 * {Boolean} A new page was displayed.
189 pagePrevious: function() {
192 if(this.num === null) {
193 this.num = this.pageCount();
195 var start = (this.num - 1) * this.length;
196 changed = this.page(start);
203 * Display the page starting at the given index from the cache.
206 * {Boolean} A new page was displayed.
208 page: function(start, event) {
211 if(start >= 0 && start < this.features.length) {
212 var num = Math.floor(start / this.length);
213 if(num != this.num) {
215 var features = this.features.slice(start, start + this.length);
216 this.layer.removeFeatures(this.layer.features);
218 // modify the event if any
219 if(event && event.features) {
220 // this.was called by an event listener
221 event.features = features;
223 // this was called directly on the strategy
224 this.layer.addFeatures(features);
234 CLASS_NAME: "OpenLayers.Strategy.Paging"