photon.js
| 1 | /* jshint onevar: false */ |
| 2 | |
| 3 | (function($){ |
| 4 | /** |
| 5 | * For images lacking explicit dimensions and needing them, try to add them. |
| 6 | */ |
| 7 | var restore_dims = function() { |
| 8 | $( 'img[data-recalc-dims]' ).each( function recalc() { |
| 9 | var $this = $( this ); |
| 10 | if ( this.complete ) { |
| 11 | |
| 12 | // Support for lazy loading: if there is a lazy-src |
| 13 | // attribute and it's value is not the same as the current src we |
| 14 | // should wait until the image load event |
| 15 | if ( $this.data( 'lazy-src' ) && $this.attr( 'src' ) !== $this.data( 'lazy-src' ) ) { |
| 16 | $this.load( recalc ); |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | var width = this.width, |
| 21 | height = this.height; |
| 22 | |
| 23 | if ( width && width > 0 && height && height > 0 ) { |
| 24 | $this.attr( { |
| 25 | width: width, |
| 26 | height: height |
| 27 | } ); |
| 28 | |
| 29 | reset_for_retina( this ); |
| 30 | } |
| 31 | } |
| 32 | else { |
| 33 | $this.load( recalc ); |
| 34 | } |
| 35 | } ); |
| 36 | }, |
| 37 | |
| 38 | /** |
| 39 | * Modify given image's markup so that devicepx-jetpack.js will act on the image and it won't be reprocessed by this script. |
| 40 | */ |
| 41 | reset_for_retina = function( img ) { |
| 42 | $( img ).removeAttr( 'data-recalc-dims' ).removeAttr( 'scale' ); |
| 43 | }; |
| 44 | |
| 45 | /** |
| 46 | * Check both when page loads, and when IS is triggered. |
| 47 | */ |
| 48 | $( document ).ready( restore_dims ); |
| 49 | |
| 50 | if ( 'on' in $.fn ) { |
| 51 | $( document.body ).on( 'post-load', restore_dims ); |
| 52 | } else { |
| 53 | $( document ).delegate( 'body', 'post-load', restore_dims ); |
| 54 | } |
| 55 | })(jQuery); |
| 56 |