Project

General

Profile

1
/*! UIkit 2.27.5 | http://www.getuikit.com | (c) 2014 YOOtheme | MIT License */
2
/*
3
 * Based on simplePagination - Copyright (c) 2012 Flavius Matis - http://flaviusmatis.github.com/simplePagination.js/ (MIT)
4
 */
5
(function(addon) {
6

    
7
    var component;
8

    
9
    if (window.UIkit2) {
10
        component = addon(UIkit2);
11
    }
12

    
13
    if (typeof define == 'function' && define.amd) {
14
        define('uikit-pagination', ['uikit'], function(){
15
            return component || addon(UIkit2);
16
        });
17
    }
18

    
19
})(function(UI){
20

    
21
    "use strict";
22

    
23
    UI.component('pagination', {
24

    
25
        defaults: {
26
            items          : 1,
27
            itemsOnPage    : 1,
28
            pages          : 0,
29
            displayedPages : 7,
30
            edges          : 1,
31
            currentPage    : 0,
32
            lblPrev        : false,
33
            lblNext        : false,
34
            onSelectPage   : function() {}
35
        },
36

    
37
        boot: function() {
38

    
39
            // init code
40
            UI.ready(function(context) {
41

    
42
                UI.$('[data-uk-pagination]', context).each(function(){
43
                    var ele = UI.$(this);
44

    
45
                    if (!ele.data('pagination')) {
46
                        UI.pagination(ele, UI.Utils.options(ele.attr('data-uk-pagination')));
47
                    }
48
                });
49
            });
50
        },
51

    
52
        init: function() {
53

    
54
            var $this = this;
55

    
56
            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;
57
            this.currentPage   = this.options.currentPage;
58
            this.halfDisplayed = this.options.displayedPages / 2;
59

    
60
            this.on('click', 'a[data-page]', function(e){
61
                e.preventDefault();
62
                $this.selectPage(UI.$(this).data('page'));
63
            });
64

    
65
            this._render();
66
        },
67

    
68
        _getInterval: function() {
69

    
70
            return {
71
                start: Math.ceil(this.currentPage > this.halfDisplayed ? Math.max(Math.min(this.currentPage - this.halfDisplayed, (this.pages - this.options.displayedPages)), 0) : 0),
72
                end  : Math.ceil(this.currentPage > this.halfDisplayed ? Math.min(this.currentPage + this.halfDisplayed, this.pages) : Math.min(this.options.displayedPages, this.pages))
73
            };
74
        },
75

    
76
        render: function(pages) {
77
            this.pages = pages ? pages : this.pages;
78
            this._render();
79
        },
80

    
81
        selectPage: function(pageIndex, pages) {
82
            this.currentPage = pageIndex;
83
            this.render(pages);
84

    
85
            this.options.onSelectPage.apply(this, [pageIndex]);
86
            this.trigger('select.uk.pagination', [pageIndex, this]);
87
        },
88

    
89
        _render: function() {
90

    
91
            var o = this.options, interval = this._getInterval(), i;
92

    
93
            this.element.empty();
94

    
95
            // Generate Prev link
96
            if (o.lblPrev) this._append(this.currentPage - 1, {text: o.lblPrev});
97

    
98
            // Generate start edges
99
            if (interval.start > 0 && o.edges > 0) {
100

    
101
                var end = Math.min(o.edges, interval.start);
102

    
103
                for (i = 0; i < end; i++) this._append(i);
104

    
105
                if (o.edges < interval.start && (interval.start - o.edges != 1)) {
106
                    this.element.append('<li><span>...</span></li>');
107
                } else if (interval.start - o.edges == 1) {
108
                    this._append(o.edges);
109
                }
110
            }
111

    
112
            // Generate interval links
113
            for (i = interval.start; i < interval.end; i++) this._append(i);
114

    
115
            // Generate end edges
116
            if (interval.end < this.pages && o.edges > 0) {
117

    
118
                if (this.pages - o.edges > interval.end && (this.pages - o.edges - interval.end != 1)) {
119
                    this.element.append('<li><span>...</span></li>');
120
                } else if (this.pages - o.edges - interval.end == 1) {
121
                    this._append(interval.end++);
122
                }
123

    
124
                var begin = Math.max(this.pages - o.edges, interval.end);
125

    
126
                for (i = begin; i < this.pages; i++) this._append(i);
127
            }
128

    
129
            // Generate Next link (unless option is set for at front)
130
            if (o.lblNext) this._append(this.currentPage + 1, {text: o.lblNext});
131
        },
132

    
133
        _append: function(pageIndex, opts) {
134

    
135
            var item, options;
136

    
137
            pageIndex = pageIndex < 0 ? 0 : (pageIndex < this.pages ? pageIndex : this.pages - 1);
138
            options   = UI.$.extend({ text: pageIndex + 1 }, opts);
139

    
140
            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>';
141

    
142
            this.element.append(item);
143
        }
144
    });
145

    
146
    return UI.pagination;
147
});
(23-23/46)