1 | /* |
---|
2 | * jQuery store - Plugin for persistent data storage using localStorage, userData (and window.name) |
---|
3 | * |
---|
4 | * Authors: Rodney Rehm |
---|
5 | * Web: http://medialize.github.com/jQuery-store/ |
---|
6 | * |
---|
7 | * Licensed under the MIT License: |
---|
8 | * http://www.opensource.org/licenses/mit-license.php |
---|
9 | * |
---|
10 | */ |
---|
11 | |
---|
12 | /********************************************************************************** |
---|
13 | * INITIALIZE EXAMPLES: |
---|
14 | ********************************************************************************** |
---|
15 | * // automatically detect best suited storage driver and use default serializers |
---|
16 | * $.storage = new $.store(); |
---|
17 | * // optionally initialize with specific driver and or serializers |
---|
18 | * $.storage = new $.store( [driver] [, serializers] ); |
---|
19 | * driver can be the key (e.g. "windowName") or the driver-object itself |
---|
20 | * serializers can be a list of named serializers like $.store.serializers |
---|
21 | ********************************************************************************** |
---|
22 | * USAGE EXAMPLES: |
---|
23 | ********************************************************************************** |
---|
24 | * $.storage.get( key ); // retrieves a value |
---|
25 | * $.storage.set( key, value ); // saves a value |
---|
26 | * $.storage.del( key ); // deletes a value |
---|
27 | * $.storage.flush(); // deletes aall values |
---|
28 | ********************************************************************************** |
---|
29 | */ |
---|
30 | |
---|
31 | (function($,undefined){ |
---|
32 | |
---|
33 | /********************************************************************************** |
---|
34 | * $.store base and convinience accessor |
---|
35 | **********************************************************************************/ |
---|
36 | |
---|
37 | $.store = function( driver, serializers ) |
---|
38 | { |
---|
39 | var that = this; |
---|
40 | |
---|
41 | if( typeof driver == 'string' ) |
---|
42 | { |
---|
43 | if( $.store.drivers[ driver ] ) |
---|
44 | this.driver = $.store.drivers[ driver ]; |
---|
45 | else |
---|
46 | throw new Error( 'Unknown driver '+ driver ); |
---|
47 | } |
---|
48 | else if( typeof driver == 'object' ) |
---|
49 | { |
---|
50 | var invalidAPI = !$.isFunction( driver.init ) |
---|
51 | || !$.isFunction( driver.get ) |
---|
52 | || !$.isFunction( driver.set ) |
---|
53 | || !$.isFunction( driver.del ) |
---|
54 | || !$.isFunction( driver.flush ); |
---|
55 | |
---|
56 | if( invalidAPI ) |
---|
57 | throw new Error( 'The specified driver does not fulfill the API requirements' ); |
---|
58 | |
---|
59 | this.driver = driver; |
---|
60 | } |
---|
61 | else |
---|
62 | { |
---|
63 | // detect and initialize storage driver |
---|
64 | $.each( $.store.drivers, function() |
---|
65 | { |
---|
66 | // skip unavailable drivers |
---|
67 | if( !$.isFunction( this.available ) || !this.available() ) |
---|
68 | return true; // continue; |
---|
69 | |
---|
70 | that.driver = this; |
---|
71 | if( that.driver.init() === false ) |
---|
72 | { |
---|
73 | that.driver = null; |
---|
74 | return true; // continue; |
---|
75 | } |
---|
76 | |
---|
77 | return false; // break; |
---|
78 | }); |
---|
79 | } |
---|
80 | |
---|
81 | // use default serializers if not told otherwise |
---|
82 | if( !serializers ) |
---|
83 | serializers = $.store.serializers; |
---|
84 | |
---|
85 | // intialize serializers |
---|
86 | this.serializers = {}; |
---|
87 | $.each( serializers, function( key, serializer ) |
---|
88 | { |
---|
89 | // skip invalid processors |
---|
90 | if( !$.isFunction( this.init ) ) |
---|
91 | return true; // continue; |
---|
92 | |
---|
93 | that.serializers[ key ] = this; |
---|
94 | that.serializers[ key ].init( that.encoders, that.decoders ); |
---|
95 | }); |
---|
96 | }; |
---|
97 | |
---|
98 | |
---|
99 | /********************************************************************************** |
---|
100 | * $.store API |
---|
101 | **********************************************************************************/ |
---|
102 | |
---|
103 | $.extend( $.store.prototype, { |
---|
104 | get: function( key ) |
---|
105 | { |
---|
106 | var value = this.driver.get( key ); |
---|
107 | return this.driver.encodes ? value : this.unserialize( value ); |
---|
108 | }, |
---|
109 | set: function( key, value ) |
---|
110 | { |
---|
111 | this.driver.set( key, this.driver.encodes ? value : this.serialize( value ) ); |
---|
112 | }, |
---|
113 | del: function( key ) |
---|
114 | { |
---|
115 | this.driver.del( key ); |
---|
116 | }, |
---|
117 | flush: function() |
---|
118 | { |
---|
119 | this.driver.flush(); |
---|
120 | }, |
---|
121 | driver : undefined, |
---|
122 | encoders : [], |
---|
123 | decoders : [], |
---|
124 | serialize: function( value ) |
---|
125 | { |
---|
126 | var that = this; |
---|
127 | |
---|
128 | $.each( this.encoders, function() |
---|
129 | { |
---|
130 | var serializer = that.serializers[ this + "" ]; |
---|
131 | if( !serializer || !serializer.encode ) |
---|
132 | return true; // continue; |
---|
133 | try |
---|
134 | { |
---|
135 | value = serializer.encode( value ); |
---|
136 | } |
---|
137 | catch( e ){} |
---|
138 | }); |
---|
139 | |
---|
140 | return value; |
---|
141 | }, |
---|
142 | unserialize: function( value ) |
---|
143 | { |
---|
144 | var that = this; |
---|
145 | if( !value ) |
---|
146 | return value; |
---|
147 | |
---|
148 | $.each( this.decoders, function() |
---|
149 | { |
---|
150 | var serializer = that.serializers[ this + "" ]; |
---|
151 | if( !serializer || !serializer.decode ) |
---|
152 | return true; // continue; |
---|
153 | |
---|
154 | value = serializer.decode( value ); |
---|
155 | }); |
---|
156 | |
---|
157 | return value; |
---|
158 | } |
---|
159 | }); |
---|
160 | |
---|
161 | |
---|
162 | /********************************************************************************** |
---|
163 | * $.store drivers |
---|
164 | **********************************************************************************/ |
---|
165 | |
---|
166 | $.store.drivers = { |
---|
167 | // Firefox 3.5, Safari 4.0, Chrome 5, Opera 10.5, IE8 |
---|
168 | 'localStorage': { |
---|
169 | // see https://developer.mozilla.org/en/dom/storage#localStorage |
---|
170 | ident: "$.store.drivers.localStorage", |
---|
171 | scope: 'browser', |
---|
172 | available: function() |
---|
173 | { |
---|
174 | try |
---|
175 | { |
---|
176 | return !!window.localStorage; |
---|
177 | } |
---|
178 | catch(e) |
---|
179 | { |
---|
180 | // Firefox won't allow localStorage if cookies are disabled |
---|
181 | return false; |
---|
182 | } |
---|
183 | }, |
---|
184 | init: $.noop, |
---|
185 | get: function( key ) |
---|
186 | { |
---|
187 | return window.localStorage.getItem( key ); |
---|
188 | }, |
---|
189 | set: function( key, value ) |
---|
190 | { |
---|
191 | window.localStorage.setItem( key, value ); |
---|
192 | }, |
---|
193 | del: function( key ) |
---|
194 | { |
---|
195 | window.localStorage.removeItem( key ); |
---|
196 | }, |
---|
197 | flush: function() |
---|
198 | { |
---|
199 | window.localStorage.clear(); |
---|
200 | } |
---|
201 | }, |
---|
202 | |
---|
203 | // IE6, IE7 |
---|
204 | 'userData': { |
---|
205 | // see http://msdn.microsoft.com/en-us/library/ms531424.aspx |
---|
206 | ident: "$.store.drivers.userData", |
---|
207 | element: null, |
---|
208 | nodeName: 'userdatadriver', |
---|
209 | scope: 'browser', |
---|
210 | initialized: false, |
---|
211 | available: function() |
---|
212 | { |
---|
213 | try |
---|
214 | { |
---|
215 | return !!( document.documentElement && document.documentElement.addBehavior ); |
---|
216 | } |
---|
217 | catch(e) |
---|
218 | { |
---|
219 | return false; |
---|
220 | } |
---|
221 | }, |
---|
222 | init: function() |
---|
223 | { |
---|
224 | // $.store can only utilize one userData store at a time, thus avoid duplicate initialization |
---|
225 | if( this.initialized ) |
---|
226 | return; |
---|
227 | |
---|
228 | try |
---|
229 | { |
---|
230 | // Create a non-existing element and append it to the root element (html) |
---|
231 | this.element = document.createElement( this.nodeName ); |
---|
232 | document.documentElement.insertBefore( this.element, document.getElementsByTagName('title')[0] ); |
---|
233 | // Apply userData behavior |
---|
234 | this.element.addBehavior( "#default#userData" ); |
---|
235 | this.initialized = true; |
---|
236 | } |
---|
237 | catch( e ) |
---|
238 | { |
---|
239 | return false; |
---|
240 | } |
---|
241 | }, |
---|
242 | get: function( key ) |
---|
243 | { |
---|
244 | this.element.load( this.nodeName ); |
---|
245 | return this.element.getAttribute( key ); |
---|
246 | }, |
---|
247 | set: function( key, value ) |
---|
248 | { |
---|
249 | this.element.setAttribute( key, value ); |
---|
250 | this.element.save( this.nodeName ); |
---|
251 | }, |
---|
252 | del: function( key ) |
---|
253 | { |
---|
254 | this.element.removeAttribute( key ); |
---|
255 | this.element.save( this.nodeName ); |
---|
256 | |
---|
257 | }, |
---|
258 | flush: function() |
---|
259 | { |
---|
260 | // flush by expiration |
---|
261 | this.element.expires = (new Date).toUTCString(); |
---|
262 | this.element.save( this.nodeName ); |
---|
263 | } |
---|
264 | }, |
---|
265 | |
---|
266 | // most other browsers |
---|
267 | 'windowName': { |
---|
268 | ident: "$.store.drivers.windowName", |
---|
269 | scope: 'window', |
---|
270 | cache: {}, |
---|
271 | encodes: true, |
---|
272 | available: function() |
---|
273 | { |
---|
274 | return true; |
---|
275 | }, |
---|
276 | init: function() |
---|
277 | { |
---|
278 | this.load(); |
---|
279 | }, |
---|
280 | save: function() |
---|
281 | { |
---|
282 | window.name = $.store.serializers.json.encode( this.cache ); |
---|
283 | }, |
---|
284 | load: function() |
---|
285 | { |
---|
286 | try |
---|
287 | { |
---|
288 | this.cache = $.store.serializers.json.decode( window.name + "" ); |
---|
289 | if( typeof this.cache != "object" ) |
---|
290 | this.cache = {}; |
---|
291 | } |
---|
292 | catch(e) |
---|
293 | { |
---|
294 | this.cache = {}; |
---|
295 | window.name = "{}"; |
---|
296 | } |
---|
297 | }, |
---|
298 | get: function( key ) |
---|
299 | { |
---|
300 | return this.cache[ key ]; |
---|
301 | }, |
---|
302 | set: function( key, value ) |
---|
303 | { |
---|
304 | this.cache[ key ] = value; |
---|
305 | this.save(); |
---|
306 | }, |
---|
307 | del: function( key ) |
---|
308 | { |
---|
309 | try |
---|
310 | { |
---|
311 | delete this.cache[ key ]; |
---|
312 | } |
---|
313 | catch(e) |
---|
314 | { |
---|
315 | this.cache[ key ] = undefined; |
---|
316 | } |
---|
317 | |
---|
318 | this.save(); |
---|
319 | }, |
---|
320 | flush: function() |
---|
321 | { |
---|
322 | window.name = "{}"; |
---|
323 | } |
---|
324 | } |
---|
325 | }; |
---|
326 | |
---|
327 | /********************************************************************************** |
---|
328 | * $.store serializers |
---|
329 | **********************************************************************************/ |
---|
330 | |
---|
331 | $.store.serializers = { |
---|
332 | |
---|
333 | 'json': { |
---|
334 | ident: "$.store.serializers.json", |
---|
335 | init: function( encoders, decoders ) |
---|
336 | { |
---|
337 | encoders.push( "json" ); |
---|
338 | decoders.push( "json" ); |
---|
339 | }, |
---|
340 | encode: JSON.stringify, |
---|
341 | decode: JSON.parse |
---|
342 | }, |
---|
343 | |
---|
344 | // TODO: html serializer |
---|
345 | // 'html' : {}, |
---|
346 | |
---|
347 | 'xml': { |
---|
348 | ident: "$.store.serializers.xml", |
---|
349 | init: function( encoders, decoders ) |
---|
350 | { |
---|
351 | encoders.unshift( "xml" ); |
---|
352 | decoders.push( "xml" ); |
---|
353 | }, |
---|
354 | |
---|
355 | // wouldn't be necessary if jQuery exposed this function |
---|
356 | isXML: function( value ) |
---|
357 | { |
---|
358 | var documentElement = ( value ? value.ownerDocument || value : 0 ).documentElement; |
---|
359 | return documentElement ? documentElement.nodeName.toLowerCase() !== "html" : false; |
---|
360 | }, |
---|
361 | |
---|
362 | // encodes a XML node to string (taken from $.jStorage, MIT License) |
---|
363 | encode: function( value ) |
---|
364 | { |
---|
365 | if( !value || value._serialized || !this.isXML( value ) ) |
---|
366 | return value; |
---|
367 | |
---|
368 | var _value = { _serialized: this.ident, value: value }; |
---|
369 | |
---|
370 | try |
---|
371 | { |
---|
372 | // Mozilla, Webkit, Opera |
---|
373 | _value.value = new XMLSerializer().serializeToString( value ); |
---|
374 | return _value; |
---|
375 | } |
---|
376 | catch(E1) |
---|
377 | { |
---|
378 | try |
---|
379 | { |
---|
380 | // Internet Explorer |
---|
381 | _value.value = value.xml; |
---|
382 | return _value; |
---|
383 | } |
---|
384 | catch(E2){} |
---|
385 | } |
---|
386 | |
---|
387 | return value; |
---|
388 | }, |
---|
389 | |
---|
390 | // decodes a XML node from string (taken from $.jStorage, MIT License) |
---|
391 | decode: function( value ) |
---|
392 | { |
---|
393 | if( !value || !value._serialized || value._serialized != this.ident ) |
---|
394 | return value; |
---|
395 | |
---|
396 | var dom_parser = ( "DOMParser" in window && (new DOMParser()).parseFromString ); |
---|
397 | if( !dom_parser && window.ActiveXObject ) |
---|
398 | { |
---|
399 | dom_parser = function( _xmlString ) |
---|
400 | { |
---|
401 | var xml_doc = new ActiveXObject( 'Microsoft.XMLDOM' ); |
---|
402 | xml_doc.async = 'false'; |
---|
403 | xml_doc.loadXML( _xmlString ); |
---|
404 | return xml_doc; |
---|
405 | } |
---|
406 | } |
---|
407 | |
---|
408 | if( !dom_parser ) |
---|
409 | { |
---|
410 | return undefined; |
---|
411 | } |
---|
412 | |
---|
413 | value.value = dom_parser.call( |
---|
414 | "DOMParser" in window && (new DOMParser()) || window, |
---|
415 | value.value, |
---|
416 | 'text/xml' |
---|
417 | ); |
---|
418 | |
---|
419 | return this.isXML( value.value ) ? value.value : undefined; |
---|
420 | } |
---|
421 | } |
---|
422 | }; |
---|
423 | |
---|
424 | })(jQuery); |
---|