1 | /* Copyright (c) 2006-2010 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 | /** |
---|
7 | * @requires OpenLayers/Strategy.js |
---|
8 | */ |
---|
9 | |
---|
10 | /** |
---|
11 | * Class: OpenLayers.Strategy.Paging |
---|
12 | * Strategy for vector feature paging |
---|
13 | * |
---|
14 | * Inherits from: |
---|
15 | * - <OpenLayers.Strategy> |
---|
16 | */ |
---|
17 | OpenLayers.Strategy.Paging = OpenLayers.Class(OpenLayers.Strategy, { |
---|
18 | |
---|
19 | /** |
---|
20 | * Property: features |
---|
21 | * {Array(<OpenLayers.Feature.Vector>)} Cached features. |
---|
22 | */ |
---|
23 | features: null, |
---|
24 | |
---|
25 | /** |
---|
26 | * Property: length |
---|
27 | * {Integer} Number of features per page. Default is 10. |
---|
28 | */ |
---|
29 | length: 10, |
---|
30 | |
---|
31 | /** |
---|
32 | * Property: num |
---|
33 | * {Integer} The currently displayed page number. |
---|
34 | */ |
---|
35 | num: null, |
---|
36 | |
---|
37 | /** |
---|
38 | * Property: paging |
---|
39 | * {Boolean} The strategy is currently changing pages. |
---|
40 | */ |
---|
41 | paging: false, |
---|
42 | |
---|
43 | /** |
---|
44 | * Constructor: OpenLayers.Strategy.Paging |
---|
45 | * Create a new paging strategy. |
---|
46 | * |
---|
47 | * Parameters: |
---|
48 | * options - {Object} Optional object whose properties will be set on the |
---|
49 | * instance. |
---|
50 | */ |
---|
51 | initialize: function(options) { |
---|
52 | OpenLayers.Strategy.prototype.initialize.apply(this, [options]); |
---|
53 | }, |
---|
54 | |
---|
55 | /** |
---|
56 | * APIMethod: activate |
---|
57 | * Activate the strategy. Register any listeners, do appropriate setup. |
---|
58 | * |
---|
59 | * Returns: |
---|
60 | * {Boolean} The strategy was successfully activated. |
---|
61 | */ |
---|
62 | activate: function() { |
---|
63 | var activated = OpenLayers.Strategy.prototype.activate.call(this); |
---|
64 | if(activated) { |
---|
65 | this.layer.events.on({ |
---|
66 | "beforefeaturesadded": this.cacheFeatures, |
---|
67 | scope: this |
---|
68 | }); |
---|
69 | } |
---|
70 | return activated; |
---|
71 | }, |
---|
72 | |
---|
73 | /** |
---|
74 | * APIMethod: deactivate |
---|
75 | * Deactivate the strategy. Unregister any listeners, do appropriate |
---|
76 | * tear-down. |
---|
77 | * |
---|
78 | * Returns: |
---|
79 | * {Boolean} The strategy was successfully deactivated. |
---|
80 | */ |
---|
81 | deactivate: function() { |
---|
82 | var deactivated = OpenLayers.Strategy.prototype.deactivate.call(this); |
---|
83 | if(deactivated) { |
---|
84 | this.clearCache(); |
---|
85 | this.layer.events.un({ |
---|
86 | "beforefeaturesadded": this.cacheFeatures, |
---|
87 | scope: this |
---|
88 | }); |
---|
89 | } |
---|
90 | return deactivated; |
---|
91 | }, |
---|
92 | |
---|
93 | /** |
---|
94 | * Method: cacheFeatures |
---|
95 | * Cache features before they are added to the layer. |
---|
96 | * |
---|
97 | * Parameters: |
---|
98 | * event - {Object} The event that this was listening for. This will come |
---|
99 | * with a batch of features to be paged. |
---|
100 | */ |
---|
101 | cacheFeatures: function(event) { |
---|
102 | if(!this.paging) { |
---|
103 | this.clearCache(); |
---|
104 | this.features = event.features; |
---|
105 | this.pageNext(event); |
---|
106 | } |
---|
107 | }, |
---|
108 | |
---|
109 | /** |
---|
110 | * Method: clearCache |
---|
111 | * Clear out the cached features. This destroys features, assuming |
---|
112 | * nothing else has a reference. |
---|
113 | */ |
---|
114 | clearCache: function() { |
---|
115 | if(this.features) { |
---|
116 | for(var i=0; i<this.features.length; ++i) { |
---|
117 | this.features[i].destroy(); |
---|
118 | } |
---|
119 | } |
---|
120 | this.features = null; |
---|
121 | this.num = null; |
---|
122 | }, |
---|
123 | |
---|
124 | /** |
---|
125 | * APIMethod: pageCount |
---|
126 | * Get the total count of pages given the current cache of features. |
---|
127 | * |
---|
128 | * Returns: |
---|
129 | * {Integer} The page count. |
---|
130 | */ |
---|
131 | pageCount: function() { |
---|
132 | var numFeatures = this.features ? this.features.length : 0; |
---|
133 | return Math.ceil(numFeatures / this.length); |
---|
134 | }, |
---|
135 | |
---|
136 | /** |
---|
137 | * APIMethod: pageNum |
---|
138 | * Get the zero based page number. |
---|
139 | * |
---|
140 | * Returns: |
---|
141 | * {Integer} The current page number being displayed. |
---|
142 | */ |
---|
143 | pageNum: function() { |
---|
144 | return this.num; |
---|
145 | }, |
---|
146 | |
---|
147 | /** |
---|
148 | * APIMethod: pageLength |
---|
149 | * Gets or sets page length. |
---|
150 | * |
---|
151 | * Parameters: |
---|
152 | * newLength: {Integer} Optional length to be set. |
---|
153 | * |
---|
154 | * Returns: |
---|
155 | * {Integer} The length of a page (number of features per page). |
---|
156 | */ |
---|
157 | pageLength: function(newLength) { |
---|
158 | if(newLength && newLength > 0) { |
---|
159 | this.length = newLength; |
---|
160 | } |
---|
161 | return this.length; |
---|
162 | }, |
---|
163 | |
---|
164 | /** |
---|
165 | * APIMethod: pageNext |
---|
166 | * Display the next page of features. |
---|
167 | * |
---|
168 | * Returns: |
---|
169 | * {Boolean} A new page was displayed. |
---|
170 | */ |
---|
171 | pageNext: function(event) { |
---|
172 | var changed = false; |
---|
173 | if(this.features) { |
---|
174 | if(this.num === null) { |
---|
175 | this.num = -1; |
---|
176 | } |
---|
177 | var start = (this.num + 1) * this.length; |
---|
178 | changed = this.page(start, event); |
---|
179 | } |
---|
180 | return changed; |
---|
181 | }, |
---|
182 | |
---|
183 | /** |
---|
184 | * APIMethod: pagePrevious |
---|
185 | * Display the previous page of features. |
---|
186 | * |
---|
187 | * Returns: |
---|
188 | * {Boolean} A new page was displayed. |
---|
189 | */ |
---|
190 | pagePrevious: function() { |
---|
191 | var changed = false; |
---|
192 | if(this.features) { |
---|
193 | if(this.num === null) { |
---|
194 | this.num = this.pageCount(); |
---|
195 | } |
---|
196 | var start = (this.num - 1) * this.length; |
---|
197 | changed = this.page(start); |
---|
198 | } |
---|
199 | return changed; |
---|
200 | }, |
---|
201 | |
---|
202 | /** |
---|
203 | * Method: page |
---|
204 | * Display the page starting at the given index from the cache. |
---|
205 | * |
---|
206 | * Returns: |
---|
207 | * {Boolean} A new page was displayed. |
---|
208 | */ |
---|
209 | page: function(start, event) { |
---|
210 | var changed = false; |
---|
211 | if(this.features) { |
---|
212 | if(start >= 0 && start < this.features.length) { |
---|
213 | var num = Math.floor(start / this.length); |
---|
214 | if(num != this.num) { |
---|
215 | this.paging = true; |
---|
216 | var features = this.features.slice(start, start + this.length); |
---|
217 | this.layer.removeFeatures(this.layer.features); |
---|
218 | this.num = num; |
---|
219 | // modify the event if any |
---|
220 | if(event && event.features) { |
---|
221 | // this.was called by an event listener |
---|
222 | event.features = features; |
---|
223 | } else { |
---|
224 | // this was called directly on the strategy |
---|
225 | this.layer.addFeatures(features); |
---|
226 | } |
---|
227 | this.paging = false; |
---|
228 | changed = true; |
---|
229 | } |
---|
230 | } |
---|
231 | } |
---|
232 | return changed; |
---|
233 | }, |
---|
234 | |
---|
235 | CLASS_NAME: "OpenLayers.Strategy.Paging" |
---|
236 | }); |
---|