Paging.js 6.2 KB

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