PageSourceSearch

https://www.jf-matosinhos.pt/frontend/js/jquery.simplePagination.js

js jf-matosinhos.pt collected 2026-10-06 13:15:28 UTC 9,013 bytes, 324 lines download raw bytes

vendor: 9,013 bytes, lines 1-324
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				hrefTextPrefix: '#page-',
23				hrefTextSuffix: '',
24				prevText: 'Prev',
25				nextText: 'Next',
26				ellipseText: '…',
27				cssStyle: 'pagination',
28				labelMap: [],
29				selectOnClick: true,
30				nextAtFront: false,
31				invertPageOrder: false,
32				onPageClick: function(pageNumber, event) {
33					// Callback triggered when a page is clicked
34					// Page number is given as an optional parameter
35				},
36				onInit: function() {
37					// Callback triggered immediately after initialization
38				}
39			}, options || {});
40
41			var self = this;
42
43			o.pages = o.pages ? o.pages : Math.ceil(o.items / o.itemsOnPage) ? Math.ceil(o.items / o.itemsOnPage) : 1;
44			if (o.currentPage)
45				o.currentPage = o.currentPage - 1;
46			else
47				o.currentPage = !o.invertPageOrder ? 0 : o.pages - 1;
48			o.halfDisplayed = o.displayedPages / 2;
49
50			this.each(function() {
51				self.addClass(o.cssStyle).data('pagination', o);
52				methods._draw.call(self);
53			});
54
55			o.onInit();
56
57			return this;
58		},
59
60		selectPage: function(page) {
61			methods._selectPage.call(this, page - 1);
62			return this;
63		},
64
65		prevPage: function() {
66			var o = this.data('pagination');
67			if (!o.invertPageOrder) {
68				if (o.currentPage > 0) {
69					methods._selectPage.call(this, o.currentPage - 1);
70				}
71			} else {
72				if (o.currentPage < o.pages - 1) {
73					methods._selectPage.call(this, o.currentPage + 1);
74				}
75			}
76			return this;
77		},
78
79		nextPage: function() {
80			var o = this.data('pagination');
81			if (!o.invertPageOrder) {
82				if (o.currentPage < o.pages - 1) {
83					methods._selectPage.call(this, o.currentPage + 1);
84				}
85			} else {
86				if (o.currentPage > 0) {
87					methods._selectPage.call(this, o.currentPage - 1);
88				}
89			}
90			return this;
91		},
92
93		getPagesCount: function() {
94			return this.data('pagination').pages;
95		},
96
97		getCurrentPage: function () {
98			return this.data('pagination').currentPage + 1;
99		},
100
101		destroy: function(){
102			this.empty();
103			return this;
104		},
105
106		drawPage: function (page) {
107			var o = this.data('pagination');
108			o.currentPage = page - 1;
109			this.data('pagination', o);
110			methods._draw.call(this);
111			return this;
112		},
113
114		redraw: function(){
115			methods._draw.call(this);
116			return this;
117		},
118
119		disable: function(){
120			var o = this.data('pagination');
121			o.disabled = true;
122			this.data('pagination', o);
123			methods._draw.call(this);
124			return this;
125		},
126
127		enable: function(){
128			var o = this.data('pagination');
129			o.disabled = false;
130			this.data('pagination', o);
131			methods._draw.call(this);
132			return this;
133		},
134
135		updateItems: function (newItems) {
136			var o = this.data('pagination');
137			o.items = newItems;
138			o.pages = methods._getPages(o);
139			this.data('pagination', o);
140			methods._draw.call(this);
141		},
142
143		updateItemsOnPage: function (itemsOnPage) {
144			var o = this.data('pagination');
145			o.itemsOnPage = itemsOnPage;
146			o.pages = methods._getPages(o);
147			this.data('pagination', o);
148			methods._selectPage.call(this, 0);
149			return this;
150		},
151
152		_draw: function() {
153			var	o = this.data('pagination'),
154				interval = methods._getInterval(o),
155				i,
156				tagName;
157
158			methods.destroy.call(this);
159			
160			tagName = (typeof this.prop === 'function') ? this.prop('tagName') : this.attr('tagName');
161
162			var $panel = tagName === 'UL' ? this : $('<ul class="pagination mt-0 mb-0"></ul>').appendTo(this);
163
164			// Generate Prev link
165			if (o.prevText) {
166				methods._appendItem.call(this, !o.invertPageOrder ? o.currentPage - 1 : o.currentPage + 1, {text: o.prevText, classes: 'prev'});
167			}
168
169			// Generate Next link (if option set for at front)
170			if (o.nextText && o.nextAtFront) {
171				methods._appendItem.call(this, !o.invertPageOrder ? o.currentPage + 1 : o.currentPage - 1, {text: o.nextText, classes: 'next'});
172			}
173
174			// Generate start edges
175			if (!o.invertPageOrder) {
176				if (interval.start > 0 && o.edges > 0) {
177					var end = Math.min(o.edges, interval.start);
178					for (i = 0; i < end; i++) {
179						methods._appendItem.call(this, i);
180					}
181					if (o.edges < interval.start && (interval.start - o.edges != 1)) {
182						$panel.append('<li class="disabled"><span class="ellipse">' + o.ellipseText + '</span></li>');
183					} else if (interval.start - o.edges == 1) {
184						methods._appendItem.call(this, o.edges);
185					}
186				}
187			} else {
188				if (interval.end < o.pages && o.edges > 0) {
189					var begin = Math.max(o.pages - o.edges, interval.end);
190					for (i = o.pages - 1; i >= begin; i--) {
191						methods._appendItem.call(this, i);
192					}
193					if (o.pages - o.edges > interval.end && (o.pages - o.edges - interval.end != 1)) {
194						$panel.append('<li class="disabled"><span class="ellipse">' + o.ellipseText + '</span></li>');
195					} else if (o.pages - o.edges - interval.end == 1) {
196						methods._appendItem.call(this, interval.end);
197					}
198				}
199			}
200
201			// Generate interval links
202			if (!o.invertPageOrder) {
203				for (i = interval.start; i < interval.end; i++) {
204					methods._appendItem.call(this, i);
205				}
206			} else {
207				for (i = interval.end - 1; i >= interval.start; i--) {
208					methods._appendItem.call(this, i);
209				}
210			}
211
212			// Generate end edges
213			if (!o.invertPageOrder) {
214				if (interval.end < o.pages && o.edges > 0) {
215					if (o.pages - o.edges > interval.end && (o.pages - o.edges - interval.end != 1)) {
216						$panel.append('<li class="disabled"><span class="ellipse">' + o.ellipseText + '</span></li>');
217					} else if (o.pages - o.edges - interval.end == 1) {
218						methods._appendItem.call(this, interval.end);
219					}
220					var begin = Math.max(o.pages - o.edges, interval.end);
221					for (i = begin; i < o.pages; i++) {
222						methods._appendItem.call(this, i);
223					}
224				}
225			} else {
226				if (interval.start > 0 && o.edges > 0) {
227					if (o.edges < interval.start && (interval.start - o.edges != 1)) {
228						$panel.append('<li class="disabled"><span class="ellipse">' + o.ellipseText + '</span></li>');
229					} else if (interval.start - o.edges == 1) {
230						methods._appendItem.call(this, o.edges);
231					}
232					var end = Math.min(o.edges, interval.start);
233					for (i = end - 1; i >= 0; i--) {
234						methods._appendItem.call(this, i);
235					}
236				}
237			}
238
239			// Generate Next link (unless option is set for at front)
240			if (o.nextText && !o.nextAtFront) {
241				methods._appendItem.call(this, !o.invertPageOrder ? o.currentPage + 1 : o.currentPage - 1, {text: o.nextText, classes: 'next'});
242			}
243		},
244
245		_getPages: function(o) {
246			var pages = Math.ceil(o.items / o.itemsOnPage);
247			return pages || 1;
248		},
249
250		_getInterval: function(o) {
251			return {
252				start: Math.ceil(o.currentPage > o.halfDisplayed ? Math.max(Math.min(o.currentPage - o.halfDisplayed, (o.pages - o.displayedPages)), 0) : 0),
253				end: Math.ceil(o.currentPage > o.halfDisplayed ? Math.min(o.currentPage + o.halfDisplayed, o.pages) : Math.min(o.displayedPages, o.pages))
254			};
255		},
256
257		_appendItem: function(pageIndex, opts) {
258			var self = this, options, $link, o = self.data('pagination'), $linkWrapper = $('<li></li>'), $ul = self.find('ul');
259
260			pageIndex = pageIndex < 0 ? 0 : (pageIndex < o.pages ? pageIndex : o.pages - 1);
261
262			options = {
263				text: pageIndex + 1,
264				classes: ''
265			};
266
267			if (o.labelMap.length && o.labelMap[pageIndex]) {
268				options.text = o.labelMap[pageIndex];
269			}
270
271			options = $.extend(options, opts || {});
272
273			if (pageIndex == o.currentPage || o.disabled) {
274				if (o.disabled) {
275					$linkWrapper.addClass('disabled');
276				} else {
277					$linkWrapper.addClass('active');
278				}
279				$link = $('<span class="current">' + (options.text) + '</span>');
280			} else {
281				$link = $('<a href="' + o.hrefTextPrefix + (pageIndex + 1) + o.hrefTextSuffix + '" class="page-link">' + (options.text) + '</a>');
282				$link.click(function(event){
283					return methods._selectPage.call(self, pageIndex, event);
284				});
285			}
286
287			if (options.classes) {
288				$link.addClass(options.classes);
289			}
290
291			$linkWrapper.append($link);
292
293			if ($ul.length) {
294				$ul.append($linkWrapper);
295			} else {
296				self.append($linkWrapper);
297			}
298		},
299
300		_selectPage: function(pageIndex, event) {
301			var o = this.data('pagination');
302			o.currentPage = pageIndex;
303			if (o.selectOnClick) {
304				methods._draw.call(this);
305			}
306			return o.onPageClick(pageIndex + 1, event);
307		}
308
309	};
310
311	$.fn.pagination = function(method) {
312
313		// Method calling logic
314		if (methods[method] && method.charAt(0) != '_') {
315			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
316		} else if (typeof method === 'object' || !method) {
317			return methods.init.apply(this, arguments);
318		} else {
319			$.error('Method ' +  method + ' does not exist on jQuery.pagination');
320		}
321
322	};
323
324})(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.