Skip to content

Commit d754686

Browse files
committed
Update indexedDB
cleaned up the script and made a minified version of it.
1 parent 2e9706e commit d754686

File tree

4 files changed

+19
-31
lines changed

4 files changed

+19
-31
lines changed

cacheImages.jquery.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"offline"
3131
],
3232
"homepage": "http://freshvine.github.io/jQuery-cache-images/",
33-
"docs": "https://github.com/FreshVine/jQuery-cache-images",
33+
"docs": "https://github.com/FreshVine/jQuery-cache-images/",
3434
"demo": "http://freshvine.github.io/jQuery-cache-images/demo.html",
3535
"bugs": "https://github.com/FreshVine/jQuery-cache-images/issues"
3636
}

demo.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ <h2>Flickr Test</h2>
5656
// $.fn.cacheImages.defaults.always = function(){
5757
// console.log('finished running');
5858
// };
59-
// $.fn.cacheImages.defaults.done = function(){
59+
// $.fn.cacheImages.defaults.done = function( image ){
6060
// console.log('Finished Correctly');
6161
// };
6262
// $.fn.cacheImages.defaults.fail = function(){
@@ -110,7 +110,7 @@ <h2>Flickr Test</h2>
110110
}
111111

112112
if( cacheKeys.length === 0 ){
113-
if( window.debug ){ console.log( 'No Images to Drop' ); }
113+
if( window.debug ){ console.log( 'No Images to Display' ); }
114114
return true;
115115
}
116116

jquery.cacheImages.indexeddb.js

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,27 @@ window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndex
5757

5858
if( $.fn.cacheImages.defaults.indexedDbStatus === true ){
5959
callbackFunction.call( thisElem, i, img ); // This is the structure to use for our callbacks
60+
if( $.fn.cacheImages.defaults.debug ){ console.log('indexedDB already ready'); }
6061
return;
6162
}
6263

6364
var intervalCount = 0,
6465
thisInterval = setInterval(function(){
6566
intervalCount++;
6667
if( $.fn.cacheImages.defaults.indexedDbStatus === true ){
67-
if( $.fn.cacheImages.defaults.debug ){ console.log('indexedDB ready to use'); }
68+
if( $.fn.cacheImages.defaults.debug ){ console.log('indexedDB ready to use', 'indexedDB took ' + (intervalCount*50) + 'ms to conenct'); }
6869
callbackFunction.call( thisElem, i, img ); // This is the structure to use for our callbacks
6970
clearInterval(thisInterval); return;
7071
}
71-
if( intervalCount >= 41 ){ if( $.fn.cacheImages.defaults.debug ){ console.log('indexedDB did not load'); } clearInterval(thisInterval); return; } // only run for 2 seconds max
72+
if( intervalCount >= 41 ){ // only run for 2 seconds max - if it isn't available then there are other issues at play
73+
if( $.fn.cacheImages.defaults.debug ){ console.log('indexedDB did not load'); }
74+
clearInterval(thisInterval);
75+
return;
76+
}
7277

7378
if( $.fn.cacheImages.defaults.debug ){ console.log('indexedDB not yet available'); }
7479
}, 50 );
75-
76-
// if( $.fn.cacheImages.defaults.ready === false ){}
77-
// setTimeout(function(){ return 10; }, 1500);
78-
return $.fn.cacheImages.defaults.indexedDbStatus;
80+
return;
7981
};
8082
/*
8183
* Saves the encoded image data into the storage tool for the provided key
@@ -90,7 +92,7 @@ window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndex
9092
if( typeof callbackFunction === 'function' ){
9193
callbackFunction.call( thisElem, key, encodedString ); // This is the structure to use for our callbacks
9294
}
93-
};
95+
}
9496
request.onerror = function(e){
9597
self.cacheImagesConfig.fail.call( this );
9698
self.cacheImagesConfig.always.call( this );
@@ -104,7 +106,9 @@ window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndex
104106
var tmp = window.cacheImagesDb.transaction("offlineImages",'readonly').objectStore("offlineImages").get(key);
105107
tmp.onsuccess = function(e){
106108
encodedString = '';
107-
if( typeof e.target.result !== 'undefined' && typeof e.target.result.image === 'string' ){ encodedString = e.target.result.image; }
109+
if( typeof e.target.result !== 'undefined' && typeof e.target.result.image === 'string' ){
110+
encodedString = e.target.result.image;
111+
}
108112

109113
if( typeof callbackFunction === 'function' ){
110114
callbackFunction.call( thisElem, key, encodedString ); // This is the structure to use for our callbacks
@@ -134,10 +138,8 @@ window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndex
134138
if( /^data:image/.test( $.fn.cacheImages.defaults.defaultImage ) === true ){
135139
image = $.fn.cacheImages.defaults.defaultImage; // this is an encoded string
136140
}else{
137-
tempKey = storagePrefix + ':' + this.cacheImagesConfig.defaultImage;
138-
if( window.localStorage.getItem( tempKey ) != null ){
139-
return window.localStorage.getItem( tempKey ); // Default URL was already cached
140-
}
141+
$.fn.cacheImages.Output( $.fn.cacheImages.defaults.defaultImage, storagePrefix, callbackFunction ); // pass the callback through
142+
return;
141143
}
142144
}
143145

@@ -146,29 +148,14 @@ window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndex
146148
}
147149
}
148150

149-
150-
// if( window.localStorage.getItem( tempKey ) != null ){
151-
// return window.localStorage.getItem( tempKey ); // Image exists in the cache
152-
// }else{
153-
//
154-
// if( /^data:image/.test( $.fn.cacheImages.defaults.defaultImage ) === true ){
155-
// return this.cacheImagesConfig.defaultImage; // this is an encoded string
156-
// }
157-
//
158-
// tempKey = storagePrefix + ':' + this.cacheImagesConfig.defaultImage;
159-
// if( window.localStorage.getItem( tempKey ) != null ){
160-
// return window.localStorage.getItem( tempKey ); // Default URL was already cached
161-
// }
162-
// }
163-
164151
return null; // Neither the image or the cached version existsed
165152
};
166153
/*
167154
* Will remove all of the cached images from their localStorage
168155
*/
169156
$.fn.cacheImages.drop = function( storagePrefix ){
170157
var dropKeys = [], // Store the keys we need to drop here
171-
debug = true;
158+
debug = false;
172159
if( typeof storagePrefix === 'undefined' ){ storagePrefix = $.fn.cacheImages.defaults.storagePrefix; }
173160

174161
var request = window.cacheImagesDb.transaction("offlineImages", "readonly").objectStore("offlineImages").openCursor();

jquery.cacheImages.indexeddb.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)