1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | ( function( global, factory ) {
|
14 |
|
15 | if ( typeof module == 'object' && module.exports ) {
|
16 |
|
17 | module.exports = factory();
|
18 | } else {
|
19 |
|
20 | global.EvEmitter = factory();
|
21 | }
|
22 |
|
23 | }( typeof window != 'undefined' ? window : this, function() {
|
24 |
|
25 | function EvEmitter() {}
|
26 |
|
27 | let proto = EvEmitter.prototype;
|
28 |
|
29 | proto.on = function( eventName, listener ) {
|
30 | if ( !eventName || !listener ) return this;
|
31 |
|
32 |
|
33 | let events = this._events = this._events || {};
|
34 |
|
35 | let listeners = events[ eventName ] = events[ eventName ] || [];
|
36 |
|
37 | if ( !listeners.includes( listener ) ) {
|
38 | listeners.push( listener );
|
39 | }
|
40 |
|
41 | return this;
|
42 | };
|
43 |
|
44 | proto.once = function( eventName, listener ) {
|
45 | if ( !eventName || !listener ) return this;
|
46 |
|
47 |
|
48 | this.on( eventName, listener );
|
49 |
|
50 |
|
51 | let onceEvents = this._onceEvents = this._onceEvents || {};
|
52 |
|
53 | let onceListeners = onceEvents[ eventName ] = onceEvents[ eventName ] || {};
|
54 |
|
55 | onceListeners[ listener ] = true;
|
56 |
|
57 | return this;
|
58 | };
|
59 |
|
60 | proto.off = function( eventName, listener ) {
|
61 | let listeners = this._events && this._events[ eventName ];
|
62 | if ( !listeners || !listeners.length ) return this;
|
63 |
|
64 | let index = listeners.indexOf( listener );
|
65 | if ( index != -1 ) {
|
66 | listeners.splice( index, 1 );
|
67 | }
|
68 |
|
69 | return this;
|
70 | };
|
71 |
|
72 | proto.emitEvent = function( eventName, args ) {
|
73 | let listeners = this._events && this._events[ eventName ];
|
74 | if ( !listeners || !listeners.length ) return this;
|
75 |
|
76 |
|
77 | listeners = listeners.slice( 0 );
|
78 | args = args || [];
|
79 |
|
80 | let onceListeners = this._onceEvents && this._onceEvents[ eventName ];
|
81 |
|
82 | for ( let listener of listeners ) {
|
83 | let isOnce = onceListeners && onceListeners[ listener ];
|
84 | if ( isOnce ) {
|
85 |
|
86 |
|
87 | this.off( eventName, listener );
|
88 |
|
89 | delete onceListeners[ listener ];
|
90 | }
|
91 |
|
92 | listener.apply( this, args );
|
93 | }
|
94 |
|
95 | return this;
|
96 | };
|
97 |
|
98 | proto.allOff = function() {
|
99 | delete this._events;
|
100 | delete this._onceEvents;
|
101 | return this;
|
102 | };
|
103 |
|
104 | return EvEmitter;
|
105 |
|
106 | } ) );
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | ( function( window, factory ) {
|
114 |
|
115 | if ( typeof module == 'object' && module.exports ) {
|
116 |
|
117 | module.exports = factory( window, require('ev-emitter') );
|
118 | } else {
|
119 |
|
120 | window.imagesLoaded = factory( window, window.EvEmitter );
|
121 | }
|
122 |
|
123 | } )( typeof window !== 'undefined' ? window : this,
|
124 | function factory( window, EvEmitter ) {
|
125 |
|
126 | let $ = window.jQuery;
|
127 | let console = window.console;
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | function makeArray( obj ) {
|
133 |
|
134 | if ( Array.isArray( obj ) ) return obj;
|
135 |
|
136 | let isArrayLike = typeof obj == 'object' && typeof obj.length == 'number';
|
137 |
|
138 | if ( isArrayLike ) return [ ...obj ];
|
139 |
|
140 |
|
141 | return [ obj ];
|
142 | }
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | function ImagesLoaded( elem, options, onAlways ) {
|
153 |
|
154 | if ( !( this instanceof ImagesLoaded ) ) {
|
155 | return new ImagesLoaded( elem, options, onAlways );
|
156 | }
|
157 |
|
158 | let queryElem = elem;
|
159 | if ( typeof elem == 'string' ) {
|
160 | queryElem = document.querySelectorAll( elem );
|
161 | }
|
162 |
|
163 | if ( !queryElem ) {
|
164 | console.error(`Bad element for imagesLoaded ${queryElem || elem}`);
|
165 | return;
|
166 | }
|
167 |
|
168 | this.elements = makeArray( queryElem );
|
169 | this.options = {};
|
170 |
|
171 | if ( typeof options == 'function' ) {
|
172 | onAlways = options;
|
173 | } else {
|
174 | Object.assign( this.options, options );
|
175 | }
|
176 |
|
177 | if ( onAlways ) this.on( 'always', onAlways );
|
178 |
|
179 | this.getImages();
|
180 |
|
181 | if ( $ ) this.jqDeferred = new $.Deferred();
|
182 |
|
183 |
|
184 | setTimeout( this.check.bind( this ) );
|
185 | }
|
186 |
|
187 | ImagesLoaded.prototype = Object.create( EvEmitter.prototype );
|
188 |
|
189 | ImagesLoaded.prototype.getImages = function() {
|
190 | this.images = [];
|
191 |
|
192 |
|
193 | this.elements.forEach( this.addElementImages, this );
|
194 | };
|
195 |
|
196 | const elementNodeTypes = [ 1, 9, 11 ];
|
197 |
|
198 |
|
199 |
|
200 |
|
201 | ImagesLoaded.prototype.addElementImages = function( elem ) {
|
202 |
|
203 | if ( elem.nodeName === 'IMG' ) {
|
204 | this.addImage( elem );
|
205 | }
|
206 |
|
207 | if ( this.options.background === true ) {
|
208 | this.addElementBackgroundImages( elem );
|
209 | }
|
210 |
|
211 |
|
212 |
|
213 | let { nodeType } = elem;
|
214 | if ( !nodeType || !elementNodeTypes.includes( nodeType ) ) return;
|
215 |
|
216 | let childImgs = elem.querySelectorAll('img');
|
217 |
|
218 | for ( let img of childImgs ) {
|
219 | this.addImage( img );
|
220 | }
|
221 |
|
222 |
|
223 | if ( typeof this.options.background == 'string' ) {
|
224 | let children = elem.querySelectorAll( this.options.background );
|
225 | for ( let child of children ) {
|
226 | this.addElementBackgroundImages( child );
|
227 | }
|
228 | }
|
229 | };
|
230 |
|
231 | const reURL = /url\((['"])?(.*?)\1\)/gi;
|
232 |
|
233 | ImagesLoaded.prototype.addElementBackgroundImages = function( elem ) {
|
234 | let style = getComputedStyle( elem );
|
235 |
|
236 | if ( !style ) return;
|
237 |
|
238 |
|
239 | let matches = reURL.exec( style.backgroundImage );
|
240 | while ( matches !== null ) {
|
241 | let url = matches && matches[2];
|
242 | if ( url ) {
|
243 | this.addBackground( url, elem );
|
244 | }
|
245 | matches = reURL.exec( style.backgroundImage );
|
246 | }
|
247 | };
|
248 |
|
249 |
|
250 |
|
251 |
|
252 | ImagesLoaded.prototype.addImage = function( img ) {
|
253 | let loadingImage = new LoadingImage( img );
|
254 | this.images.push( loadingImage );
|
255 | };
|
256 |
|
257 | ImagesLoaded.prototype.addBackground = function( url, elem ) {
|
258 | let background = new Background( url, elem );
|
259 | this.images.push( background );
|
260 | };
|
261 |
|
262 | ImagesLoaded.prototype.check = function() {
|
263 | this.progressedCount = 0;
|
264 | this.hasAnyBroken = false;
|
265 |
|
266 | if ( !this.images.length ) {
|
267 | this.complete();
|
268 | return;
|
269 | }
|
270 |
|
271 |
|
272 | let onProgress = ( image, elem, message ) => {
|
273 |
|
274 | setTimeout( () => {
|
275 | this.progress( image, elem, message );
|
276 | } );
|
277 | };
|
278 |
|
279 | this.images.forEach( function( loadingImage ) {
|
280 | loadingImage.once( 'progress', onProgress );
|
281 | loadingImage.check();
|
282 | } );
|
283 | };
|
284 |
|
285 | ImagesLoaded.prototype.progress = function( image, elem, message ) {
|
286 | this.progressedCount++;
|
287 | this.hasAnyBroken = this.hasAnyBroken || !image.isLoaded;
|
288 |
|
289 | this.emitEvent( 'progress', [ this, image, elem ] );
|
290 | if ( this.jqDeferred && this.jqDeferred.notify ) {
|
291 | this.jqDeferred.notify( this, image );
|
292 | }
|
293 |
|
294 | if ( this.progressedCount === this.images.length ) {
|
295 | this.complete();
|
296 | }
|
297 |
|
298 | if ( this.options.debug && console ) {
|
299 | console.log( `progress: ${message}`, image, elem );
|
300 | }
|
301 | };
|
302 |
|
303 | ImagesLoaded.prototype.complete = function() {
|
304 | let eventName = this.hasAnyBroken ? 'fail' : 'done';
|
305 | this.isComplete = true;
|
306 | this.emitEvent( eventName, [ this ] );
|
307 | this.emitEvent( 'always', [ this ] );
|
308 | if ( this.jqDeferred ) {
|
309 | let jqMethod = this.hasAnyBroken ? 'reject' : 'resolve';
|
310 | this.jqDeferred[ jqMethod ]( this );
|
311 | }
|
312 | };
|
313 |
|
314 |
|
315 |
|
316 | function LoadingImage( img ) {
|
317 | this.img = img;
|
318 | }
|
319 |
|
320 | LoadingImage.prototype = Object.create( EvEmitter.prototype );
|
321 |
|
322 | LoadingImage.prototype.check = function() {
|
323 |
|
324 |
|
325 | let isComplete = this.getIsImageComplete();
|
326 | if ( isComplete ) {
|
327 |
|
328 | this.confirm( this.img.naturalWidth !== 0, 'naturalWidth' );
|
329 | return;
|
330 | }
|
331 |
|
332 |
|
333 | this.proxyImage = new Image();
|
334 |
|
335 | if ( this.img.crossOrigin ) {
|
336 | this.proxyImage.crossOrigin = this.img.crossOrigin;
|
337 | }
|
338 | this.proxyImage.addEventListener( 'load', this );
|
339 | this.proxyImage.addEventListener( 'error', this );
|
340 |
|
341 | this.img.addEventListener( 'load', this );
|
342 | this.img.addEventListener( 'error', this );
|
343 | this.proxyImage.src = this.img.currentSrc || this.img.src;
|
344 | };
|
345 |
|
346 | LoadingImage.prototype.getIsImageComplete = function() {
|
347 |
|
348 |
|
349 | return this.img.complete && this.img.naturalWidth;
|
350 | };
|
351 |
|
352 | LoadingImage.prototype.confirm = function( isLoaded, message ) {
|
353 | this.isLoaded = isLoaded;
|
354 | let { parentNode } = this.img;
|
355 |
|
356 | let elem = parentNode.nodeName === 'PICTURE' ? parentNode : this.img;
|
357 | this.emitEvent( 'progress', [ this, elem, message ] );
|
358 | };
|
359 |
|
360 |
|
361 |
|
362 |
|
363 | LoadingImage.prototype.handleEvent = function( event ) {
|
364 | let method = 'on' + event.type;
|
365 | if ( this[ method ] ) {
|
366 | this[ method ]( event );
|
367 | }
|
368 | };
|
369 |
|
370 | LoadingImage.prototype.onload = function() {
|
371 | this.confirm( true, 'onload' );
|
372 | this.unbindEvents();
|
373 | };
|
374 |
|
375 | LoadingImage.prototype.onerror = function() {
|
376 | this.confirm( false, 'onerror' );
|
377 | this.unbindEvents();
|
378 | };
|
379 |
|
380 | LoadingImage.prototype.unbindEvents = function() {
|
381 | this.proxyImage.removeEventListener( 'load', this );
|
382 | this.proxyImage.removeEventListener( 'error', this );
|
383 | this.img.removeEventListener( 'load', this );
|
384 | this.img.removeEventListener( 'error', this );
|
385 | };
|
386 |
|
387 |
|
388 |
|
389 | function Background( url, element ) {
|
390 | this.url = url;
|
391 | this.element = element;
|
392 | this.img = new Image();
|
393 | }
|
394 |
|
395 |
|
396 | Background.prototype = Object.create( LoadingImage.prototype );
|
397 |
|
398 | Background.prototype.check = function() {
|
399 | this.img.addEventListener( 'load', this );
|
400 | this.img.addEventListener( 'error', this );
|
401 | this.img.src = this.url;
|
402 |
|
403 | let isComplete = this.getIsImageComplete();
|
404 | if ( isComplete ) {
|
405 | this.confirm( this.img.naturalWidth !== 0, 'naturalWidth' );
|
406 | this.unbindEvents();
|
407 | }
|
408 | };
|
409 |
|
410 | Background.prototype.unbindEvents = function() {
|
411 | this.img.removeEventListener( 'load', this );
|
412 | this.img.removeEventListener( 'error', this );
|
413 | };
|
414 |
|
415 | Background.prototype.confirm = function( isLoaded, message ) {
|
416 | this.isLoaded = isLoaded;
|
417 | this.emitEvent( 'progress', [ this, this.element, message ] );
|
418 | };
|
419 |
|
420 |
|
421 |
|
422 | ImagesLoaded.makeJQueryPlugin = function( jQuery ) {
|
423 | jQuery = jQuery || window.jQuery;
|
424 | if ( !jQuery ) return;
|
425 |
|
426 |
|
427 | $ = jQuery;
|
428 |
|
429 | $.fn.imagesLoaded = function( options, onAlways ) {
|
430 | let instance = new ImagesLoaded( this, options, onAlways );
|
431 | return instance.jqDeferred.promise( $( this ) );
|
432 | };
|
433 | };
|
434 |
|
435 | ImagesLoaded.makeJQueryPlugin();
|
436 |
|
437 |
|
438 |
|
439 | return ImagesLoaded;
|
440 |
|
441 | } );
|