PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/backbone.paginator.js

https://github.com/dgs700/backbone.paginator
JavaScript | 298 lines | 205 code | 60 blank | 33 comment | 27 complexity | c3613fc8d59d3daab2d2277429db69da MD5 | raw file
Possible License(s): MIT
  1. /*globals Backbone:true, _:true, $:true*/
  2. Backbone.Paginator = (function (Backbone, _, $) {
  3. "use strict";
  4. var Paginator = {};
  5. Paginator.version = "0.15";
  6. // @name: clientPager
  7. //
  8. // @tagline: Paginator for client-side data
  9. //
  10. // @description:
  11. // This paginator is responsible for providing pagination
  12. // and sort capabilities for a single payload of data
  13. // we wish to paginate by the UI for easier browsering.
  14. //
  15. Paginator.clientPager = Backbone.Collection.extend({
  16. sync: function (method, model, options) {
  17. var queryMap = {};
  18. queryMap[this.perPageAttribute] = this.perPage;
  19. queryMap[this.skipAttribute] = this.page * this.perPage;
  20. queryMap[this.orderAttribute] = this.sortField;
  21. queryMap[this.customAttribute1] = this.customParam1;
  22. queryMap[this.formatAttribute] = this.format;
  23. queryMap[this.customAttribute2] = this.customParam2;
  24. queryMap[this.queryAttribute] = this.query;
  25. var params = _.extend({
  26. type: 'GET',
  27. dataType: 'jsonp',
  28. jsonpCallback: 'callback',
  29. data: decodeURIComponent($.param(queryMap)),
  30. url: this.url,
  31. processData: false
  32. }, options);
  33. return $.ajax(params);
  34. },
  35. nextPage: function () {
  36. this.page = this.page++;
  37. this.pager();
  38. },
  39. previousPage: function () {
  40. this.page = --this.page || 1;
  41. this.pager();
  42. },
  43. goTo: function (page) {
  44. this.page = parseInt(page, 10);
  45. this.pager();
  46. },
  47. howManyPer: function (perPage) {
  48. this.displayPerPage = perPage;
  49. this.pager();
  50. },
  51. // where column is the key to sort on
  52. setSort: function (column, direction) {
  53. this.pager(column, direction);
  54. },
  55. pager: function (sort, direction) {
  56. var self = this,
  57. start = (self.page - 1) * this.displayPerPage,
  58. stop = start + this.displayPerPage;
  59. if (self.origModels === undefined) {
  60. self.origModels = self.models;
  61. }
  62. self.models = self.origModels;
  63. if (sort) {
  64. self.models = self._sort(self.models, sort, direction);
  65. }
  66. self.reset(
  67. self.models.slice(start, stop));
  68. },
  69. _sort: function (models, sort, direction) {
  70. models = models.sort(function (a, b) {
  71. var ac = a.get(sort),
  72. bc = b.get(sort);
  73. if (direction === 'desc') {
  74. if (ac > bc) {
  75. return -1;
  76. }
  77. if (ac < bc) {
  78. return 1;
  79. }
  80. } else {
  81. if (ac < bc) {
  82. return -1;
  83. }
  84. if (ac > bc) {
  85. return 1;
  86. }
  87. }
  88. return 0;
  89. });
  90. return models;
  91. },
  92. info: function () {
  93. var self = this,
  94. info = {},
  95. totalRecords = (self.origModels) ? self.origModels.length : self.length,
  96. totalPages = Math.ceil(totalRecords / self.perPage);
  97. info = {
  98. totalRecords: totalRecords,
  99. page: self.page,
  100. perPage: this.displayPerPage,
  101. totalPages: totalPages,
  102. lastPage: totalPages,
  103. lastPagem1: totalPages - 1,
  104. previous: false,
  105. next: false,
  106. page_set: [],
  107. startRecord: (self.page - 1) * this.displayPerPage + 1,
  108. endRecord: Math.min(totalRecords, self.page * this.displayPerPage)
  109. };
  110. if (self.page > 1) {
  111. info.prev = self.page - 1;
  112. }
  113. if (self.page < info.totalPages) {
  114. info.next = self.page + 1;
  115. }
  116. info.pageSet = self.setPagination(info);
  117. self.information = info;
  118. return info;
  119. },
  120. setPagination: function (info) {
  121. var pages = [], i = 0, l = 0;
  122. // How many adjacent pages should be shown on each side?
  123. var ADJACENT = 3;
  124. var ADJACENTx2 = ADJACENT * 2;
  125. var LASTPAGE = Math.ceil(info.totalRecords / info.perPage);
  126. var LPM1 = -1;
  127. if (LASTPAGE > 1) {
  128. // not enough pages to bother breaking it up
  129. if (LASTPAGE < (7 + ADJACENTx2)) {
  130. for (i = 1, l = LASTPAGE; i <= l; i++) {
  131. pages.push(i);
  132. }
  133. }
  134. // enough pages to hide some
  135. else if (LASTPAGE > (5 + ADJACENTx2)) {
  136. //close to beginning; only hide later pages
  137. if (info.page < (1 + ADJACENTx2)) {
  138. for (i = 1, l = 4 + ADJACENTx2; i < l; i++) {
  139. pages.push(i);
  140. }
  141. }
  142. // in middle; hide some front and some back
  143. else if (LASTPAGE - ADJACENTx2 > info.page && info.page > ADJACENTx2) {
  144. for (i = info.page - ADJACENT; i <= info.page + ADJACENT; i++) {
  145. pages.push(i);
  146. }
  147. }
  148. // close to end; only hide early pages
  149. else {
  150. for (i = LASTPAGE - (2 + ADJACENTx2); i <= LASTPAGE; i++) {
  151. pages.push(i);
  152. }
  153. }
  154. }
  155. }
  156. return pages;
  157. }
  158. });
  159. // @name: requestPager
  160. //
  161. // Paginator for server-side data being requested from a backend/API
  162. //
  163. // @description:
  164. // This paginator is responsible for providing pagination
  165. // and sort capabilities for requests to a server-side
  166. // data service (e.g an API)
  167. //
  168. Paginator.requestPager = Backbone.Collection.extend({
  169. sync: function (method, model, options) {
  170. var queryMap = {}, params;
  171. queryMap[this.perPageAttribute] = this.perPage;
  172. queryMap[this.skipAttribute] = this.page * this.perPage;
  173. queryMap[this.orderAttribute] = this.sortField;
  174. queryMap[this.customAttribute1] = this.customParam1;
  175. queryMap[this.formatAttribute] = this.format;
  176. queryMap[this.customAttribute2] = this.customParam2;
  177. queryMap[this.queryAttribute] = this.query;
  178. params = _.extend({
  179. type: 'GET',
  180. dataType: 'jsonp',
  181. jsonpCallback: 'callback',
  182. data: decodeURIComponent($.param(queryMap)),
  183. url: this.url,
  184. processData: false
  185. }, options);
  186. return $.ajax(params);
  187. },
  188. requestNextPage: function () {
  189. if (this.page >= 0) {
  190. this.page += 1;
  191. // customize as needed. For the Netflix API, skipping ahead based on
  192. // page * number of results per page was necessary. You may have a
  193. // simpler server-side pagination API where just updating
  194. // the 'page' value is all you need to do.
  195. // This applies similarly to requestPreviousPage()
  196. this.pager();
  197. }
  198. },
  199. requestPreviousPage: function () {
  200. if (this.page >= 0) {
  201. this.page -= 1;
  202. // customize as needed.
  203. this.pager();
  204. }
  205. },
  206. updateOrder: function (column) {
  207. if (column) {
  208. this.sortField = column;
  209. this.pager();
  210. }
  211. },
  212. goTo: function (page) {
  213. this.page = parseInt(page, 10);
  214. this.pager();
  215. },
  216. howManyPer: function (count) {
  217. this.page = 1;
  218. this.perPage = count;
  219. this.pager();
  220. },
  221. sort: function () {
  222. },
  223. info: function () {
  224. var info = {
  225. page: this.page,
  226. totalPages: this.totalPages,
  227. lastPage: this.totalPages
  228. };
  229. this.information = info;
  230. return info;
  231. },
  232. // fetches the latest results from the server
  233. pager: function () {
  234. this.fetch({});
  235. }
  236. });
  237. return Paginator;
  238. }(Backbone, _, $));