/* * pagination.js 2.0.0 * A jQuery plugin to provide simple yet fully customisable pagination. * https://github.com/superRaytin/paginationjs * * Copyright 2014, 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 pagination = { initialize: function(){ var self = this; // Save attributes of current instance if(!container.data('pagination')){ container.data('pagination', {}); } // Before initialize if(self.callHook('beforeInit') === false) return; // If pagination has been initialized, destroy it if(container.data('pagination').initialized){ $('.paginationjs', container).remove(); } // Inline style if(attributes.inlineStyle === true){ addStyle(); } // 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){ // Whether simulated pagination self.sync = $.isArray(dataSource); if(self.sync){ model.totalNumber = attributes.totalNumber = dataSource.length; } // Obtain the total number of pages model.totalPage = self.getTotalPage(); // Less than one page //if(model.totalPage <= 1) return; var el = self.render(true); // Extra className if(attributes.className){ el.addClass(attributes.className); } model.el = el; // Load template container[attributes.position === 'bottom' ? 'append' : 'prepend'](el); // Binding events self.observer(); // initialized flag container.data('pagination').initialized = true; // After initialize self.callHook('afterInit', el); }); }, render: function(isBoot){ var self = this; var model = self.model; var el = model.el || $('
'); var isForced = isBoot !== true; // Before render self.callHook('beforeRender', isForced); var currentPage = model.pageNumber || attributes.pageNumber; var pageRange = attributes.pageRange; var totalPage = model.totalPage; 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, totalPage: totalPage, rangeStart: rangeStart, rangeEnd: rangeEnd })); // After render self.callHook('afterRender', isForced); return el; }, // Create template createTemplate: function(args){ var self = this; var currentPage = args.currentPage; var totalPage = args.totalPage; var rangeStart = args.rangeStart; var rangeEnd = args.rangeEnd; var totalNumber = attributes.totalNumber; 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(showPrevious || showPageNumbers || showNext){ html += '
'; if(ulClassName){ html += '