forked from malsup/cycle2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.cycle2.command.js
168 lines (146 loc) · 4.94 KB
/
jquery.cycle2.command.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*! command plugin for Cycle2; version: BETA-20120923 */
(function($) {
"use strict";
var c2 = $.fn.cycle;
$.fn.cycle = function( options ) {
var cmd, cmdFn, opts;
var args = $.makeArray( arguments );
if ( $.type( options ) == 'number' ) {
return this.cycle( 'goto', options );
}
if ( $.type( options ) == 'string' ) {
return this.each(function() {
var cmdArgs;
cmd = options;
opts = $(this).data('cycle.opts');
if ( opts === undefined ) {
c2.log('slideshow must be initialized before sending commands; "' + cmd + '" ignored');
return;
}
else {
cmd = cmd == 'goto' ? 'jump' : cmd; // issue #3; change 'goto' to 'jump' internally
cmdFn = opts.API[ cmd ];
if ( $.isFunction( cmdFn )) {
cmdArgs = $.makeArray( args );
cmdArgs.shift();
return cmdFn.apply( opts.API, cmdArgs );
}
else {
c2.log( 'unknown command: ', cmd );
}
}
});
}
else {
return c2.apply( this, arguments );
}
};
// copy props
$.extend( $.fn.cycle, c2 );
$.extend( c2.API, {
next: function() {
var opts = this.opts();
var count = opts.reverse ? -1 : 1;
if ( opts.allowWrap === false && ( opts.currSlide + count ) >= opts.slideCount )
return;
opts.API.advanceSlide( count );
opts.API.trigger('cycle-next', [ opts ]).log('cycle-next');
},
prev: function() {
var opts = this.opts();
var count = opts.reverse ? 1 : -1;
if ( opts.allowWrap === false && ( opts.currSlide + count ) < 0 )
return;
opts.API.advanceSlide( count );
opts.API.trigger('cycle-prev', [ opts ]).log('cycle-prev');
},
destroy: function() {
var opts = this.opts();
clearTimeout(opts.timeoutId);
opts.timeoutId = 0;
opts.API.stop();
opts.API.trigger( 'cycle-destroyed', [ opts ] ).log('cycle-destroyed');
opts.container.removeData( 'cycle.opts' );
},
jump: function( index ) {
// go to the requested slide
var opts = this.opts();
var num = parseInt( index, 10 );
if (isNaN(num) || num < 0 || num >= opts.slides.length) {
opts.API.log('goto: invalid slide index: ' + num);
return;
}
if (num == opts.currSlide) {
opts.API.log('goto: skipping, already on slide', num);
return;
}
opts.nextSlide = num;
clearTimeout(opts.timeoutId);
opts.timeoutId = 0;
opts.API.log('goto: ', num, ' (zero-index)');
opts.API.prepareTx( true, !opts.reverse );
},
stop: function() {
var opts = this.opts();
var pauseObj = opts.container;
clearTimeout(opts.timeoutId);
opts.timeoutId = 0;
opts.API.stopTransition();
if ( opts.pauseOnHover ) {
if ( opts.pauseOnHover !== true )
pauseObj = $( opts.pauseOnHover );
pauseObj.off('mouseenter mouseleave');
}
opts.API.trigger('cycle-stopped', [ opts ]).log('cycle-stopped');
},
pause: function() {
var opts = this.opts();
opts.paused = true;
opts.API.trigger('cycle-paused', [ opts ]).log('cycle-paused');
},
resume: function() {
var opts = this.opts();
opts.paused = false;
opts.API.trigger('cycle-resumed', [ opts ]).log('cycle-resumed');
},
reinit: function() {
var opts = this.opts();
opts.API.destroy();
opts.container.cycle();
},
remove: function( index ) {
var opts = this.opts();
var slide, slideToRemove, slides = [], slideNum = 1;
for ( var i=0; i < opts.slides.length; i++ ) {
slide = opts.slides[i];
if ( i == index ) {
slideToRemove = slide;
}
else {
slides.push( slide );
slide.data('cycle.opts').slideNum = slideNum;
slideNum++;
}
}
if ( slideToRemove ) {
opts.slides = $( slides );
opts.slideCount--;
$( slideToRemove ).remove();
if (index == opts.currSlide) {
opts.API.advanceSlide( 1 );
}
opts.API.trigger('cycle-slide-removed', [ opts, index, slideToRemove ]).log('cycle-slide-removed');
opts.API.updateView();
}
}
});
// listen for clicks on elements with data-cycle-cmd attribute
$(document).on('click.cycle', '[data-cycle-cmd]', function(e) {
// issue cycle command
e.preventDefault();
var el = $(this);
var command = el.data('cycle-cmd');
var context = el.data('cycle-context') || '.cycle-slideshow';
$(context).cycle(command, el.data('cycle-arg'));
});
})(jQuery);