imagesloaded.pkgd.js
498 lines
| 1 | /*! |
| 2 | * imagesLoaded PACKAGED v4.1.4 |
| 3 | * JavaScript is all like "You images are done yet or what?" |
| 4 | * MIT License |
| 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * EvEmitter v1.1.0 |
| 9 | * Lil' event emitter |
| 10 | * MIT License |
| 11 | */ |
| 12 | |
| 13 | /* jshint unused: true, undef: true, strict: true */ |
| 14 | |
| 15 | ( function( global, factory ) { |
| 16 | // universal module definition |
| 17 | /* jshint strict: false */ /* globals define, module, window */ |
| 18 | if ( typeof define == 'function' && define.amd ) { |
| 19 | // AMD - RequireJS |
| 20 | define( 'ev-emitter/ev-emitter',factory ); |
| 21 | } else if ( typeof module == 'object' && module.exports ) { |
| 22 | // CommonJS - Browserify, Webpack |
| 23 | module.exports = factory(); |
| 24 | } else { |
| 25 | // Browser globals |
| 26 | global.EvEmitter = factory(); |
| 27 | } |
| 28 | |
| 29 | }( typeof window != 'undefined' ? window : this, function() { |
| 30 | |
| 31 | |
| 32 | |
| 33 | function EvEmitter() {} |
| 34 | |
| 35 | var proto = EvEmitter.prototype; |
| 36 | |
| 37 | proto.on = function( eventName, listener ) { |
| 38 | if ( !eventName || !listener ) { |
| 39 | return; |
| 40 | } |
| 41 | // set events hash |
| 42 | var events = this._events = this._events || {}; |
| 43 | // set listeners array |
| 44 | var listeners = events[ eventName ] = events[ eventName ] || []; |
| 45 | // only add once |
| 46 | if ( listeners.indexOf( listener ) == -1 ) { |
| 47 | listeners.push( listener ); |
| 48 | } |
| 49 | |
| 50 | return this; |
| 51 | }; |
| 52 | |
| 53 | proto.once = function( eventName, listener ) { |
| 54 | if ( !eventName || !listener ) { |
| 55 | return; |
| 56 | } |
| 57 | // add event |
| 58 | this.on( eventName, listener ); |
| 59 | // set once flag |
| 60 | // set onceEvents hash |
| 61 | var onceEvents = this._onceEvents = this._onceEvents || {}; |
| 62 | // set onceListeners object |
| 63 | var onceListeners = onceEvents[ eventName ] = onceEvents[ eventName ] || {}; |
| 64 | // set flag |
| 65 | onceListeners[ listener ] = true; |
| 66 | |
| 67 | return this; |
| 68 | }; |
| 69 | |
| 70 | proto.off = function( eventName, listener ) { |
| 71 | var listeners = this._events && this._events[ eventName ]; |
| 72 | if ( !listeners || !listeners.length ) { |
| 73 | return; |
| 74 | } |
| 75 | var index = listeners.indexOf( listener ); |
| 76 | if ( index != -1 ) { |
| 77 | listeners.splice( index, 1 ); |
| 78 | } |
| 79 | |
| 80 | return this; |
| 81 | }; |
| 82 | |
| 83 | proto.emitEvent = function( eventName, args ) { |
| 84 | var listeners = this._events && this._events[ eventName ]; |
| 85 | if ( !listeners || !listeners.length ) { |
| 86 | return; |
| 87 | } |
| 88 | // copy over to avoid interference if .off() in listener |
| 89 | listeners = listeners.slice(0); |
| 90 | args = args || []; |
| 91 | // once stuff |
| 92 | var onceListeners = this._onceEvents && this._onceEvents[ eventName ]; |
| 93 | |
| 94 | for ( var i=0; i < listeners.length; i++ ) { |
| 95 | var listener = listeners[i] |
| 96 | var isOnce = onceListeners && onceListeners[ listener ]; |
| 97 | if ( isOnce ) { |
| 98 | // remove listener |
| 99 | // remove before trigger to prevent recursion |
| 100 | this.off( eventName, listener ); |
| 101 | // unset once flag |
| 102 | delete onceListeners[ listener ]; |
| 103 | } |
| 104 | // trigger listener |
| 105 | listener.apply( this, args ); |
| 106 | } |
| 107 | |
| 108 | return this; |
| 109 | }; |
| 110 | |
| 111 | proto.allOff = function() { |
| 112 | delete this._events; |
| 113 | delete this._onceEvents; |
| 114 | }; |
| 115 | |
| 116 | return EvEmitter; |
| 117 | |
| 118 | })); |
| 119 | |
| 120 | /*! |
| 121 | * imagesLoaded v4.1.4 |
| 122 | * JavaScript is all like "You images are done yet or what?" |
| 123 | * MIT License |
| 124 | */ |
| 125 | |
| 126 | ( function( window, factory ) { 'use strict'; |
| 127 | // universal module definition |
| 128 | |
| 129 | /*global define: false, module: false, require: false */ |
| 130 | |
| 131 | if ( typeof define == 'function' && define.amd ) { |
| 132 | // AMD |
| 133 | define( [ |
| 134 | 'ev-emitter/ev-emitter' |
| 135 | ], function( EvEmitter ) { |
| 136 | return factory( window, EvEmitter ); |
| 137 | }); |
| 138 | } else if ( typeof module == 'object' && module.exports ) { |
| 139 | // CommonJS |
| 140 | module.exports = factory( |
| 141 | window, |
| 142 | require('ev-emitter') |
| 143 | ); |
| 144 | } else { |
| 145 | // browser global |
| 146 | window.imagesLoaded = factory( |
| 147 | window, |
| 148 | window.EvEmitter |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | })( typeof window !== 'undefined' ? window : this, |
| 153 | |
| 154 | // -------------------------- factory -------------------------- // |
| 155 | |
| 156 | function factory( window, EvEmitter ) { |
| 157 | |
| 158 | |
| 159 | |
| 160 | var $ = window.jQuery; |
| 161 | var console = window.console; |
| 162 | |
| 163 | // -------------------------- helpers -------------------------- // |
| 164 | |
| 165 | // extend objects |
| 166 | function extend( a, b ) { |
| 167 | for ( var prop in b ) { |
| 168 | a[ prop ] = b[ prop ]; |
| 169 | } |
| 170 | return a; |
| 171 | } |
| 172 | |
| 173 | var arraySlice = Array.prototype.slice; |
| 174 | |
| 175 | // turn element or nodeList into an array |
| 176 | function makeArray( obj ) { |
| 177 | if ( Array.isArray( obj ) ) { |
| 178 | // use object if already an array |
| 179 | return obj; |
| 180 | } |
| 181 | |
| 182 | var isArrayLike = typeof obj == 'object' && typeof obj.length == 'number'; |
| 183 | if ( isArrayLike ) { |
| 184 | // convert nodeList to array |
| 185 | return arraySlice.call( obj ); |
| 186 | } |
| 187 | |
| 188 | // array of single index |
| 189 | return [ obj ]; |
| 190 | } |
| 191 | |
| 192 | // -------------------------- imagesLoaded -------------------------- // |
| 193 | |
| 194 | /** |
| 195 | * @param {Array, Element, NodeList, String} elem |
| 196 | * @param {Object or Function} options - if function, use as callback |
| 197 | * @param {Function} onAlways - callback function |
| 198 | */ |
| 199 | function ImagesLoaded( elem, options, onAlways ) { |
| 200 | // coerce ImagesLoaded() without new, to be new ImagesLoaded() |
| 201 | if ( !( this instanceof ImagesLoaded ) ) { |
| 202 | return new ImagesLoaded( elem, options, onAlways ); |
| 203 | } |
| 204 | // use elem as selector string |
| 205 | var queryElem = elem; |
| 206 | if ( typeof elem == 'string' ) { |
| 207 | queryElem = document.querySelectorAll( elem ); |
| 208 | } |
| 209 | // bail if bad element |
| 210 | if ( !queryElem ) { |
| 211 | console.error( 'Bad element for imagesLoaded ' + ( queryElem || elem ) ); |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | this.elements = makeArray( queryElem ); |
| 216 | this.options = extend( {}, this.options ); |
| 217 | // shift arguments if no options set |
| 218 | if ( typeof options == 'function' ) { |
| 219 | onAlways = options; |
| 220 | } else { |
| 221 | extend( this.options, options ); |
| 222 | } |
| 223 | |
| 224 | if ( onAlways ) { |
| 225 | this.on( 'always', onAlways ); |
| 226 | } |
| 227 | |
| 228 | this.getImages(); |
| 229 | |
| 230 | if ( $ ) { |
| 231 | // add jQuery Deferred object |
| 232 | this.jqDeferred = new $.Deferred(); |
| 233 | } |
| 234 | |
| 235 | // HACK check async to allow time to bind listeners |
| 236 | setTimeout( this.check.bind( this ) ); |
| 237 | } |
| 238 | |
| 239 | ImagesLoaded.prototype = Object.create( EvEmitter.prototype ); |
| 240 | |
| 241 | ImagesLoaded.prototype.options = {}; |
| 242 | |
| 243 | ImagesLoaded.prototype.getImages = function() { |
| 244 | this.images = []; |
| 245 | |
| 246 | // filter & find items if we have an item selector |
| 247 | this.elements.forEach( this.addElementImages, this ); |
| 248 | }; |
| 249 | |
| 250 | /** |
| 251 | * @param {Node} element |
| 252 | */ |
| 253 | ImagesLoaded.prototype.addElementImages = function( elem ) { |
| 254 | // filter siblings |
| 255 | if ( elem.nodeName == 'IMG' ) { |
| 256 | this.addImage( elem ); |
| 257 | } |
| 258 | // get background image on element |
| 259 | if ( this.options.background === true ) { |
| 260 | this.addElementBackgroundImages( elem ); |
| 261 | } |
| 262 | |
| 263 | // find children |
| 264 | // no non-element nodes, #143 |
| 265 | var nodeType = elem.nodeType; |
| 266 | if ( !nodeType || !elementNodeTypes[ nodeType ] ) { |
| 267 | return; |
| 268 | } |
| 269 | var childImgs = elem.querySelectorAll('img'); |
| 270 | // concat childElems to filterFound array |
| 271 | for ( var i=0; i < childImgs.length; i++ ) { |
| 272 | var img = childImgs[i]; |
| 273 | this.addImage( img ); |
| 274 | } |
| 275 | |
| 276 | // get child background images |
| 277 | if ( typeof this.options.background == 'string' ) { |
| 278 | var children = elem.querySelectorAll( this.options.background ); |
| 279 | for ( i=0; i < children.length; i++ ) { |
| 280 | var child = children[i]; |
| 281 | this.addElementBackgroundImages( child ); |
| 282 | } |
| 283 | } |
| 284 | }; |
| 285 | |
| 286 | var elementNodeTypes = { |
| 287 | 1: true, |
| 288 | 9: true, |
| 289 | 11: true |
| 290 | }; |
| 291 | |
| 292 | ImagesLoaded.prototype.addElementBackgroundImages = function( elem ) { |
| 293 | var style = getComputedStyle( elem ); |
| 294 | if ( !style ) { |
| 295 | // Firefox returns null if in a hidden iframe https://bugzil.la/548397 |
| 296 | return; |
| 297 | } |
| 298 | // get url inside url("...") |
| 299 | var reURL = /url\((['"])?(.*?)\1\)/gi; |
| 300 | var matches = reURL.exec( style.backgroundImage ); |
| 301 | while ( matches !== null ) { |
| 302 | var url = matches && matches[2]; |
| 303 | if ( url ) { |
| 304 | this.addBackground( url, elem ); |
| 305 | } |
| 306 | matches = reURL.exec( style.backgroundImage ); |
| 307 | } |
| 308 | }; |
| 309 | |
| 310 | /** |
| 311 | * @param {Image} img |
| 312 | */ |
| 313 | ImagesLoaded.prototype.addImage = function( img ) { |
| 314 | var loadingImage = new LoadingImage( img ); |
| 315 | this.images.push( loadingImage ); |
| 316 | }; |
| 317 | |
| 318 | ImagesLoaded.prototype.addBackground = function( url, elem ) { |
| 319 | var background = new Background( url, elem ); |
| 320 | this.images.push( background ); |
| 321 | }; |
| 322 | |
| 323 | ImagesLoaded.prototype.check = function() { |
| 324 | var _this = this; |
| 325 | this.progressedCount = 0; |
| 326 | this.hasAnyBroken = false; |
| 327 | // complete if no images |
| 328 | if ( !this.images.length ) { |
| 329 | this.complete(); |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | function onProgress( image, elem, message ) { |
| 334 | // HACK - Chrome triggers event before object properties have changed. #83 |
| 335 | setTimeout( function() { |
| 336 | _this.progress( image, elem, message ); |
| 337 | }); |
| 338 | } |
| 339 | |
| 340 | this.images.forEach( function( loadingImage ) { |
| 341 | loadingImage.once( 'progress', onProgress ); |
| 342 | loadingImage.check(); |
| 343 | }); |
| 344 | }; |
| 345 | |
| 346 | ImagesLoaded.prototype.progress = function( image, elem, message ) { |
| 347 | this.progressedCount++; |
| 348 | this.hasAnyBroken = this.hasAnyBroken || !image.isLoaded; |
| 349 | // progress event |
| 350 | this.emitEvent( 'progress', [ this, image, elem ] ); |
| 351 | if ( this.jqDeferred && this.jqDeferred.notify ) { |
| 352 | this.jqDeferred.notify( this, image ); |
| 353 | } |
| 354 | // check if completed |
| 355 | if ( this.progressedCount == this.images.length ) { |
| 356 | this.complete(); |
| 357 | } |
| 358 | |
| 359 | if ( this.options.debug && console ) { |
| 360 | console.log( 'progress: ' + message, image, elem ); |
| 361 | } |
| 362 | }; |
| 363 | |
| 364 | ImagesLoaded.prototype.complete = function() { |
| 365 | var eventName = this.hasAnyBroken ? 'fail' : 'done'; |
| 366 | this.isComplete = true; |
| 367 | this.emitEvent( eventName, [ this ] ); |
| 368 | this.emitEvent( 'always', [ this ] ); |
| 369 | if ( this.jqDeferred ) { |
| 370 | var jqMethod = this.hasAnyBroken ? 'reject' : 'resolve'; |
| 371 | this.jqDeferred[ jqMethod ]( this ); |
| 372 | } |
| 373 | }; |
| 374 | |
| 375 | // -------------------------- -------------------------- // |
| 376 | |
| 377 | function LoadingImage( img ) { |
| 378 | this.img = img; |
| 379 | } |
| 380 | |
| 381 | LoadingImage.prototype = Object.create( EvEmitter.prototype ); |
| 382 | |
| 383 | LoadingImage.prototype.check = function() { |
| 384 | // If complete is true and browser supports natural sizes, |
| 385 | // try to check for image status manually. |
| 386 | var isComplete = this.getIsImageComplete(); |
| 387 | if ( isComplete ) { |
| 388 | // report based on naturalWidth |
| 389 | this.confirm( this.img.naturalWidth !== 0, 'naturalWidth' ); |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | // If none of the checks above matched, simulate loading on detached element. |
| 394 | this.proxyImage = new Image(); |
| 395 | this.proxyImage.addEventListener( 'load', this ); |
| 396 | this.proxyImage.addEventListener( 'error', this ); |
| 397 | // bind to image as well for Firefox. #191 |
| 398 | this.img.addEventListener( 'load', this ); |
| 399 | this.img.addEventListener( 'error', this ); |
| 400 | this.proxyImage.src = this.img.src; |
| 401 | }; |
| 402 | |
| 403 | LoadingImage.prototype.getIsImageComplete = function() { |
| 404 | // check for non-zero, non-undefined naturalWidth |
| 405 | // fixes Safari+InfiniteScroll+Masonry bug infinite-scroll#671 |
| 406 | return this.img.complete && this.img.naturalWidth; |
| 407 | }; |
| 408 | |
| 409 | LoadingImage.prototype.confirm = function( isLoaded, message ) { |
| 410 | this.isLoaded = isLoaded; |
| 411 | this.emitEvent( 'progress', [ this, this.img, message ] ); |
| 412 | }; |
| 413 | |
| 414 | // ----- events ----- // |
| 415 | |
| 416 | // trigger specified handler for event type |
| 417 | LoadingImage.prototype.handleEvent = function( event ) { |
| 418 | var method = 'on' + event.type; |
| 419 | if ( this[ method ] ) { |
| 420 | this[ method ]( event ); |
| 421 | } |
| 422 | }; |
| 423 | |
| 424 | LoadingImage.prototype.onload = function() { |
| 425 | this.confirm( true, 'onload' ); |
| 426 | this.unbindEvents(); |
| 427 | }; |
| 428 | |
| 429 | LoadingImage.prototype.onerror = function() { |
| 430 | this.confirm( false, 'onerror' ); |
| 431 | this.unbindEvents(); |
| 432 | }; |
| 433 | |
| 434 | LoadingImage.prototype.unbindEvents = function() { |
| 435 | this.proxyImage.removeEventListener( 'load', this ); |
| 436 | this.proxyImage.removeEventListener( 'error', this ); |
| 437 | this.img.removeEventListener( 'load', this ); |
| 438 | this.img.removeEventListener( 'error', this ); |
| 439 | }; |
| 440 | |
| 441 | // -------------------------- Background -------------------------- // |
| 442 | |
| 443 | function Background( url, element ) { |
| 444 | this.url = url; |
| 445 | this.element = element; |
| 446 | this.img = new Image(); |
| 447 | } |
| 448 | |
| 449 | // inherit LoadingImage prototype |
| 450 | Background.prototype = Object.create( LoadingImage.prototype ); |
| 451 | |
| 452 | Background.prototype.check = function() { |
| 453 | this.img.addEventListener( 'load', this ); |
| 454 | this.img.addEventListener( 'error', this ); |
| 455 | this.img.src = this.url; |
| 456 | // check if image is already complete |
| 457 | var isComplete = this.getIsImageComplete(); |
| 458 | if ( isComplete ) { |
| 459 | this.confirm( this.img.naturalWidth !== 0, 'naturalWidth' ); |
| 460 | this.unbindEvents(); |
| 461 | } |
| 462 | }; |
| 463 | |
| 464 | Background.prototype.unbindEvents = function() { |
| 465 | this.img.removeEventListener( 'load', this ); |
| 466 | this.img.removeEventListener( 'error', this ); |
| 467 | }; |
| 468 | |
| 469 | Background.prototype.confirm = function( isLoaded, message ) { |
| 470 | this.isLoaded = isLoaded; |
| 471 | this.emitEvent( 'progress', [ this, this.element, message ] ); |
| 472 | }; |
| 473 | |
| 474 | // -------------------------- jQuery -------------------------- // |
| 475 | |
| 476 | ImagesLoaded.makeJQueryPlugin = function( jQuery ) { |
| 477 | jQuery = jQuery || window.jQuery; |
| 478 | if ( !jQuery ) { |
| 479 | return; |
| 480 | } |
| 481 | // set local variable |
| 482 | $ = jQuery; |
| 483 | // $().imagesLoaded() |
| 484 | $.fn.imagesLoaded = function( options, callback ) { |
| 485 | var instance = new ImagesLoaded( this, options, callback ); |
| 486 | return instance.jqDeferred.promise( $(this) ); |
| 487 | }; |
| 488 | }; |
| 489 | // try making plugin |
| 490 | ImagesLoaded.makeJQueryPlugin(); |
| 491 | |
| 492 | // -------------------------- -------------------------- // |
| 493 | |
| 494 | return ImagesLoaded; |
| 495 | |
| 496 | }); |
| 497 | |
| 498 |