Component-Builder/media/uikit-v2/js/components/pagination.js

148 lines
4.8 KiB
JavaScript
Raw Normal View History

2017-11-12 00:33:10 +00:00
/*! UIkit 2.27.4 | http://www.getuikit.com | (c) 2014 YOOtheme | MIT License */
2016-01-30 20:28:43 +00:00
/*
* Based on simplePagination - Copyright (c) 2012 Flavius Matis - http://flaviusmatis.github.com/simplePagination.js/ (MIT)
*/
(function(addon) {
var component;
2017-11-12 00:33:10 +00:00
if (window.UIkit2) {
component = addon(UIkit2);
2016-01-30 20:28:43 +00:00
}
2017-11-12 00:33:10 +00:00
if (typeof define == 'function' && define.amd) {
define('uikit-pagination', ['uikit'], function(){
return component || addon(UIkit2);
2016-01-30 20:28:43 +00:00
});
}
})(function(UI){
"use strict";
UI.component('pagination', {
defaults: {
items : 1,
itemsOnPage : 1,
pages : 0,
2016-03-19 01:51:35 +00:00
displayedPages : 7,
edges : 1,
currentPage : 0,
2016-01-30 20:28:43 +00:00
lblPrev : false,
lblNext : false,
onSelectPage : function() {}
},
boot: function() {
// init code
UI.ready(function(context) {
2017-11-12 00:33:10 +00:00
UI.$('[data-uk-pagination]', context).each(function(){
2016-01-30 20:28:43 +00:00
var ele = UI.$(this);
2017-11-12 00:33:10 +00:00
if (!ele.data('pagination')) {
UI.pagination(ele, UI.Utils.options(ele.attr('data-uk-pagination')));
2016-01-30 20:28:43 +00:00
}
});
});
},
init: function() {
var $this = this;
this.pages = this.options.pages ? this.options.pages : Math.ceil(this.options.items / this.options.itemsOnPage) ? Math.ceil(this.options.items / this.options.itemsOnPage) : 1;
2016-03-19 01:51:35 +00:00
this.currentPage = this.options.currentPage;
2016-01-30 20:28:43 +00:00
this.halfDisplayed = this.options.displayedPages / 2;
2017-11-12 00:33:10 +00:00
this.on('click', 'a[data-page]', function(e){
2016-01-30 20:28:43 +00:00
e.preventDefault();
2017-11-12 00:33:10 +00:00
$this.selectPage(UI.$(this).data('page'));
2016-01-30 20:28:43 +00:00
});
this._render();
},
_getInterval: function() {
return {
start: Math.ceil(this.currentPage > this.halfDisplayed ? Math.max(Math.min(this.currentPage - this.halfDisplayed, (this.pages - this.options.displayedPages)), 0) : 0),
end : Math.ceil(this.currentPage > this.halfDisplayed ? Math.min(this.currentPage + this.halfDisplayed, this.pages) : Math.min(this.options.displayedPages, this.pages))
};
},
render: function(pages) {
this.pages = pages ? pages : this.pages;
this._render();
},
selectPage: function(pageIndex, pages) {
this.currentPage = pageIndex;
this.render(pages);
this.options.onSelectPage.apply(this, [pageIndex]);
this.trigger('select.uk.pagination', [pageIndex, this]);
},
_render: function() {
var o = this.options, interval = this._getInterval(), i;
this.element.empty();
// Generate Prev link
2016-03-19 01:51:35 +00:00
if (o.lblPrev) this._append(this.currentPage - 1, {text: o.lblPrev});
2016-01-30 20:28:43 +00:00
// Generate start edges
if (interval.start > 0 && o.edges > 0) {
var end = Math.min(o.edges, interval.start);
for (i = 0; i < end; i++) this._append(i);
if (o.edges < interval.start && (interval.start - o.edges != 1)) {
this.element.append('<li><span>...</span></li>');
} else if (interval.start - o.edges == 1) {
this._append(o.edges);
}
}
// Generate interval links
for (i = interval.start; i < interval.end; i++) this._append(i);
// Generate end edges
if (interval.end < this.pages && o.edges > 0) {
if (this.pages - o.edges > interval.end && (this.pages - o.edges - interval.end != 1)) {
this.element.append('<li><span>...</span></li>');
} else if (this.pages - o.edges - interval.end == 1) {
this._append(interval.end++);
}
var begin = Math.max(this.pages - o.edges, interval.end);
for (i = begin; i < this.pages; i++) this._append(i);
}
// Generate Next link (unless option is set for at front)
2016-03-19 01:51:35 +00:00
if (o.lblNext) this._append(this.currentPage + 1, {text: o.lblNext});
2016-01-30 20:28:43 +00:00
},
_append: function(pageIndex, opts) {
2016-03-19 01:51:35 +00:00
var item, options;
2016-01-30 20:28:43 +00:00
pageIndex = pageIndex < 0 ? 0 : (pageIndex < this.pages ? pageIndex : this.pages - 1);
options = UI.$.extend({ text: pageIndex + 1 }, opts);
2016-03-19 01:51:35 +00:00
item = (pageIndex == this.currentPage) ? '<li class="uk-active"><span>' + (options.text) + '</span></li>' : '<li><a href="#page-'+(pageIndex+1)+'" data-page="'+pageIndex+'">'+options.text+'</a></li>';
2016-01-30 20:28:43 +00:00
this.element.append(item);
}
});
return UI.pagination;
});