@etsoo/shared 1.0.80 → 1.0.84
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/__tests__/StorageUtils.ts +1 -0
- package/__tests__/Utils.ts +1 -0
- package/lib/cjs/DomUtils.d.ts +2 -12
- package/lib/cjs/DomUtils.js +6 -22
- package/lib/cjs/StorageUtils.d.ts +13 -2
- package/lib/cjs/StorageUtils.js +7 -0
- package/lib/cjs/Utils.d.ts +8 -1
- package/lib/cjs/Utils.js +1 -0
- package/lib/cjs/index.d.ts +2 -0
- package/lib/cjs/index.js +2 -0
- package/lib/cjs/storage/IStorage.d.ts +33 -0
- package/lib/cjs/storage/IStorage.js +2 -0
- package/lib/cjs/storage/WindowStorage.d.ts +35 -0
- package/lib/cjs/storage/WindowStorage.js +68 -0
- package/lib/mjs/DomUtils.d.ts +2 -12
- package/lib/mjs/DomUtils.js +6 -22
- package/lib/mjs/StorageUtils.d.ts +13 -2
- package/lib/mjs/StorageUtils.js +7 -0
- package/lib/mjs/Utils.d.ts +8 -1
- package/lib/mjs/Utils.js +1 -0
- package/lib/mjs/index.d.ts +2 -0
- package/lib/mjs/index.js +2 -0
- package/lib/mjs/storage/IStorage.d.ts +33 -0
- package/lib/mjs/storage/IStorage.js +1 -0
- package/lib/mjs/storage/WindowStorage.d.ts +35 -0
- package/lib/mjs/storage/WindowStorage.js +64 -0
- package/package.json +1 -1
- package/src/DomUtils.ts +8 -22
- package/src/StorageUtils.ts +41 -8
- package/src/Utils.ts +29 -6
- package/src/index.ts +4 -0
- package/src/storage/IStorage.ts +37 -0
- package/src/storage/WindowStorage.ts +81 -0
package/README.md
CHANGED
|
@@ -15,6 +15,9 @@ Using yarn:
|
|
|
15
15
|
$ yarn add @etsoo/shared
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
+
## storage
|
|
19
|
+
Storage interface and browser storage implementation
|
|
20
|
+
|
|
18
21
|
## DataTypes
|
|
19
22
|
Data type definitions and type safe functions
|
|
20
23
|
|
|
@@ -92,8 +95,6 @@ DOM/window related utilities
|
|
|
92
95
|
|isJSONContentType|Is JSON content type|
|
|
93
96
|
|mergeFormData|Merge form data to primary one|
|
|
94
97
|
|mergeURLSearchParams|Merge URL search parameters|
|
|
95
|
-
|saveCountry|Save country name|
|
|
96
|
-
|saveCulture|Save culture name|
|
|
97
98
|
|setFocus|Set HTML element focus by name|
|
|
98
99
|
|
|
99
100
|
## ExtendUtils
|
|
@@ -8,6 +8,7 @@ test('Tests for all', () => {
|
|
|
8
8
|
StorageUtils.setSessionData('test', { id: 123, name: 'test' });
|
|
9
9
|
|
|
10
10
|
expect(StorageUtils.getSessionData<string>('string')).toBe('test');
|
|
11
|
+
expect(StorageUtils.getSessionData('string1', '')).toBe('');
|
|
11
12
|
expect(StorageUtils.getSessionData('boolean', false)).toBe(true);
|
|
12
13
|
expect(StorageUtils.getSessionData('number', 0)).toBe(3.14);
|
|
13
14
|
expect(StorageUtils.getSessionData('test', {})).toHaveProperty('id', 123);
|
package/__tests__/Utils.ts
CHANGED
|
@@ -101,6 +101,7 @@ test('Tests for objectUpdated', () => {
|
|
|
101
101
|
|
|
102
102
|
test('Tests for parseString', () => {
|
|
103
103
|
expect(Utils.parseString<string>('test')).toBe('test');
|
|
104
|
+
expect(Utils.parseString('test', '')).toBe('test');
|
|
104
105
|
expect(Utils.parseString('true', false)).toBe(true);
|
|
105
106
|
expect(Utils.parseString('', false)).toBeFalsy();
|
|
106
107
|
expect(Utils.parseString<boolean>('')).toBeUndefined();
|
package/lib/cjs/DomUtils.d.ts
CHANGED
|
@@ -8,11 +8,11 @@ export declare namespace DomUtils {
|
|
|
8
8
|
/**
|
|
9
9
|
* Language cache parameter name
|
|
10
10
|
*/
|
|
11
|
-
const
|
|
11
|
+
const CultureField = "EtsooCulture";
|
|
12
12
|
/**
|
|
13
13
|
* Country cache parameter name
|
|
14
14
|
*/
|
|
15
|
-
const
|
|
15
|
+
const CountryField = "EtsooCountry";
|
|
16
16
|
/**
|
|
17
17
|
* Clear form data
|
|
18
18
|
* @param data Form data
|
|
@@ -94,16 +94,6 @@ export declare namespace DomUtils {
|
|
|
94
94
|
* @param data New simple object data to merge
|
|
95
95
|
*/
|
|
96
96
|
function mergeURLSearchParams(base: URLSearchParams, data: DataTypes.SimpleObject): URLSearchParams;
|
|
97
|
-
/**
|
|
98
|
-
* Save country name
|
|
99
|
-
* @param country Country name
|
|
100
|
-
*/
|
|
101
|
-
function saveCountry(country: string): void;
|
|
102
|
-
/**
|
|
103
|
-
* Save culture name
|
|
104
|
-
* @param culture Culture name
|
|
105
|
-
*/
|
|
106
|
-
function saveCulture(culture: string): void;
|
|
107
97
|
/**
|
|
108
98
|
* Set HTML element focus by name
|
|
109
99
|
* @param name Element name or first collection item
|
package/lib/cjs/DomUtils.js
CHANGED
|
@@ -16,11 +16,11 @@ var DomUtils;
|
|
|
16
16
|
/**
|
|
17
17
|
* Language cache parameter name
|
|
18
18
|
*/
|
|
19
|
-
DomUtils.
|
|
19
|
+
DomUtils.CultureField = 'EtsooCulture';
|
|
20
20
|
/**
|
|
21
21
|
* Country cache parameter name
|
|
22
22
|
*/
|
|
23
|
-
DomUtils.
|
|
23
|
+
DomUtils.CountryField = 'EtsooCountry';
|
|
24
24
|
/**
|
|
25
25
|
* Clear form data
|
|
26
26
|
* @param data Form data
|
|
@@ -156,12 +156,12 @@ var DomUtils;
|
|
|
156
156
|
* Current detected country
|
|
157
157
|
*/
|
|
158
158
|
DomUtils.detectedCountry = (() => {
|
|
159
|
+
var _a, _b;
|
|
159
160
|
// URL first, then local storage
|
|
160
161
|
let country;
|
|
161
162
|
try {
|
|
162
163
|
country =
|
|
163
|
-
new URL(location.href).searchParams.get(DomUtils.
|
|
164
|
-
localStorage.getItem(DomUtils.Country);
|
|
164
|
+
(_b = (_a = new URL(location.href).searchParams.get(DomUtils.CountryField)) !== null && _a !== void 0 ? _a : sessionStorage.getItem(DomUtils.CountryField)) !== null && _b !== void 0 ? _b : localStorage.getItem(DomUtils.CountryField);
|
|
165
165
|
}
|
|
166
166
|
catch {
|
|
167
167
|
country = null;
|
|
@@ -173,12 +173,12 @@ var DomUtils;
|
|
|
173
173
|
* Current detected culture
|
|
174
174
|
*/
|
|
175
175
|
DomUtils.detectedCulture = (() => {
|
|
176
|
+
var _a, _b;
|
|
176
177
|
// URL first, then local storage
|
|
177
178
|
let culture;
|
|
178
179
|
try {
|
|
179
180
|
culture =
|
|
180
|
-
new URL(location.href).searchParams.get(DomUtils.
|
|
181
|
-
localStorage.getItem(DomUtils.Culture);
|
|
181
|
+
(_b = (_a = new URL(location.href).searchParams.get(DomUtils.CultureField)) !== null && _a !== void 0 ? _a : sessionStorage.getItem(DomUtils.CultureField)) !== null && _b !== void 0 ? _b : localStorage.getItem(DomUtils.CultureField);
|
|
182
182
|
}
|
|
183
183
|
catch {
|
|
184
184
|
culture = null;
|
|
@@ -342,22 +342,6 @@ var DomUtils;
|
|
|
342
342
|
return base;
|
|
343
343
|
}
|
|
344
344
|
DomUtils.mergeURLSearchParams = mergeURLSearchParams;
|
|
345
|
-
/**
|
|
346
|
-
* Save country name
|
|
347
|
-
* @param country Country name
|
|
348
|
-
*/
|
|
349
|
-
function saveCountry(country) {
|
|
350
|
-
localStorage.setItem(DomUtils.Country, country);
|
|
351
|
-
}
|
|
352
|
-
DomUtils.saveCountry = saveCountry;
|
|
353
|
-
/**
|
|
354
|
-
* Save culture name
|
|
355
|
-
* @param culture Culture name
|
|
356
|
-
*/
|
|
357
|
-
function saveCulture(culture) {
|
|
358
|
-
localStorage.setItem(DomUtils.Culture, culture);
|
|
359
|
-
}
|
|
360
|
-
DomUtils.saveCulture = saveCulture;
|
|
361
345
|
/**
|
|
362
346
|
* Set HTML element focus by name
|
|
363
347
|
* @param name Element name or first collection item
|
|
@@ -15,12 +15,17 @@ export declare namespace StorageUtils {
|
|
|
15
15
|
* @param data Data, null for removal
|
|
16
16
|
*/
|
|
17
17
|
function setSessionData(key: string, data: unknown): void;
|
|
18
|
+
/**
|
|
19
|
+
* Get local storage data
|
|
20
|
+
* @param key Key name
|
|
21
|
+
*/
|
|
22
|
+
function getLocalData<T>(key: string): T | undefined;
|
|
18
23
|
/**
|
|
19
24
|
* Get local storage data
|
|
20
25
|
* @param key Key name
|
|
21
26
|
* @param defaultValue Default value
|
|
22
27
|
*/
|
|
23
|
-
function getLocalData<T
|
|
28
|
+
function getLocalData<T>(key: string, defaultValue: T): T;
|
|
24
29
|
/**
|
|
25
30
|
* Get local storage object data
|
|
26
31
|
* @param key Key name
|
|
@@ -30,7 +35,13 @@ export declare namespace StorageUtils {
|
|
|
30
35
|
* Get session storage data
|
|
31
36
|
* @param key Key name
|
|
32
37
|
*/
|
|
33
|
-
function getSessionData<T
|
|
38
|
+
function getSessionData<T>(key: string): T | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Get session storage data with default value
|
|
41
|
+
* @param key Key name
|
|
42
|
+
* @param defaultValue Default value
|
|
43
|
+
*/
|
|
44
|
+
function getSessionData<T>(key: string, defaultValue: T): T;
|
|
34
45
|
/**
|
|
35
46
|
* Get session storage object data
|
|
36
47
|
* @param key Key name
|
package/lib/cjs/StorageUtils.js
CHANGED
|
@@ -47,6 +47,9 @@ var StorageUtils;
|
|
|
47
47
|
function getLocalData(key, defaultValue) {
|
|
48
48
|
// Get storage
|
|
49
49
|
const data = localStorage.getItem(key);
|
|
50
|
+
// No default value
|
|
51
|
+
if (defaultValue == null)
|
|
52
|
+
return Utils_1.Utils.parseString(data);
|
|
50
53
|
// Return
|
|
51
54
|
return Utils_1.Utils.parseString(data, defaultValue);
|
|
52
55
|
}
|
|
@@ -66,10 +69,14 @@ var StorageUtils;
|
|
|
66
69
|
/**
|
|
67
70
|
* Get session storage data
|
|
68
71
|
* @param key Key name
|
|
72
|
+
* @param defaultValue Default value
|
|
69
73
|
*/
|
|
70
74
|
function getSessionData(key, defaultValue) {
|
|
71
75
|
// Get storage
|
|
72
76
|
const data = sessionStorage.getItem(key);
|
|
77
|
+
// No default value
|
|
78
|
+
if (defaultValue == null)
|
|
79
|
+
return Utils_1.Utils.parseString(data);
|
|
73
80
|
// Return
|
|
74
81
|
return Utils_1.Utils.parseString(data, defaultValue);
|
|
75
82
|
}
|
package/lib/cjs/Utils.d.ts
CHANGED
|
@@ -134,6 +134,13 @@ export declare namespace Utils {
|
|
|
134
134
|
* @returns Updated fields
|
|
135
135
|
*/
|
|
136
136
|
function objectUpdated(objNew: {}, objPrev: {}, ignoreFields?: string[], strict?: number): string[];
|
|
137
|
+
/**
|
|
138
|
+
* Parse string (JSON) to specific type, no type conversion
|
|
139
|
+
* For type conversion, please use DataTypes.convert
|
|
140
|
+
* @param input Input string
|
|
141
|
+
* @returns Parsed value
|
|
142
|
+
*/
|
|
143
|
+
function parseString<T>(input: string | undefined | null): T | undefined;
|
|
137
144
|
/**
|
|
138
145
|
* Parse string (JSON) to specific type, no type conversion
|
|
139
146
|
* For type conversion, please use DataTypes.convert
|
|
@@ -141,7 +148,7 @@ export declare namespace Utils {
|
|
|
141
148
|
* @param defaultValue Default value
|
|
142
149
|
* @returns Parsed value
|
|
143
150
|
*/
|
|
144
|
-
function parseString<T
|
|
151
|
+
function parseString<T>(input: string | undefined | null, defaultValue: T): T;
|
|
145
152
|
/**
|
|
146
153
|
* Remove non letters
|
|
147
154
|
* @param input Input string
|
package/lib/cjs/Utils.js
CHANGED
|
@@ -268,6 +268,7 @@ var Utils;
|
|
|
268
268
|
Utils.objectUpdated = objectUpdated;
|
|
269
269
|
/**
|
|
270
270
|
* Parse string (JSON) to specific type, no type conversion
|
|
271
|
+
* When return type depends on parameter value, uses function overloading, otherwise uses conditional type
|
|
271
272
|
* For type conversion, please use DataTypes.convert
|
|
272
273
|
* @param input Input string
|
|
273
274
|
* @param defaultValue Default value
|
package/lib/cjs/index.d.ts
CHANGED
package/lib/cjs/index.js
CHANGED
|
@@ -11,6 +11,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
11
11
|
};
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
13
|
__exportStar(require("./types/FormData"), exports);
|
|
14
|
+
__exportStar(require("./storage/IStorage"), exports);
|
|
15
|
+
__exportStar(require("./storage/WindowStorage"), exports);
|
|
14
16
|
__exportStar(require("./DataTypes"), exports);
|
|
15
17
|
__exportStar(require("./DateUtils"), exports);
|
|
16
18
|
__exportStar(require("./DomUtils"), exports);
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage interface
|
|
3
|
+
*/
|
|
4
|
+
export interface IStorage {
|
|
5
|
+
/**
|
|
6
|
+
* Get data
|
|
7
|
+
* @param key Key name
|
|
8
|
+
*/
|
|
9
|
+
getData<T>(key: string): T | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Get data with default value
|
|
12
|
+
* @param key Key name
|
|
13
|
+
* @param defaultValue Default value
|
|
14
|
+
*/
|
|
15
|
+
getData<T>(key: string, defaultValue: T): T;
|
|
16
|
+
/**
|
|
17
|
+
* Get session storage object data
|
|
18
|
+
* @param key Key name
|
|
19
|
+
*/
|
|
20
|
+
getObject<T extends {}>(key: string): T | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Set data
|
|
23
|
+
* @param key Key name
|
|
24
|
+
* @param data Data, null for removal
|
|
25
|
+
*/
|
|
26
|
+
setData(key: string, data: unknown): void;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Storage constructor interface
|
|
30
|
+
*/
|
|
31
|
+
export interface IStorageConstructor {
|
|
32
|
+
new (globalFields: string[]): IStorage;
|
|
33
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { IStorage } from './IStorage';
|
|
2
|
+
/**
|
|
3
|
+
* Window storage
|
|
4
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/Storage
|
|
5
|
+
*/
|
|
6
|
+
export declare class WindowStorage implements IStorage {
|
|
7
|
+
private globalFields;
|
|
8
|
+
/**
|
|
9
|
+
* Constructor
|
|
10
|
+
* @param globalFields Global fields
|
|
11
|
+
*/
|
|
12
|
+
constructor(globalFields: string[]);
|
|
13
|
+
/**
|
|
14
|
+
* Get data
|
|
15
|
+
* @param key Key name
|
|
16
|
+
*/
|
|
17
|
+
getData<T>(key: string): T | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Get data with default value
|
|
20
|
+
* @param key Key name
|
|
21
|
+
* @param defaultValue Default value
|
|
22
|
+
*/
|
|
23
|
+
getData<T>(key: string, defaultValue: T): T;
|
|
24
|
+
/**
|
|
25
|
+
* Get object data
|
|
26
|
+
* @param key Key name
|
|
27
|
+
*/
|
|
28
|
+
getObject<T extends {}>(key: string): T | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Set data
|
|
31
|
+
* @param key Key name
|
|
32
|
+
* @param data Data, null for removal
|
|
33
|
+
*/
|
|
34
|
+
setData(key: string, data: unknown): void;
|
|
35
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WindowStorage = void 0;
|
|
4
|
+
const StorageUtils_1 = require("../StorageUtils");
|
|
5
|
+
const Utils_1 = require("../Utils");
|
|
6
|
+
/**
|
|
7
|
+
* Window storage
|
|
8
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/Storage
|
|
9
|
+
*/
|
|
10
|
+
class WindowStorage {
|
|
11
|
+
/**
|
|
12
|
+
* Constructor
|
|
13
|
+
* @param globalFields Global fields
|
|
14
|
+
*/
|
|
15
|
+
constructor(globalFields) {
|
|
16
|
+
this.globalFields = globalFields;
|
|
17
|
+
if (globalFields.length == 0)
|
|
18
|
+
return;
|
|
19
|
+
// Copy global fields to session storage where first item does not exist
|
|
20
|
+
// Duplicate browser tab would copy the session storage
|
|
21
|
+
const firsItem = sessionStorage.getItem(globalFields[0]);
|
|
22
|
+
if (firsItem)
|
|
23
|
+
return;
|
|
24
|
+
globalFields.forEach((field) => {
|
|
25
|
+
const data = localStorage.getItem(field);
|
|
26
|
+
if (data != null) {
|
|
27
|
+
sessionStorage.setItem(field, data);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Get data
|
|
33
|
+
* @param key Key name
|
|
34
|
+
* @param defaultValue Default value
|
|
35
|
+
*/
|
|
36
|
+
getData(key, defaultValue) {
|
|
37
|
+
// Get storage
|
|
38
|
+
const data = sessionStorage.getItem(key);
|
|
39
|
+
// No default value
|
|
40
|
+
if (defaultValue == null)
|
|
41
|
+
return Utils_1.Utils.parseString(data);
|
|
42
|
+
// Return
|
|
43
|
+
return Utils_1.Utils.parseString(data, defaultValue);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Get object data
|
|
47
|
+
* @param key Key name
|
|
48
|
+
*/
|
|
49
|
+
getObject(key) {
|
|
50
|
+
// Get storage
|
|
51
|
+
const data = sessionStorage.getItem(key);
|
|
52
|
+
if (data == null)
|
|
53
|
+
return undefined;
|
|
54
|
+
return JSON.parse(data);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Set data
|
|
58
|
+
* @param key Key name
|
|
59
|
+
* @param data Data, null for removal
|
|
60
|
+
*/
|
|
61
|
+
setData(key, data) {
|
|
62
|
+
StorageUtils_1.StorageUtils.setSessionData(key, data);
|
|
63
|
+
if (this.globalFields.includes(key)) {
|
|
64
|
+
StorageUtils_1.StorageUtils.setLocalData(key, data);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.WindowStorage = WindowStorage;
|
package/lib/mjs/DomUtils.d.ts
CHANGED
|
@@ -8,11 +8,11 @@ export declare namespace DomUtils {
|
|
|
8
8
|
/**
|
|
9
9
|
* Language cache parameter name
|
|
10
10
|
*/
|
|
11
|
-
const
|
|
11
|
+
const CultureField = "EtsooCulture";
|
|
12
12
|
/**
|
|
13
13
|
* Country cache parameter name
|
|
14
14
|
*/
|
|
15
|
-
const
|
|
15
|
+
const CountryField = "EtsooCountry";
|
|
16
16
|
/**
|
|
17
17
|
* Clear form data
|
|
18
18
|
* @param data Form data
|
|
@@ -94,16 +94,6 @@ export declare namespace DomUtils {
|
|
|
94
94
|
* @param data New simple object data to merge
|
|
95
95
|
*/
|
|
96
96
|
function mergeURLSearchParams(base: URLSearchParams, data: DataTypes.SimpleObject): URLSearchParams;
|
|
97
|
-
/**
|
|
98
|
-
* Save country name
|
|
99
|
-
* @param country Country name
|
|
100
|
-
*/
|
|
101
|
-
function saveCountry(country: string): void;
|
|
102
|
-
/**
|
|
103
|
-
* Save culture name
|
|
104
|
-
* @param culture Culture name
|
|
105
|
-
*/
|
|
106
|
-
function saveCulture(culture: string): void;
|
|
107
97
|
/**
|
|
108
98
|
* Set HTML element focus by name
|
|
109
99
|
* @param name Element name or first collection item
|
package/lib/mjs/DomUtils.js
CHANGED
|
@@ -13,11 +13,11 @@ export var DomUtils;
|
|
|
13
13
|
/**
|
|
14
14
|
* Language cache parameter name
|
|
15
15
|
*/
|
|
16
|
-
DomUtils.
|
|
16
|
+
DomUtils.CultureField = 'EtsooCulture';
|
|
17
17
|
/**
|
|
18
18
|
* Country cache parameter name
|
|
19
19
|
*/
|
|
20
|
-
DomUtils.
|
|
20
|
+
DomUtils.CountryField = 'EtsooCountry';
|
|
21
21
|
/**
|
|
22
22
|
* Clear form data
|
|
23
23
|
* @param data Form data
|
|
@@ -153,12 +153,12 @@ export var DomUtils;
|
|
|
153
153
|
* Current detected country
|
|
154
154
|
*/
|
|
155
155
|
DomUtils.detectedCountry = (() => {
|
|
156
|
+
var _a, _b;
|
|
156
157
|
// URL first, then local storage
|
|
157
158
|
let country;
|
|
158
159
|
try {
|
|
159
160
|
country =
|
|
160
|
-
new URL(location.href).searchParams.get(DomUtils.
|
|
161
|
-
localStorage.getItem(DomUtils.Country);
|
|
161
|
+
(_b = (_a = new URL(location.href).searchParams.get(DomUtils.CountryField)) !== null && _a !== void 0 ? _a : sessionStorage.getItem(DomUtils.CountryField)) !== null && _b !== void 0 ? _b : localStorage.getItem(DomUtils.CountryField);
|
|
162
162
|
}
|
|
163
163
|
catch {
|
|
164
164
|
country = null;
|
|
@@ -170,12 +170,12 @@ export var DomUtils;
|
|
|
170
170
|
* Current detected culture
|
|
171
171
|
*/
|
|
172
172
|
DomUtils.detectedCulture = (() => {
|
|
173
|
+
var _a, _b;
|
|
173
174
|
// URL first, then local storage
|
|
174
175
|
let culture;
|
|
175
176
|
try {
|
|
176
177
|
culture =
|
|
177
|
-
new URL(location.href).searchParams.get(DomUtils.
|
|
178
|
-
localStorage.getItem(DomUtils.Culture);
|
|
178
|
+
(_b = (_a = new URL(location.href).searchParams.get(DomUtils.CultureField)) !== null && _a !== void 0 ? _a : sessionStorage.getItem(DomUtils.CultureField)) !== null && _b !== void 0 ? _b : localStorage.getItem(DomUtils.CultureField);
|
|
179
179
|
}
|
|
180
180
|
catch {
|
|
181
181
|
culture = null;
|
|
@@ -339,22 +339,6 @@ export var DomUtils;
|
|
|
339
339
|
return base;
|
|
340
340
|
}
|
|
341
341
|
DomUtils.mergeURLSearchParams = mergeURLSearchParams;
|
|
342
|
-
/**
|
|
343
|
-
* Save country name
|
|
344
|
-
* @param country Country name
|
|
345
|
-
*/
|
|
346
|
-
function saveCountry(country) {
|
|
347
|
-
localStorage.setItem(DomUtils.Country, country);
|
|
348
|
-
}
|
|
349
|
-
DomUtils.saveCountry = saveCountry;
|
|
350
|
-
/**
|
|
351
|
-
* Save culture name
|
|
352
|
-
* @param culture Culture name
|
|
353
|
-
*/
|
|
354
|
-
function saveCulture(culture) {
|
|
355
|
-
localStorage.setItem(DomUtils.Culture, culture);
|
|
356
|
-
}
|
|
357
|
-
DomUtils.saveCulture = saveCulture;
|
|
358
342
|
/**
|
|
359
343
|
* Set HTML element focus by name
|
|
360
344
|
* @param name Element name or first collection item
|
|
@@ -15,12 +15,17 @@ export declare namespace StorageUtils {
|
|
|
15
15
|
* @param data Data, null for removal
|
|
16
16
|
*/
|
|
17
17
|
function setSessionData(key: string, data: unknown): void;
|
|
18
|
+
/**
|
|
19
|
+
* Get local storage data
|
|
20
|
+
* @param key Key name
|
|
21
|
+
*/
|
|
22
|
+
function getLocalData<T>(key: string): T | undefined;
|
|
18
23
|
/**
|
|
19
24
|
* Get local storage data
|
|
20
25
|
* @param key Key name
|
|
21
26
|
* @param defaultValue Default value
|
|
22
27
|
*/
|
|
23
|
-
function getLocalData<T
|
|
28
|
+
function getLocalData<T>(key: string, defaultValue: T): T;
|
|
24
29
|
/**
|
|
25
30
|
* Get local storage object data
|
|
26
31
|
* @param key Key name
|
|
@@ -30,7 +35,13 @@ export declare namespace StorageUtils {
|
|
|
30
35
|
* Get session storage data
|
|
31
36
|
* @param key Key name
|
|
32
37
|
*/
|
|
33
|
-
function getSessionData<T
|
|
38
|
+
function getSessionData<T>(key: string): T | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Get session storage data with default value
|
|
41
|
+
* @param key Key name
|
|
42
|
+
* @param defaultValue Default value
|
|
43
|
+
*/
|
|
44
|
+
function getSessionData<T>(key: string, defaultValue: T): T;
|
|
34
45
|
/**
|
|
35
46
|
* Get session storage object data
|
|
36
47
|
* @param key Key name
|
package/lib/mjs/StorageUtils.js
CHANGED
|
@@ -44,6 +44,9 @@ export var StorageUtils;
|
|
|
44
44
|
function getLocalData(key, defaultValue) {
|
|
45
45
|
// Get storage
|
|
46
46
|
const data = localStorage.getItem(key);
|
|
47
|
+
// No default value
|
|
48
|
+
if (defaultValue == null)
|
|
49
|
+
return Utils.parseString(data);
|
|
47
50
|
// Return
|
|
48
51
|
return Utils.parseString(data, defaultValue);
|
|
49
52
|
}
|
|
@@ -63,10 +66,14 @@ export var StorageUtils;
|
|
|
63
66
|
/**
|
|
64
67
|
* Get session storage data
|
|
65
68
|
* @param key Key name
|
|
69
|
+
* @param defaultValue Default value
|
|
66
70
|
*/
|
|
67
71
|
function getSessionData(key, defaultValue) {
|
|
68
72
|
// Get storage
|
|
69
73
|
const data = sessionStorage.getItem(key);
|
|
74
|
+
// No default value
|
|
75
|
+
if (defaultValue == null)
|
|
76
|
+
return Utils.parseString(data);
|
|
70
77
|
// Return
|
|
71
78
|
return Utils.parseString(data, defaultValue);
|
|
72
79
|
}
|
package/lib/mjs/Utils.d.ts
CHANGED
|
@@ -134,6 +134,13 @@ export declare namespace Utils {
|
|
|
134
134
|
* @returns Updated fields
|
|
135
135
|
*/
|
|
136
136
|
function objectUpdated(objNew: {}, objPrev: {}, ignoreFields?: string[], strict?: number): string[];
|
|
137
|
+
/**
|
|
138
|
+
* Parse string (JSON) to specific type, no type conversion
|
|
139
|
+
* For type conversion, please use DataTypes.convert
|
|
140
|
+
* @param input Input string
|
|
141
|
+
* @returns Parsed value
|
|
142
|
+
*/
|
|
143
|
+
function parseString<T>(input: string | undefined | null): T | undefined;
|
|
137
144
|
/**
|
|
138
145
|
* Parse string (JSON) to specific type, no type conversion
|
|
139
146
|
* For type conversion, please use DataTypes.convert
|
|
@@ -141,7 +148,7 @@ export declare namespace Utils {
|
|
|
141
148
|
* @param defaultValue Default value
|
|
142
149
|
* @returns Parsed value
|
|
143
150
|
*/
|
|
144
|
-
function parseString<T
|
|
151
|
+
function parseString<T>(input: string | undefined | null, defaultValue: T): T;
|
|
145
152
|
/**
|
|
146
153
|
* Remove non letters
|
|
147
154
|
* @param input Input string
|
package/lib/mjs/Utils.js
CHANGED
|
@@ -265,6 +265,7 @@ export var Utils;
|
|
|
265
265
|
Utils.objectUpdated = objectUpdated;
|
|
266
266
|
/**
|
|
267
267
|
* Parse string (JSON) to specific type, no type conversion
|
|
268
|
+
* When return type depends on parameter value, uses function overloading, otherwise uses conditional type
|
|
268
269
|
* For type conversion, please use DataTypes.convert
|
|
269
270
|
* @param input Input string
|
|
270
271
|
* @param defaultValue Default value
|
package/lib/mjs/index.d.ts
CHANGED
package/lib/mjs/index.js
CHANGED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage interface
|
|
3
|
+
*/
|
|
4
|
+
export interface IStorage {
|
|
5
|
+
/**
|
|
6
|
+
* Get data
|
|
7
|
+
* @param key Key name
|
|
8
|
+
*/
|
|
9
|
+
getData<T>(key: string): T | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Get data with default value
|
|
12
|
+
* @param key Key name
|
|
13
|
+
* @param defaultValue Default value
|
|
14
|
+
*/
|
|
15
|
+
getData<T>(key: string, defaultValue: T): T;
|
|
16
|
+
/**
|
|
17
|
+
* Get session storage object data
|
|
18
|
+
* @param key Key name
|
|
19
|
+
*/
|
|
20
|
+
getObject<T extends {}>(key: string): T | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Set data
|
|
23
|
+
* @param key Key name
|
|
24
|
+
* @param data Data, null for removal
|
|
25
|
+
*/
|
|
26
|
+
setData(key: string, data: unknown): void;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Storage constructor interface
|
|
30
|
+
*/
|
|
31
|
+
export interface IStorageConstructor {
|
|
32
|
+
new (globalFields: string[]): IStorage;
|
|
33
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { IStorage } from './IStorage';
|
|
2
|
+
/**
|
|
3
|
+
* Window storage
|
|
4
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/Storage
|
|
5
|
+
*/
|
|
6
|
+
export declare class WindowStorage implements IStorage {
|
|
7
|
+
private globalFields;
|
|
8
|
+
/**
|
|
9
|
+
* Constructor
|
|
10
|
+
* @param globalFields Global fields
|
|
11
|
+
*/
|
|
12
|
+
constructor(globalFields: string[]);
|
|
13
|
+
/**
|
|
14
|
+
* Get data
|
|
15
|
+
* @param key Key name
|
|
16
|
+
*/
|
|
17
|
+
getData<T>(key: string): T | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Get data with default value
|
|
20
|
+
* @param key Key name
|
|
21
|
+
* @param defaultValue Default value
|
|
22
|
+
*/
|
|
23
|
+
getData<T>(key: string, defaultValue: T): T;
|
|
24
|
+
/**
|
|
25
|
+
* Get object data
|
|
26
|
+
* @param key Key name
|
|
27
|
+
*/
|
|
28
|
+
getObject<T extends {}>(key: string): T | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Set data
|
|
31
|
+
* @param key Key name
|
|
32
|
+
* @param data Data, null for removal
|
|
33
|
+
*/
|
|
34
|
+
setData(key: string, data: unknown): void;
|
|
35
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { StorageUtils } from '../StorageUtils';
|
|
2
|
+
import { Utils } from '../Utils';
|
|
3
|
+
/**
|
|
4
|
+
* Window storage
|
|
5
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/Storage
|
|
6
|
+
*/
|
|
7
|
+
export class WindowStorage {
|
|
8
|
+
/**
|
|
9
|
+
* Constructor
|
|
10
|
+
* @param globalFields Global fields
|
|
11
|
+
*/
|
|
12
|
+
constructor(globalFields) {
|
|
13
|
+
this.globalFields = globalFields;
|
|
14
|
+
if (globalFields.length == 0)
|
|
15
|
+
return;
|
|
16
|
+
// Copy global fields to session storage where first item does not exist
|
|
17
|
+
// Duplicate browser tab would copy the session storage
|
|
18
|
+
const firsItem = sessionStorage.getItem(globalFields[0]);
|
|
19
|
+
if (firsItem)
|
|
20
|
+
return;
|
|
21
|
+
globalFields.forEach((field) => {
|
|
22
|
+
const data = localStorage.getItem(field);
|
|
23
|
+
if (data != null) {
|
|
24
|
+
sessionStorage.setItem(field, data);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Get data
|
|
30
|
+
* @param key Key name
|
|
31
|
+
* @param defaultValue Default value
|
|
32
|
+
*/
|
|
33
|
+
getData(key, defaultValue) {
|
|
34
|
+
// Get storage
|
|
35
|
+
const data = sessionStorage.getItem(key);
|
|
36
|
+
// No default value
|
|
37
|
+
if (defaultValue == null)
|
|
38
|
+
return Utils.parseString(data);
|
|
39
|
+
// Return
|
|
40
|
+
return Utils.parseString(data, defaultValue);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Get object data
|
|
44
|
+
* @param key Key name
|
|
45
|
+
*/
|
|
46
|
+
getObject(key) {
|
|
47
|
+
// Get storage
|
|
48
|
+
const data = sessionStorage.getItem(key);
|
|
49
|
+
if (data == null)
|
|
50
|
+
return undefined;
|
|
51
|
+
return JSON.parse(data);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Set data
|
|
55
|
+
* @param key Key name
|
|
56
|
+
* @param data Data, null for removal
|
|
57
|
+
*/
|
|
58
|
+
setData(key, data) {
|
|
59
|
+
StorageUtils.setSessionData(key, data);
|
|
60
|
+
if (this.globalFields.includes(key)) {
|
|
61
|
+
StorageUtils.setLocalData(key, data);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
package/package.json
CHANGED
package/src/DomUtils.ts
CHANGED
|
@@ -15,12 +15,12 @@ export namespace DomUtils {
|
|
|
15
15
|
/**
|
|
16
16
|
* Language cache parameter name
|
|
17
17
|
*/
|
|
18
|
-
export const
|
|
18
|
+
export const CultureField = 'EtsooCulture';
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
21
|
* Country cache parameter name
|
|
22
22
|
*/
|
|
23
|
-
export const
|
|
23
|
+
export const CountryField = 'EtsooCountry';
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
26
|
* Clear form data
|
|
@@ -198,8 +198,9 @@ export namespace DomUtils {
|
|
|
198
198
|
let country: string | null;
|
|
199
199
|
try {
|
|
200
200
|
country =
|
|
201
|
-
new URL(location.href).searchParams.get(
|
|
202
|
-
|
|
201
|
+
new URL(location.href).searchParams.get(CountryField) ??
|
|
202
|
+
sessionStorage.getItem(CountryField) ??
|
|
203
|
+
localStorage.getItem(CountryField);
|
|
203
204
|
} catch {
|
|
204
205
|
country = null;
|
|
205
206
|
}
|
|
@@ -216,8 +217,9 @@ export namespace DomUtils {
|
|
|
216
217
|
let culture: string | null;
|
|
217
218
|
try {
|
|
218
219
|
culture =
|
|
219
|
-
new URL(location.href).searchParams.get(
|
|
220
|
-
|
|
220
|
+
new URL(location.href).searchParams.get(CultureField) ??
|
|
221
|
+
sessionStorage.getItem(CultureField) ??
|
|
222
|
+
localStorage.getItem(CultureField);
|
|
221
223
|
} catch {
|
|
222
224
|
culture = null;
|
|
223
225
|
}
|
|
@@ -415,22 +417,6 @@ export namespace DomUtils {
|
|
|
415
417
|
return base;
|
|
416
418
|
}
|
|
417
419
|
|
|
418
|
-
/**
|
|
419
|
-
* Save country name
|
|
420
|
-
* @param country Country name
|
|
421
|
-
*/
|
|
422
|
-
export function saveCountry(country: string) {
|
|
423
|
-
localStorage.setItem(Country, country);
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
/**
|
|
427
|
-
* Save culture name
|
|
428
|
-
* @param culture Culture name
|
|
429
|
-
*/
|
|
430
|
-
export function saveCulture(culture: string) {
|
|
431
|
-
localStorage.setItem(Culture, culture);
|
|
432
|
-
}
|
|
433
|
-
|
|
434
420
|
/**
|
|
435
421
|
* Set HTML element focus by name
|
|
436
422
|
* @param name Element name or first collection item
|
package/src/StorageUtils.ts
CHANGED
|
@@ -44,20 +44,36 @@ export namespace StorageUtils {
|
|
|
44
44
|
);
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
/**
|
|
48
|
+
* Get local storage data
|
|
49
|
+
* @param key Key name
|
|
50
|
+
*/
|
|
51
|
+
export function getLocalData<T>(key: string): T | undefined;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Get local storage data
|
|
55
|
+
* @param key Key name
|
|
56
|
+
* @param defaultValue Default value
|
|
57
|
+
*/
|
|
58
|
+
export function getLocalData<T>(key: string, defaultValue: T): T;
|
|
59
|
+
|
|
47
60
|
/**
|
|
48
61
|
* Get local storage data
|
|
49
62
|
* @param key Key name
|
|
50
63
|
* @param defaultValue Default value
|
|
51
64
|
*/
|
|
52
|
-
export function getLocalData<T
|
|
65
|
+
export function getLocalData<T>(
|
|
53
66
|
key: string,
|
|
54
|
-
defaultValue?:
|
|
55
|
-
):
|
|
67
|
+
defaultValue?: T
|
|
68
|
+
): T | undefined {
|
|
56
69
|
// Get storage
|
|
57
70
|
const data = localStorage.getItem(key);
|
|
58
71
|
|
|
72
|
+
// No default value
|
|
73
|
+
if (defaultValue == null) return Utils.parseString<T>(data);
|
|
74
|
+
|
|
59
75
|
// Return
|
|
60
|
-
return Utils.parseString(data, defaultValue);
|
|
76
|
+
return Utils.parseString<T>(data, defaultValue);
|
|
61
77
|
}
|
|
62
78
|
|
|
63
79
|
/**
|
|
@@ -75,15 +91,32 @@ export namespace StorageUtils {
|
|
|
75
91
|
* Get session storage data
|
|
76
92
|
* @param key Key name
|
|
77
93
|
*/
|
|
78
|
-
export function getSessionData<T
|
|
94
|
+
export function getSessionData<T>(key: string): T | undefined;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Get session storage data with default value
|
|
98
|
+
* @param key Key name
|
|
99
|
+
* @param defaultValue Default value
|
|
100
|
+
*/
|
|
101
|
+
export function getSessionData<T>(key: string, defaultValue: T): T;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Get session storage data
|
|
105
|
+
* @param key Key name
|
|
106
|
+
* @param defaultValue Default value
|
|
107
|
+
*/
|
|
108
|
+
export function getSessionData<T>(
|
|
79
109
|
key: string,
|
|
80
|
-
defaultValue?:
|
|
81
|
-
):
|
|
110
|
+
defaultValue?: T
|
|
111
|
+
): T | undefined {
|
|
82
112
|
// Get storage
|
|
83
113
|
const data = sessionStorage.getItem(key);
|
|
84
114
|
|
|
115
|
+
// No default value
|
|
116
|
+
if (defaultValue == null) return Utils.parseString<T>(data);
|
|
117
|
+
|
|
85
118
|
// Return
|
|
86
|
-
return Utils.parseString(data, defaultValue);
|
|
119
|
+
return Utils.parseString<T>(data, defaultValue);
|
|
87
120
|
}
|
|
88
121
|
|
|
89
122
|
/**
|
package/src/Utils.ts
CHANGED
|
@@ -365,15 +365,38 @@ export namespace Utils {
|
|
|
365
365
|
* Parse string (JSON) to specific type, no type conversion
|
|
366
366
|
* For type conversion, please use DataTypes.convert
|
|
367
367
|
* @param input Input string
|
|
368
|
+
* @returns Parsed value
|
|
369
|
+
*/
|
|
370
|
+
export function parseString<T>(
|
|
371
|
+
input: string | undefined | null
|
|
372
|
+
): T | undefined;
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Parse string (JSON) to specific type, no type conversion
|
|
376
|
+
* For type conversion, please use DataTypes.convert
|
|
377
|
+
* @param input Input string
|
|
378
|
+
* @param defaultValue Default value
|
|
379
|
+
* @returns Parsed value
|
|
380
|
+
*/
|
|
381
|
+
export function parseString<T>(
|
|
382
|
+
input: string | undefined | null,
|
|
383
|
+
defaultValue: T
|
|
384
|
+
): T;
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Parse string (JSON) to specific type, no type conversion
|
|
388
|
+
* When return type depends on parameter value, uses function overloading, otherwise uses conditional type
|
|
389
|
+
* For type conversion, please use DataTypes.convert
|
|
390
|
+
* @param input Input string
|
|
368
391
|
* @param defaultValue Default value
|
|
369
392
|
* @returns Parsed value
|
|
370
393
|
*/
|
|
371
|
-
export function parseString<T
|
|
394
|
+
export function parseString<T>(
|
|
372
395
|
input: string | undefined | null,
|
|
373
|
-
defaultValue?:
|
|
374
|
-
):
|
|
396
|
+
defaultValue?: T
|
|
397
|
+
): T | undefined {
|
|
375
398
|
// Undefined and empty case, return default value
|
|
376
|
-
if (input == null || input === '') return <
|
|
399
|
+
if (input == null || input === '') return <T>defaultValue;
|
|
377
400
|
|
|
378
401
|
// String
|
|
379
402
|
if (typeof defaultValue === 'string') return <any>input;
|
|
@@ -390,10 +413,10 @@ export namespace Utils {
|
|
|
390
413
|
const json = JSON.parse(input);
|
|
391
414
|
|
|
392
415
|
// Return
|
|
393
|
-
return json;
|
|
416
|
+
return <T>json;
|
|
394
417
|
} catch {
|
|
395
418
|
if (defaultValue == null) return <any>input;
|
|
396
|
-
return <
|
|
419
|
+
return <T>defaultValue;
|
|
397
420
|
}
|
|
398
421
|
}
|
|
399
422
|
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage interface
|
|
3
|
+
*/
|
|
4
|
+
export interface IStorage {
|
|
5
|
+
/**
|
|
6
|
+
* Get data
|
|
7
|
+
* @param key Key name
|
|
8
|
+
*/
|
|
9
|
+
getData<T>(key: string): T | undefined;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Get data with default value
|
|
13
|
+
* @param key Key name
|
|
14
|
+
* @param defaultValue Default value
|
|
15
|
+
*/
|
|
16
|
+
getData<T>(key: string, defaultValue: T): T;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Get session storage object data
|
|
20
|
+
* @param key Key name
|
|
21
|
+
*/
|
|
22
|
+
getObject<T extends {}>(key: string): T | undefined;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Set data
|
|
26
|
+
* @param key Key name
|
|
27
|
+
* @param data Data, null for removal
|
|
28
|
+
*/
|
|
29
|
+
setData(key: string, data: unknown): void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Storage constructor interface
|
|
34
|
+
*/
|
|
35
|
+
export interface IStorageConstructor {
|
|
36
|
+
new (globalFields: string[]): IStorage;
|
|
37
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { StorageUtils } from '../StorageUtils';
|
|
2
|
+
import { Utils } from '../Utils';
|
|
3
|
+
import { IStorage } from './IStorage';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Window storage
|
|
7
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/Storage
|
|
8
|
+
*/
|
|
9
|
+
export class WindowStorage implements IStorage {
|
|
10
|
+
/**
|
|
11
|
+
* Constructor
|
|
12
|
+
* @param globalFields Global fields
|
|
13
|
+
*/
|
|
14
|
+
constructor(private globalFields: string[]) {
|
|
15
|
+
if (globalFields.length == 0) return;
|
|
16
|
+
|
|
17
|
+
// Copy global fields to session storage where first item does not exist
|
|
18
|
+
// Duplicate browser tab would copy the session storage
|
|
19
|
+
const firsItem = sessionStorage.getItem(globalFields[0]);
|
|
20
|
+
if (firsItem) return;
|
|
21
|
+
|
|
22
|
+
globalFields.forEach((field) => {
|
|
23
|
+
const data = localStorage.getItem(field);
|
|
24
|
+
if (data != null) {
|
|
25
|
+
sessionStorage.setItem(field, data);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Get data
|
|
32
|
+
* @param key Key name
|
|
33
|
+
*/
|
|
34
|
+
getData<T>(key: string): T | undefined;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Get data with default value
|
|
38
|
+
* @param key Key name
|
|
39
|
+
* @param defaultValue Default value
|
|
40
|
+
*/
|
|
41
|
+
getData<T>(key: string, defaultValue: T): T;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Get data
|
|
45
|
+
* @param key Key name
|
|
46
|
+
* @param defaultValue Default value
|
|
47
|
+
*/
|
|
48
|
+
getData<T>(key: string, defaultValue?: T): T | undefined {
|
|
49
|
+
// Get storage
|
|
50
|
+
const data = sessionStorage.getItem(key);
|
|
51
|
+
|
|
52
|
+
// No default value
|
|
53
|
+
if (defaultValue == null) return Utils.parseString<T>(data);
|
|
54
|
+
|
|
55
|
+
// Return
|
|
56
|
+
return Utils.parseString<T>(data, defaultValue);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Get object data
|
|
61
|
+
* @param key Key name
|
|
62
|
+
*/
|
|
63
|
+
getObject<T extends {}>(key: string) {
|
|
64
|
+
// Get storage
|
|
65
|
+
const data = sessionStorage.getItem(key);
|
|
66
|
+
if (data == null) return undefined;
|
|
67
|
+
return <T>JSON.parse(data);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Set data
|
|
72
|
+
* @param key Key name
|
|
73
|
+
* @param data Data, null for removal
|
|
74
|
+
*/
|
|
75
|
+
setData(key: string, data: unknown) {
|
|
76
|
+
StorageUtils.setSessionData(key, data);
|
|
77
|
+
if (this.globalFields.includes(key)) {
|
|
78
|
+
StorageUtils.setLocalData(key, data);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|