/* * FireFox / Mozilla SessionStorage Provider for Ext JS Library * Copyright (c) 2008, David Davis * * License: BSD v2 * http://xant.us/ext-ux/ */ /** * @class Ext.state.SessionStorageProvider * @extends Ext.state.Provider * A session storage Provider implementation which saves state via Mozilla SessionStorage. *
Usage:

   var cp = new Ext.state.SessionStorageProvider({
       path: "/foo/",
       domain: "extjs.com"
   });
   Ext.state.Manager.setProvider(cp);
 
* @cfg {String} path The path for which the cookie is active (defaults to root '/' which makes it active for all pages in the site) * @cfg {String} domain The domain to save the session for. Note that you cannot specify a different domain than * your page is on, but you can specify a sub-domain, or simply the domain itself like 'extjs.com' to include * all sub-domains if you need to access sessions across different sub-domains (defaults to null which uses the same * domain the page is running on including the 'www' like 'www.extjs.com') * @constructor * Create a new SessionStorageProvider * @param {Object} config The configuration object */ (function() { log = function() { }; if ( window.console && window.console.log ) log = window.console.log; Ext.state.SessionStorageProvider = function(config){ Ext.state.SessionStorageProvider.superclass.constructor.call(this); this.path = "/"; this.domain = null; this.secure = false; Ext.apply(this, config); this.state = this.readSessionStorage(); }; Ext.extend(Ext.state.SessionStorageProvider, Ext.state.Provider, { init: function() { Ext.state.SessionStorageProvider.superclass.init.apply(this,arguments); this.storage = ( window.globalStorage ) ? new Ext.ux.WhatWGStorage( domain, this.eventStorage.createDelegate(this) ) : new Ext.ux.IEPersistentStorage( domain, this.eventStorage.createDelegate(this) ); log('init'); }, eventStorage: function() { log('event storage'); }, // private set: function(name, value){ if(typeof value == "undefined" || value === null){ this.clear(name); return; } this.storage.setItem(name, value); Ext.state.SessionStorageProvider.superclass.set.call(this, name, value); }, // private clear: function(name){ this.clearSessionStorage(name); Ext.state.SessionStorageProvider.superclass.clear.call(this, name); }, // private readSessionStorage: function(){ var cookies = {}; var c = document.cookie + ";"; var re = /\s?(.*?)=(.*?);/g; var matches; while((matches = re.exec(c)) != null){ var name = matches[1]; var value = matches[2]; if(name && name.substring(0,3) == "ys-"){ cookies[name.substr(3)] = this.decodeValue(value); } } return cookies; }, // private setSessionStorage: function(name, value){ // name + "=" + this.encodeValue(value) + }, // private clearSessionStorage: function(name){ } }); Ext.ux.WhatWGStorage = function( config ) { Ext.apply( this, config ); this.init.apply( this, arguments ); }; Ext.ux.WhatWGStorage.prototype = { init: function() { if ( !this.domain ) throw "domain needed"; if ( !this.callback ) throw "domain needed"; this.storage = globalStorage[ domain ]; DOM.addEventListener( document, "storage", this.callback ); }, getItem: function() { return this.storage.getItem.apply( this.storage, arguments ); }, setItem: function() { return this.storage.setItem.apply( this.storage, arguments ); } }; Ext.ux.IEPersistentStorage = function( config ) { Ext.apply( this, config ); this.init( this, arguments ); }; /* IE 5.1+ */ Ext.ux.IEPersistentStorage.prototype = { init: function() { if ( !this.domain ) throw "domain needed"; if ( !this.callback ) throw "domain needed"; this.items = {}; this.itemlist = []; this.timer = new Timer( this.getIndirectMethod( "checkValue" ), 150 ); }, checkValue: function() { if ( !this.itemlist.length ) return; var l = this.itemlist.length; for ( var i = 0; i < l; i++ ) { var key = this.itemlist[ i ]; var storage = this.items[ key ]; var va = storage.getAttribute( key ); try { storage.load( "oXMLStore" ); } catch( e ) { if ( e.message ) e = e.message; log( e ); }; var vv = storage.getAttribute( key ); if ( this.callback && va != vv ) this.callback( { domain: this.domain, key: key } ); } }, getItem: function( key ) { var storage = this.getStorage( key, true ); if ( !storage ) return null; while( 1 ) { /* IE doesn't lock thread access to storage * it just throws an error instead */ try { storage.load( "oXMLStore" ); } catch( e ) { if ( e.message ) e = e.message; log( e ); continue; }; break; } var v = storage.getAttribute( key ); return v; }, setItem: function( key, value ) { var storage = this.getStorage( key, true ); if ( !storage ) return false; storage.setAttribute( key, value ); while( 1 ) { /* IE doesn't lock thread access to storage * it just throws an error instead */ try { storage.save( "oXMLStore" ); } catch( e ) { if ( e.message ) e = e.message; log( e ); continue; }; break; } if ( this.callback ) this.callback( { domain: this.domain, key: key } ); return true; }, getStorage: function( key, create ) { var storage; if ( this.items.hasOwnProperty( key ) ) storage = this.items[ key ]; else { if ( !create ) return null; storage = document.createElement( "input" ); storage.setAttribute( "type", "hidden" ); storage.addBehavior ( "#default#userData" ); storage.className = "userData"; storage.setAttribute( "id", "storage-" + key ); document.body.appendChild( storage ); this.items[ key ] = storage; this.itemlist.push( key ); } return storage; } }; })();