Statistics
| Branch: | Tag: | Revision:

root / ui / static / snf / js / jquery.pagination.js @ 7970aa87

History | View | Annotate | Download (7.8 kB)

1
/**
2
 * This jQuery plugin displays pagination links inside the selected elements.
3
 * 
4
 * This plugin needs at least jQuery 1.4.2
5
 *
6
 * @author Gabriel Birke (birke *at* d-scribe *dot* de)
7
 * @version 2.2
8
 * @param {int} maxentries Number of entries to paginate
9
 * @param {Object} opts Several options (see README for documentation)
10
 * @return {Object} jQuery Object
11
 */
12
 (function($){
13
        /**
14
         * @class Class for calculating pagination values
15
         */
16
        $.PaginationCalculator = function(maxentries, opts) {
17
                this.maxentries = maxentries;
18
                this.opts = opts;
19
        }
20
        
21
        $.extend($.PaginationCalculator.prototype, {
22
                /**
23
                 * Calculate the maximum number of pages
24
                 * @method
25
                 * @returns {Number}
26
                 */
27
                numPages:function() {
28
                        return Math.ceil(this.maxentries/this.opts.items_per_page);
29
                },
30
                /**
31
                 * Calculate start and end point of pagination links depending on 
32
                 * current_page and num_display_entries.
33
                 * @returns {Array}
34
                 */
35
                getInterval:function(current_page)  {
36
                        var ne_half = Math.floor(this.opts.num_display_entries/2);
37
                        var np = this.numPages();
38
                        var upper_limit = np - this.opts.num_display_entries;
39
                        var start = current_page > ne_half ? Math.max( Math.min(current_page - ne_half, upper_limit), 0 ) : 0;
40
                        var end = current_page > ne_half?Math.min(current_page+ne_half + (this.opts.num_display_entries % 2), np):Math.min(this.opts.num_display_entries, np);
41
                        return {start:start, end:end};
42
                }
43
        });
44
        
45
        // Initialize jQuery object container for pagination renderers
46
        $.PaginationRenderers = {}
47
        
48
        /**
49
         * @class Default renderer for rendering pagination links
50
         */
51
        $.PaginationRenderers.defaultRenderer = function(maxentries, opts) {
52
                this.maxentries = maxentries;
53
                this.opts = opts;
54
                this.pc = new $.PaginationCalculator(maxentries, opts);
55
        }
56
        $.extend($.PaginationRenderers.defaultRenderer.prototype, {
57
                /**
58
                 * Helper function for generating a single link (or a span tag if it's the current page)
59
                 * @param {Number} page_id The page id for the new item
60
                 * @param {Number} current_page 
61
                 * @param {Object} appendopts Options for the new item: text and classes
62
                 * @returns {jQuery} jQuery object containing the link
63
                 */
64
                createLink:function(page_id, current_page, appendopts){
65
                        var lnk, np = this.pc.numPages();
66
                        page_id = page_id<0?0:(page_id<np?page_id:np-1); // Normalize page id to sane value
67
                        appendopts = $.extend({text:page_id+1, classes:""}, appendopts||{});
68
                        if(page_id == current_page){
69
                                lnk = $("<span class='current'>" + appendopts.text + "</span>");
70
                        }
71
                        else
72
                        {
73
                                lnk = $("<a>" + appendopts.text + "</a>")
74
                                        .attr('href', this.opts.link_to.replace(/__id__/,page_id));
75
                        }
76
                        if(appendopts.classes){ lnk.addClass(appendopts.classes); }
77
                        lnk.data('page_id', page_id);
78
                        return lnk;
79
                },
80
                // Generate a range of numeric links 
81
                appendRange:function(container, current_page, start, end, opts) {
82
                        var i;
83
                        for(i=start; i<end; i++) {
84
                                this.createLink(i, current_page, opts).appendTo(container);
85
                        }
86
                },
87
                getLinks:function(current_page, eventHandler) {
88
                        var begin, end,
89
                                interval = this.pc.getInterval(current_page),
90
                                np = this.pc.numPages(),
91
                                fragment = $("<div class='pagination'></div>");
92
                        
93
                        // Generate "Previous"-Link
94
                        if(this.opts.prev_text && (current_page > 0 || this.opts.prev_show_always)){
95
                                fragment.append(this.createLink(current_page-1, current_page, {text:this.opts.prev_text, classes:"prev"}));
96
                        }
97
                        // Generate starting points
98
                        if (interval.start > 0 && this.opts.num_edge_entries > 0)
99
                        {
100
                                end = Math.min(this.opts.num_edge_entries, interval.start);
101
                                this.appendRange(fragment, current_page, 0, end, {classes:'sp'});
102
                                if(this.opts.num_edge_entries < interval.start && this.opts.ellipse_text)
103
                                {
104
                                        jQuery("<span>"+this.opts.ellipse_text+"</span>").appendTo(fragment);
105
                                }
106
                        }
107
                        // Generate interval links
108
                        this.appendRange(fragment, current_page, interval.start, interval.end);
109
                        // Generate ending points
110
                        if (interval.end < np && this.opts.num_edge_entries > 0)
111
                        {
112
                                if(np-this.opts.num_edge_entries > interval.end && this.opts.ellipse_text)
113
                                {
114
                                        jQuery("<span>"+this.opts.ellipse_text+"</span>").appendTo(fragment);
115
                                }
116
                                begin = Math.max(np-this.opts.num_edge_entries, interval.end);
117
                                this.appendRange(fragment, current_page, begin, np, {classes:'ep'});
118
                                
119
                        }
120
                        // Generate "Next"-Link
121
                        if(this.opts.next_text && (current_page < np-1 || this.opts.next_show_always)){
122
                                fragment.append(this.createLink(current_page+1, current_page, {text:this.opts.next_text, classes:"next"}));
123
                        }
124
                        $('a', fragment).click(eventHandler);
125
                        return fragment;
126
                }
127
        });
128
        
129
        // Extend jQuery
130
        $.fn.pagination = function(maxentries, opts){
131
                
132
                // Initialize options with default values
133
                opts = jQuery.extend({
134
                        items_per_page:10,
135
                        num_display_entries:11,
136
                        current_page:0,
137
                        num_edge_entries:0,
138
                        link_to:"#",
139
                        prev_text:"Prev",
140
                        next_text:"Next",
141
                        ellipse_text:"...",
142
                        prev_show_always:true,
143
                        next_show_always:true,
144
                        renderer:"defaultRenderer",
145
                        load_first_page:false,
146
                        callback:function(){return false;}
147
                },opts||{});
148
                
149
                var containers = this,
150
                        renderer, links, current_page;
151
                
152
                /**
153
                 * This is the event handling function for the pagination links. 
154
                 * @param {int} page_id The new page number
155
                 */
156
                function paginationClickHandler(evt){
157
                        var links, 
158
                                new_current_page = $(evt.target).data('page_id'),
159
                                continuePropagation = selectPage(new_current_page);
160
                        if (!continuePropagation) {
161
                                evt.stopPropagation();
162
                        }
163
                        return continuePropagation;
164
                }
165
                
166
                /**
167
                 * This is a utility function for the internal event handlers. 
168
                 * It sets the new current page on the pagination container objects, 
169
                 * generates a new HTMl fragment for the pagination links and calls
170
                 * the callback function.
171
                 */
172
                function selectPage(new_current_page) {
173
                        // update the link display of a all containers
174
                        containers.data('current_page', new_current_page);
175
                        links = renderer.getLinks(new_current_page, paginationClickHandler);
176
                        containers.empty();
177
                        links.appendTo(containers);
178
                        // call the callback and propagate the event if it does not return false
179
                        var continuePropagation = opts.callback(new_current_page, containers);
180
                        return continuePropagation;
181
                }
182
                
183
                // -----------------------------------
184
                // Initialize containers
185
                // -----------------------------------
186
                current_page = opts.current_page;
187
                containers.data('current_page', current_page);
188
                // Create a sane value for maxentries and items_per_page
189
                maxentries = (!maxentries || maxentries < 0)?1:maxentries;
190
                opts.items_per_page = (!opts.items_per_page || opts.items_per_page < 0)?1:opts.items_per_page;
191
                
192
                if(!$.PaginationRenderers[opts.renderer])
193
                {
194
                        throw new ReferenceError("Pagination renderer '" + opts.renderer + "' was not found in jQuery.PaginationRenderers object.");
195
                }
196
                renderer = new $.PaginationRenderers[opts.renderer](maxentries, opts);
197
                
198
                // Attach control events to the DOM elements
199
                var pc = new $.PaginationCalculator(maxentries, opts);
200
                var np = pc.numPages();
201
                containers.bind('setPage', {numPages:np}, function(evt, page_id) { 
202
                                if(page_id >= 0 && page_id < evt.data.numPages) {
203
                                        selectPage(page_id); return false;
204
                                }
205
                });
206
                containers.bind('prevPage', function(evt){
207
                                var current_page = $(this).data('current_page');
208
                                if (current_page > 0) {
209
                                        selectPage(current_page - 1);
210
                                }
211
                                return false;
212
                });
213
                containers.bind('nextPage', {numPages:np}, function(evt){
214
                                var current_page = $(this).data('current_page');
215
                                if(current_page < evt.data.numPages - 1) {
216
                                        selectPage(current_page + 1);
217
                                }
218
                                return false;
219
                });
220
                
221
                // When all initialisation is done, draw the links
222
                links = renderer.getLinks(current_page, paginationClickHandler);
223
                containers.empty();
224
                links.appendTo(containers);
225
                // call callback function
226
                if(opts.load_first_page) {
227
                        opts.callback(current_page, containers);
228
                }
229
        } // End of $.fn.pagination block
230
        
231
})(jQuery);