|
| 1 | +try { |
| 2 | + if (!window.localStorage || !window.sessionStorage) { |
| 3 | + throw new Error(); |
| 4 | + } |
| 5 | + |
| 6 | + localStorage.setItem('storage_test', 1); |
| 7 | + localStorage.removeItem('storage_test'); |
| 8 | +} catch (e) { |
| 9 | + (function () { |
| 10 | + 'use strict'; |
| 11 | + |
| 12 | + /** |
| 13 | + * Returns a storage object to shim local or sessionStorage |
| 14 | + * @param {String} type - either 'local' or 'session' |
| 15 | + */ |
| 16 | + var Storage = function (type) { |
| 17 | + var data; |
| 18 | + |
| 19 | + /** |
| 20 | + * Creates a cookie |
| 21 | + * @param {String} name |
| 22 | + * @param {String} value |
| 23 | + * @param {Integer} days |
| 24 | + */ |
| 25 | + function createCookie(name, value, days) { |
| 26 | + var date, expires; |
| 27 | + |
| 28 | + if (days) { |
| 29 | + date = new Date(); |
| 30 | + date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); |
| 31 | + expires = '; expires=' + date.toGMTString(); |
| 32 | + } else { |
| 33 | + expires = ''; |
| 34 | + } |
| 35 | + document.cookie = name + '=' + value + expires + '; path=/'; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Reads value of a cookie |
| 40 | + * @param {String} name |
| 41 | + */ |
| 42 | + function readCookie(name) { |
| 43 | + var nameEQ = name + '=', |
| 44 | + ca = document.cookie.split(';'), |
| 45 | + i = 0, |
| 46 | + c; |
| 47 | + |
| 48 | + for (i = 0; i < ca.length; i++) { |
| 49 | + c = ca[i]; |
| 50 | + |
| 51 | + while (c.charAt(0) === ' ') { |
| 52 | + c = c.substring(1, c.length); |
| 53 | + } |
| 54 | + |
| 55 | + if (c.indexOf(nameEQ) === 0) { |
| 56 | + return c.substring(nameEQ.length, c.length); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Returns cookie name based upon the storage type. |
| 65 | + * If this is session storage, the function returns a unique cookie per tab |
| 66 | + */ |
| 67 | + function getCookieName() { |
| 68 | + |
| 69 | + if (type !== 'session') { |
| 70 | + return 'localstorage'; |
| 71 | + } |
| 72 | + |
| 73 | + if (!window.name) { |
| 74 | + window.name = new Date().getTime(); |
| 75 | + } |
| 76 | + |
| 77 | + return 'sessionStorage' + window.name; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Sets storage cookie to a data object |
| 82 | + * @param {Object} dataObject |
| 83 | + */ |
| 84 | + function setData(dataObject) { |
| 85 | + data = encodeURIComponent(JSON.stringify(dataObject)); |
| 86 | + createCookie(getCookieName(), data, 365); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Clears value of cookie data |
| 91 | + */ |
| 92 | + function clearData() { |
| 93 | + createCookie(getCookieName(), '', 365); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @returns value of cookie data |
| 98 | + */ |
| 99 | + function getData() { |
| 100 | + var dataResponse = readCookie(getCookieName()); |
| 101 | + |
| 102 | + return dataResponse ? JSON.parse(decodeURIComponent(dataResponse)) : {}; |
| 103 | + } |
| 104 | + |
| 105 | + data = getData(); |
| 106 | + |
| 107 | + return { |
| 108 | + length: 0, |
| 109 | + |
| 110 | + /** |
| 111 | + * Clears data from storage |
| 112 | + */ |
| 113 | + clear: function () { |
| 114 | + data = {}; |
| 115 | + this.length = 0; |
| 116 | + clearData(); |
| 117 | + }, |
| 118 | + |
| 119 | + /** |
| 120 | + * Gets an item from storage |
| 121 | + * @param {String} key |
| 122 | + */ |
| 123 | + getItem: function (key) { |
| 124 | + return data[key] === undefined ? null : data[key]; |
| 125 | + }, |
| 126 | + |
| 127 | + /** |
| 128 | + * Gets an item by index from storage |
| 129 | + * @param {Integer} i |
| 130 | + */ |
| 131 | + key: function (i) { |
| 132 | + var ctr = 0, |
| 133 | + k; |
| 134 | + |
| 135 | + for (k in data) { |
| 136 | + |
| 137 | + if (data.hasOwnProperty(k)) { |
| 138 | + |
| 139 | + // eslint-disable-next-line max-depth |
| 140 | + if (ctr.toString() === i.toString()) { |
| 141 | + return k; |
| 142 | + } |
| 143 | + ctr++; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return null; |
| 148 | + }, |
| 149 | + |
| 150 | + /** |
| 151 | + * Removes an item from storage |
| 152 | + * @param {String} key |
| 153 | + */ |
| 154 | + removeItem: function (key) { |
| 155 | + delete data[key]; |
| 156 | + this.length--; |
| 157 | + setData(data); |
| 158 | + }, |
| 159 | + |
| 160 | + /** |
| 161 | + * Sets an item from storage |
| 162 | + * @param {String} key |
| 163 | + * @param {String} value |
| 164 | + */ |
| 165 | + setItem: function (key, value) { |
| 166 | + data[key] = value.toString(); |
| 167 | + this.length++; |
| 168 | + setData(data); |
| 169 | + } |
| 170 | + }; |
| 171 | + }; |
| 172 | + |
| 173 | + window.localStorage.prototype = window.localStorage = new Storage('local'); |
| 174 | + window.sessionStorage.prototype = window.sessionStorage = new Storage('session'); |
| 175 | + })(); |
| 176 | +} |
0 commit comments