/* * pagination.js 2.1.5 * A jQuery plugin to provide simple yet fully customisable pagination. * https://github.com/superRaytin/paginationjs * * Homepage: http://pagination.js.org * * Copyright 2014-2100, superRaytin * Released under the MIT license. */ (function(global, $) { if (typeof $ === 'undefined') { throwError('Pagination requires jQuery.'); } var pluginName = 'pagination'; var pluginHookMethod = 'addHook'; var eventPrefix = '__pagination-'; // Conflict, use backup if ($.fn.pagination) { pluginName = 'pagination2'; } $.fn[pluginName] = function(options) { if (typeof options === 'undefined') { return this; } var container = $(this); var attributes = $.extend({}, $.fn[pluginName].defaults, options); var pagination = { initialize: function() { var self = this; // Cache attributes of current instance if (!container.data('pagination')) { container.data('pagination', {}); } if (self.callHook('beforeInit') === false) return; // Pagination has been initialized, destroy it if (container.data('pagination').initialized) { $('.paginationjs', container).remove(); } // Whether to disable Pagination at the initialization self.disabled = !!attributes.disabled; // Model will be passed to the callback function var model = self.model = { pageRange: attributes.pageRange, pageSize: attributes.pageSize }; // dataSource`s type is unknown, parse it to find true data self.parseDataSource(attributes.dataSource, function(dataSource) { // Currently in asynchronous mode self.isAsync = Helpers.isString(dataSource); if (Helpers.isArray(dataSource)) { model.totalNumber = attributes.totalNumber = dataSource.length; } // Currently in asynchronous mode and a totalNumberLocator is specified self.isDynamicTotalNumber = self.isAsync && attributes.totalNumberLocator; var el = self.render(true); // Add extra className to the pagination element if (attributes.className) { el.addClass(attributes.className); } model.el = el; // Append/prepend pagination element to the container container[attributes.position === 'bottom' ? 'append' : 'prepend'](el); // Bind events self.observer(); // Pagination is currently initialized container.data('pagination').initialized = true; // Will be invoked after initialized self.callHook('afterInit', el); }); }, render: function(isBoot) { var self = this; var model = self.model; var el = model.el || $('
'); var isForced = isBoot !== true; self.callHook('beforeRender', isForced); var currentPage = model.pageNumber || attributes.pageNumber; var pageRange = attributes.pageRange || 0; var totalPage = self.getTotalPage(); var rangeStart = currentPage - pageRange; var rangeEnd = currentPage + pageRange; if (rangeEnd > totalPage) { rangeEnd = totalPage; rangeStart = totalPage - pageRange * 2; rangeStart = rangeStart < 1 ? 1 : rangeStart; } if (rangeStart <= 1) { rangeStart = 1; rangeEnd = Math.min(pageRange * 2 + 1, totalPage); } el.html(self.generateHTML({ currentPage: currentPage, pageRange: pageRange, rangeStart: rangeStart, rangeEnd: rangeEnd })); // There is only one page if (attributes.hideWhenLessThanOnePage) { el[totalPage <= 1 ? 'hide' : 'show'](); } self.callHook('afterRender', isForced); return el; }, // Generate HTML of the pages generatePageNumbersHTML: function(args) { var self = this; var currentPage = args.currentPage; var totalPage = self.getTotalPage(); var rangeStart = args.rangeStart; var rangeEnd = args.rangeEnd; var html = ''; var i; var pageLink = attributes.pageLink; var ellipsisText = attributes.ellipsisText; var classPrefix = attributes.classPrefix; var activeClassName = attributes.activeClassName; var disableClassName = attributes.disableClassName; // Disable page range, display all the pages if (attributes.pageRange === null) { for (i = 1; i <= totalPage; i++) { if (i == currentPage) { html += '
  • ' + i + '<\/a><\/li>'; } else { html += '
  • ' + i + '<\/a><\/li>'; } } return html; } if (rangeStart <= 3) { for (i = 1; i < rangeStart; i++) { if (i == currentPage) { html += '
  • ' + i + '<\/a><\/li>'; } else { html += '
  • ' + i + '<\/a><\/li>'; } } } else { if (attributes.showFirstOnEllipsisShow) { html += '
  • 1<\/a><\/li>'; } html += '
  • ' + ellipsisText + '<\/a><\/li>'; } for (i = rangeStart; i <= rangeEnd; i++) { if (i == currentPage) { html += '
  • ' + i + '<\/a><\/li>'; } else { html += '
  • ' + i + '<\/a><\/li>'; } } if (rangeEnd >= totalPage - 2) { for (i = rangeEnd + 1; i <= totalPage; i++) { html += '
  • ' + i + '<\/a><\/li>'; } } else { html += '
  • ' + ellipsisText + '<\/a><\/li>'; if (attributes.showLastOnEllipsisShow) { html += '
  • ' + totalPage + '<\/a><\/li>'; } } return html; }, // Generate HTML content from the template generateHTML: function(args) { var self = this; var currentPage = args.currentPage; var totalPage = self.getTotalPage(); var totalNumber = self.getTotalNumber(); var showPrevious = attributes.showPrevious; var showNext = attributes.showNext; var showPageNumbers = attributes.showPageNumbers; var showNavigator = attributes.showNavigator; var showGoInput = attributes.showGoInput; var showGoButton = attributes.showGoButton; var pageLink = attributes.pageLink; var prevText = attributes.prevText; var nextText = attributes.nextText; var goButtonText = attributes.goButtonText; var classPrefix = attributes.classPrefix; var disableClassName = attributes.disableClassName; var ulClassName = attributes.ulClassName; var html = ''; var goInput = ''; var goButton = ''; var formattedString; var formatNavigator = $.isFunction(attributes.formatNavigator) ? attributes.formatNavigator(currentPage, totalPage, totalNumber) : attributes.formatNavigator; var formatGoInput = $.isFunction(attributes.formatGoInput) ? attributes.formatGoInput(goInput, currentPage, totalPage, totalNumber) : attributes.formatGoInput; var formatGoButton = $.isFunction(attributes.formatGoButton) ? attributes.formatGoButton(goButton, currentPage, totalPage, totalNumber) : attributes.formatGoButton; var autoHidePrevious = $.isFunction(attributes.autoHidePrevious) ? attributes.autoHidePrevious() : attributes.autoHidePrevious; var autoHideNext = $.isFunction(attributes.autoHideNext) ? attributes.autoHideNext() : attributes.autoHideNext; var header = $.isFunction(attributes.header) ? attributes.header(currentPage, totalPage, totalNumber) : attributes.header; var footer = $.isFunction(attributes.footer) ? attributes.footer(currentPage, totalPage, totalNumber) : attributes.footer; // Whether to display header if (header) { formattedString = self.replaceVariables(header, { currentPage: currentPage, totalPage: totalPage, totalNumber: totalNumber }); html += formattedString; } if (showPrevious || showPageNumbers || showNext) { html += '
    '; if (ulClassName) { html += '