25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

179 satır
5.1 KiB

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