PageRenderTime 484ms CodeModel.GetById 41ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/themes/auto-repair/vamtam/assets/js/plugins/thirdparty/jquery.imagesloaded.js

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