root / trunk / web / dojo / dojox / storage / WhatWGStorageProvider.js
History | View | Annotate | Download (3.9 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.storage.WhatWGStorageProvider"]){ |
||
9 | dojo._hasResource["dojox.storage.WhatWGStorageProvider"]=true; |
||
10 | dojo.provide("dojox.storage.WhatWGStorageProvider");
|
||
11 | dojo.require("dojox.storage.Provider");
|
||
12 | dojo.require("dojox.storage.manager");
|
||
13 | dojo.declare("dojox.storage.WhatWGStorageProvider",[dojox.storage.Provider],{initialized:false,_domain:null,_available:null,_statusHandler:null,_allNamespaces:null,_storageEventListener:null,initialize:function(){ |
||
14 | if(dojo.config["disableWhatWGStorage"]==true){ |
||
15 | return;
|
||
16 | } |
||
17 | this._domain=location.hostname;
|
||
18 | this.initialized=true; |
||
19 | dojox.storage.manager.loaded(); |
||
20 | },isAvailable:function(){ |
||
21 | try{
|
||
22 | var _1=globalStorage[location.hostname];
|
||
23 | } |
||
24 | catch(e){
|
||
25 | this._available=false; |
||
26 | return this._available; |
||
27 | } |
||
28 | this._available=true; |
||
29 | return this._available; |
||
30 | },put:function(_2,_3,_4,_5){ |
||
31 | if(this.isValidKey(_2)==false){ |
||
32 | throw new Error("Invalid key given: "+_2); |
||
33 | } |
||
34 | _5=_5||this.DEFAULT_NAMESPACE;
|
||
35 | _2=this.getFullKey(_2,_5);
|
||
36 | this._statusHandler=_4;
|
||
37 | if(dojo.isString(_3)){
|
||
38 | _3="string:"+_3;
|
||
39 | }else{
|
||
40 | _3=dojo.toJson(_3); |
||
41 | } |
||
42 | var _6=dojo.hitch(this,function(_7){ |
||
43 | window.removeEventListener("storage",_6,false); |
||
44 | if(_4){
|
||
45 | _4.call(null,this.SUCCESS,_2,null,_5); |
||
46 | } |
||
47 | }); |
||
48 | window.addEventListener("storage",_6,false); |
||
49 | try{
|
||
50 | var _8=globalStorage[this._domain]; |
||
51 | _8.setItem(_2,_3); |
||
52 | } |
||
53 | catch(e){
|
||
54 | this._statusHandler.call(null,this.FAILED,_2,e.toString(),_5); |
||
55 | } |
||
56 | },get:function(_9,_a){ |
||
57 | if(this.isValidKey(_9)==false){ |
||
58 | throw new Error("Invalid key given: "+_9); |
||
59 | } |
||
60 | _a=_a||this.DEFAULT_NAMESPACE;
|
||
61 | _9=this.getFullKey(_9,_a);
|
||
62 | var _b=globalStorage[this._domain]; |
||
63 | var _c=_b.getItem(_9);
|
||
64 | if(_c==null||_c==""){ |
||
65 | return null; |
||
66 | } |
||
67 | _c=_c.value; |
||
68 | if(dojo.isString(_c)&&(/^string:/.test(_c))){ |
||
69 | _c=_c.substring("string:".length);
|
||
70 | }else{
|
||
71 | _c=dojo.fromJson(_c); |
||
72 | } |
||
73 | return _c;
|
||
74 | },getNamespaces:function(){ |
||
75 | var _d=[this.DEFAULT_NAMESPACE]; |
||
76 | var _e={};
|
||
77 | var _f=globalStorage[this._domain]; |
||
78 | var _10=/^__([^_]*)_/; |
||
79 | for(var i=0;i<_f.length;i++){ |
||
80 | var _11=_f.key(i);
|
||
81 | if(_10.test(_11)==true){ |
||
82 | var _12=_11.match(_10)[1]; |
||
83 | if(typeof _e[_12]=="undefined"){ |
||
84 | _e[_12]=true;
|
||
85 | _d.push(_12); |
||
86 | } |
||
87 | } |
||
88 | } |
||
89 | return _d;
|
||
90 | },getKeys:function(_13){ |
||
91 | _13=_13||this.DEFAULT_NAMESPACE;
|
||
92 | if(this.isValidKey(_13)==false){ |
||
93 | throw new Error("Invalid namespace given: "+_13); |
||
94 | } |
||
95 | var _14;
|
||
96 | if(_13==this.DEFAULT_NAMESPACE){ |
||
97 | _14=new RegExp("^([^_]{2}.*)$"); |
||
98 | }else{
|
||
99 | _14=new RegExp("^__"+_13+"_(.*)$"); |
||
100 | } |
||
101 | var _15=globalStorage[this._domain]; |
||
102 | var _16=[];
|
||
103 | for(var i=0;i<_15.length;i++){ |
||
104 | var _17=_15.key(i);
|
||
105 | if(_14.test(_17)==true){ |
||
106 | _17=_17.match(_14)[1];
|
||
107 | _16.push(_17); |
||
108 | } |
||
109 | } |
||
110 | return _16;
|
||
111 | },clear:function(_18){ |
||
112 | _18=_18||this.DEFAULT_NAMESPACE;
|
||
113 | if(this.isValidKey(_18)==false){ |
||
114 | throw new Error("Invalid namespace given: "+_18); |
||
115 | } |
||
116 | var _19;
|
||
117 | if(_18==this.DEFAULT_NAMESPACE){ |
||
118 | _19=new RegExp("^[^_]{2}"); |
||
119 | }else{
|
||
120 | _19=new RegExp("^__"+_18+"_"); |
||
121 | } |
||
122 | var _1a=globalStorage[this._domain]; |
||
123 | var _1b=[];
|
||
124 | for(var i=0;i<_1a.length;i++){ |
||
125 | if(_19.test(_1a.key(i))==true){ |
||
126 | _1b[_1b.length]=_1a.key(i); |
||
127 | } |
||
128 | } |
||
129 | dojo.forEach(_1b,dojo.hitch(_1a,"removeItem"));
|
||
130 | },remove:function(key,_1c){ |
||
131 | key=this.getFullKey(key,_1c);
|
||
132 | var _1d=globalStorage[this._domain]; |
||
133 | _1d.removeItem(key); |
||
134 | },isPermanent:function(){ |
||
135 | return true; |
||
136 | },getMaximumSize:function(){ |
||
137 | return this.SIZE_NO_LIMIT; |
||
138 | },hasSettingsUI:function(){ |
||
139 | return false; |
||
140 | },showSettingsUI:function(){ |
||
141 | throw new Error(this.declaredClass+" does not support a storage settings user-interface"); |
||
142 | },hideSettingsUI:function(){ |
||
143 | throw new Error(this.declaredClass+" does not support a storage settings user-interface"); |
||
144 | },getFullKey:function(key,_1e){ |
||
145 | _1e=_1e||this.DEFAULT_NAMESPACE;
|
||
146 | if(this.isValidKey(_1e)==false){ |
||
147 | throw new Error("Invalid namespace given: "+_1e); |
||
148 | } |
||
149 | if(_1e==this.DEFAULT_NAMESPACE){ |
||
150 | return key;
|
||
151 | }else{
|
||
152 | return "__"+_1e+"_"+key; |
||
153 | } |
||
154 | }}); |
||
155 | dojox.storage.manager.register("dojox.storage.WhatWGStorageProvider",new dojox.storage.WhatWGStorageProvider()); |
||
156 | } |