Skip to content

Commit 01fece1

Browse files
committed
Update demo to work with localStorage or indexedDB
1 parent e107327 commit 01fece1

File tree

1 file changed

+48
-11
lines changed

1 file changed

+48
-11
lines changed

demo.html

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ <h2>Flickr Test</h2>
5050

5151
<script src="assets/jquery-1.9.1.js" type="text/javascript" charset="utf-8"></script>
5252
<script src="jquery.cacheImages.js" type="text/javascript" charset="utf-8"></script>
53-
<!-- <script src="jquery.cacheImages.indexeddb.js" type="text/javascript" charset="utf-8"></script> --><!-- Uncomment this to test indexedDB storage -->
53+
<!-- Uncomment the next line to test indexedDB storage -->
54+
<!-- <script src="jquery.cacheImages.indexeddb.js" type="text/javascript" charset="utf-8"></script> -->
5455
<script type="text/javascript">
5556
$.fn.cacheImages.defaults.debug = true;
56-
$.fn.cacheImages.defaults.defaultImage = 'assets/no-image.png';
57+
$.fn.cacheImages.defaults.defaultImage = window.location.origin + window.location.pathname.substr(0, window.location.pathname.length - 10) + '/assets/no-image.png';
58+
5759
// $.fn.cacheImages.defaults.always = function(){
5860
// console.log('finished running');
5961
// };
@@ -63,6 +65,7 @@ <h2>Flickr Test</h2>
6365
// $.fn.cacheImages.defaults.fail = function(){
6466
// console.log('Error Happened');
6567
// };
68+
6669
$(document).ready(function(){
6770
$('#DemoImages img').cacheImages();
6871
});
@@ -71,7 +74,10 @@ <h2>Flickr Test</h2>
7174
$(document).on('click','#dropCache', function(){ $.fn.cacheImages.drop(); });
7275
$(document).on('click','#cacheLoadAll', function(){ cacheImagesShowAll( $('#statWindow') ); });
7376
$(document).on('click','#corruptCat', function(){
74-
localStorage['cached:' + window.location.origin + window.location.pathname.substr(0, window.location.pathname.length - 10) + '/assets/cat.gif'] = 'data:image/gif;base64,';
77+
$.fn.cacheImages.set(
78+
this,
79+
'cached:' + window.location.origin + window.location.pathname.substr(0, window.location.pathname.length - 10) + '/assets/cat.gif',
80+
'data:image/gif;base64,totally not a valid image at all=' );
7581
});
7682
$(document).on('click','#FlickrTest', function(){
7783
if( $('#FlickrTags').val() === ''){
@@ -108,12 +114,37 @@ <h2>Flickr Test</h2>
108114
if( typeof storagePrefix === 'undefined' ){ storagePrefix = 'cached'; }
109115
var cacheKeys = []; // Store the keys we need to drop here
110116
// Lets get our loop on
111-
for (i = 0; i < window.localStorage.length; i++) {
112-
if( window.localStorage.key(i).substr( 0, storagePrefix.length + 1 ) !== storagePrefix + ':' ){ continue; } // Does not match our prefix?
113117

114-
cacheKeys.push( window.localStorage.key(i).substr( storagePrefix.length+1 ) ); // Droping the keys here re-indexes the localStorage so that the offset in our loop is wrong
118+
if( $.fn.cacheImages.defaults.storageDB == 'localStorage' ){
119+
// Using Local Storage
120+
for (i = 0; i < window.localStorage.length; i++) {
121+
if( window.localStorage.key(i).substr( 0, storagePrefix.length + 1 ) !== storagePrefix + ':' ){ continue; } // Does not match our prefix?
122+
123+
cacheKeys.push( window.localStorage.key(i).substr( storagePrefix.length+1 ) ); // Droping the keys here re-indexes the localStorage so that the offset in our loop is wrong
124+
}
125+
126+
cacheImagesDislayAll( container, cacheKeys );
127+
}else{
128+
// Using IndexedDB
129+
130+
var request = window.cacheImagesDb.transaction("offlineImages", "readonly").objectStore("offlineImages").openCursor();
131+
request.onsuccess = function(e) {
132+
var cursor = e.target.result;
133+
if (cursor) {
134+
// Called for each matching record.
135+
if( cursor.value.key.substr( 0,storagePrefix.length + 1 ) === storagePrefix + ':' ){ // Does not match our prefix?
136+
cacheKeys.push( cursor.value.key.substr( storagePrefix.length+1 ) );
137+
}
138+
cursor.continue();
139+
}else{
140+
cacheImagesDislayAll( container, cacheKeys );
141+
}
142+
};
115143
}
116144

145+
return true;
146+
},
147+
cacheImagesDislayAll = function( container, cacheKeys ){
117148
if( cacheKeys.length === 0 ){
118149
if( window.debug ){ console.log( 'No Images to Display' ); }
119150
return true;
@@ -125,14 +156,20 @@ <h2>Flickr Test</h2>
125156
if( window.localStorage.getItem( cacheKeys[i] ) === 'pending' ){ continue; }
126157
if( window.localStorage.getItem( cacheKeys[i] ) === 'error' ){ continue; }
127158

128-
$.fn.cacheImages.Output( cacheKeys[i], function( image ){
129-
// console.log( image.substr(0, 50) );
130-
container.append( $('<img />').prop( 'src', image ).height('175px') );
131-
});
159+
if( true ){
160+
// Display using the cacheImages native function [Preferred]
161+
container.append( $('<img />').height('175px').cacheImages({ url: cacheKeys[i] }));
162+
}else{
163+
// Display using teh cacheImages Output() function via the callback.
164+
$.fn.cacheImages.Output( cacheKeys[i], function( image ){
165+
// console.log( image.substr(0, 50) );
166+
container.append( $('<img />').prop( 'src', image ).height('175px') );
167+
});
168+
}
132169
}
133170

134171
return true;
135-
};
172+
}
136173
</script>
137174
</body>
138175
</html>

0 commit comments

Comments
 (0)