PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/js/extensions/paginator/backgrid-paginator.js

https://github.com/rickhewes/backbone-pageable
JavaScript | 205 lines | 121 code | 32 blank | 52 comment | 16 complexity | 68514237100bd76a4960907820a66545 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.<Backgrid.Column>|Array.<Backgrid.Column>|Array.<Object>} options.columns
  44. Column metadata.
  45. @param {Backbone.Collection} options.collection
  46. @param {boolean} [options.fastForwardHandleLabels] Whether to render fast forward buttons.
  47. */
  48. initialize: function (options) {
  49. Backgrid.requireOptions(options, ["columns", "collection"]);
  50. this.columns = options.columns;
  51. if (!(this.columns instanceof Backbone.Collection)) {
  52. this.columns = new Backgrid.Columns(this.columns);
  53. }
  54. var columns = this.columns;
  55. this.listenTo(columns, "add", this.render);
  56. this.listenTo(columns, "remove", this.render);
  57. this.listenTo(columns, "change:renderable", this.render);
  58. var collection = this.collection;
  59. var fullCollection = collection.fullCollection;
  60. if (fullCollection) {
  61. this.listenTo(fullCollection, "add", this.render);
  62. this.listenTo(fullCollection, "remove", this.render);
  63. this.listenTo(fullCollection, "reset", this.render);
  64. }
  65. else {
  66. this.listenTo(collection, "add", this.render);
  67. this.listenTo(collection, "remove", this.render);
  68. this.listenTo(collection, "reset", this.render);
  69. }
  70. },
  71. /**
  72. jQuery event handler for the page handlers. Goes to the right page upon
  73. clicking.
  74. @param {Event} e
  75. */
  76. changePage: function (e) {
  77. e.preventDefault();
  78. var label = $(e.target).text();
  79. var ffLabels = this.fastForwardHandleLabels;
  80. var collection = this.collection;
  81. if (ffLabels) {
  82. switch (label) {
  83. case ffLabels.first:
  84. collection.getFirstPage();
  85. return;
  86. case ffLabels.prev:
  87. if (collection.hasPrevious()) collection.getPreviousPage();
  88. return;
  89. case ffLabels.next:
  90. if (collection.hasNext()) collection.getNextPage();
  91. return;
  92. case ffLabels.last:
  93. collection.getLastPage();
  94. return;
  95. }
  96. }
  97. var state = collection.state;
  98. var pageIndex = $(e.target).text() * 1;
  99. collection.getPage(state.firstPage === 0 ? pageIndex - 1 : pageIndex);
  100. },
  101. /**
  102. Internal method to create a list of page handle objects for the template
  103. to render them.
  104. @return {Array.<Object>} an array of page handle objects hashes
  105. */
  106. makeHandles: function () {
  107. var handles = [];
  108. var collection = this.collection;
  109. var state = collection.state;
  110. // convert all indices to 0-based here
  111. var lastPage = state.lastPage ? state.lastPage : state.firstPage;
  112. lastPage = state.firstPage === 0 ? lastPage : lastPage - 1;
  113. var currentPage = state.firstPage === 0 ? state.currentPage : state.currentPage - 1;
  114. var windowStart = Math.floor(currentPage / this.windowSize) * this.windowSize;
  115. var windowEnd = windowStart + this.windowSize;
  116. windowEnd = windowEnd <= lastPage ? windowEnd : lastPage + 1;
  117. if (collection.mode !== "infinite") {
  118. for (var i = windowStart; i < windowEnd; i++) {
  119. handles.push({
  120. label: i + 1,
  121. title: "No. " + (i + 1),
  122. className: currentPage === i ? "active" : undefined
  123. });
  124. }
  125. }
  126. var ffLabels = this.fastForwardHandleLabels;
  127. if (ffLabels) {
  128. if (ffLabels.prev) {
  129. handles.unshift({
  130. label: ffLabels.prev,
  131. className: collection.hasPrevious() ? void 0 : "disabled"
  132. });
  133. }
  134. if (ffLabels.first) {
  135. handles.unshift({
  136. label: ffLabels.first,
  137. className: collection.hasPrevious() ? void 0 : "disabled"
  138. });
  139. }
  140. if (ffLabels.next) {
  141. handles.push({
  142. label: ffLabels.next,
  143. className: collection.hasNext() ? void 0 : "disabled"
  144. });
  145. }
  146. if (ffLabels.last) {
  147. handles.push({
  148. label: ffLabels.last,
  149. className: collection.hasNext() ? void 0 : "disabled"
  150. });
  151. }
  152. }
  153. return handles;
  154. },
  155. /**
  156. Render the paginator handles inside an unordered list placed inside a
  157. cell that spans all the columns.
  158. */
  159. render: function () {
  160. this.$el.empty();
  161. this.$el.append(this.template({
  162. handles: this.makeHandles()
  163. }));
  164. this.delegateEvents();
  165. return this;
  166. }
  167. });
  168. }(jQuery, _, Backbone, Backgrid));