Advertisement
Guest User

$.store

a guest
Mar 25th, 2013
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Copyright (c) 2010 Marcus Westin
  2.  *
  3.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  4.  * of this software and associated documentation files (the "Software"), to deal
  5.  * in the Software without restriction, including without limitation the rights
  6.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7.  * copies of the Software, and to permit persons to whom the Software is
  8.  * furnished to do so, subject to the following conditions:
  9.  *
  10.  * The above copyright notice and this permission notice shall be included in
  11.  * all copies or substantial portions of the Software.
  12.  *
  13.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19.  * THE SOFTWARE.
  20.  */
  21.  
  22. !function($) {
  23.     $.store = (function(){
  24.         var api = {},
  25.             win = window,
  26.             doc = win.document,
  27.             localStorageName = 'localStorage',
  28.             globalStorageName = 'globalStorage',
  29.             storage
  30.  
  31.         api.disabled = false
  32.         api.set = function(key, value) {}
  33.         api.get = function(key) {}
  34.         api.remove = function(key) {}
  35.         api.clear = function() {}
  36.         api.transact = function(key, transactionFn) {
  37.             var val = api.get(key)
  38.             if (typeof val == 'undefined') { val = {} }
  39.             transactionFn(val)
  40.             api.set(key, val)
  41.         }
  42.  
  43.         api.serialize = function(value) {
  44.             return JSON.stringify(value);
  45.         }
  46.         api.deserialize = function(value) {
  47.             return (typeof value != 'string') ? undefined : JSON.parse(value);
  48.         }
  49.  
  50.         // Functions to encapsulate questionable FireFox 3.6.13 behavior
  51.         // when about.config::dom.storage.enabled === false
  52.         // See https://github.com/marcuswestin/store.js/issues#issue/13
  53.         function isLocalStorageNameSupported() {
  54.             try { return (localStorageName in win && win[localStorageName]) }
  55.             catch(err) { return false }
  56.         }
  57.        
  58.         function isGlobalStorageNameSupported() {
  59.             try { return (globalStorageName in win && win[globalStorageName] && win[globalStorageName][win.location.hostname]) }
  60.             catch(err) { return false }
  61.         }  
  62.  
  63.         if (isLocalStorageNameSupported()) {
  64.             storage = win[localStorageName]
  65.             api.set = function(key, val) { storage.setItem(key, api.serialize(val)) }
  66.             api.get = function(key) { return api.deserialize(storage.getItem(key)) }
  67.             api.remove = function(key) { storage.removeItem(key) }
  68.             api.clear = function() { storage.clear() }
  69.  
  70.         } else if (isGlobalStorageNameSupported()) {
  71.             storage = win[globalStorageName][win.location.hostname]
  72.             api.set = function(key, val) { storage[key] = api.serialize(val) }
  73.             api.get = function(key) { return api.deserialize(storage[key] && storage[key].value) }
  74.             api.remove = function(key) { delete storage[key] }
  75.             api.clear = function() { for (var key in storage ) { delete storage[key] } }
  76.  
  77.         } else if (doc.documentElement.addBehavior) {
  78.             var storage = doc.createElement('div');
  79.             function withIEStorage(storeFunction) {
  80.                 return function() {
  81.                     var args = Array.prototype.slice.call(arguments, 0);
  82.                     args.unshift(storage);
  83.                     // See http://msdn.microsoft.com/en-us/library/ms531081(v=VS.85).aspx
  84.                     // and http://msdn.microsoft.com/en-us/library/ms531424(v=VS.85).aspx
  85.                     doc.body.appendChild(storage);
  86.                     storage.addBehavior('#default#userData');
  87.                     storage.load(localStorageName);
  88.                     var result = storeFunction.apply(api, args);
  89.                     doc.body.removeChild(storage);
  90.                     return result;
  91.                 }
  92.             }
  93.             api.set = withIEStorage(function(storage, key, val) {
  94.                 storage.setAttribute(key, api.serialize(val));
  95.                 storage.save(localStorageName);
  96.             })
  97.             api.get = withIEStorage(function(storage, key) {
  98.                 return api.deserialize(storage.getAttribute(key));
  99.             })
  100.             api.remove = withIEStorage(function(storage, key) {
  101.                 storage.removeAttribute(key);
  102.                 storage.save(localStorageName);
  103.             })
  104.             api.clear = withIEStorage(function(storage) {
  105.                 var attributes = storage.XMLDocument.documentElement.attributes;
  106.                 storage.load(localStorageName);
  107.                 for (var i=0, attr; attr = attributes[i]; i++) {
  108.                     storage.removeAttribute(attr.name);
  109.                 }
  110.                 storage.save(localStorageName);
  111.             })
  112.         } else {
  113.             api.disabled = true;
  114.         }
  115.  
  116.         return api
  117.     })();
  118. }(jQuery || Zepto);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement