1(function($) { 2 3 function WebStorageAdapter() 4 { 5 if (typeof localStorage === 'undefined') { 6 throw new TypeError('Missing localStorage object'); 7 } 8 9 if (typeof sessionStorage === 'undefined') { 10 //throw new TypeError('Missing sessionStorage object'); 11 } 12 } 13 14 function createKey(namespace, key) { 15 return namespace + '::' + key; 16 }; 17 18 function localKeyExists(fullKey) { 19 if (typeof localStorage === 'undefined') { 20 return false; 21 } 22 return (localStorage[fullKey] !== undefined); 23 }; 24 25 function sessionKeyExists(fullKey) { 26 if (typeof sessionStorage === 'undefined') { 27 return false; 28 } 29 return (sessionStorage[fullKey] !== undefined); 30 }; 31 32 WebStorageAdapter.prototype.get = function(namespace, key) 33 { 34 var fullKey = createKey(namespace, key), 35 result = null, 36 data = null, 37 now = new Date().getTime(), 38 ignoreParse = false;
39 40 if (sessionKeyExists(fullKey)) { 41 result = sessionStorage[fullKey]; 42 } else if (localKeyExists(fullKey)){ 43 result = localStorage[fullKey]; 44 } else { 45 data = $.cookies.get(fullKey); 46 ignoreParse = true; 47 } 48 49 if (false === ignoreParse) { 50 if (result === null || result === 'null') { 51 return null; 52 } 53 try { 54 data = JSON.parse(result); 55 } catch (e) { 56 this.del(namespace, key); 57 return null; 58 } 59 } 60 61 if (null === data) { 62 return null; 63 } 64 65 if (data.ttl === 0) { 66 return data.data; 67 } 68 69 if ((data.created + data.ttl) < now) { 70 this.del(namespace, key); 71 72 return null; 73 } 74 75 return data.data; 76 }; 77 78 WebStorageAdapter.prototype.set = function(namespace, key, value, session, ttl) 79 { 80 var expiresAt = new Date(), 81 domainParts = window.location.host.split('.'), 82 fullKey = createKey(namespace, key), 83 data = { 84 created : new Date().getTime(), 85 data : value, 86 ttl : ttl !== undefined ? (ttl * 1000) : 86400000 87 }, 88 cookieOptions; 89 90 expiresAt.setTime( 91 expiresAt.getTime() + (ttl !== undefined ? (ttl * 1000) : 86400000) 92 ); 93 delete domainParts[0]; 94 95 cookieOptions = { 96 domain : domainParts.join('.'), 97 path : '/', 98 expiresAt : (session ? 'Session' : expiresAt) 99 }; 100 101 try { 102 if (session && typeof sessionStorage !== 'undefined') { 103 sessionStorage[fullKey] = JSON.stringify(data); 104 } else if (!session && typeof localStorage !== 'undefined') { 105 localStorage[fullKey] = JSON.stringify(data); 106 } 107 } catch (err) { 108 // ON error fallback to cookie if available... 109 if (typeof $.cookies !== 'undefined') { 110 $.cookies.set(fullKey, data, cookieOptions); 111 } 112 } 113 114 return true; 115 }; 116 117 WebStorageAdapter.prototype.del = function(namespace, key) 118 { 119 var fullKey = createKey(namespace, key), 120 cleared = false; 121 122 if (sessionKeyExists(fullKey)) { 123 try { 124 delete(sessionStorage[fullKey]); 125 } catch (err) { 126 sessionStorage[fullKey] = null; 127 } 128 cleared = true; 129 } 130 131 if (localKeyExists(fullKey)) { 132 try { 133 delete(localStorage[fullKey]); 134 } catch (err) { 135 localStorage[fullKey] = null; 136 } 137 cleared = true; 138 } 139 140 if (false === cleared) { 141 $.cookies.del(fullKey); 142 cleared = true; 143 } 144 145 return cleared; 146 }; 147 148 WebStorageAdapter.prototype.clear = function(namespace) 149 { 150 var match = new RegExp('^' + namespace + '::'), 151 i = null, 152 item = null; 153 154 for (i = 0; i < localStorage.length; i++) { 155 item = localStorage[i]; 156 if (typeof item !== 'undefined' && item.match(match)) { 157 try { 158 delete(localStorage[i]); 159 } catch (err) { 160 localStorage[i] = null; 161 } 162 } 163 } 164 165 if (typeof sessionStorage === 'undefined') { 166 return true; 167 } 168 169 for (i = 0; i < sessionStorage.length; i++) { 170 item = sessionStorage[i]; 171 if (typeof item !== 'undefined' && item.match(match)) { 172 try { 173 delete(sessionStorage[i]); 174 } catch (err) { 175 sessionStorage[i] = null; 176 } 177 } 178 } 179 180 return true; 181 }; 182 183 if (typeof window === 'object' && typeof window.document === 'object' && typeof window.storage === 'undefined') { 184 window.storage = new WebStorageAdapter(); 185 } 186 187}(jQuery));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.