imagesloaded.js 9.4 KB

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