From 5c231ceade17e7f5b1bba90e845dd2d17e54da1c Mon Sep 17 00:00:00 2001 From: Valentin Heinz Date: Sat, 8 Apr 2017 14:03:25 +0200 Subject: uikit demo theme ported to pelican (#385) --- uikit/static/js/components/pagination.js | 148 +++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 uikit/static/js/components/pagination.js (limited to 'uikit/static/js/components/pagination.js') diff --git a/uikit/static/js/components/pagination.js b/uikit/static/js/components/pagination.js new file mode 100644 index 0000000..fa01908 --- /dev/null +++ b/uikit/static/js/components/pagination.js @@ -0,0 +1,148 @@ +/*! UIkit 2.21.0 | http://www.getuikit.com | (c) 2014 YOOtheme | MIT License */ +/* + * Based on simplePagination - Copyright (c) 2012 Flavius Matis - http://flaviusmatis.github.com/simplePagination.js/ (MIT) + */ +(function(addon) { + + var component; + + if (window.UIkit) { + component = addon(UIkit); + } + + if (typeof define == "function" && define.amd) { + define("uikit-pagination", ["uikit"], function(){ + return component || addon(UIkit); + }); + } + +})(function(UI){ + + "use strict"; + + UI.component('pagination', { + + defaults: { + items : 1, + itemsOnPage : 1, + pages : 0, + displayedPages : 3, + edges : 3, + currentPage : 1, + lblPrev : false, + lblNext : false, + onSelectPage : function() {} + }, + + boot: function() { + + // init code + UI.ready(function(context) { + + UI.$("[data-uk-pagination]", context).each(function(){ + var ele = UI.$(this); + + if (!ele.data("pagination")) { + var obj = UI.pagination(ele, UI.Utils.options(ele.attr("data-uk-pagination"))); + } + }); + }); + }, + + 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; + this.currentPage = this.options.currentPage - 1; + this.halfDisplayed = this.options.displayedPages / 2; + + this.on("click", "a[data-page]", function(e){ + e.preventDefault(); + $this.selectPage(UI.$(this).data("page")); + }); + + 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 + if (o.lblPrev) this._append(o.currentPage - 1, {text: o.lblPrev}); + + // 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('
  • ...
  • '); + } 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('
  • ...
  • '); + } 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) + if (o.lblNext) this._append(o.currentPage + 1, {text: o.lblNext}); + }, + + _append: function(pageIndex, opts) { + + var $this = this, item, link, options; + + pageIndex = pageIndex < 0 ? 0 : (pageIndex < this.pages ? pageIndex : this.pages - 1); + options = UI.$.extend({ text: pageIndex + 1 }, opts); + + item = (pageIndex == this.currentPage) ? '
  • ' + (options.text) + '
  • ' + : '
  • '+options.text+'
  • '; + + this.element.append(item); + } + }); + + return UI.pagination; +}); -- cgit