|
| 1 | +'use strict'; |
1 | 2 |
|
2 |
| -import { window } from '@angular/platform-browser/src/facade/browser'; |
3 |
| -import { Json } from '@angular/platform-browser/src/facade/lang'; |
| 3 | +Object.defineProperty(exports, "__esModule", { |
| 4 | + value: true |
| 5 | +}); |
| 6 | +exports.StorageService = exports.StorageSettings = undefined; |
4 | 7 |
|
5 |
| -const storageAvailable = (type) => { |
| 8 | +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); |
| 9 | + |
| 10 | +var _browser = require('@angular/platform-browser/src/facade/browser'); |
| 11 | + |
| 12 | +var _lang = require('@angular/platform-browser/src/facade/lang'); |
| 13 | + |
| 14 | +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } |
| 15 | + |
| 16 | +var storageAvailable = function storageAvailable(type) { |
6 | 17 | try {
|
7 |
| - const storage = window[type]; |
8 |
| - const test = '__storage_test__'; |
| 18 | + var storage = _browser.window[type]; |
| 19 | + var test = '__storage_test__'; |
9 | 20 | storage.setItem(test, test);
|
10 | 21 | storage.removeItem(test);
|
11 | 22 | return true;
|
12 |
| - } |
13 |
| - catch(e) { |
| 23 | + } catch (e) { |
14 | 24 | return false;
|
15 | 25 | }
|
16 | 26 | };
|
17 | 27 |
|
18 |
| -const buildProxy = (type, { prefix, serialize }) => { |
19 |
| - const service = window[type]; |
| 28 | +var buildProxy = function buildProxy(type, _ref) { |
| 29 | + var prefix = _ref.prefix; |
| 30 | + var serialize = _ref.serialize; |
| 31 | + |
| 32 | + var service = _browser.window[type]; |
20 | 33 |
|
21 |
| - const proxyData = { |
22 |
| - get: (target, key) => { |
| 34 | + var proxyData = { |
| 35 | + get: function get(target, key) { |
23 | 36 |
|
24 | 37 | // || null to prevent undefined errors
|
25 |
| - return serialize.parse(target[`${prefix}-${key}`] || null); |
| 38 | + return serialize.parse(target[prefix + '-' + key] || null); |
26 | 39 | },
|
27 | 40 |
|
28 |
| - set: (target, key, value) => { |
29 |
| - return target[`${prefix}-${key}`] = serialize.stringify(value); |
| 41 | + set: function set(target, key, value) { |
| 42 | + return target[prefix + '-' + key] = serialize.stringify(value); |
30 | 43 | }
|
31 | 44 | };
|
32 | 45 |
|
33 | 46 | return new Proxy(service, proxyData);
|
34 | 47 | };
|
35 | 48 |
|
36 |
| -export class StorageSettings { |
37 |
| - constructor() {} |
38 |
| -} |
| 49 | +var StorageSettings = exports.StorageSettings = function StorageSettings() { |
| 50 | + _classCallCheck(this, StorageSettings); |
| 51 | +}; |
39 | 52 |
|
| 53 | +var StorageService = exports.StorageService = function () { |
| 54 | + _createClass(StorageService, null, [{ |
| 55 | + key: 'parameters', |
| 56 | + get: function get() { |
| 57 | + return [[StorageSettings]]; |
| 58 | + } |
| 59 | + }]); |
40 | 60 |
|
41 |
| -export class StorageService { |
42 |
| - static get parameters() { |
43 |
| - return [[StorageSettings]]; |
44 |
| - } |
| 61 | + function StorageService(storageSettings) { |
| 62 | + _classCallCheck(this, StorageService); |
45 | 63 |
|
46 |
| - constructor(storageSettings) { |
47 |
| - this.storageSettings = Object.assign({ prefix: 'ng2-storage', serialize: Json }, storageSettings); |
48 |
| - if(typeof this.storageSettings.prefix === 'undefined') { |
| 64 | + this.storageSettings = Object.assign({ prefix: 'ng2-storage', serialize: _lang.Json }, storageSettings); |
| 65 | + if (typeof this.storageSettings.prefix === 'undefined') { |
49 | 66 | throw new Error('storageSettings.prefix must be a string');
|
50 | 67 | }
|
51 | 68 |
|
52 |
| - if(!this.storageSettings.serialize) { |
| 69 | + if (!this.storageSettings.serialize) { |
53 | 70 | throw new Error('storageSettings.serialize must be an object { stringify, parse }');
|
54 | 71 | }
|
55 | 72 |
|
56 |
| - if(typeof this.storageSettings.serialize.stringify !== 'function') { |
| 73 | + if (typeof this.storageSettings.serialize.stringify !== 'function') { |
57 | 74 | throw new Error('storageSettings.serialize.stringify must be a function');
|
58 | 75 | }
|
59 | 76 |
|
60 |
| - if(typeof this.storageSettings.serialize.parse !== 'function') { |
| 77 | + if (typeof this.storageSettings.serialize.parse !== 'function') { |
61 | 78 | throw new Error('storageSettings.serialize.parse must be a function');
|
62 | 79 | }
|
63 | 80 |
|
64 |
| - if(!storageAvailable('localStorage')) { |
| 81 | + if (!storageAvailable('localStorage')) { |
65 | 82 | console.warn('localStorage is not available!');
|
66 | 83 | } else {
|
67 |
| - this.local = buildProxy('localStorage', this.storageSettings); |
| 84 | + this.local = buildProxy('localStorage', this.storageSettings); |
68 | 85 | }
|
69 | 86 |
|
70 |
| - if(!storageAvailable('sessionStorage')) { |
| 87 | + if (!storageAvailable('sessionStorage')) { |
71 | 88 | console.warn('sessionStorage is not available!');
|
72 | 89 | } else {
|
73 | 90 | this.session = buildProxy('sessionStorage', this.storageSettings);
|
74 | 91 | }
|
75 |
| - |
76 | 92 | }
|
77 |
| -} |
| 93 | + |
| 94 | + return StorageService; |
| 95 | +}(); |
0 commit comments