polyfill.js 5.1 KB

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