forked from malsup/cycle2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.cycle2.pager.js
93 lines (80 loc) · 2.66 KB
/
jquery.cycle2.pager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*! pager plugin for Cycle2; version: 20130525 */
(function($) {
"use strict";
$.extend($.fn.cycle.defaults, {
pager: '> .cycle-pager',
pagerActiveClass: 'cycle-pager-active',
pagerEvent: 'click.cycle',
pagerTemplate: '<span>•</span>'
});
$(document).on( 'cycle-bootstrap', function( e, opts, API ) {
// add method to API
API.buildPagerLink = buildPagerLink;
});
$(document).on( 'cycle-slide-added', function( e, opts, slideOpts, slideAdded ) {
if ( opts.pager ) {
opts.API.buildPagerLink ( opts, slideOpts, slideAdded );
opts.API.page = page;
}
});
$(document).on( 'cycle-slide-removed', function( e, opts, index, slideRemoved ) {
if ( opts.pager ) {
var pagers = opts.API.getComponent( 'pager' );
pagers.each(function() {
var pager = $(this);
$( pager.children()[index] ).remove();
});
}
});
$(document).on( 'cycle-update-view', function( e, opts, slideOpts ) {
var pagers;
if ( opts.pager ) {
pagers = opts.API.getComponent( 'pager' );
pagers.each(function() {
$(this).children().removeClass( opts.pagerActiveClass )
.eq( opts.currSlide ).addClass( opts.pagerActiveClass );
});
}
});
$(document).on( 'cycle-destroyed', function( e, opts ) {
var pager = opts.API.getComponent( 'pager' );
if ( pager ) {
pager.children().off( opts.pagerEvent ); // #202
if ( opts.pagerTemplate )
pager.empty();
}
});
function buildPagerLink( opts, slideOpts, slide ) {
var pagerLink;
var pagers = opts.API.getComponent( 'pager' );
pagers.each(function() {
var pager = $(this);
if ( slideOpts.pagerTemplate ) {
var markup = opts.API.tmpl( slideOpts.pagerTemplate, slideOpts, opts, slide[0] );
pagerLink = $( markup ).appendTo( pager );
}
else {
pagerLink = pager.children().eq( opts.slideCount - 1 );
}
pagerLink.on( opts.pagerEvent, function(e) {
e.preventDefault();
opts.API.page( pager, e.currentTarget);
});
});
}
function page( pager, target ) {
/*jshint validthis:true */
var opts = this.opts();
if ( opts.busy && ! opts.manualTrump )
return;
var index = pager.children().index( target );
var nextSlide = index;
var fwd = opts.currSlide < nextSlide;
if (opts.currSlide == nextSlide) {
return; // no op, clicked pager for the currently displayed slide
}
opts.nextSlide = nextSlide;
opts.API.prepareTx( true, fwd );
opts.API.trigger('cycle-pager-activated', [opts, pager, target ]);
}
})(jQuery);