imagesloaded.pkgd.js 12 KB

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