| 1 |
dojo.provide("dojox.storage.BehaviorStorageProvider"); |
| 2 |
|
| 3 |
dojo.require("dojox.storage.Provider"); |
| 4 |
dojo.require("dojox.storage.manager"); |
| 5 |
|
| 6 |
dojo.declare( |
| 7 |
"dojox.storage.BehaviorStorageProvider", |
| 8 |
[dojox.storage.Provider], |
| 9 |
{ |
| 10 |
store: null, |
| 11 |
|
| 12 |
storeName: '__dojox_BehaviorStorage', |
| 13 |
|
| 14 |
keys: [], |
| 15 |
|
| 16 |
initialize: function(){ |
| 17 |
try{ |
| 18 |
this.store = this._createStore(); |
| 19 |
this.store.load(this.storeName); |
| 20 |
}catch(e){ |
| 21 |
throw new Error("Store is not available: " + e); |
| 22 |
} |
| 23 |
|
| 24 |
var keys = this.get('keys','dojoxSystemNS'); |
| 25 |
this.keys = keys || []; |
| 26 |
|
| 27 |
this.initialized = true; |
| 28 |
dojox.storage.manager.loaded(); |
| 29 |
|
| 30 |
}, |
| 31 |
|
| 32 |
isAvailable: function(){ |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
return dojo.isIE && dojo.isIE >= 5; |
| 38 |
}, |
| 39 |
|
| 40 |
_createStore: function() { |
| 41 |
var storeNode = dojo.create( |
| 42 |
'link', |
| 43 |
{id: this.storeName + 'Node', style: {'display':'none'}}, |
| 44 |
dojo.query('head')[0] |
| 45 |
); |
| 46 |
storeNode.addBehavior('#default#userdata'); |
| 47 |
|
| 48 |
return storeNode; |
| 49 |
}, |
| 50 |
|
| 51 |
put: function( key, |
| 52 |
value, |
| 53 |
resultsHandler, |
| 54 |
namespace){ |
| 55 |
|
| 56 |
this._assertIsValidKey(key); |
| 57 |
|
| 58 |
namespace = namespace||this.DEFAULT_NAMESPACE; |
| 59 |
this._assertIsValidNamespace(namespace); |
| 60 |
|
| 61 |
var fullKey = this.getFullKey(key,namespace); |
| 62 |
value = dojo.toJson(value); |
| 63 |
|
| 64 |
this.store.setAttribute(fullKey, value); |
| 65 |
this.store.save(this.storeName); |
| 66 |
|
| 67 |
var success = this.store.getAttribute(fullKey) === value; |
| 68 |
if(success){ |
| 69 |
this._addKey(fullKey); |
| 70 |
this.store.setAttribute('__dojoxSystemNS_keys', dojo.toJson(this.keys)); |
| 71 |
this.store.save(this.storeName); |
| 72 |
} |
| 73 |
|
| 74 |
if(resultsHandler){ |
| 75 |
resultsHandler(success ? this.SUCCESS : this.FAILED, key, null, namespace); |
| 76 |
} |
| 77 |
}, |
| 78 |
|
| 79 |
get: function( key, namespace){ |
| 80 |
this._assertIsValidKey(key); |
| 81 |
|
| 82 |
namespace = namespace||this.DEFAULT_NAMESPACE; |
| 83 |
this._assertIsValidNamespace(namespace); |
| 84 |
|
| 85 |
key = this.getFullKey(key, namespace); |
| 86 |
|
| 87 |
return dojo.fromJson(this.store.getAttribute(key)); |
| 88 |
}, |
| 89 |
|
| 90 |
getKeys: function( namespace){ |
| 91 |
namespace = namespace||this.DEFAULT_NAMESPACE; |
| 92 |
this._assertIsValidNamespace(namespace); |
| 93 |
|
| 94 |
namespace = '__'+namespace+'_'; |
| 95 |
|
| 96 |
var keys = []; |
| 97 |
for(var i = 0; i < this.keys.length; i++){ |
| 98 |
var currentKey = this.keys[i]; |
| 99 |
if(this._beginsWith(currentKey,namespace)){ |
| 100 |
currentKey = currentKey.substring(namespace.length); |
| 101 |
keys.push(currentKey); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
return keys; |
| 106 |
}, |
| 107 |
|
| 108 |
clear: function( namespace){ |
| 109 |
namespace = namespace||this.DEFAULT_NAMESPACE; |
| 110 |
this._assertIsValidNamespace(namespace); |
| 111 |
|
| 112 |
namespace = '__'+namespace+'_'; |
| 113 |
|
| 114 |
var keys = []; |
| 115 |
for(var i = 0; i < this.keys.length; i++){ |
| 116 |
var currentKey = this.keys[i]; |
| 117 |
if(this._beginsWith(currentKey,namespace)){ |
| 118 |
keys.push(currentKey); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
dojo.forEach(keys, function(key){ |
| 123 |
this.store.removeAttribute(key); |
| 124 |
this._removeKey(key); |
| 125 |
}, this); |
| 126 |
|
| 127 |
this.put('keys', this.keys, null, 'dojoxSystemNS'); |
| 128 |
this.store.save(this.storeName); |
| 129 |
}, |
| 130 |
|
| 131 |
remove: function( key, namespace){ |
| 132 |
this._assertIsValidKey(key); |
| 133 |
|
| 134 |
namespace = namespace||this.DEFAULT_NAMESPACE; |
| 135 |
this._assertIsValidNamespace(namespace); |
| 136 |
|
| 137 |
key = this.getFullKey(key, namespace); |
| 138 |
this.store.removeAttribute(key); |
| 139 |
|
| 140 |
this._removeKey(key); |
| 141 |
this.put('keys', this.keys, null, 'dojoxSystemNS'); |
| 142 |
this.store.save(this.storeName); |
| 143 |
|
| 144 |
}, |
| 145 |
|
| 146 |
getNamespaces: function(){ |
| 147 |
|
| 148 |
|
| 149 |
var results = [ this.DEFAULT_NAMESPACE]; |
| 150 |
|
| 151 |
var found = {}; |
| 152 |
found[this.DEFAULT_NAMESPACE] = true; |
| 153 |
var tester = /^__([^_]*)_/; |
| 154 |
|
| 155 |
for(var i = 0; i < this.keys.length; i++){ |
| 156 |
var currentKey = this.keys[i]; |
| 157 |
if(tester.test(currentKey) == true){ |
| 158 |
var currentNS = currentKey.match(tester)[1]; |
| 159 |
if(typeof found[currentNS] == "undefined"){ |
| 160 |
found[currentNS] = true; |
| 161 |
results.push(currentNS); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
return results; |
| 167 |
|
| 168 |
}, |
| 169 |
|
| 170 |
isPermanent: function(){ |
| 171 |
return true; |
| 172 |
}, |
| 173 |
|
| 174 |
getMaximumSize: function(){ |
| 175 |
|
| 176 |
|
| 177 |
return 64; |
| 178 |
}, |
| 179 |
|
| 180 |
hasSettingsUI: function(){ |
| 181 |
return false; |
| 182 |
}, |
| 183 |
|
| 184 |
isValidKey: function( keyName){ |
| 185 |
if(keyName === null || keyName === undefined){ |
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
return /^[0-9A-Za-z_-]*$/.test(keyName); |
| 190 |
}, |
| 191 |
|
| 192 |
isValidNamespace: function( keyName){ |
| 193 |
|
| 194 |
if(keyName === null || keyName === undefined){ |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
return /^[0-9A-Za-z-]*$/.test(keyName); |
| 199 |
}, |
| 200 |
|
| 201 |
getFullKey: function(key, namespace){ |
| 202 |
|
| 203 |
|
| 204 |
return "__" + namespace + "_" + key; |
| 205 |
}, |
| 206 |
|
| 207 |
_beginsWith: function( haystack, needle) { |
| 208 |
if(needle.length > haystack.length) { |
| 209 |
return false; |
| 210 |
} |
| 211 |
return haystack.substring(0,needle.length) === needle; |
| 212 |
}, |
| 213 |
|
| 214 |
_assertIsValidNamespace: function( namespace){ |
| 215 |
if(this.isValidNamespace(namespace) === false){ |
| 216 |
throw new Error("Invalid namespace given: " + namespace); |
| 217 |
} |
| 218 |
}, |
| 219 |
|
| 220 |
_assertIsValidKey: function( key){ |
| 221 |
if(this.isValidKey(key) === false){ |
| 222 |
throw new Error("Invalid key given: " + key); |
| 223 |
} |
| 224 |
}, |
| 225 |
|
| 226 |
_addKey: function(key){ |
| 227 |
this._removeKey(key); |
| 228 |
this.keys.push(key); |
| 229 |
}, |
| 230 |
|
| 231 |
_removeKey: function(key){ |
| 232 |
this.keys = dojo.filter(this.keys,function(item){ return item !== key;},this); |
| 233 |
} |
| 234 |
} |
| 235 |
); |
| 236 |
|
| 237 |
dojox.storage.manager.register("dojox.storage.BehaviorStorageProvider", new dojox.storage.BehaviorStorageProvider()); |