jquery.twbsPagination.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*!
  2. * jQuery pagination plugin v1.4.1
  3. * http://esimakin.github.io/twbs-pagination/
  4. *
  5. * Copyright 2014-2016, Eugene Simakin
  6. * Released under Apache 2.0 license
  7. * http://apache.org/licenses/LICENSE-2.0.html
  8. */
  9. (function ($, window, document, undefined) {
  10. 'use strict';
  11. var old = $.fn.twbsPagination;
  12. // PROTOTYPE AND CONSTRUCTOR
  13. var TwbsPagination = function (element, options) {
  14. this.$element = $(element);
  15. this.options = $.extend({}, $.fn.twbsPagination.defaults, options);
  16. if (this.options.startPage < 1 || this.options.startPage > this.options.totalPages) {
  17. throw new Error('Start page option is incorrect');
  18. }
  19. this.options.totalPages = parseInt(this.options.totalPages);
  20. if (isNaN(this.options.totalPages)) {
  21. throw new Error('Total pages option is not correct!');
  22. }
  23. this.options.visiblePages = parseInt(this.options.visiblePages);
  24. if (isNaN(this.options.visiblePages)) {
  25. throw new Error('Visible pages option is not correct!');
  26. }
  27. if (this.options.onPageClick instanceof Function) {
  28. this.$element.first().on('page', this.options.onPageClick);
  29. }
  30. // hide if only one page exists
  31. if (this.options.hideOnlyOnePage && this.options.totalPages == 1) {
  32. this.$element.trigger('page', 1);
  33. return this;
  34. }
  35. if (this.options.totalPages < this.options.visiblePages) {
  36. this.options.visiblePages = this.options.totalPages;
  37. }
  38. if (this.options.href) {
  39. this.options.startPage = this.getPageFromQueryString();
  40. if (!this.options.startPage) {
  41. this.options.startPage = 1;
  42. }
  43. }
  44. var tagName = (typeof this.$element.prop === 'function') ?
  45. this.$element.prop('tagName') : this.$element.attr('tagName');
  46. if (tagName === 'UL') {
  47. this.$listContainer = this.$element;
  48. } else {
  49. this.$listContainer = $('<ul></ul>');
  50. }
  51. this.$listContainer.addClass(this.options.paginationClass);
  52. if (tagName !== 'UL') {
  53. this.$element.append(this.$listContainer);
  54. }
  55. if (this.options.initiateStartPageClick) {
  56. this.show(this.options.startPage);
  57. } else {
  58. this.render(this.getPages(this.options.startPage));
  59. this.setupEvents();
  60. }
  61. return this;
  62. };
  63. TwbsPagination.prototype = {
  64. constructor: TwbsPagination,
  65. destroy: function () {
  66. this.$element.empty();
  67. this.$element.removeData('twbs-pagination');
  68. this.$element.off('page');
  69. return this;
  70. },
  71. show: function (page) {
  72. if (page < 1 || page > this.options.totalPages) {
  73. throw new Error('Page is incorrect.');
  74. }
  75. this.currentPage = page;
  76. this.render(this.getPages(page));
  77. this.setupEvents();
  78. this.$element.trigger('page', page);
  79. return this;
  80. },
  81. buildListItems: function (pages) {
  82. var listItems = [];
  83. if (this.options.first) {
  84. listItems.push(this.buildItem('first', 1));
  85. }
  86. if (this.options.prev) {
  87. var prev = pages.currentPage > 1 ? pages.currentPage - 1 : this.options.loop ? this.options.totalPages : 1;
  88. listItems.push(this.buildItem('prev', prev));
  89. }
  90. for (var i = 0; i < pages.numeric.length; i++) {
  91. listItems.push(this.buildItem('page', pages.numeric[i]));
  92. }
  93. if (this.options.next) {
  94. var next = pages.currentPage < this.options.totalPages ? pages.currentPage + 1 : this.options.loop ? 1 : this.options.totalPages;
  95. listItems.push(this.buildItem('next', next));
  96. }
  97. if (this.options.last) {
  98. listItems.push(this.buildItem('last', this.options.totalPages));
  99. }
  100. return listItems;
  101. },
  102. buildItem: function (type, page) {
  103. var $itemContainer = $('<li></li>'),
  104. $itemContent = $('<a></a>'),
  105. itemText = this.options[type] ? this.makeText(this.options[type], page) : page;
  106. $itemContainer.addClass(this.options[type + 'Class']);
  107. $itemContainer.data('page', page);
  108. $itemContainer.data('page-type', type);
  109. $itemContainer.append($itemContent.attr('href', this.makeHref(page)).addClass(this.options.anchorClass).html(itemText));
  110. return $itemContainer;
  111. },
  112. getPages: function (currentPage) {
  113. var pages = [];
  114. var half = Math.floor(this.options.visiblePages / 2);
  115. var start = currentPage - half + 1 - this.options.visiblePages % 2;
  116. var end = currentPage + half;
  117. // handle boundary case
  118. if (start <= 0) {
  119. start = 1;
  120. end = this.options.visiblePages;
  121. }
  122. if (end > this.options.totalPages) {
  123. start = this.options.totalPages - this.options.visiblePages + 1;
  124. end = this.options.totalPages;
  125. }
  126. var itPage = start;
  127. while (itPage <= end) {
  128. pages.push(itPage);
  129. itPage++;
  130. }
  131. return {"currentPage": currentPage, "numeric": pages};
  132. },
  133. render: function (pages) {
  134. var _this = this;
  135. this.$listContainer.children().remove();
  136. var items = this.buildListItems(pages);
  137. jQuery.each(items, function(key, item){
  138. _this.$listContainer.append(item);
  139. });
  140. this.$listContainer.children().each(function () {
  141. var $this = $(this),
  142. pageType = $this.data('page-type');
  143. switch (pageType) {
  144. case 'page':
  145. if ($this.data('page') === pages.currentPage) {
  146. $this.addClass(_this.options.activeClass);
  147. }
  148. break;
  149. case 'first':
  150. $this.toggleClass(_this.options.disabledClass, pages.currentPage === 1);
  151. break;
  152. case 'last':
  153. $this.toggleClass(_this.options.disabledClass, pages.currentPage === _this.options.totalPages);
  154. break;
  155. case 'prev':
  156. $this.toggleClass(_this.options.disabledClass, !_this.options.loop && pages.currentPage === 1);
  157. break;
  158. case 'next':
  159. $this.toggleClass(_this.options.disabledClass,
  160. !_this.options.loop && pages.currentPage === _this.options.totalPages);
  161. break;
  162. default:
  163. break;
  164. }
  165. });
  166. },
  167. setupEvents: function () {
  168. var _this = this;
  169. this.$listContainer.off('click').on('click', 'li', function (evt) {
  170. var $this = $(this);
  171. if ($this.hasClass(_this.options.disabledClass) || $this.hasClass(_this.options.activeClass)) {
  172. return false;
  173. }
  174. // Prevent click event if href is not set.
  175. !_this.options.href && evt.preventDefault();
  176. _this.show(parseInt($this.data('page')));
  177. });
  178. },
  179. makeHref: function (page) {
  180. return this.options.href ? this.generateQueryString(page) : "#";
  181. },
  182. makeText: function (text, page) {
  183. return text.replace(this.options.pageVariable, page)
  184. .replace(this.options.totalPagesVariable, this.options.totalPages)
  185. },
  186. getPageFromQueryString: function (searchStr) {
  187. var search = this.getSearchString(searchStr),
  188. regex = new RegExp(this.options.pageVariable + '(=([^&#]*)|&|#|$)'),
  189. page = regex.exec(search);
  190. if (!page || !page[2]) {
  191. return null;
  192. }
  193. page = decodeURIComponent(page[2]);
  194. page = parseInt(page);
  195. if (isNaN(page)) {
  196. return null;
  197. }
  198. return page;
  199. },
  200. generateQueryString: function (pageNumber, searchStr) {
  201. var search = this.getSearchString(searchStr),
  202. regex = new RegExp(this.options.pageVariable + '=*[^&#]*');
  203. if (!search) return '';
  204. return '?' + search.replace(regex, this.options.pageVariable + '=' + pageNumber);
  205. },
  206. getSearchString: function (searchStr) {
  207. var search = searchStr || window.location.search;
  208. if (search === '') {
  209. return null;
  210. }
  211. if (search.indexOf('?') === 0) search = search.substr(1);
  212. return search;
  213. }
  214. };
  215. // PLUGIN DEFINITION
  216. $.fn.twbsPagination = function (option) {
  217. var args = Array.prototype.slice.call(arguments, 1);
  218. var methodReturn;
  219. var $this = $(this);
  220. var data = $this.data('twbs-pagination');
  221. var options = typeof option === 'object' ? option : {};
  222. if (!data) $this.data('twbs-pagination', (data = new TwbsPagination(this, options) ));
  223. if (typeof option === 'string') methodReturn = data[ option ].apply(data, args);
  224. return ( methodReturn === undefined ) ? $this : methodReturn;
  225. };
  226. $.fn.twbsPagination.defaults = {
  227. totalPages: 1,
  228. startPage: 1,
  229. visiblePages: 5,
  230. initiateStartPageClick: true,
  231. hideOnlyOnePage: false,
  232. href: false,
  233. pageVariable: '{{page}}',
  234. totalPagesVariable: '{{total_pages}}',
  235. page: null,
  236. first: 'First',
  237. prev: 'Previous',
  238. next: 'Next',
  239. last: 'Last',
  240. loop: false,
  241. onPageClick: null,
  242. paginationClass: 'pagination',
  243. nextClass: 'page-item next',
  244. prevClass: 'page-item prev',
  245. lastClass: 'page-item last',
  246. firstClass: 'page-item first',
  247. pageClass: 'page-item',
  248. activeClass: 'active',
  249. disabledClass: 'disabled',
  250. anchorClass: 'page-link'
  251. };
  252. $.fn.twbsPagination.Constructor = TwbsPagination;
  253. $.fn.twbsPagination.noConflict = function () {
  254. $.fn.twbsPagination = old;
  255. return this;
  256. };
  257. $.fn.twbsPagination.version = "1.4.1";
  258. })(window.jQuery, window, document);