From 261cd8e54d357c649b30a722bf99c3b438f1b0e1 Mon Sep 17 00:00:00 2001 From: Alessandro Arantes Date: Tue, 28 Nov 2017 13:22:14 -0200 Subject: [PATCH] support for multiple synced paginators --- src/pagination.js | 2075 +++++++++++++++++++++++---------------------- 1 file changed, 1038 insertions(+), 1037 deletions(-) diff --git a/src/pagination.js b/src/pagination.js index 88272e8..abc7aa6 100644 --- a/src/pagination.js +++ b/src/pagination.js @@ -10,1081 +10,1082 @@ */ (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; - - // 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) { - - // is async mode - self.isAsync = Helpers.isString(dataSource); - if (Helpers.isArray(dataSource)) { - model.totalNumber = attributes.totalNumber = dataSource.length; - } - - // in async mode and specified totalNumber locator - self.isDynamicTotalNumber = self.isAsync && attributes.totalNumberLocator; - - // Have only one page - if (attributes.hideWhenLessThanOnePage) { - if (self.getTotalPage() <= 1) return; - } - - var el = self.render(true); - - // Add extra className - if (attributes.className) { - el.addClass(attributes.className); - } - - model.el = el; - - // Append pagination element to container - container[attributes.position === 'bottom' ? 'append' : 'prepend'](el); - - // Bind events - self.observer(); - - // Add initialization flag - 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; - 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.createTemplate({ - currentPage: currentPage, - pageRange: pageRange, - rangeStart: rangeStart, - rangeEnd: rangeEnd - })); - - self.callHook('afterRender', isForced); - - return el; - }, - - // Create template - createTemplate: function(args) { - var self = this; - var currentPage = args.currentPage; - var totalPage = self.getTotalPage(); - var rangeStart = args.rangeStart; - var rangeEnd = args.rangeEnd; - - 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 ellipsisText = attributes.ellipsisText; - var goButtonText = attributes.goButtonText; - - var classPrefix = attributes.classPrefix; - var activeClassName = attributes.activeClassName; - var disableClassName = attributes.disableClassName; - var ulClassName = attributes.ulClassName; - - var formatNavigator = $.isFunction(attributes.formatNavigator) ? attributes.formatNavigator() : attributes.formatNavigator; - var formatGoInput = $.isFunction(attributes.formatGoInput) ? attributes.formatGoInput() : attributes.formatGoInput; - var formatGoButton = $.isFunction(attributes.formatGoButton) ? attributes.formatGoButton() : 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() : attributes.header; - var footer = $.isFunction(attributes.footer) ? attributes.footer() : attributes.footer; - - var html = ''; - var goInput = ''; - var goButton = ''; - var formattedString; - var i; - - if (header) { - formattedString = self.replaceVariables(header, { - currentPage: currentPage, - totalPage: totalPage, - totalNumber: totalNumber - }); - html += formattedString; + + 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; } - - if (showPrevious || showPageNumbers || showNext) { - html += '
'; - - if (ulClassName) { - html += '