| 1 |
define([ |
| 2 |
"dojo/_base/declare", |
| 3 |
"dojox/storage/Provider", |
| 4 |
"dojox/storage/manager", |
| 5 |
"dojo/_base/array", |
| 6 |
"dojo/_base/lang", |
| 7 |
"dojo/json" |
| 8 |
], function(declare, Provider, storageManager, array, lang, JSON){ |
| 9 |
var LocalStorageProvider = declare("dojox.storage.LocalStorageProvider", [Provider], { |
| 10 |
store: null, |
| 11 |
initialize: function(){ |
| 12 |
|
| 13 |
this.store = localStorage; |
| 14 |
|
| 15 |
this.initialized = true; |
| 16 |
storageManager.loaded(); |
| 17 |
}, |
| 18 |
|
| 19 |
isAvailable: function(){ |
| 20 |
return typeof localStorage != "undefined"; |
| 21 |
}, |
| 22 |
|
| 23 |
put: function( key, |
| 24 |
value, |
| 25 |
resultsHandler, |
| 26 |
namespace){ |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
this._assertIsValidKey(key); |
| 32 |
|
| 33 |
namespace = namespace||this.DEFAULT_NAMESPACE; |
| 34 |
this._assertIsValidNamespace(namespace); |
| 35 |
|
| 36 |
var fullKey = this.getFullKey(key,namespace); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
value = JSON.stringify(value); |
| 43 |
|
| 44 |
try { |
| 45 |
this.store.setItem(fullKey,value); |
| 46 |
|
| 47 |
if(resultsHandler){ |
| 48 |
resultsHandler(this.SUCCESS, key, null, namespace); |
| 49 |
} |
| 50 |
} catch(e) { |
| 51 |
if(resultsHandler){ |
| 52 |
resultsHandler(this.FAILED, key, e.toString(), namespace); |
| 53 |
} |
| 54 |
} |
| 55 |
}, |
| 56 |
|
| 57 |
get: function( key, namespace){ |
| 58 |
this._assertIsValidKey(key); |
| 59 |
|
| 60 |
namespace = namespace||this.DEFAULT_NAMESPACE; |
| 61 |
this._assertIsValidNamespace(namespace); |
| 62 |
|
| 63 |
|
| 64 |
key = this.getFullKey(key, namespace); |
| 65 |
|
| 66 |
return JSON.parse(this.store.getItem(key)); |
| 67 |
}, |
| 68 |
|
| 69 |
getKeys: function( namespace){ |
| 70 |
namespace = namespace||this.DEFAULT_NAMESPACE; |
| 71 |
this._assertIsValidNamespace(namespace); |
| 72 |
|
| 73 |
namespace = '__'+namespace+'_' |
| 74 |
|
| 75 |
var keys = []; |
| 76 |
for(var i = 0; i < this.store.length; i++){ |
| 77 |
var currentKey = this.store.key(i); |
| 78 |
if(this._beginsWith(currentKey,namespace)){ |
| 79 |
currentKey = currentKey.substring(namespace.length); |
| 80 |
keys.push(currentKey); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
return keys; |
| 85 |
}, |
| 86 |
|
| 87 |
clear: function( namespace){ |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
namespace = namespace||this.DEFAULT_NAMESPACE; |
| 95 |
this._assertIsValidNamespace(namespace); |
| 96 |
|
| 97 |
namespace = '__'+namespace+'_'; |
| 98 |
|
| 99 |
var keys = []; |
| 100 |
for(var i = 0; i < this.store.length; i++){ |
| 101 |
if(this._beginsWith(this.store.key(i),namespace)){ |
| 102 |
keys.push(this.store.key(i)); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
array.forEach(keys, lang.hitch(this.store, "removeItem")); |
| 107 |
}, |
| 108 |
|
| 109 |
remove: function( key, namespace){ |
| 110 |
namespace = namespace||this.DEFAULT_NAMESPACE; |
| 111 |
this._assertIsValidNamespace(namespace); |
| 112 |
|
| 113 |
this.store.removeItem(this.getFullKey(key, namespace)); |
| 114 |
}, |
| 115 |
|
| 116 |
getNamespaces: function(){ |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
var results = [ this.DEFAULT_NAMESPACE]; |
| 122 |
|
| 123 |
var found = {}; |
| 124 |
found[this.DEFAULT_NAMESPACE] = true; |
| 125 |
var tester = /^__([^_]*)_/; |
| 126 |
|
| 127 |
for(var i = 0; i < this.store.length; i++){ |
| 128 |
var currentKey = this.store.key(i); |
| 129 |
if(tester.test(currentKey) == true){ |
| 130 |
var currentNS = currentKey.match(tester)[1]; |
| 131 |
if(typeof found[currentNS] == "undefined"){ |
| 132 |
found[currentNS] = true; |
| 133 |
results.push(currentNS); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
return results; |
| 139 |
}, |
| 140 |
|
| 141 |
isPermanent: function(){ |
| 142 |
return true; |
| 143 |
}, |
| 144 |
|
| 145 |
getMaximumSize: function(){ |
| 146 |
return dojox.storage.SIZE_NO_LIMIT; |
| 147 |
}, |
| 148 |
|
| 149 |
hasSettingsUI: function(){ |
| 150 |
return false; |
| 151 |
}, |
| 152 |
|
| 153 |
isValidKey: function( keyName){ |
| 154 |
if(keyName === null || keyName === undefined){ |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
return /^[0-9A-Za-z_-]*$/.test(keyName); |
| 159 |
}, |
| 160 |
|
| 161 |
isValidNamespace: function( keyName){ |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
if(keyName === null || keyName === undefined){ |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
return /^[0-9A-Za-z-]*$/.test(keyName); |
| 175 |
}, |
| 176 |
|
| 177 |
getFullKey: function(key, namespace){ |
| 178 |
|
| 179 |
|
| 180 |
return "__" + namespace + "_" + key; |
| 181 |
}, |
| 182 |
|
| 183 |
_beginsWith: function( haystack, needle) { |
| 184 |
if(needle.length > haystack.length) { |
| 185 |
return false; |
| 186 |
} |
| 187 |
return haystack.substring(0,needle.length) === needle; |
| 188 |
}, |
| 189 |
|
| 190 |
_assertIsValidNamespace: function( namespace){ |
| 191 |
if(this.isValidNamespace(namespace) === false){ |
| 192 |
throw new Error("Invalid namespace given: " + namespace); |
| 193 |
} |
| 194 |
}, |
| 195 |
|
| 196 |
_assertIsValidKey: function( key){ |
| 197 |
if(this.isValidKey(key) === false){ |
| 198 |
throw new Error("Invalid key given: " + key); |
| 199 |
} |
| 200 |
} |
| 201 |
}); |
| 202 |
storageManager.register("dojox.storage.LocalStorageProvider", new LocalStorageProvider()); |
| 203 |
return LocalStorageProvider; |
| 204 |
}); |