forked from desandro/imagesloaded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagesloaded.js
335 lines (277 loc) · 8.56 KB
/
imagesloaded.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*!
* imagesLoaded v3.1.8
* JavaScript is all like "You images are done yet or what?"
* MIT License
*/
( function( window, factory ) { 'use strict';
// universal module definition
/*global define: false, module: false, require: false */
if ( typeof define === 'function' && define.amd ) {
// AMD
define( [
'eventEmitter/EventEmitter',
'eventie/eventie'
], function( EventEmitter, eventie ) {
return factory( window, EventEmitter, eventie );
});
} else if ( typeof exports === 'object' ) {
// CommonJS
module.exports = factory(
window,
require('wolfy87-eventemitter'),
require('eventie')
);
} else {
// browser global
window.imagesLoaded = factory(
window,
window.EventEmitter,
window.eventie
);
}
})( window,
// -------------------------- factory -------------------------- //
function factory( window, EventEmitter, eventie ) {
'use strict';
var $ = window.jQuery;
var console = window.console;
var hasConsole = typeof console !== 'undefined';
// -------------------------- helpers -------------------------- //
// extend objects
function extend( a, b ) {
for ( var prop in b ) {
a[ prop ] = b[ prop ];
}
return a;
}
var objToString = Object.prototype.toString;
function isArray( obj ) {
return objToString.call( obj ) === '[object Array]';
}
// turn element or nodeList into an array
function makeArray( obj ) {
var ary = [];
if ( isArray( obj ) ) {
// use object if already an array
ary = obj;
} else if ( typeof obj.length === 'number' ) {
// convert nodeList to array
for ( var i=0, len = obj.length; i < len; i++ ) {
ary.push( obj[i] );
}
} else {
// array of single index
ary.push( obj );
}
return ary;
}
// -------------------------- imagesLoaded -------------------------- //
/**
* @param {Array, Element, NodeList, String} elem
* @param {Object or Function} options - if function, use as callback
* @param {Function} onAlways - callback function
*/
function ImagesLoaded( elem, options, onAlways ) {
// coerce ImagesLoaded() without new, to be new ImagesLoaded()
if ( !( this instanceof ImagesLoaded ) ) {
return new ImagesLoaded( elem, options );
}
// use elem as selector string
if ( typeof elem === 'string' ) {
elem = document.querySelectorAll( elem );
}
this.elements = makeArray( elem );
this.options = extend( {}, this.options );
if ( typeof options === 'function' ) {
onAlways = options;
} else {
extend( this.options, options );
}
if ( onAlways ) {
this.on( 'always', onAlways );
}
this.getImages();
if ( $ ) {
// add jQuery Deferred object
this.jqDeferred = new $.Deferred();
}
// HACK check async to allow time to bind listeners
var _this = this;
setTimeout( function() {
_this.check();
});
}
ImagesLoaded.prototype = new EventEmitter();
ImagesLoaded.prototype.options = {};
ImagesLoaded.prototype.getImages = function() {
this.images = [];
// filter & find items if we have an item selector
for ( var i=0, len = this.elements.length; i < len; i++ ) {
var elem = this.elements[i];
// filter siblings
if ( elem.nodeName === 'IMG' ) {
this.addImage( elem );
}
// find children
// no non-element nodes, #143
var nodeType = elem.nodeType;
if ( !nodeType || !( nodeType === 1 || nodeType === 9 || nodeType === 11 ) ) {
continue;
}
var childElems = elem.querySelectorAll('img');
// concat childElems to filterFound array
for ( var j=0, jLen = childElems.length; j < jLen; j++ ) {
var img = childElems[j];
this.addImage( img );
}
}
};
/**
* @param {Image} img
*/
ImagesLoaded.prototype.addImage = function( img ) {
var loadingImage = new LoadingImage( img );
this.images.push( loadingImage );
};
ImagesLoaded.prototype.check = function() {
var _this = this;
var checkedCount = 0;
var length = this.images.length;
this.hasAnyBroken = false;
// complete if no images
if ( !length ) {
this.complete();
return;
}
function onConfirm( image, message ) {
if ( _this.options.debug && hasConsole ) {
console.log( 'confirm', image, message );
}
_this.progress( image );
checkedCount++;
if ( checkedCount === length ) {
_this.complete();
}
return true; // bind once
}
for ( var i=0; i < length; i++ ) {
var loadingImage = this.images[i];
loadingImage.on( 'confirm', onConfirm );
loadingImage.check();
}
};
ImagesLoaded.prototype.progress = function( image ) {
this.hasAnyBroken = this.hasAnyBroken || !image.isLoaded;
// HACK - Chrome triggers event before object properties have changed. #83
var _this = this;
setTimeout( function() {
_this.emit( 'progress', _this, image );
if ( _this.jqDeferred && _this.jqDeferred.notify ) {
_this.jqDeferred.notify( _this, image );
}
});
};
ImagesLoaded.prototype.complete = function() {
var eventName = this.hasAnyBroken ? 'fail' : 'done';
this.isComplete = true;
var _this = this;
// HACK - another setTimeout so that confirm happens after progress
setTimeout( function() {
_this.emit( eventName, _this );
_this.emit( 'always', _this );
if ( _this.jqDeferred ) {
var jqMethod = _this.hasAnyBroken ? 'reject' : 'resolve';
_this.jqDeferred[ jqMethod ]( _this );
}
});
};
// -------------------------- jquery -------------------------- //
if ( $ ) {
$.fn.imagesLoaded = function( options, callback ) {
var instance = new ImagesLoaded( this, options, callback );
return instance.jqDeferred.promise( $(this) );
};
}
// -------------------------- -------------------------- //
function LoadingImage( img ) {
this.img = img;
}
LoadingImage.prototype = new EventEmitter();
LoadingImage.prototype.check = function() {
// first check cached any previous images that have same src
var resource = cache[ this.img.src ] || new Resource( this.img.src );
if ( resource.isConfirmed ) {
this.confirm( resource.isLoaded, 'cached was confirmed' );
return;
}
// If complete is true and browser supports natural sizes,
// try to check for image status manually.
if ( this.img.complete && this.img.naturalWidth !== undefined ) {
// report based on naturalWidth
this.confirm( this.img.naturalWidth !== 0, 'naturalWidth' );
return;
}
// If none of the checks above matched, simulate loading on detached element.
var _this = this;
resource.on( 'confirm', function( resrc, message ) {
_this.confirm( resrc.isLoaded, message );
return true;
});
resource.check();
};
LoadingImage.prototype.confirm = function( isLoaded, message ) {
this.isLoaded = isLoaded;
this.emit( 'confirm', this, message );
};
// -------------------------- Resource -------------------------- //
// Resource checks each src, only once
// separate class from LoadingImage to prevent memory leaks. See #115
var cache = {};
function Resource( src ) {
this.src = src;
// add to cache
cache[ src ] = this;
}
Resource.prototype = new EventEmitter();
Resource.prototype.check = function() {
// only trigger checking once
if ( this.isChecked ) {
return;
}
// simulate loading on detached element
var proxyImage = new Image();
eventie.bind( proxyImage, 'load', this );
eventie.bind( proxyImage, 'error', this );
proxyImage.src = this.src;
// set flag
this.isChecked = true;
};
// ----- events ----- //
// trigger specified handler for event type
Resource.prototype.handleEvent = function( event ) {
var method = 'on' + event.type;
if ( this[ method ] ) {
this[ method ]( event );
}
};
Resource.prototype.onload = function( event ) {
this.confirm( true, 'onload' );
this.unbindProxyEvents( event );
};
Resource.prototype.onerror = function( event ) {
this.confirm( false, 'onerror' );
this.unbindProxyEvents( event );
};
// ----- confirm ----- //
Resource.prototype.confirm = function( isLoaded, message ) {
this.isConfirmed = true;
this.isLoaded = isLoaded;
this.emit( 'confirm', this, message );
};
Resource.prototype.unbindProxyEvents = function( event ) {
eventie.unbind( event.target, 'load', this );
eventie.unbind( event.target, 'error', this );
};
// ----- ----- //
return ImagesLoaded;
});