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