1 | (function (global, factory) {
|
2 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
3 | typeof define === 'function' && define.amd ? define(factory) :
|
4 | (global = global || self, global.VueStorage = factory());
|
5 | }(this, function () { 'use strict';
|
6 |
|
7 | function _classCallCheck(instance, Constructor) {
|
8 | if (!(instance instanceof Constructor)) {
|
9 | throw new TypeError("Cannot call a class as a function");
|
10 | }
|
11 | }
|
12 |
|
13 | function _defineProperties(target, props) {
|
14 | for (var i = 0; i < props.length; i++) {
|
15 | var descriptor = props[i];
|
16 | descriptor.enumerable = descriptor.enumerable || false;
|
17 | descriptor.configurable = true;
|
18 | if ("value" in descriptor) descriptor.writable = true;
|
19 | Object.defineProperty(target, descriptor.key, descriptor);
|
20 | }
|
21 | }
|
22 |
|
23 | function _createClass(Constructor, protoProps, staticProps) {
|
24 | if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
25 | if (staticProps) _defineProperties(Constructor, staticProps);
|
26 | return Constructor;
|
27 | }
|
28 |
|
29 |
|
30 | var ls = {};
|
31 |
|
32 | var MemoryStorageInterface =
|
33 |
|
34 | function () {
|
35 | function MemoryStorageInterface() {
|
36 | _classCallCheck(this, MemoryStorageInterface);
|
37 |
|
38 | Object.defineProperty(this, 'length', {
|
39 | |
40 |
|
41 |
|
42 |
|
43 |
|
44 | get: function get() {
|
45 | return Object.keys(ls).length;
|
46 | }
|
47 | });
|
48 | }
|
49 | |
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | _createClass(MemoryStorageInterface, [{
|
58 | key: "getItem",
|
59 | value: function getItem(name) {
|
60 | return name in ls ? ls[name] : null;
|
61 | }
|
62 | |
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | }, {
|
71 | key: "setItem",
|
72 | value: function setItem(name, value) {
|
73 | ls[name] = value;
|
74 | return true;
|
75 | }
|
76 | |
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | }, {
|
84 | key: "removeItem",
|
85 | value: function removeItem(name) {
|
86 | var found = name in ls;
|
87 |
|
88 | if (found) {
|
89 | return delete ls[name];
|
90 | }
|
91 |
|
92 | return false;
|
93 | }
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | }, {
|
101 | key: "clear",
|
102 | value: function clear() {
|
103 | ls = {};
|
104 | return true;
|
105 | }
|
106 | |
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | }, {
|
114 | key: "key",
|
115 | value: function key(index) {
|
116 | var keys = Object.keys(ls);
|
117 | return typeof keys[index] !== 'undefined' ? keys[index] : null;
|
118 | }
|
119 | }]);
|
120 |
|
121 | return MemoryStorageInterface;
|
122 | }();
|
123 |
|
124 | var MemoryStorage = new MemoryStorageInterface();
|
125 |
|
126 | var listeners = {};
|
127 | |
128 |
|
129 |
|
130 |
|
131 | var WebStorageEvent =
|
132 |
|
133 | function () {
|
134 | function WebStorageEvent() {
|
135 | _classCallCheck(this, WebStorageEvent);
|
136 | }
|
137 |
|
138 | _createClass(WebStorageEvent, null, [{
|
139 | key: "on",
|
140 |
|
141 | |
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | value: function on(name, callback) {
|
148 | if (typeof listeners[name] === 'undefined') {
|
149 | listeners[name] = [];
|
150 | }
|
151 |
|
152 | listeners[name].push(callback);
|
153 | }
|
154 | |
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | }, {
|
162 | key: "off",
|
163 | value: function off(name, callback) {
|
164 | if (listeners[name].length) {
|
165 | listeners[name].splice(listeners[name].indexOf(callback), 1);
|
166 | } else {
|
167 | listeners[name] = [];
|
168 | }
|
169 | }
|
170 | |
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | }, {
|
177 | key: "emit",
|
178 | value: function emit(event) {
|
179 | var e = event || window.event;
|
180 |
|
181 | var getValue = function getValue(data) {
|
182 | try {
|
183 | return JSON.parse(data).value;
|
184 | } catch (err) {
|
185 | return data;
|
186 | }
|
187 | };
|
188 |
|
189 | var fire = function fire(listener) {
|
190 | var newValue = getValue(e.newValue);
|
191 | var oldValue = getValue(e.oldValue);
|
192 | listener(newValue, oldValue, e.url || e.uri);
|
193 | };
|
194 |
|
195 | if (typeof e === 'undefined' || typeof e.key === 'undefined') {
|
196 | return;
|
197 | }
|
198 |
|
199 | var all = listeners[e.key];
|
200 |
|
201 | if (typeof all !== 'undefined') {
|
202 | all.forEach(fire);
|
203 | }
|
204 | }
|
205 | }]);
|
206 |
|
207 | return WebStorageEvent;
|
208 | }();
|
209 |
|
210 | |
211 |
|
212 |
|
213 |
|
214 | var WebStorage =
|
215 |
|
216 | function () {
|
217 | |
218 |
|
219 |
|
220 | function WebStorage(storage) {
|
221 | _classCallCheck(this, WebStorage);
|
222 |
|
223 | this.storage = storage;
|
224 | this.options = {
|
225 | namespace: '',
|
226 | events: ['storage']
|
227 | };
|
228 | Object.defineProperty(this, 'length', {
|
229 | |
230 |
|
231 |
|
232 |
|
233 |
|
234 | get: function get() {
|
235 | return this.storage.length;
|
236 | }
|
237 | });
|
238 |
|
239 | if (typeof window !== 'undefined') {
|
240 | for (var i in this.options.events) {
|
241 | if (window.addEventListener) {
|
242 | window.addEventListener(this.options.events[i], WebStorageEvent.emit, false);
|
243 | } else if (window.attachEvent) {
|
244 | window.attachEvent("on".concat(this.options.events[i]), WebStorageEvent.emit);
|
245 | } else {
|
246 | window["on".concat(this.options.events[i])] = WebStorageEvent.emit;
|
247 | }
|
248 | }
|
249 | }
|
250 | }
|
251 | |
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 | _createClass(WebStorage, [{
|
259 | key: "setOptions",
|
260 | value: function setOptions() {
|
261 | var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
262 | this.options = Object.assign(this.options, options);
|
263 | }
|
264 | |
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 | }, {
|
273 | key: "set",
|
274 | value: function set(name, value) {
|
275 | var expire = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
|
276 | var stringifyValue = JSON.stringify({
|
277 | value: value,
|
278 | expire: expire !== null ? new Date().getTime() + expire : null
|
279 | });
|
280 | this.storage.setItem(this.options.namespace + name, stringifyValue);
|
281 | }
|
282 | |
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 | }, {
|
291 | key: "get",
|
292 | value: function get(name) {
|
293 | var def = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
294 | var item = this.storage.getItem(this.options.namespace + name);
|
295 |
|
296 | if (item !== null) {
|
297 | try {
|
298 | var data = JSON.parse(item);
|
299 |
|
300 | if (data.expire === null) {
|
301 | return data.value;
|
302 | }
|
303 |
|
304 | if (data.expire >= new Date().getTime()) {
|
305 | return data.value;
|
306 | }
|
307 |
|
308 | this.remove(name);
|
309 | } catch (err) {
|
310 | return def;
|
311 | }
|
312 | }
|
313 |
|
314 | return def;
|
315 | }
|
316 | |
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 | }, {
|
324 | key: "key",
|
325 | value: function key(index) {
|
326 | return this.storage.key(index);
|
327 | }
|
328 | |
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 | }, {
|
336 | key: "remove",
|
337 | value: function remove(name) {
|
338 | return this.storage.removeItem(this.options.namespace + name);
|
339 | }
|
340 | |
341 |
|
342 |
|
343 |
|
344 | }, {
|
345 | key: "clear",
|
346 | value: function clear() {
|
347 | if (this.length === 0) {
|
348 | return;
|
349 | }
|
350 |
|
351 | var removedKeys = [];
|
352 |
|
353 | for (var i = 0; i < this.length; i++) {
|
354 | var key = this.storage.key(i);
|
355 | var regexp = new RegExp("^".concat(this.options.namespace, ".+"), 'i');
|
356 |
|
357 | if (regexp.test(key) === false) {
|
358 | continue;
|
359 | }
|
360 |
|
361 | removedKeys.push(key);
|
362 | }
|
363 |
|
364 | for (var _key in removedKeys) {
|
365 | this.storage.removeItem(removedKeys[_key]);
|
366 | }
|
367 | }
|
368 | |
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 | }, {
|
376 | key: "on",
|
377 | value: function on(name, callback) {
|
378 | WebStorageEvent.on(this.options.namespace + name, callback);
|
379 | }
|
380 | |
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 | }, {
|
388 | key: "off",
|
389 | value: function off(name, callback) {
|
390 | WebStorageEvent.off(this.options.namespace + name, callback);
|
391 | }
|
392 | }]);
|
393 |
|
394 | return WebStorage;
|
395 | }();
|
396 |
|
397 | var _global = typeof window !== 'undefined' ? window : global || {};
|
398 | |
399 |
|
400 |
|
401 |
|
402 |
|
403 | var VueStorage = {
|
404 | |
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 | install: function install(Vue) {
|
412 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
413 |
|
414 | var _options = Object.assign({}, options, {
|
415 | storage: options.storage || 'local',
|
416 | name: options.name || 'ls'
|
417 | });
|
418 |
|
419 | if (_options.storage && ['memory', 'local', 'session'].indexOf(_options.storage) === -1) {
|
420 | throw new Error("Vue-ls: Storage \"".concat(_options.storage, "\" is not supported"));
|
421 | }
|
422 |
|
423 | var store = null;
|
424 |
|
425 | switch (_options.storage) {
|
426 |
|
427 | case 'local':
|
428 | store = 'localStorage' in _global ? _global.localStorage : null;
|
429 | break;
|
430 |
|
431 | case 'session':
|
432 | store = 'sessionStorage' in _global ? _global.sessionStorage : null;
|
433 | break;
|
434 |
|
435 | case 'memory':
|
436 | store = MemoryStorage;
|
437 | break;
|
438 | }
|
439 |
|
440 | if (!store) {
|
441 | store = MemoryStorage;
|
442 |
|
443 | console.error("Vue-ls: Storage \"".concat(_options.storage, "\" is not supported your system, use memory storage"));
|
444 | }
|
445 |
|
446 | var ls = new WebStorage(store);
|
447 | ls.setOptions(Object.assign(ls.options, {
|
448 | namespace: ''
|
449 | }, _options || {}));
|
450 | Vue[_options.name] = ls;
|
451 |
|
452 | Object.defineProperty(Vue.prototype, "$".concat(_options.name), {
|
453 | |
454 |
|
455 |
|
456 |
|
457 |
|
458 | get: function get() {
|
459 | return ls;
|
460 | }
|
461 | });
|
462 | }
|
463 | };
|
464 | _global.VueStorage = VueStorage;
|
465 |
|
466 | return VueStorage;
|
467 |
|
468 | }));
|