|
32 | 32 |
|
33 | 33 | var self = this; |
34 | 34 |
|
| 35 | + |
| 36 | + /* |
| 37 | + * Ensure we have the default image cached and ready for use |
| 38 | + */ |
| 39 | + if( /^data:image/.test( window.cacheImagesConfig.defaultImage ) === false ){ |
| 40 | + window.cacheImagesConfig.defaultSrc = window.cacheImagesConfig.defaultImage; |
| 41 | + } |
| 42 | + |
| 43 | + |
35 | 44 | /* |
36 | 45 | * Takes the image and uses a canvas element to encode the media |
37 | 46 | * response | string | this is the raw XHR resposne data |
|
141 | 150 | $this.prop('src', localSrcEncoded); |
142 | 151 | }else{ |
143 | 152 | // The image has not yet been cached, so we need to get on that. |
144 | | - $this.prop('src', src); |
| 153 | + $this.prop('src', ''); // This will cancel the request if it hasn't already been finished |
145 | 154 | var imgType = src.match(/\.(jpg|jpeg|png|gif)$/i); // Break out the filename to get the type |
146 | 155 | if( imgType && imgType.length){ // Get us the type of file |
147 | 156 | imgType = imgType[1].toLowerCase() == 'jpg' ? 'jpeg' : imgType[1].toLowerCase(); |
|
158 | 167 | localStorage[key] = src = base64EncodeCanvas( img ); |
159 | 168 | if( src.substring(0,5) === 'data:' ){ |
160 | 169 | $this.prop('src', localStorage[key] ); |
161 | | - if( $this.data('cacheImagesRemove') === 'true' ){ |
| 170 | + if( $this.is('.cacheImagesRemove') ){ |
162 | 171 | $this.remove(); |
163 | 172 | } |
164 | 173 | } |
|
167 | 176 | var xhr = new XMLHttpRequest(); |
168 | 177 | xhr.open('GET', src, true); |
169 | 178 | xhr.responseType = 'arraybuffer'; // Cannot use the jQuery ajax method until it support this response type |
170 | | - xhr.onload = function( e ) { |
171 | | - if (this.status == 200){ |
| 179 | + xhr.onload = function( e ){ |
| 180 | + if (this.status == 200 && e.totalSize > 0 ){ |
172 | 181 | localStorage[key] = 'data:image/' + imgType + ';base64,' + base64EncodeResponse( this.response ); |
173 | 182 | $this.prop('src', localStorage[key] ); |
174 | | - if( $this.data('cacheImagesRemove') === 'true' ){ |
| 183 | + if( $this.is('.cacheImagesRemove') ){ |
175 | 184 | $this.remove(); |
176 | 185 | } |
177 | 186 | }else{ |
178 | 187 | localStorage[key] = 'error'; |
| 188 | + // Display the default image |
| 189 | + if( typeof window.cacheImagesConfig.defaultSrc !== 'undefined' ){ |
| 190 | + var defaultKey = window.cacheImagesConfig.storagePrefix + ':' + window.cacheImagesConfig.defaultSrc; |
| 191 | + if( typeof localStorage[defaultKey] !== 'undefined' ){ |
| 192 | + $this.prop('src', localStorage[defaultKey] ); |
| 193 | + }else{ |
| 194 | + $this.cacheImages({url: window.cacheImagesConfig.defaultSrc }); // Will cache it, and display it here |
| 195 | + } |
| 196 | + }else{ |
| 197 | + $this.prop('src', window.cacheImagesConfig.defaultImage ); |
| 198 | + } |
179 | 199 | } |
180 | 200 | }; |
181 | 201 | xhr.send(); |
|
191 | 211 | * Manually cache an image into the local storage |
192 | 212 | */ |
193 | 213 | window.cacheImageFetchURL = function( url ){ |
194 | | - $('body').append($('<img style="display: none;" />').prop('src', url ).data('cacheImagesRemove', 'true').cacheImages()); |
| 214 | + $('body').append( $('<img style="display: none;" />').addClass('cacheImagesRemove').cacheImages({url: url}) ); |
195 | 215 | }; |
196 | 216 | /* |
197 | 217 | * Will remove all of the cached images from their localStorage |
|
0 commit comments