| 1 |
(function($) { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
var Paginator = function() { |
| 5 |
return { |
| 6 |
// Attributes |
| 7 |
obj: null, |
| 8 |
options: null, |
| 9 |
nav: null, |
| 10 |
|
| 11 |
// Methods |
| 12 |
build: function(obj, opts) { |
| 13 |
this.obj = obj; |
| 14 |
this.options = opts; |
| 15 |
|
| 16 |
if(!this.options.optional || this._totalRows() > this.options.limit) { |
| 17 |
this._createNavigation(); |
| 18 |
this._setPage(); |
| 19 |
} |
| 20 |
|
| 21 |
if(this.options.onCreate) this.options.onCreate(obj); |
| 22 |
|
| 23 |
return this.obj; |
| 24 |
}, |
| 25 |
|
| 26 |
_createNavigation: function() { |
| 27 |
this._createNavigationWrapper(); |
| 28 |
this._createNavigationButtons(); |
| 29 |
this._appendNavigation(); |
| 30 |
this._addNavigationCallbacks(); |
| 31 |
}, |
| 32 |
_createNavigationWrapper: function() { |
| 33 |
this.nav = $('<div>', { |
| 34 |
class: this.options.navigationClass |
| 35 |
}); |
| 36 |
}, |
| 37 |
_createNavigationButtons: function() { |
| 38 |
// Add 'first' button |
| 39 |
if(this.options.first) { |
| 40 |
this._createNavigationButton(this.options.firstText, { |
| 41 |
'data-first': true |
| 42 |
}); |
| 43 |
} |
| 44 |
|
| 45 |
// Add 'previous' button |
| 46 |
if(this.options.previous) { |
| 47 |
this._createNavigationButton(this.options.previousText, { |
| 48 |
'data-direction': -1, |
| 49 |
'data-previous': true |
| 50 |
}); |
| 51 |
} |
| 52 |
|
| 53 |
// Add page buttons |
| 54 |
for(var i = 0; i < this._totalPages(); ++i) { |
| 55 |
this._createNavigationButton(this.options.pageToText(i), { |
| 56 |
'data-page': i |
| 57 |
}); |
| 58 |
} |
| 59 |
|
| 60 |
// Add 'next' button |
| 61 |
if(this.options.next) { |
| 62 |
this._createNavigationButton(this.options.nextText, { |
| 63 |
'data-direction': 1, |
| 64 |
'data-next': true |
| 65 |
}); |
| 66 |
} |
| 67 |
|
| 68 |
// Add 'last' button |
| 69 |
if(this.options.last) { |
| 70 |
this._createNavigationButton(this.options.lastText, { |
| 71 |
'data-last': true |
| 72 |
}); |
| 73 |
} |
| 74 |
}, |
| 75 |
_createNavigationButton: function(text, options) { |
| 76 |
this.nav.append($('<a>', $.extend(options, { href: '#', text: text }))); |
| 77 |
}, |
| 78 |
_appendNavigation: function() { |
| 79 |
// Add the content to the navigation block |
| 80 |
if(this.options.navigationWrapper) this.options.navigationWrapper.append(this.nav); |
| 81 |
// Add it after the table |
| 82 |
else this.obj.after(this.nav); |
| 83 |
}, |
| 84 |
_addNavigationCallbacks: function() { |
| 85 |
var paginator = this; |
| 86 |
|
| 87 |
paginator.nav.find('a').click(function(e) { |
| 88 |
var direction = $(this).data('direction') * 1; |
| 89 |
|
| 90 |
// 'First' button |
| 91 |
if($(this).data('first') !== undefined) { |
| 92 |
paginator._setPage(0); |
| 93 |
} |
| 94 |
// Page button |
| 95 |
else if ($(this).data('page') !== undefined) { |
| 96 |
paginator._setPage($(this).data('page') * 1); |
| 97 |
} |
| 98 |
// 'Previous' or 'Next' button |
| 99 |
else if ($(this).data('previous') !== undefined || $(this).data('next') !== undefined) { |
| 100 |
var page = paginator._currentPage() + direction; |
| 101 |
if(page >= 0 && page <= paginator._totalPages() - 1) { |
| 102 |
paginator._setPage(page); |
| 103 |
} |
| 104 |
} |
| 105 |
// 'Last' button |
| 106 |
else if ($(this).data('last') !== undefined) { |
| 107 |
paginator._setPage(paginator._totalPages() - 1); |
| 108 |
} |
| 109 |
|
| 110 |
// Handle callback |
| 111 |
if(paginator.options.onSelect) paginator.options.onSelect(paginator.obj, paginator._currentPage()); |
| 112 |
e.preventDefault(); |
| 113 |
return false; |
| 114 |
}); |
| 115 |
}, |
| 116 |
|
| 117 |
_setPage: function(index) { |
| 118 |
if(index == undefined) index = this.options.initialPage; |
| 119 |
|
| 120 |
// Hide all elements, and then show the current page. |
| 121 |
this._rows().hide().slice(index * this.options.limit, (index + 1) * this.options.limit).show(); |
| 122 |
|
| 123 |
// Set the current button as active |
| 124 |
this.nav.find('a').removeAttr('data-selected').siblings('a[data-page=' + index + ']') |
| 125 |
.attr('data-selected', true); |
| 126 |
}, |
| 127 |
|
| 128 |
_currentPage: function() { |
| 129 |
return this.nav.find('a[data-selected=true]').data('page'); |
| 130 |
}, |
| 131 |
_totalRows: function() { |
| 132 |
// Count the total rows of the selector |
| 133 |
return this._rows().length; |
| 134 |
}, |
| 135 |
_rows: function() { |
| 136 |
return this.obj.find(this.options.childrenSelector); |
| 137 |
}, |
| 138 |
_totalPages: function() { |
| 139 |
return Math.ceil(this._totalRows() / this.options.limit); |
| 140 |
} |
| 141 |
}; |
| 142 |
}; |
| 143 |
|
| 144 |
$.fn.paginate = function(options) { |
| 145 |
switch(options) { |
| 146 |
// Example of custom actions: |
| 147 |
// case 'destroy': return pagination.destroy(this); |
| 148 |
default: return Paginator().build(this, $.extend( {}, $.fn.paginate.defaults, options)); |
| 149 |
} |
| 150 |
}; |
| 151 |
|
| 152 |
$.fn.paginate.defaults = { |
| 153 |
limit: 20, |
| 154 |
initialPage: 0, |
| 155 |
|
| 156 |
previous: true, |
| 157 |
previousText: '<', |
| 158 |
next: true, |
| 159 |
nextText: '>', |
| 160 |
first: true, |
| 161 |
firstText: '<<', |
| 162 |
last: true, |
| 163 |
lastText: '>>', |
| 164 |
|
| 165 |
optional: true, |
| 166 |
|
| 167 |
onCreate: null, |
| 168 |
onSelect: null, |
| 169 |
|
| 170 |
childrenSelector: 'tbody > tr', |
| 171 |
navigationWrapper: null, |
| 172 |
navigationClass: 'page-navigation', |
| 173 |
pageToText: function(i) { return (i + 1).toString(); } |
| 174 |
} |
| 175 |
|
| 176 |
}(jQuery)); |
| 177 |
|