Subversion Repositories cheapmusic

Rev

Rev 120 | Rev 128 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
43 - 1
(function($) {
2
    $.fn.paginate = function(options) {
3
        var defaults = {
4
            paginateElement: 'li',
5
            hashPage: 'page',
6
            elementsPerPage: 10,
124 - 7
            maxPageButtons: 5,
43 - 8
            firstButton: true,
9
            firstButtonText: '<<',
10
            lastButton: true,
11
            lastButtonText: '>>',
12
            prevButton: true,
13
            prevButtonText: '<',
14
            nextButton: true,
15
            nextButtonText: '>',
120 - 16
            extraButtonClasses: '',
17
            functionOnNav: null
119 - 18
        };
43 - 19
 
20
        return this.each(function() {
21
            if (typeof plugin == 'undefined') {
22
                plugin = {};
23
                newPlugin = true;
24
            } else {
25
                newPlugin = false;
26
            }
27
 
28
            if (newPlugin) {
29
                plugin.el = $(this);
30
                plugin.el.addClass('paginateList');
31
 
32
                plugin.settings = {
33
                    pages: 0,
34
                    objElements: Object,
35
                    currentPage: 1
119 - 36
                };
43 - 37
            }
38
 
39
            var getNbOfPages = function() {
40
                return Math.ceil(plugin.objElements.length / plugin.settings.elementsPerPage);
41
            };
42
 
43
            var displayNav = function() {
44
                htmlNav = '<div class="paginateNav text-center' + (plugin.settings.pages <= 1 ? " invisible" : "") + '">';
45
 
46
                if (plugin.settings.firstButton) {
47
                    htmlNav += '<a href="#' + plugin.settings.hashPage + ':1" title="First page" rel="1" class="first mr-1 ' + plugin.settings.extraButtonClasses + '">' + plugin.settings.firstButtonText + '</a>';
48
                }
49
 
50
                if (plugin.settings.prevButton) {
51
                    htmlNav += '<a href="" title="Previous" rel="" class="prev mr-1 ' + plugin.settings.extraButtonClasses + '">' + plugin.settings.prevButtonText + '</a>';
52
                }
53
 
124 - 54
                pagePadding = Math.floor(plugin.settings.maxPageButtons / 2);
55
                pagesLeft = plugin.settings.pages - plugin.settings.currentPage;
56
                pageAdj = (pagesLeft > pagePadding) ? 0 : pagePadding - pagesLeft;
57
                startPage = plugin.settings.currentPage - pagePadding - pageAdj;
58
                startPage = (startPage < 1) ? 1 : startPage;
59
                for (j = 1, i = startPage; j <= plugin.settings.maxPageButtons && i <= plugin.settings.pages; j++, i++) {
60
                    htmlNav += '<a href="#' + plugin.settings.hashPage + ':' + i + '" title="Page ' + i + '" rel="' + i + '" class="pageNoNav' + i + ' page mx-1 ' + plugin.settings.extraButtonClasses + '">' + i + '</a>';
119 - 61
                }
43 - 62
 
63
                if (plugin.settings.nextButton) {
64
                    htmlNav += '<a href="" title="Next" rel="" class="next ml-1 ' + plugin.settings.extraButtonClasses + '">' + plugin.settings.nextButtonText + '</a>';
65
                }
66
 
67
                if (plugin.settings.lastButton) {
68
                    htmlNav += '<a href="#' + plugin.settings.hashPage + ':' + plugin.settings.pages + '" title="Last page" rel="' + plugin.settings.pages + '" class="last ml-1 ' + plugin.settings.extraButtonClasses + '">' + plugin.settings.lastButtonText + '</a>';
69
                }
70
 
71
                htmlNav += '</div>';
72
                plugin.nav = $(htmlNav);
73
                plugin.nav.css({
74
                    'width': plugin.el.width()
75
                });
124 - 76
 
77
                $('.paginateNav').remove();
43 - 78
                plugin.el.after(plugin.nav);
79
 
80
                var elSelector = '#' + plugin.el.get(0).id + ' + ';
81
                $(elSelector + ' .paginateNav a.page,' +
82
                    elSelector + ' .paginateNav a.first,' +
83
                    elSelector + ' .paginateNav a.last').on('click', function(e) {
84
                    e.preventDefault();
85
                    displayPage($(this).attr('rel'));
120 - 86
                    if (plugin.settings.functionOnNav) plugin.settings.functionOnNav();
43 - 87
                });
88
 
89
                $(elSelector + ' .paginateNav a.prev').on('click', function(e) {
90
                    e.preventDefault();
91
                    page = plugin.settings.currentPage > 1 ? parseInt(plugin.settings.currentPage) - 1 : 1;
92
                    displayPage(page);
120 - 93
                    if (plugin.settings.functionOnNav) plugin.settings.functionOnNav();
43 - 94
                });
95
 
96
                $(elSelector + ' .paginateNav a.next').on('click', function(e) {
97
                    e.preventDefault();
98
                    page = plugin.settings.currentPage < plugin.settings.pages ? parseInt(plugin.settings.currentPage) + 1 : plugin.settings.pages;
99
                    displayPage(page);
120 - 100
                    if (plugin.settings.functionOnNav) plugin.settings.functionOnNav();
43 - 101
                });
102
            };
103
 
120 - 104
            var displayPage = function(page) {
43 - 105
                plugin.settings.currentPage = parseInt(page);
124 - 106
                displayNav();
43 - 107
                offsetStart = (page - 1) * plugin.settings.elementsPerPage;
108
                offsetEnd = page * plugin.settings.elementsPerPage;
120 - 109
                transition_default (offsetStart, offsetEnd);
43 - 110
 
111
                if (plugin.nav !== undefined) {
112
                    plugin.nav.find('.current').removeClass('current');
124 - 113
//                    plugin.nav.find('a.page:eq(' + (page - 1) + ')').addClass('current');
114
                    plugin.nav.find('a.pageNoNav' + page + '').addClass('current');
43 - 115
                }
116
 
117
                switch (plugin.settings.currentPage) {
118
                    case 1:
119
                        $('.paginateNav a', plugin).removeClass('disabled');
120
                        $('.paginateNav a.first, .paginateNav a.prev', plugin).addClass('disabled');
121
                        break;
122
                    case plugin.settings.pages:
123
                        $('.paginateNav a', plugin).removeClass('disabled');
124
                        $('.paginateNav a.last, .paginateNav a.next', plugin).addClass('disabled');
125
                        break;
126
                    default:
127
                        $('.paginateNav a', plugin).removeClass('disabled');
128
                        break;
129
                }
130
            };
131
 
132
            var transition_default = function(offsetStart, offsetEnd) {
133
                plugin.currentElements.hide();
134
                plugin.currentElements = plugin.objElements.slice(offsetStart, offsetEnd).clone();
135
                plugin.el.html(plugin.currentElements);
136
                plugin.currentElements.show();
137
            };
138
 
139
            plugin.settings = $.extend({}, defaults, options);
124 - 140
            plugin.settings.currentPage = 1;
43 - 141
            plugin.currentElements = $([]);
142
 
143
            if (newPlugin) {
144
                plugin.objElements = plugin.el.find(plugin.settings.paginateElement);
145
            } else {
146
                $('.paginateNav').remove();
147
            }
148
 
149
            plugin.settings.pages = getNbOfPages();
150
            plugin.el.html();
151
 
152
            displayNav();
153
 
154
            page = 1;
155
            if (document.location.hash.indexOf('#' + plugin.settings.hashPage + ':') != -1) {
156
                page = parseInt(document.location.hash.replace('#' + plugin.settings.hashPage + ':', ''));
157
                if (page.length <= 0 || page < 1 || page > plugin.settings.pages) {
158
                    page = 1;
159
                }
160
            }
161
 
120 - 162
            displayPage(page);
43 - 163
        });
164
    };
66 - 165
})(jQuery);