forked from malsup/malsup.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.cycle2.progressive.js
131 lines (118 loc) · 4.02 KB
/
jquery.cycle2.progressive.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
/*! progressive loader plugin for Cycle2; version: 20130315 */
(function($) {
"use strict";
$.extend($.fn.cycle.defaults, {
progressive: false
});
$(document).on( 'cycle-pre-initialize', function( e, opts ) {
if ( !opts.progressive )
return;
var API = opts.API;
var nextFn = API.next;
var prevFn = API.prev;
var prepareTxFn = API.prepareTx;
var type = $.type( opts.progressive );
var slides, scriptEl;
if ( type == 'array' ) {
slides = opts.progressive;
}
else if ($.isFunction( opts.progressive ) ) {
slides = opts.progressive( opts );
}
else if ( type == 'string' ) {
scriptEl = $( opts.progressive );
slides = $.trim( scriptEl.html() );
if ( !slides )
return;
// is it json array?
if ( /^(\[)/.test( slides ) ) {
try {
slides = $.parseJSON( slides );
}
catch(err) {
API.log( 'error parsing progressive slides', err );
return;
}
}
else {
// plain text, split on delimeter
slides = slides.split( new RegExp( scriptEl.data('cycle-split') || '\n') );
// #95; look for empty slide
if ( ! slides[ slides.length - 1 ] )
slides.pop();
}
}
if ( prepareTxFn ) {
API.prepareTx = function( manual, fwd ) {
var index, slide;
if ( manual || slides.length === 0 ) {
prepareTxFn.apply( opts.API, [ manual, fwd ] );
return;
}
if ( fwd && opts.currSlide == ( opts.slideCount-1) ) {
slide = slides[ 0 ];
slides = slides.slice( 1 );
opts.container.one('cycle-slide-added', function(e, opts ) {
setTimeout(function() {
opts.API.advanceSlide( 1 );
},50);
});
opts.API.add( slide );
}
else if ( !fwd && opts.currSlide === 0 ) {
index = slides.length-1;
slide = slides[ index ];
slides = slides.slice( 0, index );
opts.container.one('cycle-slide-added', function(e, opts ) {
setTimeout(function() {
opts.currSlide = 1;
opts.API.advanceSlide( -1 );
},50);
});
opts.API.add( slide, true );
}
else {
prepareTxFn.apply( opts.API, [ manual, fwd ] );
}
};
}
if ( nextFn ) {
API.next = function() {
var opts = this.opts();
if ( slides.length && opts.currSlide == ( opts.slideCount - 1 ) ) {
var slide = slides[ 0 ];
slides = slides.slice( 1 );
opts.container.one('cycle-slide-added', function(e, opts ) {
nextFn.apply( opts.API );
opts.container.removeClass('cycle-loading');
});
opts.container.addClass('cycle-loading');
opts.API.add( slide );
}
else {
nextFn.apply( opts.API );
}
};
}
if ( prevFn ) {
API.prev = function() {
var opts = this.opts();
if ( slides.length && opts.currSlide === 0 ) {
var index = slides.length-1;
var slide = slides[ index ];
slides = slides.slice( 0, index );
opts.container.one('cycle-slide-added', function(e, opts ) {
opts.currSlide = 1;
opts.API.advanceSlide( -1 );
opts.container.removeClass('cycle-loading');
});
opts.container.addClass('cycle-loading');
opts.API.add( slide, true );
}
else {
prevFn.apply( opts.API );
}
};
}
});
})(jQuery);