-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.cacheImages.indexeddb.min.js
1 lines (1 loc) · 5.07 KB
/
jquery.cacheImages.indexeddb.min.js
1
window.indexedDB=window.indexedDB||window.mozIndexedDB||window.webkitIndexedDB||window.msIndexedDB,$.fn.cacheImages.defaults.storageDB="indexedDB",$.fn.cacheImages.defaults.ready=!1,$.fn.cacheImages.defaults.debug&&console.log("FV.cacheImages: Using indexedDB ");var cacheImagesDb;"undefined"==typeof $.fn.cacheImages.defaults.indexedDbName&&($.fn.cacheImages.defaults.indexedDbName="cacheImages"),$.fn.cacheImages.dbRequest=window.indexedDB.open($.fn.cacheImages.defaults.indexedDbName,1),$.fn.cacheImages.defaults.indexedDbStatus=!1,$.fn.cacheImages.dbRequest.onerror=function(a){$.fn.cacheImages.defaults.indexedDbStatus=!1,$.fn.cacheImages.defaults.debug&&console.log("FV.cacheImages: Why didn't you allow my web app to use IndexedDB?!")},$.fn.cacheImages.dbRequest.onsuccess=function(a){$.fn.cacheImages.defaults.debug&&console.log("FV.cacheImages: IndexedDB open success. It is ready"),$.fn.cacheImages.defaults.ready=$.fn.cacheImages.defaults.indexedDbStatus=!0,window.cacheImagesDb=a.target.result,window.cacheImagesDb.onerror=function(a){$.fn.cacheImages.defaults.debug&&console.log("IndexedDB error: "+a.target.errorCode)}},$.fn.cacheImages.dbRequest.onupgradeneeded=function(a){$.fn.cacheImages.defaults.debug&&console.log("FV.cacheImages: running onupgradeneeded");var b=a.target.result;if(!b.objectStoreNames.contains("offlineImages")){$.fn.cacheImages.defaults.debug&&console.log("FV.cacheImages: making the offlineImages objectstore");var c=b.createObjectStore("offlineImages",{keyPath:"key"});c.createIndex("by_key","key",{unique:!0})}},$.fn.cacheImages.storageAvailable=function(a,b,c,d){if($.fn.cacheImages.defaults.debug&&console.log("FV.cacheImages: indexedDB availability check"),$.fn.cacheImages.defaults.indexedDbStatus===!0)return d.call(a,b,c),void($.fn.cacheImages.defaults.debug&&console.log("FV.cacheImages: indexedDB already ready"));var e=0,f=setInterval(function(){return e++,$.fn.cacheImages.defaults.indexedDbStatus===!0?($.fn.cacheImages.defaults.debug&&console.log("FV.cacheImages: indexedDB ready to use","indexedDB took "+50*e+"ms to conenct"),d.call(a,b,c),void clearInterval(f)):e>=41?($.fn.cacheImages.defaults.debug&&console.log("FV.cacheImages: indexedDB did not load"),void clearInterval(f)):void($.fn.cacheImages.defaults.debug&&console.log("FV.cacheImages: indexedDB not yet available"))},50)},$.fn.cacheImages.set=function(a,b,c,d){var e=window.cacheImagesDb.transaction("offlineImages","readwrite").objectStore("offlineImages"),f={key:b,image:c},g=e.put(f);g.onsuccess=function(e){"function"==typeof d&&d.call(a,b,c)},g.onerror=function(a){self.cacheImagesConfig.fail.call(this),self.cacheImagesConfig.always.call(this)}},$.fn.cacheImages.get=function(a,b,c){var d=window.cacheImagesDb.transaction("offlineImages","readonly").objectStore("offlineImages").get(b);d.onsuccess=function(d){encodedString="","undefined"!=typeof d.target.result&&"string"==typeof d.target.result.image&&(encodedString=d.target.result.image),"function"==typeof c&&c.call(a,b,encodedString)},d.onerror=function(a){self.cacheImagesConfig.fail.call(this),self.cacheImagesConfig.always.call(this)}},$.fn.cacheImages.Output=function(a,b,c,d){"undefined"!=typeof c&&"null"!=typeof c||(c=$.fn.cacheImages.defaults.storagePrefix),"boolean"!=typeof d&&(d=!1);var e=c+":"+a,g=window.cacheImagesDb.transaction("offlineImages","readonly").objectStore("offlineImages").get(e);return g.onsuccess=function(e){var f="";if("undefined"!=typeof e.target.result&&"string"==typeof e.target.result.image&&(f=e.target.result.image,0==$.fn.cacheImages.testOutput(f,!0)&&(delete f,0==d)))return void $("body").append($('<img style="display: none;" />').addClass("cacheImagesRemove").cacheImages({url:a,forceSave:!0,storagePrefix:c,done:function(d){"function"==typeof b&&$.fn.cacheImages.Output(a,b,c,!0)}}));if(0==$.fn.cacheImages.testOutput(f,!0)){if($.fn.cacheImages.defaults.debug&&console.log("FV.cacheImage.Output: Failed to load image "+a),!$.fn.cacheImages.testOutput($.fn.cacheImages.defaults.defaultImage,!0))return void $.fn.cacheImages.Output($.fn.cacheImages.defaults.defaultImage,b,c);f=$.fn.cacheImages.defaults.defaultImage}"function"==typeof b&&b.call(this,f)},null},$.fn.cacheImages.drop=function(a,b,c){var d=[];"undefined"==typeof c&&(c=$.fn.cacheImages.defaults.storagePrefix),"undefined"==typeof a&&(a=null);var e=window.cacheImagesDb.transaction("offlineImages","readonly").objectStore("offlineImages").openCursor();e.onsuccess=function(e){var f=e.target.result;if(!f){if($.fn.cacheImages.defaults.debug&&console.log("FV.cacheImages.drop: No more matching records"),0===d.length)$.fn.cacheImages.defaults.debug&&console.log("FV.cacheImages.drop: No Images to Drop");else{for(i=0;i<d.length;i++)$.fn.cacheImages.defaults.debug&&console.log("FV.cacheImages.drop: Dropping localStorage Key:",d[i]),window.cacheImagesDb.transaction("offlineImages","readwrite").objectStore("offlineImages").delete(d[i]);$.fn.cacheImages.defaults.debug&&console.log("FV.cacheImages.drop: Dropped "+d.length+" images from indexedDB")}return"function"==typeof b&&b.call(this,a),!0}f.value.key.substr(0,c.length+1)===c+":"&&(null!==a&&f.value.key!==c+":"+a||d.push(f.value.key)),f.continue()}};