forked from malsup/cycle2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.cycle2.loader.js
105 lines (90 loc) · 3.22 KB
/
jquery.cycle2.loader.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
/*! loader plugin for Cycle2; version: 20131121 */
(function($) {
"use strict";
$.extend($.fn.cycle.defaults, {
loader: false
});
$(document).on( 'cycle-bootstrap', function( e, opts ) {
var addFn;
if ( !opts.loader )
return;
// override API.add for this slideshow
addFn = opts.API.add;
opts.API.add = add;
function add( slides, prepend ) {
var slideArr = [];
if ( $.type( slides ) == 'string' )
slides = $.trim( slides );
else if ( $.type( slides) === 'array' ) {
for (var i=0; i < slides.length; i++ )
slides[i] = $(slides[i])[0];
}
slides = $( slides );
var slideCount = slides.length;
if ( ! slideCount )
return;
slides.hide().appendTo('body').each(function(i) { // appendTo fixes #56
var count = 0;
var slide = $(this);
var images = slide.is('img') ? slide : slide.find('img');
slide.data('index', i);
// allow some images to be marked as unimportant (and filter out images w/o src value)
images = images.filter(':not(.cycle-loader-ignore)').filter(':not([src=""])');
if ( ! images.length ) {
--slideCount;
slideArr.push( slide );
return;
}
count = images.length;
images.each(function() {
// add images that are already loaded
if ( this.complete ) {
imageLoaded();
}
else {
$(this).load(function() {
imageLoaded();
}).on("error", function() {
if ( --count === 0 ) {
// ignore this slide
opts.API.log('slide skipped; img not loaded:', this.src);
if ( --slideCount === 0 && opts.loader == 'wait') {
addFn.apply( opts.API, [ slideArr, prepend ] );
}
}
});
}
});
function imageLoaded() {
if ( --count === 0 ) {
--slideCount;
addSlide( slide );
}
}
});
if ( slideCount )
opts.container.addClass('cycle-loading');
function addSlide( slide ) {
var curr;
if ( opts.loader == 'wait' ) {
slideArr.push( slide );
if ( slideCount === 0 ) {
// #59; sort slides into original markup order
slideArr.sort( sorter );
addFn.apply( opts.API, [ slideArr, prepend ] );
opts.container.removeClass('cycle-loading');
}
}
else {
curr = $(opts.slides[opts.currSlide]);
addFn.apply( opts.API, [ slide, prepend ] );
curr.show();
opts.container.removeClass('cycle-loading');
}
}
function sorter(a, b) {
return a.data('index') - b.data('index');
}
}
});
})(jQuery);