PageRenderTime 47ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/extensions/paginator/backgrid-paginator.js

https://github.com/antonyraj/backgrid
JavaScript | 198 lines | 117 code | 32 blank | 49 comment | 15 complexity | cb7d1a5c26c8bb37bb5e0ec89a02162b MD5 | raw file
Possible License(s): MIT
  1. /*
  2. backgrid-paginator
  3. http://github.com/wyuenho/backgrid
  4. Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
  5. Licensed under the MIT @license.
  6. */
  7. (function ($, _, Backbone, Backgrid) {
  8. "use strict";
  9. /**
  10. Paginator is a Backgrid extension that renders a series of configurable
  11. pagination handles. This extension is best used for splitting a large data
  12. set across multiple pages. If the number of pages is larger then a
  13. threshold, which is set to 10 by default, the page handles are rendered
  14. within a sliding window, plus the fast forward, fast backward, previous and
  15. next page handles. The fast forward, fast backward, previous and next page
  16. handles can be turned off.
  17. @class Backgrid.Extension.Paginator
  18. */
  19. Backgrid.Extension.Paginator = Backbone.View.extend({
  20. /** @property */
  21. className: "backgrid-paginator",
  22. /** @property */
  23. windowSize: 10,
  24. /**
  25. @property {Object} fastForwardHandleLabels You can disable specific
  26. handles by setting its value to `null`.
  27. */
  28. fastForwardHandleLabels: {
  29. first: "《",
  30. prev: "〈",
  31. next: "〉",
  32. last: "》"
  33. },
  34. /** @property */
  35. template: _.template('<ul><% _.each(handles, function (handle) { %><li <% if (handle.className) { %>class="<%= handle.className %>"<% } %>><a href="#" <% if (handle.title) {%> title="<%= handle.title %>"<% } %>><%= handle.label %></a></li><% }); %></ul>'),
  36. /** @property */
  37. events: {
  38. "click a": "changePage"
  39. },
  40. /**
  41. Initializer.
  42. @param {Object} options
  43. @param {Backbone.Collection} options.collection
  44. @param {boolean} [options.fastForwardHandleLabels] Whether to render fast forward buttons.
  45. */
  46. initialize: function (options) {
  47. Backgrid.requireOptions(options, ["collection"]);
  48. var collection = this.collection;
  49. var fullCollection = collection.fullCollection;
  50. if (fullCollection) {
  51. this.listenTo(fullCollection, "add", this.render);
  52. this.listenTo(fullCollection, "remove", this.render);
  53. this.listenTo(fullCollection, "reset", this.render);
  54. }
  55. else {
  56. this.listenTo(collection, "add", this.render);
  57. this.listenTo(collection, "remove", this.render);
  58. this.listenTo(collection, "reset", this.render);
  59. }
  60. },
  61. /**
  62. jQuery event handler for the page handlers. Goes to the right page upon
  63. clicking.
  64. @param {Event} e
  65. */
  66. changePage: function (e) {
  67. e.preventDefault();
  68. var $li = $(e.target).parent();
  69. if (!$li.hasClass("active") && !$li.hasClass("disabled")) {
  70. var label = $(e.target).text();
  71. var ffLabels = this.fastForwardHandleLabels;
  72. var collection = this.collection;
  73. if (ffLabels) {
  74. switch (label) {
  75. case ffLabels.first:
  76. collection.getFirstPage();
  77. return;
  78. case ffLabels.prev:
  79. collection.getPreviousPage();
  80. return;
  81. case ffLabels.next:
  82. collection.getNextPage();
  83. return;
  84. case ffLabels.last:
  85. collection.getLastPage();
  86. return;
  87. }
  88. }
  89. var state = collection.state;
  90. var pageIndex = +label;
  91. collection.getPage(state.firstPage === 0 ? pageIndex - 1 : pageIndex);
  92. }
  93. },
  94. /**
  95. Internal method to create a list of page handle objects for the template
  96. to render them.
  97. @return {Array.<Object>} an array of page handle objects hashes
  98. */
  99. makeHandles: function () {
  100. var handles = [];
  101. var collection = this.collection;
  102. var state = collection.state;
  103. // convert all indices to 0-based here
  104. var firstPage = state.firstPage;
  105. var lastPage = +state.lastPage;
  106. lastPage = Math.max(0, firstPage ? lastPage - 1 : lastPage);
  107. var currentPage = Math.max(state.currentPage, state.firstPage);
  108. currentPage = firstPage ? currentPage - 1 : currentPage;
  109. var windowStart = Math.floor(currentPage / this.windowSize) * this.windowSize;
  110. var windowEnd = Math.min(lastPage + 1, windowStart + this.windowSize);
  111. if (collection.mode !== "infinite") {
  112. for (var i = windowStart; i < windowEnd; i++) {
  113. handles.push({
  114. label: i + 1,
  115. title: "No. " + (i + 1),
  116. className: currentPage === i ? "active" : undefined
  117. });
  118. }
  119. }
  120. var ffLabels = this.fastForwardHandleLabels;
  121. if (ffLabels) {
  122. if (ffLabels.prev) {
  123. handles.unshift({
  124. label: ffLabels.prev,
  125. className: collection.hasPrevious() ? void 0 : "disabled"
  126. });
  127. }
  128. if (ffLabels.first) {
  129. handles.unshift({
  130. label: ffLabels.first,
  131. className: collection.hasPrevious() ? void 0 : "disabled"
  132. });
  133. }
  134. if (ffLabels.next) {
  135. handles.push({
  136. label: ffLabels.next,
  137. className: collection.hasNext() ? void 0 : "disabled"
  138. });
  139. }
  140. if (ffLabels.last) {
  141. handles.push({
  142. label: ffLabels.last,
  143. className: collection.hasNext() ? void 0 : "disabled"
  144. });
  145. }
  146. }
  147. return handles;
  148. },
  149. /**
  150. Render the paginator handles inside an unordered list.
  151. */
  152. render: function () {
  153. this.$el.empty();
  154. this.$el.append(this.template({
  155. handles: this.makeHandles()
  156. }));
  157. this.delegateEvents();
  158. return this;
  159. }
  160. });
  161. }(jQuery, _, Backbone, Backgrid));