|
6 | 6 | * |
7 | 7 | * @author Paul Prins |
8 | 8 | * @link http://paulprins.net |
9 | | - * @version 1.5.0 |
| 9 | + * @version 1.6.0 |
10 | 10 | * @requires jQuery v1.7 or later |
11 | 11 | * |
12 | 12 | * Official jQuery plugin page: |
|
43 | 43 | // Check if we can actually continue with this |
44 | 44 | $.fn.cacheImages.storageAvailable( $(img), i, img, function(i, img){ |
45 | 45 | var $this = $(img), |
46 | | - src = $this.prop('src') || $this.data('cachedImageSrc'); |
47 | | - if( self.cacheImagesConfig.url !== null ){ // URL set in the opts |
48 | | - src = self.cacheImagesConfig.url; |
49 | | - $this.prop('src', ''); |
| 46 | + src; |
| 47 | + |
| 48 | + if( $this.prop("tagName") === 'img' ){ |
| 49 | + $this.data('cachedImageType', 'src'); |
| 50 | + |
| 51 | + var src = $this.prop('src') || $this.data('cachedImageSrc'); |
| 52 | + if( self.cacheImagesConfig.url !== null ){ // URL set in the opts |
| 53 | + src = self.cacheImagesConfig.url; |
| 54 | + $this.prop('src', ''); |
| 55 | + } |
| 56 | + }else{ |
| 57 | + $this.data('cachedImageType', 'css'); |
| 58 | + |
| 59 | + var src = $this.css('background-image').replace(/"/g,"").replace(/url\(|\)$/ig, "") || $this.data('cachedImageSrc'); |
| 60 | + if( self.cacheImagesConfig.url !== null ){ // URL set in the opts |
| 61 | + src = self.cacheImagesConfig.url; |
| 62 | + $this.css('background-image', 'url()'); |
| 63 | + } |
50 | 64 | } |
51 | 65 |
|
52 | 66 |
|
|
62 | 76 | if( localSrcEncoded && /^data:image/.test( localSrcEncoded ) ) { |
63 | 77 | // Check if the image has already been cached, if it has lets bounce out of here |
64 | 78 | this.data('cachedImageSrc', src); |
65 | | - this.prop('src', localSrcEncoded); |
| 79 | + if( this.data('cachedImageType') == 'src' ){ |
| 80 | + this.prop('src', localSrcEncoded ); |
| 81 | + }else{ |
| 82 | + this.css('background-image', 'url(' + localSrcEncoded + ')') |
| 83 | + } |
| 84 | + |
66 | 85 | self.cacheImagesConfig.done.call( this, localSrcEncoded ); |
67 | 86 | self.cacheImagesConfig.always.call( this ); |
68 | 87 | return; |
|
73 | 92 | return; // stop running |
74 | 93 | }else{ |
75 | 94 | // The image has not yet been cached, so we need to get on that. |
76 | | - this.prop('src', ''); // This will cancel the request if it hasn't already been finished |
| 95 | + |
| 96 | + if( this.data('cachedImageType') == 'src' ){ // This will cancel the request if it hasn't already been finished |
| 97 | + this.prop('src', '' ); |
| 98 | + }else{ |
| 99 | + this.css('background-image', 'url()'); |
| 100 | + } |
77 | 101 | var imgType = src.match(/\.(jpg|jpeg|png|gif)$/i); // Break out the filename to get the type |
78 | 102 | if( imgType && imgType.length){ // Get us the type of file |
79 | 103 | imgType = imgType[1].toLowerCase() == 'jpg' ? 'jpeg' : imgType[1].toLowerCase(); |
|
94 | 118 | newSrc = $.fn.cacheImages.base64EncodeCanvas( img ); // Process the image |
95 | 119 | $.fn.cacheImages.set( this, key, newSrc ); // Save the media |
96 | 120 | if( src.substring(0,5) === 'data:' ){ |
97 | | - this.prop('src', newSrc ); |
| 121 | + if( this.data('cachedImageType') == 'src' ){ |
| 122 | + this.prop('src', newSrc ); |
| 123 | + }else{ |
| 124 | + this.css('background-image', 'url(' + newSrc + ')') |
| 125 | + } |
| 126 | + |
98 | 127 | if( this.is('.cacheImagesRemove') ){ |
99 | 128 | this.remove(); |
100 | 129 | } |
|
115 | 144 | $.fn.cacheImages.set( thisElem, key, newSrc, function( key, encodedString ){ |
116 | 145 | // Default processing of the response |
117 | 146 | if( encodedString.length !== 0 && encodedString !== 'data:image/' + imgType + ';base64,'){ // it appended image data |
118 | | - this.prop('src', encodedString ); |
119 | | - if( this.is('.cacheImagesRemove') ){ |
120 | | - this.remove(); |
| 147 | + console.log( this, this.data('cachedImageType'), newSrc ); |
| 148 | + if( this.data('cachedImageType') == 'src' ){ |
| 149 | + this.prop('src', encodedString ); |
| 150 | + }else{ |
| 151 | + this.css('background-image', 'url(' + encodedString + ')') |
121 | 152 | } |
122 | 153 |
|
| 154 | + if( this.is('.cacheImagesRemove') ){ this.remove(); } |
123 | 155 | self.cacheImagesConfig.done.call( this, false ); |
124 | 156 | self.cacheImagesConfig.always.call( this ); |
125 | 157 | return; |
|
133 | 165 | this.cacheImages(self.cacheImagesConfig); |
134 | 166 | return; // stop progression |
135 | 167 | }else{ |
136 | | - this.prop('src', self.cacheImagesConfig.defaultImage ); |
| 168 | + if( this.data('cachedImageType') == 'src' ){ |
| 169 | + this.prop('src', self.cacheImagesConfig.defaultImage ); |
| 170 | + }else{ |
| 171 | + this.css('background-image', 'url(' + self.cacheImagesConfig.defaultImage + ')') |
| 172 | + } |
| 173 | + |
137 | 174 | self.cacheImagesConfig.fail.call( this ); |
138 | 175 | self.cacheImagesConfig.always.call( this ); |
139 | 176 | } |
|
0 commit comments