You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 3 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. 'use strict'
  2. // Module export pattern from
  3. // https://github.com/umdjs/umd/blob/master/returnExports.js
  4. ;(function (root, factory) {
  5. if (typeof define === 'function' && define.amd) {
  6. // AMD. Register as an anonymous module.
  7. define([], factory)
  8. } else if (typeof exports === 'object') {
  9. // Node. Does not work with strict CommonJS, but
  10. // only CommonJS-like environments that support module.exports,
  11. // like Node.
  12. module.exports = factory()
  13. } else {
  14. // Browser globals (root is window)
  15. root.store = factory()
  16. }
  17. }(this, function () {
  18. // Store.js
  19. var store = {}
  20. var win = (typeof window !== 'undefined' ? window : global)
  21. var doc = win.document
  22. var localStorageName = 'localStorage'
  23. var scriptTag = 'script'
  24. var storage
  25. store.disabled = false
  26. store.version = '1.3.20'
  27. store.set = function (key, value) {}
  28. store.get = function (key, defaultVal) {}
  29. store.has = function (key) { return store.get(key) !== undefined }
  30. store.remove = function (key) {}
  31. store.clear = function () {}
  32. store.transact = function (key, defaultVal, transactionFn) {
  33. if (transactionFn == null) {
  34. transactionFn = defaultVal
  35. defaultVal = null
  36. }
  37. if (defaultVal == null) {
  38. defaultVal = {}
  39. }
  40. var val = store.get(key, defaultVal)
  41. transactionFn(val)
  42. store.set(key, val)
  43. }
  44. store.getAll = function () {}
  45. store.forEach = function () {}
  46. store.serialize = function (value) {
  47. return JSON.stringify(value)
  48. }
  49. store.deserialize = function (value) {
  50. if (typeof value !== 'string') { return undefined }
  51. try { return JSON.parse(value) } catch (e) { return value || undefined }
  52. }
  53. // Functions to encapsulate questionable FireFox 3.6.13 behavior
  54. // when about.config::dom.storage.enabled === false
  55. // See https://github.com/marcuswestin/store.js/issues#issue/13
  56. function isLocalStorageNameSupported () {
  57. try { return (localStorageName in win && win[localStorageName]) } catch (err) { return false }
  58. }
  59. if (isLocalStorageNameSupported()) {
  60. storage = win[localStorageName]
  61. store.set = function (key, val) {
  62. if (val === undefined) { return store.remove(key) }
  63. storage.setItem(key, store.serialize(val))
  64. return val
  65. }
  66. store.get = function (key, defaultVal) {
  67. var val = store.deserialize(storage.getItem(key))
  68. return (val === undefined ? defaultVal : val)
  69. }
  70. store.remove = function (key) { storage.removeItem(key) }
  71. store.clear = function () { storage.clear() }
  72. store.getAll = function () {
  73. var ret = {}
  74. store.forEach(function (key, val) {
  75. ret[key] = val
  76. })
  77. return ret
  78. }
  79. store.forEach = function (callback) {
  80. for (var i = 0; i < storage.length; i++) {
  81. var key = storage.key(i)
  82. callback(key, store.get(key))
  83. }
  84. }
  85. } else if (doc && doc.documentElement.addBehavior) {
  86. var storageOwner,
  87. storageContainer
  88. // Since #userData storage applies only to specific paths, we need to
  89. // somehow link our data to a specific path. We choose /favicon.ico
  90. // as a pretty safe option, since all browsers already make a request to
  91. // this URL anyway and being a 404 will not hurt us here. We wrap an
  92. // iframe pointing to the favicon in an ActiveXObject(htmlfile) object
  93. // (see: http://msdn.microsoft.com/en-us/library/aa752574(v=VS.85).aspx)
  94. // since the iframe access rules appear to allow direct access and
  95. // manipulation of the document element, even for a 404 page. This
  96. // document can be used instead of the current document (which would
  97. // have been limited to the current path) to perform #userData storage.
  98. try {
  99. storageContainer = new ActiveXObject('htmlfile')
  100. storageContainer.open()
  101. storageContainer.write('<' + scriptTag + '>document.w=window</' + scriptTag + '><iframe src="/favicon.ico"></iframe>')
  102. storageContainer.close()
  103. storageOwner = storageContainer.w.frames[0].document
  104. storage = storageOwner.createElement('div')
  105. } catch (e) {
  106. // somehow ActiveXObject instantiation failed (perhaps some special
  107. // security settings or otherwse), fall back to per-path storage
  108. storage = doc.createElement('div')
  109. storageOwner = doc.body
  110. }
  111. var withIEStorage = function (storeFunction) {
  112. return function () {
  113. var args = Array.prototype.slice.call(arguments, 0)
  114. args.unshift(storage)
  115. // See http://msdn.microsoft.com/en-us/library/ms531081(v=VS.85).aspx
  116. // and http://msdn.microsoft.com/en-us/library/ms531424(v=VS.85).aspx
  117. storageOwner.appendChild(storage)
  118. storage.addBehavior('#default#userData')
  119. storage.load(localStorageName)
  120. var result = storeFunction.apply(store, args)
  121. storageOwner.removeChild(storage)
  122. return result
  123. }
  124. }
  125. // In IE7, keys cannot start with a digit or contain certain chars.
  126. // See https://github.com/marcuswestin/store.js/issues/40
  127. // See https://github.com/marcuswestin/store.js/issues/83
  128. var forbiddenCharsRegex = new RegExp("[!\"#$%&'()*+,/\\\\:;<=>?@[\\]^`{|}~]", 'g')
  129. var ieKeyFix = function (key) {
  130. return key.replace(/^d/, '___$&').replace(forbiddenCharsRegex, '___')
  131. }
  132. store.set = withIEStorage(function (storage, key, val) {
  133. key = ieKeyFix(key)
  134. if (val === undefined) { return store.remove(key) }
  135. storage.setAttribute(key, store.serialize(val))
  136. storage.save(localStorageName)
  137. return val
  138. })
  139. store.get = withIEStorage(function (storage, key, defaultVal) {
  140. key = ieKeyFix(key)
  141. var val = store.deserialize(storage.getAttribute(key))
  142. return (val === undefined ? defaultVal : val)
  143. })
  144. store.remove = withIEStorage(function (storage, key) {
  145. key = ieKeyFix(key)
  146. storage.removeAttribute(key)
  147. storage.save(localStorageName)
  148. })
  149. store.clear = withIEStorage(function (storage) {
  150. var attributes = storage.XMLDocument.documentElement.attributes
  151. storage.load(localStorageName)
  152. for (var i = attributes.length - 1; i >= 0; i--) {
  153. storage.removeAttribute(attributes[i].name)
  154. }
  155. storage.save(localStorageName)
  156. })
  157. store.getAll = function (storage) {
  158. var ret = {}
  159. store.forEach(function (key, val) {
  160. ret[key] = val
  161. })
  162. return ret
  163. }
  164. store.forEach = withIEStorage(function (storage, callback) {
  165. var attributes = storage.XMLDocument.documentElement.attributes
  166. for (var i = 0, attr; attr = attributes[i]; ++i) {
  167. callback(attr.name, store.deserialize(storage.getAttribute(attr.name)))
  168. }
  169. })
  170. }
  171. try {
  172. var testKey = '__storejs__'
  173. store.set(testKey, testKey)
  174. if (store.get(testKey) != testKey) { store.disabled = true }
  175. store.remove(testKey)
  176. } catch (e) {
  177. store.disabled = true
  178. }
  179. store.enabled = !store.disabled
  180. return store
  181. }))