2 * simplePagination.js v1.6
3 * A simple jQuery pagination plugin.
4 * http://flaviusmatis.github.com/simplePagination.js/
6 * Copyright 2012, Flavius Matis
7 * Released under the MIT license.
8 * http://flaviusmatis.github.com/license.html
14 init: function(options) {
23 hrefTextPrefix: '#page-',
27 ellipseText: '…',
29 cssStyle: 'light-theme',
34 invertPageOrder: false,
37 onPageClick: function(pageNumber, event) {
38 // Callback triggered when a page is clicked
39 // Page number is given as an optional parameter
42 // Callback triggered immediately after initialization
48 o.pages = o.pages ? o.pages : Math.ceil(o.items / o.itemsOnPage) ? Math.ceil(o.items / o.itemsOnPage) : 1;
50 o.currentPage = o.currentPage - 1;
52 o.currentPage = !o.invertPageOrder ? 0 : o.pages - 1;
53 o.halfDisplayed = o.displayedPages / 2;
55 this.each(function() {
56 self.addClass(o.cssStyle + ' simple-pagination').data('pagination', o);
57 methods._draw.call(self);
65 selectPage: function(page) {
66 methods._selectPage.call(this, page - 1);
70 prevPage: function() {
71 var o = this.data('pagination');
72 if (!o.invertPageOrder) {
73 if (o.currentPage > 0) {
74 methods._selectPage.call(this, o.currentPage - 1);
77 if (o.currentPage < o.pages - 1) {
78 methods._selectPage.call(this, o.currentPage + 1);
84 nextPage: function() {
85 var o = this.data('pagination');
86 if (!o.invertPageOrder) {
87 if (o.currentPage < o.pages - 1) {
88 methods._selectPage.call(this, o.currentPage + 1);
91 if (o.currentPage > 0) {
92 methods._selectPage.call(this, o.currentPage - 1);
98 getPagesCount: function() {
99 return this.data('pagination').pages;
102 setPagesCount: function(count) {
103 this.data('pagination').pages = count;
106 getCurrentPage: function () {
107 return this.data('pagination').currentPage + 1;
115 drawPage: function (page) {
116 var o = this.data('pagination');
117 o.currentPage = page - 1;
118 this.data('pagination', o);
119 methods._draw.call(this);
124 methods._draw.call(this);
129 var o = this.data('pagination');
131 this.data('pagination', o);
132 methods._draw.call(this);
137 var o = this.data('pagination');
139 this.data('pagination', o);
140 methods._draw.call(this);
144 updateItems: function (newItems) {
145 var o = this.data('pagination');
147 o.pages = methods._getPages(o);
148 this.data('pagination', o);
149 methods._draw.call(this);
152 updateItemsOnPage: function (itemsOnPage) {
153 var o = this.data('pagination');
154 o.itemsOnPage = itemsOnPage;
155 o.pages = methods._getPages(o);
156 this.data('pagination', o);
157 methods._selectPage.call(this, 0);
161 getItemsOnPage: function() {
162 return this.data('pagination').itemsOnPage;
166 var o = this.data('pagination'),
167 interval = methods._getInterval(o),
171 methods.destroy.call(this);
173 tagName = (typeof this.prop === 'function') ? this.prop('tagName') : this.attr('tagName');
175 var $panel = tagName === 'UL' ? this : $('<ul' + (o.listStyle ? ' class="' + o.listStyle + '"' : '') + '></ul>').appendTo(this);
177 // Generate Prev link
179 methods._appendItem.call(this, !o.invertPageOrder ? o.currentPage - 1 : o.currentPage + 1, {text: o.prevText, classes: 'prev'});
182 // Generate Next link (if option set for at front)
183 if (o.nextText && o.nextAtFront) {
184 methods._appendItem.call(this, !o.invertPageOrder ? o.currentPage + 1 : o.currentPage - 1, {text: o.nextText, classes: 'next'});
187 // Generate start edges
188 if (!o.invertPageOrder) {
189 if (interval.start > 0 && o.edges > 0) {
191 var end = Math.min(o.edges, interval.start);
192 for (i = 0; i < end; i++) {
193 methods._appendItem.call(this, i);
196 if (o.edges < interval.start && (interval.start - o.edges != 1)) {
197 $panel.append('<li class="disabled"><span class="ellipse">' + o.ellipseText + '</span></li>');
198 } else if (interval.start - o.edges == 1) {
199 methods._appendItem.call(this, o.edges);
203 if (interval.end < o.pages && o.edges > 0) {
205 var begin = Math.max(o.pages - o.edges, interval.end);
206 for (i = o.pages - 1; i >= begin; i--) {
207 methods._appendItem.call(this, i);
211 if (o.pages - o.edges > interval.end && (o.pages - o.edges - interval.end != 1)) {
212 $panel.append('<li class="disabled"><span class="ellipse">' + o.ellipseText + '</span></li>');
213 } else if (o.pages - o.edges - interval.end == 1) {
214 methods._appendItem.call(this, interval.end);
219 // Generate interval links
220 if (!o.invertPageOrder) {
221 for (i = interval.start; i < interval.end; i++) {
222 methods._appendItem.call(this, i);
225 for (i = interval.end - 1; i >= interval.start; i--) {
226 methods._appendItem.call(this, i);
230 // Generate end edges
231 if (!o.invertPageOrder) {
232 if (interval.end < o.pages && o.edges > 0) {
233 if (o.pages - o.edges > interval.end && (o.pages - o.edges - interval.end != 1)) {
234 $panel.append('<li class="disabled"><span class="ellipse">' + o.ellipseText + '</span></li>');
235 } else if (o.pages - o.edges - interval.end == 1) {
236 methods._appendItem.call(this, interval.end);
239 var begin = Math.max(o.pages - o.edges, interval.end);
240 for (i = begin; i < o.pages; i++) {
241 methods._appendItem.call(this, i);
246 if (interval.start > 0 && o.edges > 0) {
247 if (o.edges < interval.start && (interval.start - o.edges != 1)) {
248 $panel.append('<li class="disabled"><span class="ellipse">' + o.ellipseText + '</span></li>');
249 } else if (interval.start - o.edges == 1) {
250 methods._appendItem.call(this, o.edges);
254 var end = Math.min(o.edges, interval.start);
255 for (i = end - 1; i >= 0; i--) {
256 methods._appendItem.call(this, i);
262 // Generate Next link (unless option is set for at front)
263 if (o.nextText && !o.nextAtFront) {
264 methods._appendItem.call(this, !o.invertPageOrder ? o.currentPage + 1 : o.currentPage - 1, {text: o.nextText, classes: 'next'});
267 if (o.ellipsePageSet && !o.disabled) {
268 methods._ellipseClick.call(this, $panel);
273 _getPages: function(o) {
274 var pages = Math.ceil(o.items / o.itemsOnPage);
278 _getInterval: function(o) {
280 start: Math.ceil(o.currentPage > o.halfDisplayed ? Math.max(Math.min(o.currentPage - o.halfDisplayed, (o.pages - o.displayedPages)), 0) : 0),
281 end: Math.ceil(o.currentPage > o.halfDisplayed ? Math.min(o.currentPage + o.halfDisplayed, o.pages) : Math.min(o.displayedPages, o.pages))
285 _appendItem: function(pageIndex, opts) {
286 var self = this, options, $link, o = self.data('pagination'), $linkWrapper = $('<li></li>'), $ul = self.find('ul');
288 pageIndex = pageIndex < 0 ? 0 : (pageIndex < o.pages ? pageIndex : o.pages - 1);
295 if (o.labelMap.length && o.labelMap[pageIndex]) {
296 options.text = o.labelMap[pageIndex];
299 options = $.extend(options, opts || {});
301 if (pageIndex == o.currentPage || o.disabled) {
302 if (o.disabled || options.classes === 'prev' || options.classes === 'next') {
303 $linkWrapper.addClass('disabled');
305 $linkWrapper.addClass('active');
307 $link = $('<span class="current">' + (options.text) + '</span>');
310 $link = $('<a href="' + o.hrefTextPrefix + (pageIndex + 1) + o.hrefTextSuffix + '" class="page-link">' + (options.text) + '</a>');
312 $link = $('<span >' + (options.text) + '</span>');
314 $link.click(function(event){
315 return methods._selectPage.call(self, pageIndex, event);
319 if (options.classes) {
320 $link.addClass(options.classes);
323 $linkWrapper.append($link);
326 $ul.append($linkWrapper);
328 self.append($linkWrapper);
332 _selectPage: function(pageIndex, event) {
333 var o = this.data('pagination');
334 o.currentPage = pageIndex;
335 if (o.selectOnClick) {
336 methods._draw.call(this);
338 return o.onPageClick(pageIndex + 1, event);
342 _ellipseClick: function($panel) {
344 o = this.data('pagination'),
345 $ellip = $panel.find('.ellipse');
346 $ellip.addClass('clickable').parent().removeClass('disabled');
347 $ellip.click(function(event) {
350 val = (parseInt($this.parent().prev().text(), 10) || 0) + 1;
352 .html('<input type="number" min="1" max="' + o.pages + '" step="1" value="' + val + '">')
355 .click(function(event) {
356 // prevent input number arrows from bubbling a click event on $ellip
357 event.stopPropagation();
359 .keyup(function(event) {
360 var val = $(this).val();
361 if (event.which === 13 && val !== '') {
363 if ((val>0)&&(val<=o.pages))
364 methods._selectPage.call(self, val - 1);
365 } else if (event.which === 27) {
367 $ellip.empty().html(o.ellipseText);
370 .bind('blur', function(event) {
371 var val = $(this).val();
373 methods._selectPage.call(self, val - 1);
375 $ellip.empty().html(o.ellipseText);
385 $.fn.pagination = function(method) {
387 // Method calling logic
388 if (methods[method] && method.charAt(0) != '_') {
389 return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
390 } else if (typeof method === 'object' || !method) {
391 return methods.init.apply(this, arguments);
393 $.error('Method ' + method + ' does not exist on jQuery.pagination');