summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorhallgren <hallgren@chalmers.se>2013-04-03 20:23:32 +0000
committerhallgren <hallgren@chalmers.se>2013-04-03 20:23:32 +0000
commit0568a1a32a777c234ec044d56d46d5d4b4c0e760 (patch)
treece21ad6b330a8f0e039e8014c3a84c3250a4123e /src
parentc02d5b188b82856eeb9b9bad16646723f27d822d (diff)
Adding src/www/js/localstorage.js
A common interface to localStorage, to store JSON data under a unique prefix.
Diffstat (limited to 'src')
-rw-r--r--src/www/js/localstorage.js52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/www/js/localstorage.js b/src/www/js/localstorage.js
new file mode 100644
index 000000000..c103c28bd
--- /dev/null
+++ b/src/www/js/localstorage.js
@@ -0,0 +1,52 @@
+
+
+// See http://diveintohtml5.info/storage.html
+
+function supports_html5_storage() {
+ try {
+ return 'localStorage' in window && window['localStorage'] !== null;
+ } catch (e) {
+ return false;
+ }
+}
+
+// An interface to localStorage to store JSON data under a unique prefix
+function appLocalStorage(appPrefix,fakeIt) {
+
+ function methods(storage) {
+ return {
+ get: function (name,def) {
+ var id=appPrefix+name
+ return storage[id] ? JSON.parse(storage[id]) : def;
+ },
+ put: function (name,value) {
+ var id=appPrefix+name;
+ storage[id]=JSON.stringify(value);
+ },
+ remove: function(name) {
+ var id=appPrefix+name;
+ delete storage[id]
+ },
+ ls: function(prefix) {
+ var pre=appPrefix+prefix
+ var files=[]
+ for(var i in storage)
+ if(hasPrefix(i,pre)) files.push(i.substr(pre.length))
+ files.sort()
+ return files
+ },
+ get count() { return this.get("count",0); },
+ set count(v) { this.put("count",v); }
+ }
+ }
+
+ function get_html5_storage() {
+ try {
+ return 'localStorage' in window && window['localStorage'] || []
+ } catch (e) {
+ return []; // fake it
+ }
+ }
+
+ return methods(fakeIt || get_html5_storage())
+}