- /**
- * Create a polyfill for localStorage (type="local") or sessionStorage (type="session")
- *
- * @param {String} type Either "local" or "session"
- * @param {Boolean} forceCreate If set to true, recreate the polyfill (needed with flush)
- */
- function _createPolyfillStorage(type, forceCreate){
- var _skipSave = false,
- _length = 0,
- i,
- storage,
- storage_source = {};
-
- var rand = Math.random();
-
- if(!forceCreate && typeof window[type+"Storage"] != "undefined"){
- return;
- }
-
- // Use globalStorage for localStorage if available
- if(type == "local" && window.globalStorage){
- localStorage = window.globalStorage[window.location.hostname];
- return;
- }
-
- // only IE6/7 from this point on
- if(_backend != "userDataBehavior"){
- return;
- }
-
- // Remove existing storage element if available
- if(forceCreate && window[type+"Storage"] && window[type+"Storage"].parentNode){
- window[type+"Storage"].parentNode.removeChild(window[type+"Storage"]);
- }
-
- storage = document.createElement("button");
- document.getElementsByTagName('head')[0].appendChild(storage);
-
- if(type == "local"){
- storage_source = _storage;
- }else if(type == "session"){
- _sessionStoragePolyfillUpdate();
- }
-
- for(i in storage_source){
-
- if(storage_source.hasOwnProperty(i) && i != "__jstorage_meta" && i != "length" && typeof storage_source[i] != "undefined"){
- if(!(i in storage)){
- _length++;
- }
- storage[i] = storage_source[i];
- }
- }
-
- // Polyfill API
-
- /**
- * Indicates how many keys are stored in the storage
- */
- storage.length = _length;
-
- /**
- * Returns the key of the nth stored value
- *
- * @param {Number} n Index position
- * @return {String} Key name of the nth stored value
- */
- storage.key = function(n){
- var count = 0, i;
- _sessionStoragePolyfillUpdate();
- for(i in storage_source){
- if(storage_source.hasOwnProperty(i) && i != "__jstorage_meta" && i!="length" && typeof storage_source[i] != "undefined"){
- if(count == n){
- return i;
- }
- count++;
- }
- }
- }
-
- /**
- * Returns the current value associated with the given key
- *
- * @param {String} key key name
- * @return {Mixed} Stored value
- */
- storage.getItem = function(key){
- _sessionStoragePolyfillUpdate();
- if(type == "session"){
- return storage_source[key];
- }
- return $.jStorage.get(key);
- }
-
- /**
- * Sets or updates value for a give key
- *
- * @param {String} key Key name to be updated
- * @param {String} value String value to be stored
- */
- storage.setItem = function(key, value){
- if(typeof value == "undefined"){
- return;
- }
- storage[key] = (value || "").toString();
- }
-
- /**
- * Removes key from the storage
- *
- * @param {String} key Key name to be removed
- */
- storage.removeItem = function(key){
- if(type == "local"){
- return $.jStorage.deleteKey(key);
- }
-
- storage[key] = undefined;
-
- _skipSave = true;
- if(key in storage){
- storage.removeAttribute(key);
- }
- _skipSave = false;
- }
-
- /**
- * Clear storage
- */
- storage.clear = function(){
- if(type == "session"){
- window.name = "";
- _createPolyfillStorage("session", true);
- return;
- }
- $.jStorage.flush();
- }
-
- if(type == "local"){
-
- _localStoragePolyfillSetKey = function(key, value){
- if(key == "length"){
- return;
- }
- _skipSave = true;
- if(typeof value == "undefined"){
- if(key in storage){
- _length--;
- storage.removeAttribute(key);
- }
- }else{
- if(!(key in storage)){
- _length++;
- }
- storage[key] = (value || "").toString();
- }
- storage.length = _length;
- _skipSave = false;
- }
- }
-
- function _sessionStoragePolyfillUpdate(){
- if(type != "session"){
- return;
- }
- try{
- storage_source = JSON.parse(window.name || "{}");
- }catch(E){
- storage_source = {};
- }
- }
-
- function _sessionStoragePolyfillSave(){
- if(type != "session"){
- return;
- }
- window.name = JSON.stringify(storage_source);
- };
-
- storage.attachEvent("onpropertychange", function(e){
- if(e.propertyName == "length"){
- return;
- }
-
- if(_skipSave || e.propertyName == "length"){
- return;
- }
-
- if(type == "local"){
- if(!(e.propertyName in storage_source) && typeof storage[e.propertyName] != "undefined"){
- _length ++;
- }
- }else if(type == "session"){
- _sessionStoragePolyfillUpdate();
- if(typeof storage[e.propertyName] != "undefined" && !(e.propertyName in storage_source)){
- storage_source[e.propertyName] = storage[e.propertyName];
- _length++;
- }else if(typeof storage[e.propertyName] == "undefined" && e.propertyName in storage_source){
- delete storage_source[e.propertyName];
- _length--;
- }else{
- storage_source[e.propertyName] = storage[e.propertyName];
- }
-
- _sessionStoragePolyfillSave();
- storage.length = _length;
- return;
- }
-
- $.jStorage.set(e.propertyName, storage[e.propertyName]);
- storage.length = _length;
- });
-
- window[type+"Storage"] = storage;
- }
-