James Peret's blog. Built with Jekyll and the Mikey theme.

imagesloaded.pkgd.min.js 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /*!
  2. * imagesLoaded PACKAGED v3.1.8
  3. * JavaScript is all like "You images are done yet or what?"
  4. * MIT License
  5. */
  6. /*!
  7. * EventEmitter v4.2.6 - git.io/ee
  8. * Oliver Caldwell
  9. * MIT license
  10. * @preserve
  11. */
  12. (function () {
  13. /**
  14. * Class for managing events.
  15. * Can be extended to provide event functionality in other classes.
  16. *
  17. * @class EventEmitter Manages event registering and emitting.
  18. */
  19. function EventEmitter() {}
  20. // Shortcuts to improve speed and size
  21. var proto = EventEmitter.prototype;
  22. var exports = this;
  23. var originalGlobalValue = exports.EventEmitter;
  24. /**
  25. * Finds the index of the listener for the event in it's storage array.
  26. *
  27. * @param {Function[]} listeners Array of listeners to search through.
  28. * @param {Function} listener Method to look for.
  29. * @return {Number} Index of the specified listener, -1 if not found
  30. * @api private
  31. */
  32. function indexOfListener(listeners, listener) {
  33. var i = listeners.length;
  34. while (i--) {
  35. if (listeners[i].listener === listener) {
  36. return i;
  37. }
  38. }
  39. return -1;
  40. }
  41. /**
  42. * Alias a method while keeping the context correct, to allow for overwriting of target method.
  43. *
  44. * @param {String} name The name of the target method.
  45. * @return {Function} The aliased method
  46. * @api private
  47. */
  48. function alias(name) {
  49. return function aliasClosure() {
  50. return this[name].apply(this, arguments);
  51. };
  52. }
  53. /**
  54. * Returns the listener array for the specified event.
  55. * Will initialise the event object and listener arrays if required.
  56. * Will return an object if you use a regex search. The object contains keys for each matched event. So /ba[rz]/ might return an object containing bar and baz. But only if you have either defined them with defineEvent or added some listeners to them.
  57. * Each property in the object response is an array of listener functions.
  58. *
  59. * @param {String|RegExp} evt Name of the event to return the listeners from.
  60. * @return {Function[]|Object} All listener functions for the event.
  61. */
  62. proto.getListeners = function getListeners(evt) {
  63. var events = this._getEvents();
  64. var response;
  65. var key;
  66. // Return a concatenated array of all matching events if
  67. // the selector is a regular expression.
  68. if (typeof evt === 'object') {
  69. response = {};
  70. for (key in events) {
  71. if (events.hasOwnProperty(key) && evt.test(key)) {
  72. response[key] = events[key];
  73. }
  74. }
  75. }
  76. else {
  77. response = events[evt] || (events[evt] = []);
  78. }
  79. return response;
  80. };
  81. /**
  82. * Takes a list of listener objects and flattens it into a list of listener functions.
  83. *
  84. * @param {Object[]} listeners Raw listener objects.
  85. * @return {Function[]} Just the listener functions.
  86. */
  87. proto.flattenListeners = function flattenListeners(listeners) {
  88. var flatListeners = [];
  89. var i;
  90. for (i = 0; i < listeners.length; i += 1) {
  91. flatListeners.push(listeners[i].listener);
  92. }
  93. return flatListeners;
  94. };
  95. /**
  96. * Fetches the requested listeners via getListeners but will always return the results inside an object. This is mainly for internal use but others may find it useful.
  97. *
  98. * @param {String|RegExp} evt Name of the event to return the listeners from.
  99. * @return {Object} All listener functions for an event in an object.
  100. */
  101. proto.getListenersAsObject = function getListenersAsObject(evt) {
  102. var listeners = this.getListeners(evt);
  103. var response;
  104. if (listeners instanceof Array) {
  105. response = {};
  106. response[evt] = listeners;
  107. }
  108. return response || listeners;
  109. };
  110. /**
  111. * Adds a listener function to the specified event.
  112. * The listener will not be added if it is a duplicate.
  113. * If the listener returns true then it will be removed after it is called.
  114. * If you pass a regular expression as the event name then the listener will be added to all events that match it.
  115. *
  116. * @param {String|RegExp} evt Name of the event to attach the listener to.
  117. * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling.
  118. * @return {Object} Current instance of EventEmitter for chaining.
  119. */
  120. proto.addListener = function addListener(evt, listener) {
  121. var listeners = this.getListenersAsObject(evt);
  122. var listenerIsWrapped = typeof listener === 'object';
  123. var key;
  124. for (key in listeners) {
  125. if (listeners.hasOwnProperty(key) && indexOfListener(listeners[key], listener) === -1) {
  126. listeners[key].push(listenerIsWrapped ? listener : {
  127. listener: listener,
  128. once: false
  129. });
  130. }
  131. }
  132. return this;
  133. };
  134. /**
  135. * Alias of addListener
  136. */
  137. proto.on = alias('addListener');
  138. /**
  139. * Semi-alias of addListener. It will add a listener that will be
  140. * automatically removed after it's first execution.
  141. *
  142. * @param {String|RegExp} evt Name of the event to attach the listener to.
  143. * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling.
  144. * @return {Object} Current instance of EventEmitter for chaining.
  145. */
  146. proto.addOnceListener = function addOnceListener(evt, listener) {
  147. return this.addListener(evt, {
  148. listener: listener,
  149. once: true
  150. });
  151. };
  152. /**
  153. * Alias of addOnceListener.
  154. */
  155. proto.once = alias('addOnceListener');
  156. /**
  157. * Defines an event name. This is required if you want to use a regex to add a listener to multiple events at once. If you don't do this then how do you expect it to know what event to add to? Should it just add to every possible match for a regex? No. That is scary and bad.
  158. * You need to tell it what event names should be matched by a regex.
  159. *
  160. * @param {String} evt Name of the event to create.
  161. * @return {Object} Current instance of EventEmitter for chaining.
  162. */
  163. proto.defineEvent = function defineEvent(evt) {
  164. this.getListeners(evt);
  165. return this;
  166. };
  167. /**
  168. * Uses defineEvent to define multiple events.
  169. *
  170. * @param {String[]} evts An array of event names to define.
  171. * @return {Object} Current instance of EventEmitter for chaining.
  172. */
  173. proto.defineEvents = function defineEvents(evts) {
  174. for (var i = 0; i < evts.length; i += 1) {
  175. this.defineEvent(evts[i]);
  176. }
  177. return this;
  178. };
  179. /**
  180. * Removes a listener function from the specified event.
  181. * When passed a regular expression as the event name, it will remove the listener from all events that match it.
  182. *
  183. * @param {String|RegExp} evt Name of the event to remove the listener from.
  184. * @param {Function} listener Method to remove from the event.
  185. * @return {Object} Current instance of EventEmitter for chaining.
  186. */
  187. proto.removeListener = function removeListener(evt, listener) {
  188. var listeners = this.getListenersAsObject(evt);
  189. var index;
  190. var key;
  191. for (key in listeners) {
  192. if (listeners.hasOwnProperty(key)) {
  193. index = indexOfListener(listeners[key], listener);
  194. if (index !== -1) {
  195. listeners[key].splice(index, 1);
  196. }
  197. }
  198. }
  199. return this;
  200. };
  201. /**
  202. * Alias of removeListener
  203. */
  204. proto.off = alias('removeListener');
  205. /**
  206. * Adds listeners in bulk using the manipulateListeners method.
  207. * If you pass an object as the second argument you can add to multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. You can also pass it an event name and an array of listeners to be added.
  208. * You can also pass it a regular expression to add the array of listeners to all events that match it.
  209. * Yeah, this function does quite a bit. That's probably a bad thing.
  210. *
  211. * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add to multiple events at once.
  212. * @param {Function[]} [listeners] An optional array of listener functions to add.
  213. * @return {Object} Current instance of EventEmitter for chaining.
  214. */
  215. proto.addListeners = function addListeners(evt, listeners) {
  216. // Pass through to manipulateListeners
  217. return this.manipulateListeners(false, evt, listeners);
  218. };
  219. /**
  220. * Removes listeners in bulk using the manipulateListeners method.
  221. * If you pass an object as the second argument you can remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays.
  222. * You can also pass it an event name and an array of listeners to be removed.
  223. * You can also pass it a regular expression to remove the listeners from all events that match it.
  224. *
  225. * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to remove from multiple events at once.
  226. * @param {Function[]} [listeners] An optional array of listener functions to remove.
  227. * @return {Object} Current instance of EventEmitter for chaining.
  228. */
  229. proto.removeListeners = function removeListeners(evt, listeners) {
  230. // Pass through to manipulateListeners
  231. return this.manipulateListeners(true, evt, listeners);
  232. };
  233. /**
  234. * Edits listeners in bulk. The addListeners and removeListeners methods both use this to do their job. You should really use those instead, this is a little lower level.
  235. * The first argument will determine if the listeners are removed (true) or added (false).
  236. * If you pass an object as the second argument you can add/remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays.
  237. * You can also pass it an event name and an array of listeners to be added/removed.
  238. * You can also pass it a regular expression to manipulate the listeners of all events that match it.
  239. *
  240. * @param {Boolean} remove True if you want to remove listeners, false if you want to add.
  241. * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add/remove from multiple events at once.
  242. * @param {Function[]} [listeners] An optional array of listener functions to add/remove.
  243. * @return {Object} Current instance of EventEmitter for chaining.
  244. */
  245. proto.manipulateListeners = function manipulateListeners(remove, evt, listeners) {
  246. var i;
  247. var value;
  248. var single = remove ? this.removeListener : this.addListener;
  249. var multiple = remove ? this.removeListeners : this.addListeners;
  250. // If evt is an object then pass each of it's properties to this method
  251. if (typeof evt === 'object' && !(evt instanceof RegExp)) {
  252. for (i in evt) {
  253. if (evt.hasOwnProperty(i) && (value = evt[i])) {
  254. // Pass the single listener straight through to the singular method
  255. if (typeof value === 'function') {
  256. single.call(this, i, value);
  257. }
  258. else {
  259. // Otherwise pass back to the multiple function
  260. multiple.call(this, i, value);
  261. }
  262. }
  263. }
  264. }
  265. else {
  266. // So evt must be a string
  267. // And listeners must be an array of listeners
  268. // Loop over it and pass each one to the multiple method
  269. i = listeners.length;
  270. while (i--) {
  271. single.call(this, evt, listeners[i]);
  272. }
  273. }
  274. return this;
  275. };
  276. /**
  277. * Removes all listeners from a specified event.
  278. * If you do not specify an event then all listeners will be removed.
  279. * That means every event will be emptied.
  280. * You can also pass a regex to remove all events that match it.
  281. *
  282. * @param {String|RegExp} [evt] Optional name of the event to remove all listeners for. Will remove from every event if not passed.
  283. * @return {Object} Current instance of EventEmitter for chaining.
  284. */
  285. proto.removeEvent = function removeEvent(evt) {
  286. var type = typeof evt;
  287. var events = this._getEvents();
  288. var key;
  289. // Remove different things depending on the state of evt
  290. if (type === 'string') {
  291. // Remove all listeners for the specified event
  292. delete events[evt];
  293. }
  294. else if (type === 'object') {
  295. // Remove all events matching the regex.
  296. for (key in events) {
  297. if (events.hasOwnProperty(key) && evt.test(key)) {
  298. delete events[key];
  299. }
  300. }
  301. }
  302. else {
  303. // Remove all listeners in all events
  304. delete this._events;
  305. }
  306. return this;
  307. };
  308. /**
  309. * Alias of removeEvent.
  310. *
  311. * Added to mirror the node API.
  312. */
  313. proto.removeAllListeners = alias('removeEvent');
  314. /**
  315. * Emits an event of your choice.
  316. * When emitted, every listener attached to that event will be executed.
  317. * If you pass the optional argument array then those arguments will be passed to every listener upon execution.
  318. * Because it uses `apply`, your array of arguments will be passed as if you wrote them out separately.
  319. * So they will not arrive within the array on the other side, they will be separate.
  320. * You can also pass a regular expression to emit to all events that match it.
  321. *
  322. * @param {String|RegExp} evt Name of the event to emit and execute listeners for.
  323. * @param {Array} [args] Optional array of arguments to be passed to each listener.
  324. * @return {Object} Current instance of EventEmitter for chaining.
  325. */
  326. proto.emitEvent = function emitEvent(evt, args) {
  327. var listeners = this.getListenersAsObject(evt);
  328. var listener;
  329. var i;
  330. var key;
  331. var response;
  332. for (key in listeners) {
  333. if (listeners.hasOwnProperty(key)) {
  334. i = listeners[key].length;
  335. while (i--) {
  336. // If the listener returns true then it shall be removed from the event
  337. // The function is executed either with a basic call or an apply if there is an args array
  338. listener = listeners[key][i];
  339. if (listener.once === true) {
  340. this.removeListener(evt, listener.listener);
  341. }
  342. response = listener.listener.apply(this, args || []);
  343. if (response === this._getOnceReturnValue()) {
  344. this.removeListener(evt, listener.listener);
  345. }
  346. }
  347. }
  348. }
  349. return this;
  350. };
  351. /**
  352. * Alias of emitEvent
  353. */
  354. proto.trigger = alias('emitEvent');
  355. /**
  356. * Subtly different from emitEvent in that it will pass its arguments on to the listeners, as opposed to taking a single array of arguments to pass on.
  357. * As with emitEvent, you can pass a regex in place of the event name to emit to all events that match it.
  358. *
  359. * @param {String|RegExp} evt Name of the event to emit and execute listeners for.
  360. * @param {...*} Optional additional arguments to be passed to each listener.
  361. * @return {Object} Current instance of EventEmitter for chaining.
  362. */
  363. proto.emit = function emit(evt) {
  364. var args = Array.prototype.slice.call(arguments, 1);
  365. return this.emitEvent(evt, args);
  366. };
  367. /**
  368. * Sets the current value to check against when executing listeners. If a
  369. * listeners return value matches the one set here then it will be removed
  370. * after execution. This value defaults to true.
  371. *
  372. * @param {*} value The new value to check for when executing listeners.
  373. * @return {Object} Current instance of EventEmitter for chaining.
  374. */
  375. proto.setOnceReturnValue = function setOnceReturnValue(value) {
  376. this._onceReturnValue = value;
  377. return this;
  378. };
  379. /**
  380. * Fetches the current value to check against when executing listeners. If
  381. * the listeners return value matches this one then it should be removed
  382. * automatically. It will return true by default.
  383. *
  384. * @return {*|Boolean} The current value to check for or the default, true.
  385. * @api private
  386. */
  387. proto._getOnceReturnValue = function _getOnceReturnValue() {
  388. if (this.hasOwnProperty('_onceReturnValue')) {
  389. return this._onceReturnValue;
  390. }
  391. else {
  392. return true;
  393. }
  394. };
  395. /**
  396. * Fetches the events object and creates one if required.
  397. *
  398. * @return {Object} The events storage object.
  399. * @api private
  400. */
  401. proto._getEvents = function _getEvents() {
  402. return this._events || (this._events = {});
  403. };
  404. /**
  405. * Reverts the global {@link EventEmitter} to its previous value and returns a reference to this version.
  406. *
  407. * @return {Function} Non conflicting EventEmitter class.
  408. */
  409. EventEmitter.noConflict = function noConflict() {
  410. exports.EventEmitter = originalGlobalValue;
  411. return EventEmitter;
  412. };
  413. // Expose the class either via AMD, CommonJS or the global object
  414. if (typeof define === 'function' && define.amd) {
  415. define('eventEmitter/EventEmitter',[],function () {
  416. return EventEmitter;
  417. });
  418. }
  419. else if (typeof module === 'object' && module.exports){
  420. module.exports = EventEmitter;
  421. }
  422. else {
  423. this.EventEmitter = EventEmitter;
  424. }
  425. }.call(this));
  426. /*!
  427. * eventie v1.0.4
  428. * event binding helper
  429. * eventie.bind( elem, 'click', myFn )
  430. * eventie.unbind( elem, 'click', myFn )
  431. */
  432. /*jshint browser: true, undef: true, unused: true */
  433. /*global define: false */
  434. ( function( window ) {
  435. var docElem = document.documentElement;
  436. var bind = function() {};
  437. function getIEEvent( obj ) {
  438. var event = window.event;
  439. // add event.target
  440. event.target = event.target || event.srcElement || obj;
  441. return event;
  442. }
  443. if ( docElem.addEventListener ) {
  444. bind = function( obj, type, fn ) {
  445. obj.addEventListener( type, fn, false );
  446. };
  447. } else if ( docElem.attachEvent ) {
  448. bind = function( obj, type, fn ) {
  449. obj[ type + fn ] = fn.handleEvent ?
  450. function() {
  451. var event = getIEEvent( obj );
  452. fn.handleEvent.call( fn, event );
  453. } :
  454. function() {
  455. var event = getIEEvent( obj );
  456. fn.call( obj, event );
  457. };
  458. obj.attachEvent( "on" + type, obj[ type + fn ] );
  459. };
  460. }
  461. var unbind = function() {};
  462. if ( docElem.removeEventListener ) {
  463. unbind = function( obj, type, fn ) {
  464. obj.removeEventListener( type, fn, false );
  465. };
  466. } else if ( docElem.detachEvent ) {
  467. unbind = function( obj, type, fn ) {
  468. obj.detachEvent( "on" + type, obj[ type + fn ] );
  469. try {
  470. delete obj[ type + fn ];
  471. } catch ( err ) {
  472. // can't delete window object properties
  473. obj[ type + fn ] = undefined;
  474. }
  475. };
  476. }
  477. var eventie = {
  478. bind: bind,
  479. unbind: unbind
  480. };
  481. // transport
  482. if ( typeof define === 'function' && define.amd ) {
  483. // AMD
  484. define( 'eventie/eventie',eventie );
  485. } else {
  486. // browser global
  487. window.eventie = eventie;
  488. }
  489. })( this );
  490. /*!
  491. * imagesLoaded v3.1.8
  492. * JavaScript is all like "You images are done yet or what?"
  493. * MIT License
  494. */
  495. ( function( window, factory ) {
  496. // universal module definition
  497. /*global define: false, module: false, require: false */
  498. if ( typeof define === 'function' && define.amd ) {
  499. // AMD
  500. define( [
  501. 'eventEmitter/EventEmitter',
  502. 'eventie/eventie'
  503. ], function( EventEmitter, eventie ) {
  504. return factory( window, EventEmitter, eventie );
  505. });
  506. } else if ( typeof exports === 'object' ) {
  507. // CommonJS
  508. module.exports = factory(
  509. window,
  510. require('wolfy87-eventemitter'),
  511. require('eventie')
  512. );
  513. } else {
  514. // browser global
  515. window.imagesLoaded = factory(
  516. window,
  517. window.EventEmitter,
  518. window.eventie
  519. );
  520. }
  521. })( window,
  522. // -------------------------- factory -------------------------- //
  523. function factory( window, EventEmitter, eventie ) {
  524. var $ = window.jQuery;
  525. var console = window.console;
  526. var hasConsole = typeof console !== 'undefined';
  527. // -------------------------- helpers -------------------------- //
  528. // extend objects
  529. function extend( a, b ) {
  530. for ( var prop in b ) {
  531. a[ prop ] = b[ prop ];
  532. }
  533. return a;
  534. }
  535. var objToString = Object.prototype.toString;
  536. function isArray( obj ) {
  537. return objToString.call( obj ) === '[object Array]';
  538. }
  539. // turn element or nodeList into an array
  540. function makeArray( obj ) {
  541. var ary = [];
  542. if ( isArray( obj ) ) {
  543. // use object if already an array
  544. ary = obj;
  545. } else if ( typeof obj.length === 'number' ) {
  546. // convert nodeList to array
  547. for ( var i=0, len = obj.length; i < len; i++ ) {
  548. ary.push( obj[i] );
  549. }
  550. } else {
  551. // array of single index
  552. ary.push( obj );
  553. }
  554. return ary;
  555. }
  556. // -------------------------- imagesLoaded -------------------------- //
  557. /**
  558. * @param {Array, Element, NodeList, String} elem
  559. * @param {Object or Function} options - if function, use as callback
  560. * @param {Function} onAlways - callback function
  561. */
  562. function ImagesLoaded( elem, options, onAlways ) {
  563. // coerce ImagesLoaded() without new, to be new ImagesLoaded()
  564. if ( !( this instanceof ImagesLoaded ) ) {
  565. return new ImagesLoaded( elem, options );
  566. }
  567. // use elem as selector string
  568. if ( typeof elem === 'string' ) {
  569. elem = document.querySelectorAll( elem );
  570. }
  571. this.elements = makeArray( elem );
  572. this.options = extend( {}, this.options );
  573. if ( typeof options === 'function' ) {
  574. onAlways = options;
  575. } else {
  576. extend( this.options, options );
  577. }
  578. if ( onAlways ) {
  579. this.on( 'always', onAlways );
  580. }
  581. this.getImages();
  582. if ( $ ) {
  583. // add jQuery Deferred object
  584. this.jqDeferred = new $.Deferred();
  585. }
  586. // HACK check async to allow time to bind listeners
  587. var _this = this;
  588. setTimeout( function() {
  589. _this.check();
  590. });
  591. }
  592. ImagesLoaded.prototype = new EventEmitter();
  593. ImagesLoaded.prototype.options = {};
  594. ImagesLoaded.prototype.getImages = function() {
  595. this.images = [];
  596. // filter & find items if we have an item selector
  597. for ( var i=0, len = this.elements.length; i < len; i++ ) {
  598. var elem = this.elements[i];
  599. // filter siblings
  600. if ( elem.nodeName === 'IMG' ) {
  601. this.addImage( elem );
  602. }
  603. // find children
  604. // no non-element nodes, #143
  605. var nodeType = elem.nodeType;
  606. if ( !nodeType || !( nodeType === 1 || nodeType === 9 || nodeType === 11 ) ) {
  607. continue;
  608. }
  609. var childElems = elem.querySelectorAll('img');
  610. // concat childElems to filterFound array
  611. for ( var j=0, jLen = childElems.length; j < jLen; j++ ) {
  612. var img = childElems[j];
  613. this.addImage( img );
  614. }
  615. }
  616. };
  617. /**
  618. * @param {Image} img
  619. */
  620. ImagesLoaded.prototype.addImage = function( img ) {
  621. var loadingImage = new LoadingImage( img );
  622. this.images.push( loadingImage );
  623. };
  624. ImagesLoaded.prototype.check = function() {
  625. var _this = this;
  626. var checkedCount = 0;
  627. var length = this.images.length;
  628. this.hasAnyBroken = false;
  629. // complete if no images
  630. if ( !length ) {
  631. this.complete();
  632. return;
  633. }
  634. function onConfirm( image, message ) {
  635. if ( _this.options.debug && hasConsole ) {
  636. console.log( 'confirm', image, message );
  637. }
  638. _this.progress( image );
  639. checkedCount++;
  640. if ( checkedCount === length ) {
  641. _this.complete();
  642. }
  643. return true; // bind once
  644. }
  645. for ( var i=0; i < length; i++ ) {
  646. var loadingImage = this.images[i];
  647. loadingImage.on( 'confirm', onConfirm );
  648. loadingImage.check();
  649. }
  650. };
  651. ImagesLoaded.prototype.progress = function( image ) {
  652. this.hasAnyBroken = this.hasAnyBroken || !image.isLoaded;
  653. // HACK - Chrome triggers event before object properties have changed. #83
  654. var _this = this;
  655. setTimeout( function() {
  656. _this.emit( 'progress', _this, image );
  657. if ( _this.jqDeferred && _this.jqDeferred.notify ) {
  658. _this.jqDeferred.notify( _this, image );
  659. }
  660. });
  661. };
  662. ImagesLoaded.prototype.complete = function() {
  663. var eventName = this.hasAnyBroken ? 'fail' : 'done';
  664. this.isComplete = true;
  665. var _this = this;
  666. // HACK - another setTimeout so that confirm happens after progress
  667. setTimeout( function() {
  668. _this.emit( eventName, _this );
  669. _this.emit( 'always', _this );
  670. if ( _this.jqDeferred ) {
  671. var jqMethod = _this.hasAnyBroken ? 'reject' : 'resolve';
  672. _this.jqDeferred[ jqMethod ]( _this );
  673. }
  674. });
  675. };
  676. // -------------------------- jquery -------------------------- //
  677. if ( $ ) {
  678. $.fn.imagesLoaded = function( options, callback ) {
  679. var instance = new ImagesLoaded( this, options, callback );
  680. return instance.jqDeferred.promise( $(this) );
  681. };
  682. }
  683. // -------------------------- -------------------------- //
  684. function LoadingImage( img ) {
  685. this.img = img;
  686. }
  687. LoadingImage.prototype = new EventEmitter();
  688. LoadingImage.prototype.check = function() {
  689. // first check cached any previous images that have same src
  690. var resource = cache[ this.img.src ] || new Resource( this.img.src );
  691. if ( resource.isConfirmed ) {
  692. this.confirm( resource.isLoaded, 'cached was confirmed' );
  693. return;
  694. }
  695. // If complete is true and browser supports natural sizes,
  696. // try to check for image status manually.
  697. if ( this.img.complete && this.img.naturalWidth !== undefined ) {
  698. // report based on naturalWidth
  699. this.confirm( this.img.naturalWidth !== 0, 'naturalWidth' );
  700. return;
  701. }
  702. // If none of the checks above matched, simulate loading on detached element.
  703. var _this = this;
  704. resource.on( 'confirm', function( resrc, message ) {
  705. _this.confirm( resrc.isLoaded, message );
  706. return true;
  707. });
  708. resource.check();
  709. };
  710. LoadingImage.prototype.confirm = function( isLoaded, message ) {
  711. this.isLoaded = isLoaded;
  712. this.emit( 'confirm', this, message );
  713. };
  714. // -------------------------- Resource -------------------------- //
  715. // Resource checks each src, only once
  716. // separate class from LoadingImage to prevent memory leaks. See #115
  717. var cache = {};
  718. function Resource( src ) {
  719. this.src = src;
  720. // add to cache
  721. cache[ src ] = this;
  722. }
  723. Resource.prototype = new EventEmitter();
  724. Resource.prototype.check = function() {
  725. // only trigger checking once
  726. if ( this.isChecked ) {
  727. return;
  728. }
  729. // simulate loading on detached element
  730. var proxyImage = new Image();
  731. eventie.bind( proxyImage, 'load', this );
  732. eventie.bind( proxyImage, 'error', this );
  733. proxyImage.src = this.src;
  734. // set flag
  735. this.isChecked = true;
  736. };
  737. // ----- events ----- //
  738. // trigger specified handler for event type
  739. Resource.prototype.handleEvent = function( event ) {
  740. var method = 'on' + event.type;
  741. if ( this[ method ] ) {
  742. this[ method ]( event );
  743. }
  744. };
  745. Resource.prototype.onload = function( event ) {
  746. this.confirm( true, 'onload' );
  747. this.unbindProxyEvents( event );
  748. };
  749. Resource.prototype.onerror = function( event ) {
  750. this.confirm( false, 'onerror' );
  751. this.unbindProxyEvents( event );
  752. };
  753. // ----- confirm ----- //
  754. Resource.prototype.confirm = function( isLoaded, message ) {
  755. this.isConfirmed = true;
  756. this.isLoaded = isLoaded;
  757. this.emit( 'confirm', this, message );
  758. };
  759. Resource.prototype.unbindProxyEvents = function( event ) {
  760. eventie.unbind( event.target, 'load', this );
  761. eventie.unbind( event.target, 'error', this );
  762. };
  763. // ----- ----- //
  764. return ImagesLoaded;
  765. });