PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/misc/webapp/js/backbone.paginator.js

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