You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
7.6 KiB

2 years ago
  1. /**
  2. * simplePagination.js v1.5
  3. * A simple jQuery pagination plugin.
  4. * http://flaviusmatis.github.com/simplePagination.js/
  5. *
  6. * Copyright 2012, Flavius Matis
  7. * Released under the MIT license.
  8. * http://flaviusmatis.github.com/license.html
  9. */
  10. (function ($) {
  11. var methods = {
  12. init: function (options) {
  13. var o = $.extend({
  14. items: 1,
  15. itemsOnPage: 1,
  16. pages: 0,
  17. displayedPages: 5,
  18. edges: 2,
  19. currentPage: 1,
  20. hrefTextPrefix: '#page-',
  21. hrefTextSuffix: '',
  22. prevText: 'Prev',
  23. nextText: 'Next',
  24. ellipseText: '…',
  25. cssStyle: 'light-theme',
  26. selectOnClick: true,
  27. onPageClick: function (pageNumber, event) {
  28. // Callback triggered when a page is clicked
  29. // Page number is given as an optional parameter
  30. },
  31. onInit: function () {
  32. // Callback triggered immediately after initialization
  33. }
  34. }, options || {});
  35. var self = this;
  36. o.pages = o.pages ? o.pages : Math.ceil(o.items / o.itemsOnPage) ? Math.ceil(o.items / o.itemsOnPage) : 1;
  37. o.currentPage = o.currentPage - 1;
  38. o.halfDisplayed = o.displayedPages / 2;
  39. this.each(function () {
  40. self.addClass(o.cssStyle + ' simple-pagination').data('pagination', o);
  41. methods._draw.call(self);
  42. });
  43. o.onInit();
  44. return this;
  45. },
  46. selectPage: function (page) {
  47. methods._selectPage.call(this, page - 1);
  48. return this;
  49. },
  50. prevPage: function () {
  51. var o = this.data('pagination');
  52. if (o.currentPage > 0) {
  53. methods._selectPage.call(this, o.currentPage - 1);
  54. }
  55. return this;
  56. },
  57. nextPage: function () {
  58. var o = this.data('pagination');
  59. if (o.currentPage < o.pages - 1) {
  60. methods._selectPage.call(this, o.currentPage + 1);
  61. }
  62. return this;
  63. },
  64. getPagesCount: function () {
  65. return this.data('pagination').pages;
  66. },
  67. getCurrentPage: function () {
  68. return this.data('pagination').currentPage + 1;
  69. },
  70. destroy: function () {
  71. this.empty();
  72. return this;
  73. },
  74. redraw: function () {
  75. methods._draw.call(this);
  76. return this;
  77. },
  78. disable: function () {
  79. var o = this.data('pagination');
  80. o.disabled = true;
  81. this.data('pagination', o);
  82. methods._draw.call(this);
  83. return this;
  84. },
  85. enable: function () {
  86. var o = this.data('pagination');
  87. o.disabled = false;
  88. this.data('pagination', o);
  89. methods._draw.call(this);
  90. return this;
  91. },
  92. _draw: function () {
  93. var o = this.data('pagination'),
  94. interval = methods._getInterval(o),
  95. i;
  96. methods.destroy.call(this);
  97. var $panel = this.prop("tagName") === "UL" ? this : $('<ul></ul>').appendTo(this);
  98. // Generate Prev link
  99. if (o.prevText) {
  100. methods._appendItem.call(this, o.currentPage - 1, { text: o.prevText, classes: 'prev' });
  101. }
  102. // Generate start edges
  103. if (interval.start > 0 && o.edges > 0) {
  104. var end = Math.min(o.edges, interval.start);
  105. for (i = 0; i < end; i++) {
  106. methods._appendItem.call(this, i);
  107. }
  108. if (o.edges < interval.start && (interval.start - o.edges != 1)) {
  109. $panel.append('<li class="disabled"><span class="ellipse">' + o.ellipseText + '</span></li>');
  110. } else if (interval.start - o.edges == 1) {
  111. methods._appendItem.call(this, o.edges);
  112. }
  113. }
  114. // Generate interval links
  115. for (i = interval.start; i < interval.end; i++) {
  116. methods._appendItem.call(this, i);
  117. }
  118. // Generate end edges
  119. if (interval.end < o.pages && o.edges > 0) {
  120. if (o.pages - o.edges > interval.end && (o.pages - o.edges - interval.end != 1)) {
  121. $panel.append('<li class="disabled"><span class="ellipse">' + o.ellipseText + '</span></li>');
  122. } else if (o.pages - o.edges - interval.end == 1) {
  123. methods._appendItem.call(this, interval.end++);
  124. }
  125. var begin = Math.max(o.pages - o.edges, interval.end);
  126. for (i = begin; i < o.pages; i++) {
  127. methods._appendItem.call(this, i);
  128. }
  129. }
  130. // Generate Next link
  131. if (o.nextText) {
  132. methods._appendItem.call(this, o.currentPage + 1, { text: o.nextText, classes: 'next' });
  133. }
  134. },
  135. _getInterval: function (o) {
  136. return {
  137. start: Math.ceil(o.currentPage > o.halfDisplayed ? Math.max(Math.min(o.currentPage - o.halfDisplayed, (o.pages - o.displayedPages)), 0) : 0),
  138. end: Math.ceil(o.currentPage > o.halfDisplayed ? Math.min(o.currentPage + o.halfDisplayed, o.pages) : Math.min(o.displayedPages, o.pages))
  139. };
  140. },
  141. _appendItem: function (pageIndex, opts) {
  142. var self = this, options, $link, o = self.data('pagination'), $linkWrapper = $('<li></li>'), $ul = self.find('ul');
  143. pageIndex = pageIndex < 0 ? 0 : (pageIndex < o.pages ? pageIndex : o.pages - 1);
  144. options = $.extend({
  145. text: pageIndex + 1,
  146. classes: ''
  147. }, opts || {});
  148. if (pageIndex == o.currentPage || o.disabled) {
  149. if (o.disabled) {
  150. $linkWrapper.addClass('disabled');
  151. } else {
  152. $linkWrapper.addClass('active');
  153. }
  154. $link = $('<span class="current">' + (options.text) + '</span>');
  155. } else {
  156. $link = $('<a href="' + o.hrefTextPrefix + (pageIndex + 1) + o.hrefTextSuffix + '" class="page-link">' + (options.text) + '</a>');
  157. $link.click(function (event) {
  158. return methods._selectPage.call(self, pageIndex, event);
  159. });
  160. }
  161. if (options.classes) {
  162. $link.addClass(options.classes);
  163. }
  164. $linkWrapper.append($link);
  165. if ($ul.length) {
  166. $ul.append($linkWrapper);
  167. } else {
  168. self.append($linkWrapper);
  169. }
  170. },
  171. _selectPage: function (pageIndex, event) {
  172. var o = this.data('pagination');
  173. o.currentPage = pageIndex;
  174. if (o.selectOnClick) {
  175. methods._draw.call(this);
  176. }
  177. return o.onPageClick(pageIndex + 1, event);
  178. }
  179. };
  180. $.fn.pagination = function (method) {
  181. // Method calling logic
  182. if (methods[method] && method.charAt(0) != '_') {
  183. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  184. } else if (typeof method === 'object' || !method) {
  185. return methods.init.apply(this, arguments);
  186. } else {
  187. $.error('Method ' + method + ' does not exist on jQuery.pagination');
  188. }
  189. };
  190. })(jQuery);