PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/web-app/lib/backgrid/backgrid-paginator.js

https://github.com/delfianto/rams-web-app
JavaScript | 201 lines | 122 code | 31 blank | 48 comment | 15 complexity | bdebdd719035688b297add3a23e8bf37 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. /*
  2. backgrid-paginator
  3. http://github.com/wyuenho/backgrid
  4. Copyright (c) 2013 Jimmy Yuen Ho Wong
  5. Licensed under the MIT @license.
  6. */
  7. (function ($, _, Backbone, Backgrid) {
  8. "use strict";
  9. /**
  10. Paginator is a Footer element that re-renders a large result set in a table
  11. by splitting it across multiple pages. If the result set is still larger,
  12. the page handles are rendered within a sliding window, with 10 indexed page
  13. handles each by default, plus the fast forward, fast backward, previous and
  14. next page handles. The fast forward, fast backward, previous and next page
  15. handles can be turned off.
  16. @class Backgrid.Extension.Paginator
  17. */
  18. Backgrid.Extension.Paginator = Backgrid.Footer.extend({
  19. /** @property */
  20. className: "paginator",
  21. /** @property */
  22. windowSize: 10,
  23. /** @property */
  24. fastForwardHandleLabels: {
  25. first: "《",
  26. prev: "〈",
  27. next: "〉",
  28. last: "》"
  29. },
  30. /** @property */
  31. template: _.template('<tr><td colspan="<%= colspan %>"><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></td></tr>'),
  32. /** @property */
  33. events: {
  34. "click a": "changePage"
  35. },
  36. /**
  37. Initializer.
  38. @param {Object} options
  39. @param {Backbone.Collection.<Backgrid.Column>|Array.<Backgrid.Column>|Array.<Object>} options.columns
  40. Column metadata.
  41. @param {Backbone.Collection} options.collection
  42. @param {boolean} [options.fastForwardHandleLabels] Whether to render fast forward buttons.
  43. */
  44. initialize: function (options) {
  45. Backgrid.Footer.prototype.initialize.call(this, options);
  46. var columns = this.columns;
  47. this.listenTo(columns, "add", this.render);
  48. this.listenTo(columns, "remove", this.render);
  49. this.listenTo(columns, "change:renderable", this.render);
  50. var collection = this.collection;
  51. var fullCollection = collection.fullCollection;
  52. if (fullCollection) {
  53. this.listenTo(fullCollection, "add", this.render);
  54. this.listenTo(fullCollection, "remove", this.render);
  55. this.listenTo(fullCollection, "reset", this.render);
  56. }
  57. else {
  58. this.listenTo(collection, "add", this.render);
  59. this.listenTo(collection, "remove", this.render);
  60. this.listenTo(collection, "reset", this.render);
  61. }
  62. },
  63. /**
  64. jQuery event handler for the page handlers. Goes to the right page upon
  65. clicking.
  66. @param {Event} e
  67. */
  68. changePage: function (e) {
  69. e.preventDefault();
  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. if (collection.hasPrevious()) collection.getPreviousPage();
  80. return;
  81. case ffLabels.next:
  82. if (collection.hasNext()) collection.getNextPage();
  83. return;
  84. case ffLabels.last:
  85. collection.getLastPage();
  86. return;
  87. }
  88. }
  89. var state = collection.state;
  90. var pageIndex = $(e.target).text() * 1 - state.firstPage;
  91. collection.getPage(state.firstPage === 0 ? pageIndex : pageIndex + 1);
  92. },
  93. /**
  94. Internal method to create a list of page handle objects for the template
  95. to render them.
  96. @return Array.<Object> an array of page handle objects hashes
  97. */
  98. makeHandles: function () {
  99. var handles = [];
  100. var collection = this.collection;
  101. var state = collection.state;
  102. // convert all indices to 0-based here
  103. var lastPage = state.lastPage ? state.lastPage : state.firstPage;
  104. lastPage = state.firstPage === 0 ? lastPage : lastPage - 1;
  105. var currentPage = state.firstPage === 0 ? state.currentPage : state.currentPage - 1;
  106. var windowStart = Math.floor(currentPage / this.windowSize) * this.windowSize;
  107. var windowEnd = windowStart + this.windowSize;
  108. windowEnd = windowEnd <= lastPage ? windowEnd : lastPage + 1;
  109. if (collection.mode !== "infinite") {
  110. for (var i = windowStart; i < windowEnd; i++) {
  111. handles.push({
  112. label: i + 1,
  113. title: "No. " + (i + 1),
  114. className: currentPage === i ? "active" : undefined
  115. });
  116. }
  117. }
  118. var ffLabels = this.fastForwardHandleLabels;
  119. if (ffLabels) {
  120. if (ffLabels.prev) {
  121. handles.unshift({
  122. label: ffLabels.prev,
  123. className: collection.hasPrevious() ? void 0 : "disabled"
  124. });
  125. }
  126. if (ffLabels.first) {
  127. handles.unshift({
  128. label: ffLabels.first,
  129. className: collection.hasPrevious() ? void 0 : "disabled"
  130. });
  131. }
  132. if (ffLabels.next) {
  133. handles.push({
  134. label: ffLabels.next,
  135. className: collection.hasNext() ? void 0 : "disabled"
  136. });
  137. }
  138. if (ffLabels.last) {
  139. handles.push({
  140. label: ffLabels.last,
  141. className: collection.hasNext() ? void 0 : "disabled"
  142. });
  143. }
  144. }
  145. return handles;
  146. },
  147. /**
  148. Render the paginator handles inside an unordered list placed inside a
  149. cell that spans all the columns.
  150. */
  151. render: function () {
  152. this.$el.empty();
  153. var colspan = _.reduce(
  154. this.columns.pluck("renderable"),
  155. function (accum, renderable) {
  156. return renderable ? accum + 1 : accum;
  157. }, 0);
  158. this.$el.append($(this.template({
  159. colspan: colspan,
  160. handles: this.makeHandles()
  161. })));
  162. return this;
  163. }
  164. });
  165. }(jQuery, _, Backbone, Backgrid));