|
1 | 1 | (function () { |
2 | 2 |
|
3 | | - // Utils |
4 | | - Object.extend = function () { |
5 | | - var destination = typeof arguments[0] === 'object' ? arguments[0] : {}; |
6 | | - for (var i = 1; i < arguments.length; i++) { |
7 | | - if (arguments[i] && typeof arguments[i] === 'object') |
8 | | - for (var property in arguments[i]) |
9 | | - destination[property] = arguments[i][property]; |
10 | | - } |
11 | | - return destination; |
12 | | - }; |
13 | | - |
14 | 3 | // Basil |
15 | 4 | var Basil = function (options) { |
16 | 5 | return new Basil.Storage().init(options); |
17 | 6 | }; |
18 | 7 |
|
19 | | - Basil.version = '0.3.1'; |
| 8 | + // Version |
| 9 | + Basil.version = '0.3.2'; |
| 10 | + |
| 11 | + // Utils |
| 12 | + Basil.utils = { |
| 13 | + extend: function () { |
| 14 | + var destination = typeof arguments[0] === 'object' ? arguments[0] : {}; |
| 15 | + for (var i = 1; i < arguments.length; i++) { |
| 16 | + if (arguments[i] && typeof arguments[i] === 'object') |
| 17 | + for (var property in arguments[i]) |
| 18 | + destination[property] = arguments[i][property]; |
| 19 | + } |
| 20 | + return destination; |
| 21 | + } |
| 22 | + }; |
20 | 23 |
|
21 | | - Basil.options = Object.extend({ |
| 24 | + // Options |
| 25 | + Basil.options = Basil.utils.extend({ |
22 | 26 | namespace: 'b45i1', |
23 | 27 | storage: null, |
24 | 28 | storages: ['local', 'cookie', 'session', 'memory'], |
25 | 29 | expireDays: 365 |
26 | 30 | }, window.Basil ? window.Basil.options : {}); |
27 | 31 |
|
| 32 | + // Storage |
28 | 33 | Basil.Storage = function () { |
29 | 34 | var _salt = 'b45i1' + (Math.random() + 1) |
30 | 35 | .toString(36) |
|
77 | 82 | remove: function (name) { |
78 | 83 | this.engine.removeItem(name); |
79 | 84 | }, |
80 | | - reset: function () { |
| 85 | + reset: function (namespace) { |
81 | 86 | for (var key, i = 0; i < this.engine.length; i++) { |
82 | 87 | key = this.engine.key(i); |
83 | | - if (key.indexOf(this.options.namespace) === 0) |
| 88 | + if (key.indexOf(namespace) === 0) { |
84 | 89 | this.remove(key); |
| 90 | + i--; |
| 91 | + } |
85 | 92 | } |
86 | 93 | } |
87 | 94 | }; |
88 | 95 |
|
89 | 96 | // session storage |
90 | | - _storages['session'] = Object.extend({}, _storages['local'], { |
| 97 | + _storages['session'] = Basil.utils.extend({}, _storages['local'], { |
91 | 98 | engine: window.sessionStorage |
92 | 99 | }); |
93 | 100 |
|
|
108 | 115 | remove: function (name) { |
109 | 116 | delete this._hash[name]; |
110 | 117 | }, |
111 | | - reset: function () { |
| 118 | + reset: function (namespace) { |
112 | 119 | for (var key in this._hash) { |
113 | | - if (key.indexOf(this.options.namespace) === 0) |
| 120 | + if (key.indexOf(namespace) === 0) |
114 | 121 | this.remove(key); |
115 | 122 | } |
116 | 123 | } |
|
157 | 164 | this.set(name, '', { expireDays: -1, domain: '.' + domainParts.slice(- i).join('.') }); |
158 | 165 | } |
159 | 166 | }, |
160 | | - reset: function () { |
| 167 | + reset: function (namespace) { |
161 | 168 | var cookies = document.cookie.split(';'); |
162 | 169 |
|
163 | 170 | for (var i = 0; i < cookies.length; i++) { |
164 | 171 | var cookie = cookies[i].replace(/^\s*/, ''), |
165 | 172 | key = cookie.substr(0, cookie.indexOf('=')); |
166 | | - if (key.indexOf(this.options.namespace) === 0) |
| 173 | + if (key.indexOf(namespace) === 0) |
167 | 174 | this.remove(key); |
168 | 175 | } |
169 | 176 | } |
170 | 177 | }; |
171 | 178 |
|
172 | 179 | return { |
173 | 180 | init: function (options) { |
174 | | - this.options = Object.extend({}, Basil.options, options); |
| 181 | + this.options = Basil.utils.extend({}, Basil.options, options); |
175 | 182 |
|
176 | 183 | this.supportedStorages = {}; |
177 | 184 | for (var i = 0, storage; i < this.options.storages.length; i++) { |
|
200 | 207 | if (!(name = _toStoredKey(this.options.namespace, name))) |
201 | 208 | return; |
202 | 209 | value = _toStoredValue(value); |
203 | | - options = Object.extend({ |
| 210 | + options = Basil.utils.extend({ |
204 | 211 | expireDays: this.options.expireDays |
205 | 212 | }, options); |
206 | 213 |
|
|
245 | 252 | for (var i = 0; i < storages.length; i++) { |
246 | 253 | if (!this.check(storages[i])) |
247 | 254 | continue; |
248 | | - _storages[storages[i]].reset(); |
| 255 | + _storages[storages[i]].reset(this.options.namespace); |
249 | 256 | } |
250 | 257 | }, |
251 | 258 | // Access to native storages, without namespace or basil value decoration |
|
0 commit comments