-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.jold.paginator.js
308 lines (228 loc) · 9.1 KB
/
jquery.jold.paginator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*!
* Jold jQuery Paginator 1.0.4
*
* Copyright (c) 2018 Jold Interactive; Jurgen Oldenburg <info@jold.nl>
*
* A jQuery pagination plugin for easy and quick pagination of items inside an element.
* See README.md for usage and options
*
* Licences: BSD-3-Clause
* https://opensource.org/licenses/BSD-3-Clause
*/
(function($){
var JoldPaginator = function( element, options ) {
var obj = this;
var $container = $(element);
/**
* Pick up the options passed to the plugin
*/
var settings = $.extend({
param: 'defaultValue'
}, options || {});
/**
* defaults
* The initial default pagint parameters and settings
* for the items start, end, total, end page parameters
* @type {object}
*/
var defaults = {
'items': {
'total': 0,
'start': 1,
'end': options.perPage,
},
'pages': {
'total': 0,
'current': 1,
},
};
/**
* details
* Details about the current page, and items displayed
* @type {object}
*/
var details = {
'items': {
'total': defaults.items.total,
'start': defaults.items.start,
'end': options.perPage,
},
'pages': {
'total': defaults.pages.total,
'current': defaults.pages.current,
},
};
/**
* Get all visible items from the container
* @return integer The total number of reports
*/
this.items = function() {
var $items = $container.find( options.items + ".item-visible" );
return $items;
}
/**
* Count the total reports in a report container
* @return integer The total number of reports
*/
this.total = function() {
var $count = this.items();
return $count.length;
}
/**
* Calc the number of pages to paginate
* @return integer The total number of reports
*/
this.pages = function() {
return Math.ceil( (this.total() / options.perPage) );
}
/**
* Show items x to x in the container
* @param integer start Show items from x
* @param integer end Show items to x
* @return array The items to show
*/
this.show = function( start, end ) {
this.items().slice( start, end ).show();
}
/**
* Hide all items in the $container
* @return na
*/
this.hideAll = function() {
$container.find( options.items ).hide();
}
/**
* Find and replace in a string
* @param {string} subject String to sear for
* @param {string} replace Will be replaces with this
* @param {string} heystack The string to searh and replace in
* @return {string} Replaced string output
*/
this.replace = function( subject, replace, heystack ) {
var find = heystack.replace(subject,replace);
return find;
}
/**
* Set the indicator content with updated details
* @return na Set the content of the indicator witht formatted indicator text
*/
this.indicator = function( ) {
var selector = $(options.indicator.selector);
var content = options.indicator.text;
$( selector ).text( '' );
content = obj.replace( '{start}', details.items.start, content );
content = obj.replace( '{end}', details.items.end, content );
content = obj.replace( '{total}', details.items.total, content );
$( selector ).text( content );
}
/**
* Update the pagination details, if they validate
* @param {integer} start The start position of the items to displaying
* @param {integer} end The end position of the items to display
* @param {integer} total The total number of all items
* @param {integer} pageTotal Total pages
* @param {integer} pageCurrent Current active page
* @return {na} Returns nothing, just updates the internal details object.
*/
this.updateDetails = function( start, end, total, pageTotal, pageCurrent ) {
// console.log('Update details: ', start, end, total, pageTotal, pageCurrent)
if (start !== 'undefined' && typeof start == 'number')
details.items.start = start;
if (end !== 'undefined' && typeof end == 'number')
details.items.end = end;
if (total !== 'undefined' && typeof total == 'number')
details.items.total = total;
if (pageTotal !== 'undefined' && typeof pageTotal == 'number')
details.pages.total = pageTotal;
if (pageCurrent !== 'undefined' && typeof pageCurrent == 'number')
details.pages.current = pageCurrent;
if ( total <= end ) {
details.items.end = total;
}
// console.log('Details updated! ', details);
}
/**
* Plot the pagination items
* @return mixed Write html to the paging container
*/
this.plot = function( $paginator ) {
/** Get the number of pages to paginate */
var pages = new Array( this.pages() );
/** Update the main paginator details */
obj.updateDetails( defaults.items.start, defaults.items.end, obj.total(), obj.pages(), null );
/** Empty / reset the pagination container */
$paginator.html('');
/** Only plot pages if there are more than 1 */
if ( pages <= 1 )
return false;
/** Build the pagination container links */
$.each(pages, function(index) {
/** Create li item */
var $item = $('<li></li>', {
'class' : 'pagination__item',
});
/** Add 'pagination__item--current' class to the first item */
if (index == 0) {
$item.addClass('pagination__item--current');
}
/** Create and append a link to the li item */
var $link = $('<a></a>', {
'text' : (index + 1),
'href' : '#',
'class' : 'pagination__link',
'data-item' : (index + 1)
});
/** Register click action on pagination item */
$link.on('click', function(event) {
event.preventDefault();
var current = $(this).data('item');
var pageItems = (current * options.perPage);
var start = (pageItems - options.perPage);
var end = (pageItems);
/** Remove 'pagination__item--current' class from all items */
$paginator.find('.pagination__item--current').removeClass('pagination__item--current');
/** Add 'pagination__item--current' class to the current item */
$(this).parent().addClass('pagination__item--current');
obj.hideAll();
obj.show( start, end );
/** Update the main paginator details */
obj.updateDetails( (start + 1), end, obj.total(), obj.pages(), current );
/** Update the indicator text */
obj.indicator();
return false;
});
$item.append($link);
$paginator.append($item);
})
}
/**
* Initiate / reset pagination
*/
this.default = function() {
obj.updateDetails( defaults.start, defaults.end, obj.total(), obj.pages(), defaults.pages.current );
return false;
}
/**
* Initiate / reset pagination
*/
this.init = function() {
obj.hideAll();
obj.show( 0, options.perPage );
obj.plot( $(options.paginator) );
/** Reset indicator text */
obj.indicator();
}
// Initiate the paginator
this.init();
};
$.fn.joldPaginator = function( options ) {
var element = $(this);
// Return early if this element already has a plugin instance
if (element.data('joldPaginator')) return element.data('joldPaginator');
// pass options to plugin constructor
var joldPaginator = new JoldPaginator( this, options );
// Store plugin object in this element's data
element.data('joldPaginator', joldPaginator);
return joldPaginator;
};
})(jQuery);