PageSourceSearch

https://www.pskbearing.com/modules/flexi/js/jquery.simplePagination/jquery.simplePagination.js

js pskbearing.com collected 2026-09-25 13:04:17 UTC 10,998 bytes, 398 lines download raw bytes

1/**
2* simplePagination.js v1.6
3* A simple jQuery pagination plugin.
4* http://flaviusmatis.github.com/simplePagination.js/
5*
6* Copyright 2012, Flavius Matis
7* Released under the MIT license.
8* http://flaviusmatis.github.com/license.html
9*/
10
11(function($){
12
13	var methods = {
14		init: function(options) {
15			var o = $.extend({
16				items: 1,
17				itemsOnPage: 1,
18				pages: 0,
19				displayedPages: 5,
20				edges: 2,
21				currentPage: 0,
22				useAnchors: true,
23				hrefTextPrefix: '#page-',
24				hrefTextSuffix: '',
25				prevText: 'Prev',
26				nextText: 'Next',
27				ellipseText: '…',
28				ellipsePageSet: true,
29				cssStyle: 'light-theme',
30				listStyle: '',
31				labelMap: [],
32				selectOnClick: true,
33				nextAtFront: false,
34				invertPageOrder: false,
35				useStartEdge : true,
36				useEndEdge : true,
37				onPageClick: function(pageNumber, event) {
38					// Callback triggered when a page is clicked
39					// Page number is given as an optional parameter
40				},
41				onInit: function() {
42					// Callback triggered immediately after initialization
43				}
44			}, options || {});
45
46			var self = this;
47
48			o.pages = o.pages ? o.pages : Math.ceil(o.items / o.itemsOnPage) ? Math.ceil(o.items / o.itemsOnPage) : 1;
49			if (o.currentPage)
50				o.currentPage = o.currentPage - 1;
51			else
52				o.currentPage = !o.invertPageOrder ? 0 : o.pages - 1;
53			o.halfDisplayed = o.displayedPages / 2;
54
55			this.each(function() {
56				self.addClass(o.cssStyle + ' simple-pagination').data('pagination', o);
57				methods._draw.call(self);
58			});
59
60			o.onInit();
61
62			return this;
63		},
64
65		selectPage: function(page) {
66			methods._selectPage.call(this, page - 1);
67			return this;
68		},
69
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);
75				}
76			} else {
77				if (o.currentPage < o.pages - 1) {
78					methods._selectPage.call(this, o.currentPage + 1);
79				}
80			}
81			return this;
82		},
83
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);
89				}
90			} else {
91				if (o.currentPage > 0) {
92					methods._selectPage.call(this, o.currentPage - 1);
93				}
94			}
95			return this;
96		},
97
98		getPagesCount: function() {
99			return this.data('pagination').pages;
100		},
101
102		setPagesCount: function(count) {
103			this.data('pagination').pages = count;
104		},
105
106		getCurrentPage: function () {
107			return this.data('pagination').currentPage + 1;
108		},
109
110		destroy: function(){
111			this.empty();
112			return this;
113		},
114
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);
120			return this;
121		},
122
123		redraw: function(){
124			methods._draw.call(this);
125			return this;
126		},
127
128		disable: function(){
129			var o = this.data('pagination');
130			o.disabled = true;
131			this.data('pagination', o);
132			methods._draw.call(this);
133			return this;
134		},
135
136		enable: function(){
137			var o = this.data('pagination');
138			o.disabled = false;
139			this.data('pagination', o);
140			methods._draw.call(this);
141			return this;
142		},
143
144		updateItems: function (newItems) {
145			var o = this.data('pagination');
146			o.items = newItems;
147			o.pages = methods._getPages(o);
148			this.data('pagination', o);
149			methods._draw.call(this);
150		},
151
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);
158			return this;
159		},
160
161		getItemsOnPage: function() {
162			return this.data('pagination').itemsOnPage;
163		},
164
165		_draw: function() {
166			var	o = this.data('pagination'),
167				interval = methods._getInterval(o),
168				i,
169				tagName;
170
171			methods.destroy.call(this);
172
173			tagName = (typeof this.prop === 'function') ? this.prop('tagName') : this.attr('tagName');
174
175			var $panel = tagName === 'UL' ? this : $('<ul' + (o.listStyle ? ' class="' + o.listStyle + '"' : '') + '></ul>').appendTo(this);
176
177			// Generate Prev link
178			if (o.prevText) {
179				methods._appendItem.call(this, !o.invertPageOrder ? o.currentPage - 1 : o.currentPage + 1, {text: o.prevText, classes: 'prev'});
180			}
181
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'});
185			}
186
187			// Generate start edges
188			if (!o.invertPageOrder) {
189				if (interval.start > 0 && o.edges > 0) {
190					if(o.useStartEdge) {
191						var end = Math.min(o.edges, interval.start);
192						for (i = 0; i < end; i++) {
193							methods._appendItem.call(this, i);
194						}
195					}
196					if (o.edges < interval.start && (interval.start - o.edges != 1)
196) {
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);
200					}
201				}
202			} else {
203				if (interval.end < o.pages && o.edges > 0) {
204					if(o.useStartEdge) {
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);
208						}
209					}
210
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);
215					}
216				}
217			}
218
219			// Generate interval links
220			if (!o.invertPageOrder) {
221				for (i = interval.start; i < interval.end; i++) {
222					methods._appendItem.call(this, i);
223				}
224			} else {
225				for (i = interval.end - 1; i >= interval.start; i--) {
226					methods._appendItem.call(this, i);
227				}
228			}
229
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);
237					}
238					if(o.useEndEdge) {
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);
242						}
243					}
244				}
245			} else {
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);
251					}
252
253					if(o.useEndEdge) {
254						var end = Math.min(o.edges, interval.start);
255						for (i = end - 1; i >= 0; i--) {
256							methods._appendItem.call(this, i);
257						}
258					}
259				}
260			}
261
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'});
265			}
266
267			if (o.ellipsePageSet && !o.disabled) {
268				methods._ellipseClick.call(this, $panel);
269			}
270
271		},
272
273		_getPages: function(o) {
274			var pages = Math.ceil(o.items / o.itemsOnPage);
275			return pages || 1;
276		},
277
278		_getInterval: function(o) {
279			return {
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))
282			};
283		},
284
285		_appendItem: function(pageIndex, opts) {
286			var self = this, options, $link, o = self.data('pagination'), $linkWrapper = $('<li></li>'), $ul = self.find('ul');
287
288			pageIndex = pageIndex < 0 ? 0 : (pageIndex < o.pages ? pageIndex : o.pages - 1);
289
290			options = {
291				text: pageIndex + 1,
292				classes: ''
293			};
294
295			if (o.labelMap.length && o.labelMap[pageIndex]) {
296				options.text = o.labelMap[pageIndex];
297			}
298
299			options = $.extend(options, opts || {});
300
301			if (pageIndex == o.currentPage || o.disabled) {
302				if (o.disabled || options.classes === 'prev' || options.classes === 'next') {
303					$linkWrapper.addClass('disabled');
304				} else {
305					$linkWrapper.addClass('active');
306				}
307				$link = $('<span class="current">' + (options.text) + '</span>');
308			} else {
309				if (o.useAnchors) {
310					$link = $('<a href="' + o.hrefTextPrefix + (pageIndex + 1) + o.hrefTextSuffix + '" class="page-link">' + (options.text) + '</a>');
311				} else {
312					$link = $('<span >' + (options.text) + '</span>');
313				}
314				$link.click(function(event){
315					return methods._selectPage.call(self, pageIndex, event);
316				});
317			}
318
319			if (options.classes) {
320				$link.addClass(options.classes);
321			}
322
323			$linkWrapper.append($link);
324
325			if ($ul.length) {
326				$ul.append($linkWrapper);
327			} else {
328				self.append($linkWrapper);
329			}
330		},
331
332		_selectPage: function(pageIndex, event) {
333			var o = this.data('pagination');
334			o.currentPage = pageIndex;
335			if (o.selectOnClick) {
336				methods._draw.call(this);
337			}
338			return o.onPageClick(pageIndex + 1, event);
339		},
340
341
342		_ellipseClick: function($panel) {
343			var self = this,
344				o = this.data('pagination'),
345				$ellip = $panel.find('.ellipse');
346			$ellip.addClass('clickable').parent().removeClass('disabled');
347			$ellip.click(function(event) {
348				if (!o.disable) {
349					var $this = $(this),
350						val = (parseInt($this.parent().prev().text(), 10) || 0) + 1;
351					$this
352						.html('<input type="number" min="1" max="' + o.pages + '" step="1" value="' + val + '">')
353						.find('input')
354						.focus()
355						.click(function(event) {
356							// prevent input number arrows from bubbling a click event on $ellip
357							event.stopPropagation();
358						})
359						.keyup(function(event) {
360							var val = $(this).val();
361							if (event.which === 13 && val !== '') {
362								// enter to accept
363								if ((val>0)&&(val<=o.pages))
364								methods._selectPage.call(self, val - 1);
365							} else if (event.which === 27) {
366								// escape to cancel
367								$ellip.empty().html(o.ellipseText);
368							}
369						})
370						.bind('blur', function(event) {
371							var val = $(this).val();
372							if (val !== '') {
373								methods._selectPage.call(self, val - 1);
374							}
375							$ellip.empty().html(o.ellipseText);
376							return false;
377						});
378				}
379				return false;
380			});
381		}
382
383	};
384
385	$.fn.pagination = function(method) {
386
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);
392		} else {
393			$.error('Method ' +  method + ' does not exist on jQuery.pagination');
394		}
395
396	};
397
398})(jQuery);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.