1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | ;(function(){
|
23 | var store = {},
|
24 | win = window,
|
25 | doc = win.document,
|
26 | localStorageName = 'localStorage',
|
27 | namespace = '__storejs__',
|
28 | storage
|
29 |
|
30 | store.disabled = false
|
31 | store.set = function(key, value) {}
|
32 | store.get = function(key) {}
|
33 | store.remove = function(key) {}
|
34 | store.clear = function() {}
|
35 | store.transact = function(key, defaultVal, transactionFn) {
|
36 | var val = store.get(key)
|
37 | if (transactionFn == null) {
|
38 | transactionFn = defaultVal
|
39 | defaultVal = null
|
40 | }
|
41 | if (typeof val == 'undefined') { val = defaultVal || {} }
|
42 | transactionFn(val)
|
43 | store.set(key, val)
|
44 | }
|
45 | store.getAll = function() {}
|
46 |
|
47 | store.serialize = function(value) {
|
48 | return JSON.stringify(value)
|
49 | }
|
50 | store.deserialize = function(value) {
|
51 | if (typeof value != 'string') { return undefined }
|
52 | try { return JSON.parse(value) }
|
53 | catch(e) { return value || undefined }
|
54 | }
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | function isLocalStorageNameSupported() {
|
60 | try { return (localStorageName in win && win[localStorageName]) }
|
61 | catch(err) { return false }
|
62 | }
|
63 |
|
64 | if (isLocalStorageNameSupported()) {
|
65 | storage = win[localStorageName]
|
66 | store.set = function(key, val) {
|
67 | if (val === undefined) { return store.remove(key) }
|
68 | storage.setItem(key, store.serialize(val))
|
69 | return val
|
70 | }
|
71 | store.get = function(key) { return store.deserialize(storage.getItem(key)) }
|
72 | store.remove = function(key) { storage.removeItem(key) }
|
73 | store.clear = function() { storage.clear() }
|
74 | store.getAll = function() {
|
75 | var ret = {}
|
76 | for (var i=0; i<storage.length; ++i) {
|
77 | var key = storage.key(i)
|
78 | ret[key] = store.get(key)
|
79 | }
|
80 | return ret
|
81 | }
|
82 | } else if (doc.documentElement.addBehavior) {
|
83 | var storageOwner,
|
84 | storageContainer
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | try {
|
96 | storageContainer = new ActiveXObject('htmlfile')
|
97 | storageContainer.open()
|
98 | storageContainer.write('<s' + 'cript>document.w=window</s' + 'cript><iframe src="/favicon.ico"></frame>')
|
99 | storageContainer.close()
|
100 | storageOwner = storageContainer.w.frames[0].document
|
101 | storage = storageOwner.createElement('div')
|
102 | } catch(e) {
|
103 |
|
104 |
|
105 | storage = doc.createElement('div')
|
106 | storageOwner = doc.body
|
107 | }
|
108 | function withIEStorage(storeFunction) {
|
109 | return function() {
|
110 | var args = Array.prototype.slice.call(arguments, 0)
|
111 | args.unshift(storage)
|
112 |
|
113 |
|
114 | storageOwner.appendChild(storage)
|
115 | storage.addBehavior('#default#userData')
|
116 | storage.load(localStorageName)
|
117 | var result = storeFunction.apply(store, args)
|
118 | storageOwner.removeChild(storage)
|
119 | return result
|
120 | }
|
121 | }
|
122 |
|
123 |
|
124 | var forbiddenCharsRegex = new RegExp("[!\"#$%&'()*+,/\\\\:;<=>?@[\\]^`{|}~]", "g")
|
125 | function ieKeyFix(key) {
|
126 | return key.replace(forbiddenCharsRegex, '___')
|
127 | }
|
128 | store.set = withIEStorage(function(storage, key, val) {
|
129 | key = ieKeyFix(key)
|
130 | if (val === undefined) { return store.remove(key) }
|
131 | storage.setAttribute(key, store.serialize(val))
|
132 | storage.save(localStorageName)
|
133 | return val
|
134 | })
|
135 | store.get = withIEStorage(function(storage, key) {
|
136 | key = ieKeyFix(key)
|
137 | return store.deserialize(storage.getAttribute(key))
|
138 | })
|
139 | store.remove = withIEStorage(function(storage, key) {
|
140 | key = ieKeyFix(key)
|
141 | storage.removeAttribute(key)
|
142 | storage.save(localStorageName)
|
143 | })
|
144 | store.clear = withIEStorage(function(storage) {
|
145 | var attributes = storage.XMLDocument.documentElement.attributes
|
146 | storage.load(localStorageName)
|
147 | for (var i=0, attr; attr=attributes[i]; i++) {
|
148 | storage.removeAttribute(attr.name)
|
149 | }
|
150 | storage.save(localStorageName)
|
151 | })
|
152 | store.getAll = withIEStorage(function(storage) {
|
153 | var attributes = storage.XMLDocument.documentElement.attributes
|
154 | storage.load(localStorageName)
|
155 | var ret = {}
|
156 | for (var i=0, attr; attr=attributes[i]; ++i) {
|
157 | ret[attr] = store.get(attr)
|
158 | }
|
159 | return ret
|
160 | })
|
161 | }
|
162 |
|
163 | try {
|
164 | store.set(namespace, namespace)
|
165 | if (store.get(namespace) != namespace) { store.disabled = true }
|
166 | store.remove(namespace)
|
167 | } catch(e) {
|
168 | store.disabled = true
|
169 | }
|
170 | store.enabled = !store.disabled
|
171 |
|
172 | if (typeof module != 'undefined' && typeof module != 'function') { module.exports = store }
|
173 | else if (typeof define === 'function' && define.amd) { define(store) }
|
174 | else { this.store = store }
|
175 | })();
|