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