From f932ee79df9ad48a16822dc12518b234836ce926 Mon Sep 17 00:00:00 2001 From: mistic100 Date: Sat, 17 May 2014 12:11:47 +0000 Subject: feature 3077 : use Selectize with AJAX load/cache on picture_modify git-svn-id: http://piwigo.org/svn/trunk@28494 68402e56-0260-453c-a942-63ccdbb3a9ee --- admin/themes/default/js/LocalStorageCache.js | 48 ++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 admin/themes/default/js/LocalStorageCache.js (limited to 'admin/themes/default/js/LocalStorageCache.js') diff --git a/admin/themes/default/js/LocalStorageCache.js b/admin/themes/default/js/LocalStorageCache.js new file mode 100644 index 000000000..49a4fa98d --- /dev/null +++ b/admin/themes/default/js/LocalStorageCache.js @@ -0,0 +1,48 @@ +var LocalStorageCache = function(key, lifetime, loader) { + this.key = key; + this.lifetime = lifetime*1000; + this.loader = loader; + + this.storage = window.localStorage; + this.ready = !!this.storage; +}; + +LocalStorageCache.prototype.get = function(callback) { + var now = new Date().getTime(), + that = this; + + if (this.ready && this.storage[this.key] != undefined) { + var cache = JSON.parse(this.storage[this.key]); + + if (now - cache.timestamp <= this.lifetime) { + callback(cache.data); + return; + } + } + + this.loader(function(data) { + if (that.ready) { + that.storage[that.key] = JSON.stringify({ + timestamp: now, + data: data + }); + } + + callback(data); + }); +}; + +LocalStorageCache.prototype.set = function(data) { + if (this.ready) { + that.storage[that.key] = JSON.stringify({ + timestamp: new Date().getTime(), + data: data + }); + } +}; + +LocalStorageCache.prototype.clear = function() { + if (this.ready) { + this.storage.removeItem(this.key); + } +}; \ No newline at end of file -- cgit v1.2.3