photon.js
| 1 | /* jshint onevar: false */ |
| 2 | |
| 3 | ( function() { |
| 4 | function recalculate() { |
| 5 | if ( this.complete ) { |
| 6 | // Support for lazy loading: if there is a lazy-src attribute and it's value |
| 7 | // is not the same as the current src we should wait until the image load event |
| 8 | var lazySrc = this.getAttribute('data-lazy-src'); |
| 9 | if ( lazySrc && this.src !== lazySrc ) { |
| 10 | this.addEventListener( 'onload', recalculate ); |
| 11 | return; |
| 12 | } |
| 13 | |
| 14 | // Copying CSS width/height into element attributes. |
| 15 | var width = this.width; |
| 16 | var height = this.height; |
| 17 | if ( width && width > 0 && height && height > 0 ) { |
| 18 | this.setAttribute('width', width); |
| 19 | this.setAttribute('height', height); |
| 20 | |
| 21 | reset_for_retina( this ); |
| 22 | } |
| 23 | } |
| 24 | else { |
| 25 | this.addEventListener( 'onload', recalculate ); |
| 26 | return; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * For images lacking explicit dimensions and needing them, try to add them. |
| 32 | */ |
| 33 | var restore_dims = function() { |
| 34 | var elements = document.querySelectorAll( 'img[data-recalc-dims]' ); |
| 35 | for (var i = 0; i < elements.length; i++) { |
| 36 | recalculate.call( elements[i] ); |
| 37 | } |
| 38 | }, |
| 39 | |
| 40 | /** |
| 41 | * Modify given image's markup so that devicepx-jetpack.js will act on the image and it won't be reprocessed by this script. |
| 42 | */ |
| 43 | reset_for_retina = function( img ) { |
| 44 | img.removeAttribute( 'data-recalc-dims' ); |
| 45 | img.removeAttribute( 'scale' ); |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * Check both when page loads, and when IS is triggered. |
| 50 | */ |
| 51 | if ( typeof window !== 'undefined' && typeof document !== 'undefined' ) { |
| 52 | // `DOMContentLoaded` may fire before the script has a chance to run |
| 53 | if ( document.readyState === 'loading' ) { |
| 54 | document.addEventListener( 'DOMContentLoaded', restore_dims ); |
| 55 | } else { |
| 56 | restore_dims(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | document.body.addEventListener( 'post-load', restore_dims ); |
| 61 | } )(); |
| 62 |