|
22 | 22 | window.cacheImagesConfig = $.extend({}, { |
23 | 23 | debug: true, // Boolean value to enable or disable some of the console messaging for trouble shooting |
24 | 24 | defaultImage: 'data:image/png;base64,/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAHgAA/+4ADkFkb2JlAGTAAAAAAf/bAIQAEAsLCwwLEAwMEBcPDQ8XGxQQEBQbHxcXFxcXHx4XGhoaGhceHiMlJyUjHi8vMzMvL0BAQEBAQEBAQEBAQEBAQAERDw8RExEVEhIVFBEUERQaFBYWFBomGhocGhomMCMeHh4eIzArLicnJy4rNTUwMDU1QEA/QEBAQEBAQEBAQEBA/8AAEQgAZABkAwEiAAIRAQMRAf/EAEsAAQEAAAAAAAAAAAAAAAAAAAAFAQEAAAAAAAAAAAAAAAAAAAAAEAEAAAAAAAAAAAAAAAAAAAAAEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwCwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD//2Q==', // URL or base64 string for the default image (will obviously get cached) - default is at assets/default.jpg |
25 | | - storagePrefix: 'cached' // Used to prefix the URL in at localStorage key |
| 25 | + encodeOnCanvas: false, // This is still experimental and should be disabled in production |
| 26 | + storagePrefix: 'cached', // Used to prefix the URL in at localStorage key |
| 27 | + url: null // Allows you to directly set the url for an element |
26 | 28 | }, opts); |
27 | 29 |
|
28 | 30 | // Check for canvas support |
29 | | - window.cacheImagesConfig.canvasEncoder = typeof HTMLCanvasElement != undefined ? window.cacheImagesConfig.canvasEncoder : false; |
| 31 | + window.cacheImagesConfig.encodeOnCanvas = typeof HTMLCanvasElement != undefined ? window.cacheImagesConfig.encodeOnCanvas : false; |
30 | 32 |
|
31 | 33 | var self = this; |
32 | 34 |
|
|
36 | 38 | * filename | string | this is the url accessed/filename, it's needed so that we can parse out the type of image for mimetyping |
37 | 39 | * Code base heavily on Encoding XHR image data by @mathias - http://jsperf.com/encoding-xhr-image-data/33 |
38 | 40 | */ |
39 | | - var getBase64Image = function( img ){ |
| 41 | + var base64EncodeCanvas = function( img ){ |
40 | 42 | try { |
41 | | - var canvas = document.createElement('canvas'), |
42 | | - imgType = img.src.match(/\.(jpg|jpeg|png)$/i); |
| 43 | + var canvas = document.createElement('canvas'); |
43 | 44 | canvas.width = img.width; |
44 | 45 | canvas.height = img.height; |
45 | 46 |
|
46 | | - if (imgType && imgType.length) { |
| 47 | + var ctx = canvas.getContext('2d'); |
| 48 | + ctx.drawImage(img, 0, 0); |
| 49 | + |
| 50 | + var imgType = img.src.match(/\.(jpg|jpeg|png)$/i); |
| 51 | + if( imgType && imgType.length ) { |
47 | 52 | imgType = imgType[1].toLowerCase() == 'jpg' ? 'jpeg' : imgType[1].toLowerCase(); |
48 | 53 | } else { |
49 | 54 | throw 'Invalid image type for canvas encoder: ' + img.src; |
50 | 55 | } |
51 | 56 |
|
52 | | - var ctx = canvas.getContext('2d'); |
53 | | - ctx.drawImage(img, 0, 0); |
54 | | - |
55 | 57 | return canvas.toDataURL('image/' + imgType); |
56 | 58 | } catch (e) { |
57 | 59 | console && console.log(e); |
|
107 | 109 | return base64; |
108 | 110 | }; |
109 | 111 |
|
110 | | - $(document).ready(function () { |
| 112 | + /* |
| 113 | + * Here is the magic, this is the function that processes through the caching of the images |
| 114 | + */ |
| 115 | + $(function () { |
111 | 116 | // console.log( $(self) ); |
112 | 117 | $(self).each(function (i, img) { |
113 | 118 | var $this = $(img), |
114 | | - src = $this.prop('src') || $this.data('cachedImageSrc'), |
115 | | - key = window.cacheImagesConfig.storagePrefix + ':' + src, // Prepare the localStorage key |
| 119 | + src = $this.prop('src') || $this.data('cachedImageSrc'); |
| 120 | + if( window.cacheImagesConfig.url !== null ){ // URL set in the opts |
| 121 | + src = window.cacheImagesConfig.url; |
| 122 | + $this.prop('src', ''); |
| 123 | + } |
| 124 | + |
| 125 | + var key = window.cacheImagesConfig.storagePrefix + ':' + src, // Prepare the localStorage key |
116 | 126 | localSrcEncoded = localStorage[key]; |
117 | 127 |
|
118 | 128 | if( typeof localStorage !== "object" ){ // See if local storage is working |
119 | 129 | if( window.cacheImagesConfig.debug ){ console.log("localStorage is not available"); } |
120 | | - return false; |
| 130 | + return false; // Unable to cache, so stop looping |
121 | 131 | } |
122 | 132 |
|
123 | | - if( typeof src === 'undefined' ){ return false; } |
124 | | - |
125 | | - // Check if we need to |
| 133 | + if( typeof src === 'undefined' ){ return true; } // Move to the next item |
| 134 | + if( typeof $this.prop('src') !== 'undefined' && $this.prop('src').substring(0,5) === 'data:' ){ return true; } // This has already been converted |
126 | 135 |
|
127 | | - window.cacheImagesConfig.canvasEncoder = typeof HTMLCanvasElement != undefined ? true : false; |
| 136 | + |
128 | 137 |
|
129 | | - if( src.substring(0,5) === 'data:' ){ return true; } // This has already been converted |
130 | 138 | if( localSrcEncoded && /^data:image/.test( localSrcEncoded ) ) { |
131 | 139 | // Check if the image has already been cached, if it has lets bounce out of here |
| 140 | + $this.data('cachedImageSrc', src); |
132 | 141 | $this.prop('src', localSrcEncoded); |
133 | 142 | }else{ |
134 | 143 | // The image has not yet been cached, so we need to get on that. |
135 | 144 | $this.prop('src', src); |
136 | 145 | var imgType = src.match(/\.(jpg|jpeg|png|gif)$/i); // Break out the filename to get the type |
137 | | - if (imgType && imgType.length){ // Get us the type of file |
| 146 | + if( imgType && imgType.length){ // Get us the type of file |
138 | 147 | imgType = imgType[1].toLowerCase() == 'jpg' ? 'jpeg' : imgType[1].toLowerCase(); |
139 | 148 | } |
| 149 | + if( typeof imgType === 'undefined' ){ return true; } |
| 150 | + |
140 | 151 |
|
141 | 152 | if( localStorage[key] !== 'pending' ){ |
142 | 153 | localStorage[key] = 'pending'; |
| 154 | + $this.data('cachedImageSrc', src); |
143 | 155 |
|
144 | | - window.cacheImagesConfig.canvasEncoder = typeof HTMLCanvasElement != undefined ? true : false; |
145 | | - if( window.cacheImagesConfig.canvasEncoder && imgType !== 'gif' ){ |
| 156 | + if( window.cacheImagesConfig.encodeOnCanvas && imgType !== 'gif' ){ |
146 | 157 | $this.load(function () { |
147 | | - localStorage[key] = getBase64Image(img); |
| 158 | + localStorage[key] = src = base64EncodeCanvas( img ); |
148 | 159 | if( src.substring(0,5) === 'data:' ){ |
149 | 160 | $this.prop('src', localStorage[key] ); |
| 161 | + if( $this.data('cacheImagesRemove') === 'true' ){ |
| 162 | + $this.remove(); |
| 163 | + } |
150 | 164 | } |
151 | 165 | }); |
152 | 166 | }else{ |
|
156 | 170 | xhr.onload = function( e ) { |
157 | 171 | if (this.status == 200){ |
158 | 172 | localStorage[key] = 'data:image/' + imgType + ';base64,' + base64EncodeResponse( this.response ); |
| 173 | + $this.prop('src', localStorage[key] ); |
| 174 | + if( $this.data('cacheImagesRemove') === 'true' ){ |
| 175 | + $this.remove(); |
| 176 | + } |
159 | 177 | }else{ |
160 | 178 | localStorage[key] = 'error'; |
161 | 179 | } |
162 | 180 | }; |
163 | 181 | xhr.send(); |
164 | | - } |
| 182 | + } |
165 | 183 | } |
166 | 184 | } |
167 | 185 | }); |
168 | 186 | }); |
169 | | - |
| 187 | + |
170 | 188 | return this; |
171 | 189 | }; |
172 | | - /* |
173 | | - * Will place the cached image into the element. |
174 | | - */ |
175 | | - window.cacheImagesDisplay = function( elem, src ){ |
176 | | - console.log(elem); |
177 | | - console.log(src); |
178 | | - if( typeof elem !== "object" && typeof elem.context.tagName !== 'undefined'){ // Ensure that the element a valid DOM element |
179 | | - if( window.cacheImagesConfig.debug ){ console.log('The elem variable passed was not valid'); } |
180 | | - return false; |
181 | | - } |
182 | | - if( typeof localStorage !== "object" ){ |
183 | | - if( window.cacheImagesConfig.debug ){console.log("localStorage is not available"); } |
184 | | - return false; |
185 | | - } |
186 | | - |
187 | | - if( typeof src === 'undefined' ){ |
188 | | - if( typeof elem.prop('src') === 'string' ){ |
189 | | - src = elem.prop('src'); // use the elements SRC as the source |
190 | | - }else{ |
191 | | - if( window.cacheImagesConfig.debug ){ console.log('No source to fetch'); } |
192 | | - return false; |
193 | | - } |
194 | | - } |
195 | | - |
196 | | - // Check if it has already been cached |
197 | | - var key = window.cacheImagesConfig.storagePrefix + ':' + src; |
198 | | - if( src.substring(0,5) === 'data:' ){ return elem; } // This has already been converted |
199 | | - |
200 | | - // |
201 | | - // Lets cache it up |
202 | | - var resposne = cacheImagesFetchURL( src ); |
203 | | - |
204 | | - |
205 | | - return elem; |
206 | | - }, |
207 | 190 | /* |
208 | 191 | * Manually cache an image into the local storage |
209 | 192 | */ |
210 | 193 | window.cacheImageFetchURL = function( url ){ |
211 | | - $('body').append($('<img style="display: none;" />').prop('src', url ).cacheImages() ); |
212 | | - }, |
| 194 | + $('body').append($('<img style="display: none;" />').prop('src', url ).data('cacheImagesRemove', 'true').cacheImages()); |
| 195 | + }; |
213 | 196 | /* |
214 | 197 | * Will remove all of the cached images from their localStorage |
215 | 198 | */ |
|
0 commit comments