@etsoo/shared 1.1.32 → 1.1.35
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/__tests__/EHistory.ts +11 -3
- package/lib/cjs/DataTypes.js +1 -4
- package/lib/cjs/DateUtils.js +3 -11
- package/lib/cjs/DomUtils.js +18 -15
- package/lib/cjs/Utils.js +1 -1
- package/lib/cjs/index.js +17 -30
- package/lib/cjs/types/EHistory.d.ts +30 -10
- package/lib/cjs/types/EHistory.js +24 -4
- package/lib/cjs/types/EventClass.d.ts +15 -24
- package/lib/cjs/types/EventClass.js +2 -0
- package/lib/mjs/DataTypes.js +1 -4
- package/lib/mjs/DateUtils.js +3 -11
- package/lib/mjs/DomUtils.js +18 -15
- package/lib/mjs/Utils.js +1 -1
- package/lib/mjs/types/EHistory.d.ts +30 -10
- package/lib/mjs/types/EHistory.js +22 -3
- package/lib/mjs/types/EventClass.d.ts +15 -24
- package/lib/mjs/types/EventClass.js +2 -0
- package/package.json +9 -7
- package/src/types/EHistory.ts +49 -15
- package/src/types/EventClass.ts +42 -37
- package/tsconfig.cjs.json +3 -1
- package/tsconfig.json +3 -1
package/__tests__/EHistory.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EHistory,
|
|
1
|
+
import { EHistory, EHistoryNavigateEvent } from '../src/types/EHistory';
|
|
2
2
|
|
|
3
3
|
// Extended for tests
|
|
4
4
|
class LHistory extends EHistory<number> {}
|
|
@@ -7,11 +7,14 @@ test('Tests for history', () => {
|
|
|
7
7
|
const history = new LHistory();
|
|
8
8
|
expect(history.index).toBe(-1);
|
|
9
9
|
history.pushState(1);
|
|
10
|
+
expect(history.getStatus()).toStrictEqual([false, false]);
|
|
10
11
|
history.pushState(2);
|
|
11
12
|
history.pushState(3);
|
|
13
|
+
expect(history.getStatus()).toStrictEqual([true, false]);
|
|
12
14
|
expect(history.states).toStrictEqual([1, 2, 3]);
|
|
13
15
|
expect(history.index).toBe(2);
|
|
14
16
|
history.back();
|
|
17
|
+
expect(history.getStatus()).toStrictEqual([true, true]);
|
|
15
18
|
history.pushState(4);
|
|
16
19
|
expect(history.index).toBe(2);
|
|
17
20
|
expect(history.states).toStrictEqual([1, 2, 4]);
|
|
@@ -27,14 +30,14 @@ test('Tests for history', () => {
|
|
|
27
30
|
|
|
28
31
|
test('Tests for events', () => {
|
|
29
32
|
const navigatorFn = jest.fn();
|
|
30
|
-
const navigatorStopFn = jest.fn((event:
|
|
33
|
+
const navigatorStopFn = jest.fn((event: EHistoryNavigateEvent) => {
|
|
31
34
|
event.stopImmediatePropagation();
|
|
32
35
|
});
|
|
33
36
|
const clearFn = jest.fn();
|
|
34
37
|
const pushFn = jest.fn();
|
|
35
38
|
const replaceFn = jest.fn();
|
|
36
39
|
|
|
37
|
-
const history = new LHistory();
|
|
40
|
+
const history = new LHistory(3);
|
|
38
41
|
|
|
39
42
|
history.on({
|
|
40
43
|
clear: clearFn,
|
|
@@ -68,4 +71,9 @@ test('Tests for events', () => {
|
|
|
68
71
|
|
|
69
72
|
// Previous handler stopped propagation
|
|
70
73
|
expect(navigatorFn).toBeCalledTimes(5);
|
|
74
|
+
|
|
75
|
+
history.pushState(3);
|
|
76
|
+
history.pushState(4);
|
|
77
|
+
history.pushState(5);
|
|
78
|
+
expect(history.length).toBe(3);
|
|
71
79
|
});
|
package/lib/cjs/DataTypes.js
CHANGED
package/lib/cjs/DateUtils.js
CHANGED
|
@@ -47,19 +47,11 @@ var DateUtils;
|
|
|
47
47
|
/**
|
|
48
48
|
* Minute format, YYYY-MM-DD hh:mm
|
|
49
49
|
*/
|
|
50
|
-
DateUtils.MinuteFormat = {
|
|
51
|
-
...DateUtils.DayFormat,
|
|
52
|
-
hour: '2-digit',
|
|
53
|
-
hourCycle: 'h23',
|
|
54
|
-
minute: '2-digit'
|
|
55
|
-
};
|
|
50
|
+
DateUtils.MinuteFormat = Object.assign(Object.assign({}, DateUtils.DayFormat), { hour: '2-digit', hourCycle: 'h23', minute: '2-digit' });
|
|
56
51
|
/**
|
|
57
52
|
* Second format, YYYY-MM-DD hh:mm:ss
|
|
58
53
|
*/
|
|
59
|
-
DateUtils.SecondFormat = {
|
|
60
|
-
...DateUtils.MinuteFormat,
|
|
61
|
-
second: '2-digit'
|
|
62
|
-
};
|
|
54
|
+
DateUtils.SecondFormat = Object.assign(Object.assign({}, DateUtils.MinuteFormat), { second: '2-digit' });
|
|
63
55
|
/**
|
|
64
56
|
* Format
|
|
65
57
|
* @param input Input date time
|
|
@@ -92,7 +84,7 @@ var DateUtils;
|
|
|
92
84
|
opt = options;
|
|
93
85
|
}
|
|
94
86
|
// Clone as new options with time zone
|
|
95
|
-
const newOpt = {
|
|
87
|
+
const newOpt = Object.assign(Object.assign({}, opt), (timeZone != null && { timeZone }));
|
|
96
88
|
// Return format result
|
|
97
89
|
return new Intl.DateTimeFormat(locale, newOpt)
|
|
98
90
|
.format(parsed)
|
package/lib/cjs/DomUtils.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DomUtils = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
4
5
|
const DataTypes_1 = require("./DataTypes");
|
|
5
6
|
if (typeof navigator === 'undefined') {
|
|
6
7
|
// Test mock only
|
|
@@ -163,7 +164,7 @@ var DomUtils;
|
|
|
163
164
|
country =
|
|
164
165
|
(_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
166
|
}
|
|
166
|
-
catch {
|
|
167
|
+
catch (_c) {
|
|
167
168
|
country = null;
|
|
168
169
|
}
|
|
169
170
|
// Return
|
|
@@ -180,7 +181,7 @@ var DomUtils;
|
|
|
180
181
|
culture =
|
|
181
182
|
(_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
183
|
}
|
|
183
|
-
catch {
|
|
184
|
+
catch (_c) {
|
|
184
185
|
culture = null;
|
|
185
186
|
}
|
|
186
187
|
// Browser detected
|
|
@@ -218,19 +219,21 @@ var DomUtils;
|
|
|
218
219
|
* @param file File
|
|
219
220
|
* @returns Data URL
|
|
220
221
|
*/
|
|
221
|
-
|
|
222
|
-
return
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
222
|
+
function fileToDataURL(file) {
|
|
223
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
224
|
+
return new Promise((resolve, reject) => {
|
|
225
|
+
const reader = new FileReader();
|
|
226
|
+
reader.onerror = reject;
|
|
227
|
+
reader.onload = () => {
|
|
228
|
+
const data = reader.result;
|
|
229
|
+
if (data == null) {
|
|
230
|
+
reject();
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
resolve(data);
|
|
234
|
+
};
|
|
235
|
+
reader.readAsDataURL(file);
|
|
236
|
+
});
|
|
234
237
|
});
|
|
235
238
|
}
|
|
236
239
|
DomUtils.fileToDataURL = fileToDataURL;
|
package/lib/cjs/Utils.js
CHANGED
package/lib/cjs/index.js
CHANGED
|
@@ -1,32 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
|
|
18
|
-
__exportStar(require("./types/
|
|
19
|
-
__exportStar(require("./types/
|
|
20
|
-
__exportStar(require("./types/
|
|
21
|
-
__exportStar(require("./types/
|
|
22
|
-
__exportStar(require("./
|
|
23
|
-
__exportStar(require("./storage/
|
|
24
|
-
__exportStar(require("./
|
|
25
|
-
__exportStar(require("./
|
|
26
|
-
__exportStar(require("./
|
|
27
|
-
__exportStar(require("./
|
|
28
|
-
__exportStar(require("./
|
|
29
|
-
__exportStar(require("./
|
|
30
|
-
__exportStar(require("./
|
|
31
|
-
__exportStar(require("./
|
|
32
|
-
__exportStar(require("./
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./types/DelayedExecutorType"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./types/EColor"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./types/EHistory"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./types/EventClass"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./types/FormData"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./storage/IStorage"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./storage/WindowStorage"), exports);
|
|
11
|
+
tslib_1.__exportStar(require("./DataTypes"), exports);
|
|
12
|
+
tslib_1.__exportStar(require("./ColorUtils"), exports);
|
|
13
|
+
tslib_1.__exportStar(require("./DateUtils"), exports);
|
|
14
|
+
tslib_1.__exportStar(require("./DomUtils"), exports);
|
|
15
|
+
tslib_1.__exportStar(require("./ExtendUtils"), exports);
|
|
16
|
+
tslib_1.__exportStar(require("./Keyboard"), exports);
|
|
17
|
+
tslib_1.__exportStar(require("./NumberUtils"), exports);
|
|
18
|
+
tslib_1.__exportStar(require("./StorageUtils"), exports);
|
|
19
|
+
tslib_1.__exportStar(require("./Utils"), exports);
|
|
@@ -1,26 +1,39 @@
|
|
|
1
1
|
import { EventBase, EventClass } from './EventClass';
|
|
2
|
-
|
|
3
|
-
* ETSOO Extended history event type
|
|
4
|
-
*/
|
|
5
|
-
export declare type EHistoryEventType = 'navigate' | 'push' | 'replace' | 'clear';
|
|
6
|
-
/**
|
|
7
|
-
* ETSOO Extended history event data
|
|
8
|
-
*/
|
|
9
|
-
export interface EHistoryEventData {
|
|
2
|
+
interface EHistoryEventData {
|
|
10
3
|
/**
|
|
11
4
|
* Current index
|
|
12
5
|
*/
|
|
13
6
|
index: number;
|
|
14
7
|
}
|
|
8
|
+
interface EHistoryNavigateEventData extends EHistoryEventData {
|
|
9
|
+
/**
|
|
10
|
+
* Delta
|
|
11
|
+
*/
|
|
12
|
+
delta: number;
|
|
13
|
+
}
|
|
14
|
+
declare type EHistoryEventDef = {
|
|
15
|
+
clear: EHistoryEventData;
|
|
16
|
+
navigate: EHistoryNavigateEventData;
|
|
17
|
+
push: EHistoryEventData;
|
|
18
|
+
replace: EHistoryEventData;
|
|
19
|
+
};
|
|
20
|
+
declare type EHistoryEventType = Exclude<keyof EHistoryEventDef, 'navigate'>;
|
|
15
21
|
/**
|
|
16
22
|
* ETSOO Extended history event
|
|
17
23
|
*/
|
|
18
24
|
export declare class EHistoryEvent extends EventBase<EHistoryEventType, EHistoryEventData> {
|
|
19
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* ETSOO Extended history navigate event
|
|
28
|
+
*/
|
|
29
|
+
export declare class EHistoryNavigateEvent extends EventBase<'navigate', EHistoryNavigateEventData> {
|
|
30
|
+
constructor(target: {}, data: EHistoryNavigateEventData);
|
|
31
|
+
}
|
|
20
32
|
/**
|
|
21
33
|
* ETSOO Extended abstract history class
|
|
22
34
|
*/
|
|
23
|
-
export declare abstract class EHistory<T> extends EventClass<
|
|
35
|
+
export declare abstract class EHistory<T> extends EventClass<EHistoryEventDef> {
|
|
36
|
+
readonly maxDepth: number;
|
|
24
37
|
private _index;
|
|
25
38
|
/**
|
|
26
39
|
* States
|
|
@@ -38,6 +51,11 @@ export declare abstract class EHistory<T> extends EventClass<EHistoryEventType,
|
|
|
38
51
|
* Get current state
|
|
39
52
|
*/
|
|
40
53
|
get state(): T | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Constructor
|
|
56
|
+
* @param maxDepth Max depth of the history
|
|
57
|
+
*/
|
|
58
|
+
constructor(maxDepth?: number);
|
|
41
59
|
/**
|
|
42
60
|
* Back to the previous state
|
|
43
61
|
*/
|
|
@@ -51,7 +69,8 @@ export declare abstract class EHistory<T> extends EventClass<EHistoryEventType,
|
|
|
51
69
|
* @param type Type
|
|
52
70
|
* @param index Current index
|
|
53
71
|
*/
|
|
54
|
-
protected createEvent(type:
|
|
72
|
+
protected createEvent<T extends EHistoryEventType>(type: T, index: number): EHistoryEvent;
|
|
73
|
+
protected createNavigateEvent(index: number, delta: number): EHistoryNavigateEvent;
|
|
55
74
|
/**
|
|
56
75
|
* Forward to the next state
|
|
57
76
|
*/
|
|
@@ -76,3 +95,4 @@ export declare abstract class EHistory<T> extends EventClass<EHistoryEventType,
|
|
|
76
95
|
*/
|
|
77
96
|
replaceState(state: T): void;
|
|
78
97
|
}
|
|
98
|
+
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.EHistory = exports.EHistoryEvent = void 0;
|
|
3
|
+
exports.EHistory = exports.EHistoryNavigateEvent = exports.EHistoryEvent = void 0;
|
|
4
4
|
const EventClass_1 = require("./EventClass");
|
|
5
5
|
/**
|
|
6
6
|
* ETSOO Extended history event
|
|
@@ -8,12 +8,26 @@ const EventClass_1 = require("./EventClass");
|
|
|
8
8
|
class EHistoryEvent extends EventClass_1.EventBase {
|
|
9
9
|
}
|
|
10
10
|
exports.EHistoryEvent = EHistoryEvent;
|
|
11
|
+
/**
|
|
12
|
+
* ETSOO Extended history navigate event
|
|
13
|
+
*/
|
|
14
|
+
class EHistoryNavigateEvent extends EventClass_1.EventBase {
|
|
15
|
+
constructor(target, data) {
|
|
16
|
+
super(target, 'navigate', data);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.EHistoryNavigateEvent = EHistoryNavigateEvent;
|
|
11
20
|
/**
|
|
12
21
|
* ETSOO Extended abstract history class
|
|
13
22
|
*/
|
|
14
23
|
class EHistory extends EventClass_1.EventClass {
|
|
15
|
-
|
|
16
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Constructor
|
|
26
|
+
* @param maxDepth Max depth of the history
|
|
27
|
+
*/
|
|
28
|
+
constructor(maxDepth = 20) {
|
|
29
|
+
super();
|
|
30
|
+
this.maxDepth = maxDepth;
|
|
17
31
|
// Index
|
|
18
32
|
this._index = -1;
|
|
19
33
|
/**
|
|
@@ -64,6 +78,9 @@ class EHistory extends EventClass_1.EventClass {
|
|
|
64
78
|
createEvent(type, index) {
|
|
65
79
|
return new EHistoryEvent(this, type, { index });
|
|
66
80
|
}
|
|
81
|
+
createNavigateEvent(index, delta) {
|
|
82
|
+
return new EHistoryNavigateEvent(this, { index, delta });
|
|
83
|
+
}
|
|
67
84
|
/**
|
|
68
85
|
* Forward to the next state
|
|
69
86
|
*/
|
|
@@ -98,7 +115,7 @@ class EHistory extends EventClass_1.EventClass {
|
|
|
98
115
|
// Update the index
|
|
99
116
|
this._index = newIndex;
|
|
100
117
|
// Trigger event
|
|
101
|
-
this.trigger(this.
|
|
118
|
+
this.trigger(this.createNavigateEvent(newIndex, delta));
|
|
102
119
|
}
|
|
103
120
|
/**
|
|
104
121
|
* Adds an entry to the history stack
|
|
@@ -111,6 +128,9 @@ class EHistory extends EventClass_1.EventClass {
|
|
|
111
128
|
}
|
|
112
129
|
this.states.push(state);
|
|
113
130
|
this._index++;
|
|
131
|
+
if (this.length > this.maxDepth) {
|
|
132
|
+
this.states.shift();
|
|
133
|
+
}
|
|
114
134
|
this.trigger(this.createEvent('push', this._index));
|
|
115
135
|
}
|
|
116
136
|
/**
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
* T for type
|
|
4
4
|
* D for data
|
|
5
5
|
*/
|
|
6
|
-
export declare abstract class EventBase<T
|
|
7
|
-
readonly target:
|
|
6
|
+
export declare abstract class EventBase<T, D> {
|
|
7
|
+
readonly target: {};
|
|
8
8
|
readonly type: T;
|
|
9
9
|
readonly data: D;
|
|
10
10
|
private _propagationStopped;
|
|
@@ -21,7 +21,7 @@ export declare abstract class EventBase<T extends string, D> {
|
|
|
21
21
|
* Constructor
|
|
22
22
|
* @param type Type
|
|
23
23
|
*/
|
|
24
|
-
constructor(target:
|
|
24
|
+
constructor(target: {}, type: T, data: D);
|
|
25
25
|
/**
|
|
26
26
|
* Prevent all other listeners from being called
|
|
27
27
|
*/
|
|
@@ -40,65 +40,56 @@ interface EventOptions {
|
|
|
40
40
|
*/
|
|
41
41
|
once?: boolean;
|
|
42
42
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
* T for type
|
|
46
|
-
* D for data
|
|
47
|
-
*/
|
|
48
|
-
export declare type EventClassCallback<T extends string, D> = (event: EventBase<T, D>) => void;
|
|
49
|
-
/**
|
|
50
|
-
* Event class collection definition
|
|
51
|
-
* T for type
|
|
52
|
-
* D for data
|
|
53
|
-
*/
|
|
54
|
-
export declare type EventClassCollection<T extends string, D> = {
|
|
55
|
-
[key in T]?: EventClassCallback<T, D>;
|
|
43
|
+
declare type EventClassDef = {
|
|
44
|
+
[key: string]: {};
|
|
56
45
|
};
|
|
57
46
|
/**
|
|
58
47
|
* Event class
|
|
59
48
|
* T for type
|
|
60
49
|
* D for data
|
|
61
50
|
*/
|
|
62
|
-
export declare abstract class EventClass<
|
|
51
|
+
export declare abstract class EventClass<D extends EventClassDef> {
|
|
63
52
|
private readonly listeners;
|
|
64
53
|
/**
|
|
65
54
|
* Has specific type events
|
|
66
55
|
* @param type Type
|
|
67
56
|
*/
|
|
68
|
-
hasEvents(type: T): boolean;
|
|
57
|
+
hasEvents<T extends keyof D>(type: T): boolean;
|
|
69
58
|
/**
|
|
70
59
|
* Has specific type and callback events
|
|
71
60
|
* @param type Type
|
|
72
61
|
* @param callback Callback
|
|
73
62
|
*/
|
|
74
|
-
hasEvents(type: T, callback:
|
|
63
|
+
hasEvents<T extends keyof D>(type: T, callback: (event: EventBase<T, D[T]>) => void): boolean;
|
|
75
64
|
/**
|
|
76
65
|
* Remove all specific type events
|
|
77
66
|
* @param type Type
|
|
78
67
|
*/
|
|
79
|
-
off(type: T): void;
|
|
68
|
+
off<T extends keyof D>(type: T): void;
|
|
80
69
|
/**
|
|
81
70
|
* Remove specific type and callback event
|
|
82
71
|
* @param type Type
|
|
83
72
|
* @param callback Callback
|
|
84
73
|
*/
|
|
85
|
-
off(type: T, callback:
|
|
74
|
+
off<T extends keyof D>(type: T, callback: (event: EventBase<T, D[T]>) => void): void;
|
|
86
75
|
/**
|
|
87
76
|
* Add event listeners
|
|
88
77
|
* @param collection Collection of events
|
|
89
78
|
*/
|
|
90
|
-
on(collection:
|
|
79
|
+
on(collection: {
|
|
80
|
+
[type in keyof D]: (event: EventBase<type, D[type]>) => void;
|
|
81
|
+
}): void;
|
|
91
82
|
/**
|
|
92
83
|
* Add event listener
|
|
93
84
|
* @param type Type
|
|
94
85
|
* @param callback Callback
|
|
95
86
|
* @param options Options
|
|
96
87
|
*/
|
|
97
|
-
on(type: T, callback:
|
|
88
|
+
on<T extends keyof D>(type: T, callback: (event: EventBase<T, D[T]>) => void, options?: EventOptions): void;
|
|
98
89
|
/**
|
|
99
90
|
* Trigger event
|
|
100
91
|
* @param event Event
|
|
101
92
|
*/
|
|
102
|
-
trigger(event: EventBase<T, D>): void;
|
|
93
|
+
trigger<T extends keyof D>(event: EventBase<T, D[T]>): void;
|
|
103
94
|
}
|
|
104
95
|
export {};
|
|
@@ -102,6 +102,8 @@ class EventClass {
|
|
|
102
102
|
if (callback == null)
|
|
103
103
|
return;
|
|
104
104
|
this.listeners.has(type) || this.listeners.set(type, []);
|
|
105
|
+
// String to T conversion problem
|
|
106
|
+
// "keyofStringsOnly": true could solve part of it
|
|
105
107
|
(_b = this.listeners.get(type)) === null || _b === void 0 ? void 0 : _b.push([callback, options]);
|
|
106
108
|
}
|
|
107
109
|
/**
|
package/lib/mjs/DataTypes.js
CHANGED
|
@@ -49,10 +49,7 @@ export var DataTypes;
|
|
|
49
49
|
/**
|
|
50
50
|
* Combined type enum
|
|
51
51
|
*/
|
|
52
|
-
DataTypes.CombinedEnum = {
|
|
53
|
-
...SimpleEnum,
|
|
54
|
-
...ExtendedEnum
|
|
55
|
-
};
|
|
52
|
+
DataTypes.CombinedEnum = Object.assign(Object.assign({}, SimpleEnum), ExtendedEnum);
|
|
56
53
|
/**
|
|
57
54
|
* Horizontal align enum
|
|
58
55
|
*/
|
package/lib/mjs/DateUtils.js
CHANGED
|
@@ -44,19 +44,11 @@ export var DateUtils;
|
|
|
44
44
|
/**
|
|
45
45
|
* Minute format, YYYY-MM-DD hh:mm
|
|
46
46
|
*/
|
|
47
|
-
DateUtils.MinuteFormat = {
|
|
48
|
-
...DateUtils.DayFormat,
|
|
49
|
-
hour: '2-digit',
|
|
50
|
-
hourCycle: 'h23',
|
|
51
|
-
minute: '2-digit'
|
|
52
|
-
};
|
|
47
|
+
DateUtils.MinuteFormat = Object.assign(Object.assign({}, DateUtils.DayFormat), { hour: '2-digit', hourCycle: 'h23', minute: '2-digit' });
|
|
53
48
|
/**
|
|
54
49
|
* Second format, YYYY-MM-DD hh:mm:ss
|
|
55
50
|
*/
|
|
56
|
-
DateUtils.SecondFormat = {
|
|
57
|
-
...DateUtils.MinuteFormat,
|
|
58
|
-
second: '2-digit'
|
|
59
|
-
};
|
|
51
|
+
DateUtils.SecondFormat = Object.assign(Object.assign({}, DateUtils.MinuteFormat), { second: '2-digit' });
|
|
60
52
|
/**
|
|
61
53
|
* Format
|
|
62
54
|
* @param input Input date time
|
|
@@ -89,7 +81,7 @@ export var DateUtils;
|
|
|
89
81
|
opt = options;
|
|
90
82
|
}
|
|
91
83
|
// Clone as new options with time zone
|
|
92
|
-
const newOpt = {
|
|
84
|
+
const newOpt = Object.assign(Object.assign({}, opt), (timeZone != null && { timeZone }));
|
|
93
85
|
// Return format result
|
|
94
86
|
return new Intl.DateTimeFormat(locale, newOpt)
|
|
95
87
|
.format(parsed)
|
package/lib/mjs/DomUtils.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { __awaiter } from "tslib";
|
|
1
2
|
import { DataTypes } from './DataTypes';
|
|
2
3
|
if (typeof navigator === 'undefined') {
|
|
3
4
|
// Test mock only
|
|
@@ -160,7 +161,7 @@ export var DomUtils;
|
|
|
160
161
|
country =
|
|
161
162
|
(_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
163
|
}
|
|
163
|
-
catch {
|
|
164
|
+
catch (_c) {
|
|
164
165
|
country = null;
|
|
165
166
|
}
|
|
166
167
|
// Return
|
|
@@ -177,7 +178,7 @@ export var DomUtils;
|
|
|
177
178
|
culture =
|
|
178
179
|
(_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
180
|
}
|
|
180
|
-
catch {
|
|
181
|
+
catch (_c) {
|
|
181
182
|
culture = null;
|
|
182
183
|
}
|
|
183
184
|
// Browser detected
|
|
@@ -215,19 +216,21 @@ export var DomUtils;
|
|
|
215
216
|
* @param file File
|
|
216
217
|
* @returns Data URL
|
|
217
218
|
*/
|
|
218
|
-
|
|
219
|
-
return
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
219
|
+
function fileToDataURL(file) {
|
|
220
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
221
|
+
return new Promise((resolve, reject) => {
|
|
222
|
+
const reader = new FileReader();
|
|
223
|
+
reader.onerror = reject;
|
|
224
|
+
reader.onload = () => {
|
|
225
|
+
const data = reader.result;
|
|
226
|
+
if (data == null) {
|
|
227
|
+
reject();
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
resolve(data);
|
|
231
|
+
};
|
|
232
|
+
reader.readAsDataURL(file);
|
|
233
|
+
});
|
|
231
234
|
});
|
|
232
235
|
}
|
|
233
236
|
DomUtils.fileToDataURL = fileToDataURL;
|
package/lib/mjs/Utils.js
CHANGED
|
@@ -1,26 +1,39 @@
|
|
|
1
1
|
import { EventBase, EventClass } from './EventClass';
|
|
2
|
-
|
|
3
|
-
* ETSOO Extended history event type
|
|
4
|
-
*/
|
|
5
|
-
export declare type EHistoryEventType = 'navigate' | 'push' | 'replace' | 'clear';
|
|
6
|
-
/**
|
|
7
|
-
* ETSOO Extended history event data
|
|
8
|
-
*/
|
|
9
|
-
export interface EHistoryEventData {
|
|
2
|
+
interface EHistoryEventData {
|
|
10
3
|
/**
|
|
11
4
|
* Current index
|
|
12
5
|
*/
|
|
13
6
|
index: number;
|
|
14
7
|
}
|
|
8
|
+
interface EHistoryNavigateEventData extends EHistoryEventData {
|
|
9
|
+
/**
|
|
10
|
+
* Delta
|
|
11
|
+
*/
|
|
12
|
+
delta: number;
|
|
13
|
+
}
|
|
14
|
+
declare type EHistoryEventDef = {
|
|
15
|
+
clear: EHistoryEventData;
|
|
16
|
+
navigate: EHistoryNavigateEventData;
|
|
17
|
+
push: EHistoryEventData;
|
|
18
|
+
replace: EHistoryEventData;
|
|
19
|
+
};
|
|
20
|
+
declare type EHistoryEventType = Exclude<keyof EHistoryEventDef, 'navigate'>;
|
|
15
21
|
/**
|
|
16
22
|
* ETSOO Extended history event
|
|
17
23
|
*/
|
|
18
24
|
export declare class EHistoryEvent extends EventBase<EHistoryEventType, EHistoryEventData> {
|
|
19
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* ETSOO Extended history navigate event
|
|
28
|
+
*/
|
|
29
|
+
export declare class EHistoryNavigateEvent extends EventBase<'navigate', EHistoryNavigateEventData> {
|
|
30
|
+
constructor(target: {}, data: EHistoryNavigateEventData);
|
|
31
|
+
}
|
|
20
32
|
/**
|
|
21
33
|
* ETSOO Extended abstract history class
|
|
22
34
|
*/
|
|
23
|
-
export declare abstract class EHistory<T> extends EventClass<
|
|
35
|
+
export declare abstract class EHistory<T> extends EventClass<EHistoryEventDef> {
|
|
36
|
+
readonly maxDepth: number;
|
|
24
37
|
private _index;
|
|
25
38
|
/**
|
|
26
39
|
* States
|
|
@@ -38,6 +51,11 @@ export declare abstract class EHistory<T> extends EventClass<EHistoryEventType,
|
|
|
38
51
|
* Get current state
|
|
39
52
|
*/
|
|
40
53
|
get state(): T | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Constructor
|
|
56
|
+
* @param maxDepth Max depth of the history
|
|
57
|
+
*/
|
|
58
|
+
constructor(maxDepth?: number);
|
|
41
59
|
/**
|
|
42
60
|
* Back to the previous state
|
|
43
61
|
*/
|
|
@@ -51,7 +69,8 @@ export declare abstract class EHistory<T> extends EventClass<EHistoryEventType,
|
|
|
51
69
|
* @param type Type
|
|
52
70
|
* @param index Current index
|
|
53
71
|
*/
|
|
54
|
-
protected createEvent(type:
|
|
72
|
+
protected createEvent<T extends EHistoryEventType>(type: T, index: number): EHistoryEvent;
|
|
73
|
+
protected createNavigateEvent(index: number, delta: number): EHistoryNavigateEvent;
|
|
55
74
|
/**
|
|
56
75
|
* Forward to the next state
|
|
57
76
|
*/
|
|
@@ -76,3 +95,4 @@ export declare abstract class EHistory<T> extends EventClass<EHistoryEventType,
|
|
|
76
95
|
*/
|
|
77
96
|
replaceState(state: T): void;
|
|
78
97
|
}
|
|
98
|
+
export {};
|
|
@@ -4,12 +4,25 @@ import { EventBase, EventClass } from './EventClass';
|
|
|
4
4
|
*/
|
|
5
5
|
export class EHistoryEvent extends EventBase {
|
|
6
6
|
}
|
|
7
|
+
/**
|
|
8
|
+
* ETSOO Extended history navigate event
|
|
9
|
+
*/
|
|
10
|
+
export class EHistoryNavigateEvent extends EventBase {
|
|
11
|
+
constructor(target, data) {
|
|
12
|
+
super(target, 'navigate', data);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
7
15
|
/**
|
|
8
16
|
* ETSOO Extended abstract history class
|
|
9
17
|
*/
|
|
10
18
|
export class EHistory extends EventClass {
|
|
11
|
-
|
|
12
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Constructor
|
|
21
|
+
* @param maxDepth Max depth of the history
|
|
22
|
+
*/
|
|
23
|
+
constructor(maxDepth = 20) {
|
|
24
|
+
super();
|
|
25
|
+
this.maxDepth = maxDepth;
|
|
13
26
|
// Index
|
|
14
27
|
this._index = -1;
|
|
15
28
|
/**
|
|
@@ -60,6 +73,9 @@ export class EHistory extends EventClass {
|
|
|
60
73
|
createEvent(type, index) {
|
|
61
74
|
return new EHistoryEvent(this, type, { index });
|
|
62
75
|
}
|
|
76
|
+
createNavigateEvent(index, delta) {
|
|
77
|
+
return new EHistoryNavigateEvent(this, { index, delta });
|
|
78
|
+
}
|
|
63
79
|
/**
|
|
64
80
|
* Forward to the next state
|
|
65
81
|
*/
|
|
@@ -94,7 +110,7 @@ export class EHistory extends EventClass {
|
|
|
94
110
|
// Update the index
|
|
95
111
|
this._index = newIndex;
|
|
96
112
|
// Trigger event
|
|
97
|
-
this.trigger(this.
|
|
113
|
+
this.trigger(this.createNavigateEvent(newIndex, delta));
|
|
98
114
|
}
|
|
99
115
|
/**
|
|
100
116
|
* Adds an entry to the history stack
|
|
@@ -107,6 +123,9 @@ export class EHistory extends EventClass {
|
|
|
107
123
|
}
|
|
108
124
|
this.states.push(state);
|
|
109
125
|
this._index++;
|
|
126
|
+
if (this.length > this.maxDepth) {
|
|
127
|
+
this.states.shift();
|
|
128
|
+
}
|
|
110
129
|
this.trigger(this.createEvent('push', this._index));
|
|
111
130
|
}
|
|
112
131
|
/**
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
* T for type
|
|
4
4
|
* D for data
|
|
5
5
|
*/
|
|
6
|
-
export declare abstract class EventBase<T
|
|
7
|
-
readonly target:
|
|
6
|
+
export declare abstract class EventBase<T, D> {
|
|
7
|
+
readonly target: {};
|
|
8
8
|
readonly type: T;
|
|
9
9
|
readonly data: D;
|
|
10
10
|
private _propagationStopped;
|
|
@@ -21,7 +21,7 @@ export declare abstract class EventBase<T extends string, D> {
|
|
|
21
21
|
* Constructor
|
|
22
22
|
* @param type Type
|
|
23
23
|
*/
|
|
24
|
-
constructor(target:
|
|
24
|
+
constructor(target: {}, type: T, data: D);
|
|
25
25
|
/**
|
|
26
26
|
* Prevent all other listeners from being called
|
|
27
27
|
*/
|
|
@@ -40,65 +40,56 @@ interface EventOptions {
|
|
|
40
40
|
*/
|
|
41
41
|
once?: boolean;
|
|
42
42
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
* T for type
|
|
46
|
-
* D for data
|
|
47
|
-
*/
|
|
48
|
-
export declare type EventClassCallback<T extends string, D> = (event: EventBase<T, D>) => void;
|
|
49
|
-
/**
|
|
50
|
-
* Event class collection definition
|
|
51
|
-
* T for type
|
|
52
|
-
* D for data
|
|
53
|
-
*/
|
|
54
|
-
export declare type EventClassCollection<T extends string, D> = {
|
|
55
|
-
[key in T]?: EventClassCallback<T, D>;
|
|
43
|
+
declare type EventClassDef = {
|
|
44
|
+
[key: string]: {};
|
|
56
45
|
};
|
|
57
46
|
/**
|
|
58
47
|
* Event class
|
|
59
48
|
* T for type
|
|
60
49
|
* D for data
|
|
61
50
|
*/
|
|
62
|
-
export declare abstract class EventClass<
|
|
51
|
+
export declare abstract class EventClass<D extends EventClassDef> {
|
|
63
52
|
private readonly listeners;
|
|
64
53
|
/**
|
|
65
54
|
* Has specific type events
|
|
66
55
|
* @param type Type
|
|
67
56
|
*/
|
|
68
|
-
hasEvents(type: T): boolean;
|
|
57
|
+
hasEvents<T extends keyof D>(type: T): boolean;
|
|
69
58
|
/**
|
|
70
59
|
* Has specific type and callback events
|
|
71
60
|
* @param type Type
|
|
72
61
|
* @param callback Callback
|
|
73
62
|
*/
|
|
74
|
-
hasEvents(type: T, callback:
|
|
63
|
+
hasEvents<T extends keyof D>(type: T, callback: (event: EventBase<T, D[T]>) => void): boolean;
|
|
75
64
|
/**
|
|
76
65
|
* Remove all specific type events
|
|
77
66
|
* @param type Type
|
|
78
67
|
*/
|
|
79
|
-
off(type: T): void;
|
|
68
|
+
off<T extends keyof D>(type: T): void;
|
|
80
69
|
/**
|
|
81
70
|
* Remove specific type and callback event
|
|
82
71
|
* @param type Type
|
|
83
72
|
* @param callback Callback
|
|
84
73
|
*/
|
|
85
|
-
off(type: T, callback:
|
|
74
|
+
off<T extends keyof D>(type: T, callback: (event: EventBase<T, D[T]>) => void): void;
|
|
86
75
|
/**
|
|
87
76
|
* Add event listeners
|
|
88
77
|
* @param collection Collection of events
|
|
89
78
|
*/
|
|
90
|
-
on(collection:
|
|
79
|
+
on(collection: {
|
|
80
|
+
[type in keyof D]: (event: EventBase<type, D[type]>) => void;
|
|
81
|
+
}): void;
|
|
91
82
|
/**
|
|
92
83
|
* Add event listener
|
|
93
84
|
* @param type Type
|
|
94
85
|
* @param callback Callback
|
|
95
86
|
* @param options Options
|
|
96
87
|
*/
|
|
97
|
-
on(type: T, callback:
|
|
88
|
+
on<T extends keyof D>(type: T, callback: (event: EventBase<T, D[T]>) => void, options?: EventOptions): void;
|
|
98
89
|
/**
|
|
99
90
|
* Trigger event
|
|
100
91
|
* @param event Event
|
|
101
92
|
*/
|
|
102
|
-
trigger(event: EventBase<T, D>): void;
|
|
93
|
+
trigger<T extends keyof D>(event: EventBase<T, D[T]>): void;
|
|
103
94
|
}
|
|
104
95
|
export {};
|
|
@@ -98,6 +98,8 @@ export class EventClass {
|
|
|
98
98
|
if (callback == null)
|
|
99
99
|
return;
|
|
100
100
|
this.listeners.has(type) || this.listeners.set(type, []);
|
|
101
|
+
// String to T conversion problem
|
|
102
|
+
// "keyofStringsOnly": true could solve part of it
|
|
101
103
|
(_b = this.listeners.get(type)) === null || _b === void 0 ? void 0 : _b.push([callback, options]);
|
|
102
104
|
}
|
|
103
105
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@etsoo/shared",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.35",
|
|
4
4
|
"description": "TypeScript shared utilities and functions",
|
|
5
5
|
"main": "lib/cjs/index.js",
|
|
6
6
|
"module": "lib/mjs/index.js",
|
|
@@ -54,14 +54,16 @@
|
|
|
54
54
|
"homepage": "https://github.com/ETSOO/Shared#readme",
|
|
55
55
|
"dependencies": {},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@types/jest": "^27.
|
|
58
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
59
|
-
"@typescript-eslint/parser": "^5.
|
|
60
|
-
"eslint": "^8.
|
|
57
|
+
"@types/jest": "^27.5.0",
|
|
58
|
+
"@typescript-eslint/eslint-plugin": "^5.23.0",
|
|
59
|
+
"@typescript-eslint/parser": "^5.23.0",
|
|
60
|
+
"eslint": "^8.15.0",
|
|
61
61
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
62
62
|
"eslint-plugin-import": "^2.26.0",
|
|
63
|
-
"jest": "^
|
|
64
|
-
"
|
|
63
|
+
"jest": "^28.1.0",
|
|
64
|
+
"jest-environment-jsdom": "^28.1.0",
|
|
65
|
+
"ts-jest": "^28.0.2",
|
|
66
|
+
"tslib": "^2.4.0",
|
|
65
67
|
"typescript": "^4.6.4"
|
|
66
68
|
}
|
|
67
69
|
}
|
package/src/types/EHistory.ts
CHANGED
|
@@ -1,20 +1,28 @@
|
|
|
1
1
|
import { EventBase, EventClass } from './EventClass';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
* ETSOO Extended history event type
|
|
5
|
-
*/
|
|
6
|
-
export type EHistoryEventType = 'navigate' | 'push' | 'replace' | 'clear';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* ETSOO Extended history event data
|
|
10
|
-
*/
|
|
11
|
-
export interface EHistoryEventData {
|
|
3
|
+
interface EHistoryEventData {
|
|
12
4
|
/**
|
|
13
5
|
* Current index
|
|
14
6
|
*/
|
|
15
7
|
index: number;
|
|
16
8
|
}
|
|
17
9
|
|
|
10
|
+
interface EHistoryNavigateEventData extends EHistoryEventData {
|
|
11
|
+
/**
|
|
12
|
+
* Delta
|
|
13
|
+
*/
|
|
14
|
+
delta: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type EHistoryEventDef = {
|
|
18
|
+
clear: EHistoryEventData;
|
|
19
|
+
navigate: EHistoryNavigateEventData;
|
|
20
|
+
push: EHistoryEventData;
|
|
21
|
+
replace: EHistoryEventData;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type EHistoryEventType = Exclude<keyof EHistoryEventDef, 'navigate'>;
|
|
25
|
+
|
|
18
26
|
/**
|
|
19
27
|
* ETSOO Extended history event
|
|
20
28
|
*/
|
|
@@ -24,12 +32,21 @@ export class EHistoryEvent extends EventBase<
|
|
|
24
32
|
> {}
|
|
25
33
|
|
|
26
34
|
/**
|
|
27
|
-
* ETSOO Extended
|
|
35
|
+
* ETSOO Extended history navigate event
|
|
28
36
|
*/
|
|
29
|
-
export
|
|
30
|
-
|
|
31
|
-
|
|
37
|
+
export class EHistoryNavigateEvent extends EventBase<
|
|
38
|
+
'navigate',
|
|
39
|
+
EHistoryNavigateEventData
|
|
32
40
|
> {
|
|
41
|
+
constructor(target: {}, data: EHistoryNavigateEventData) {
|
|
42
|
+
super(target, 'navigate', data);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* ETSOO Extended abstract history class
|
|
48
|
+
*/
|
|
49
|
+
export abstract class EHistory<T> extends EventClass<EHistoryEventDef> {
|
|
33
50
|
// Index
|
|
34
51
|
private _index: number = -1;
|
|
35
52
|
|
|
@@ -60,6 +77,14 @@ export abstract class EHistory<T> extends EventClass<
|
|
|
60
77
|
return this.states[this._index];
|
|
61
78
|
}
|
|
62
79
|
|
|
80
|
+
/**
|
|
81
|
+
* Constructor
|
|
82
|
+
* @param maxDepth Max depth of the history
|
|
83
|
+
*/
|
|
84
|
+
constructor(public readonly maxDepth: number = 20) {
|
|
85
|
+
super();
|
|
86
|
+
}
|
|
87
|
+
|
|
63
88
|
/**
|
|
64
89
|
* Back to the previous state
|
|
65
90
|
*/
|
|
@@ -82,10 +107,14 @@ export abstract class EHistory<T> extends EventClass<
|
|
|
82
107
|
* @param type Type
|
|
83
108
|
* @param index Current index
|
|
84
109
|
*/
|
|
85
|
-
protected createEvent(type:
|
|
110
|
+
protected createEvent<T extends EHistoryEventType>(type: T, index: number) {
|
|
86
111
|
return new EHistoryEvent(this, type, { index });
|
|
87
112
|
}
|
|
88
113
|
|
|
114
|
+
protected createNavigateEvent(index: number, delta: number) {
|
|
115
|
+
return new EHistoryNavigateEvent(this, { index, delta });
|
|
116
|
+
}
|
|
117
|
+
|
|
89
118
|
/**
|
|
90
119
|
* Forward to the next state
|
|
91
120
|
*/
|
|
@@ -121,7 +150,7 @@ export abstract class EHistory<T> extends EventClass<
|
|
|
121
150
|
this._index = newIndex;
|
|
122
151
|
|
|
123
152
|
// Trigger event
|
|
124
|
-
this.trigger(this.
|
|
153
|
+
this.trigger(this.createNavigateEvent(newIndex, delta));
|
|
125
154
|
}
|
|
126
155
|
|
|
127
156
|
/**
|
|
@@ -136,6 +165,11 @@ export abstract class EHistory<T> extends EventClass<
|
|
|
136
165
|
|
|
137
166
|
this.states.push(state);
|
|
138
167
|
this._index++;
|
|
168
|
+
|
|
169
|
+
if (this.length > this.maxDepth) {
|
|
170
|
+
this.states.shift();
|
|
171
|
+
}
|
|
172
|
+
|
|
139
173
|
this.trigger(this.createEvent('push', this._index));
|
|
140
174
|
}
|
|
141
175
|
|
package/src/types/EventClass.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* T for type
|
|
4
4
|
* D for data
|
|
5
5
|
*/
|
|
6
|
-
export abstract class EventBase<T
|
|
6
|
+
export abstract class EventBase<T, D> {
|
|
7
7
|
private _propagationStopped: boolean = false;
|
|
8
8
|
/**
|
|
9
9
|
* stopImmediatePropagation called
|
|
@@ -25,7 +25,7 @@ export abstract class EventBase<T extends string, D> {
|
|
|
25
25
|
* @param type Type
|
|
26
26
|
*/
|
|
27
27
|
constructor(
|
|
28
|
-
public readonly target:
|
|
28
|
+
public readonly target: {},
|
|
29
29
|
public readonly type: T,
|
|
30
30
|
public readonly data: D
|
|
31
31
|
) {
|
|
@@ -55,48 +55,35 @@ interface EventOptions {
|
|
|
55
55
|
once?: boolean;
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
|
|
59
|
-
* Event class callback
|
|
60
|
-
* T for type
|
|
61
|
-
* D for data
|
|
62
|
-
*/
|
|
63
|
-
export type EventClassCallback<T extends string, D> = (
|
|
64
|
-
event: EventBase<T, D>
|
|
65
|
-
) => void;
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Event class collection definition
|
|
69
|
-
* T for type
|
|
70
|
-
* D for data
|
|
71
|
-
*/
|
|
72
|
-
export type EventClassCollection<T extends string, D> = {
|
|
73
|
-
[key in T]?: EventClassCallback<T, D>;
|
|
74
|
-
};
|
|
58
|
+
type EventClassDef = { [key: string]: {} };
|
|
75
59
|
|
|
76
60
|
/**
|
|
77
61
|
* Event class
|
|
78
62
|
* T for type
|
|
79
63
|
* D for data
|
|
80
64
|
*/
|
|
81
|
-
export abstract class EventClass<
|
|
65
|
+
export abstract class EventClass<D extends EventClassDef> {
|
|
82
66
|
// Listeners
|
|
83
67
|
private readonly listeners = new Map<
|
|
84
|
-
|
|
85
|
-
[
|
|
68
|
+
keyof D,
|
|
69
|
+
[(event: EventBase<keyof D, D[keyof D]>) => void, EventOptions?][]
|
|
86
70
|
>();
|
|
87
71
|
|
|
88
72
|
/**
|
|
89
73
|
* Has specific type events
|
|
90
74
|
* @param type Type
|
|
91
75
|
*/
|
|
92
|
-
hasEvents(type: T): boolean;
|
|
76
|
+
hasEvents<T extends keyof D>(type: T): boolean;
|
|
93
77
|
|
|
94
78
|
/**
|
|
95
79
|
* Has specific type and callback events
|
|
96
80
|
* @param type Type
|
|
97
81
|
* @param callback Callback
|
|
98
82
|
*/
|
|
99
|
-
hasEvents
|
|
83
|
+
hasEvents<T extends keyof D>(
|
|
84
|
+
type: T,
|
|
85
|
+
callback: (event: EventBase<T, D[T]>) => void
|
|
86
|
+
): boolean;
|
|
100
87
|
|
|
101
88
|
/**
|
|
102
89
|
* Has specific type and callback events
|
|
@@ -104,7 +91,10 @@ export abstract class EventClass<T extends string, D> {
|
|
|
104
91
|
* @param callback Callback
|
|
105
92
|
* @returns Result
|
|
106
93
|
*/
|
|
107
|
-
hasEvents
|
|
94
|
+
hasEvents<T extends keyof D>(
|
|
95
|
+
type: T,
|
|
96
|
+
callback?: (event: EventBase<T, D[T]>) => void
|
|
97
|
+
) {
|
|
108
98
|
const items = this.listeners.get(type);
|
|
109
99
|
if (items == null || items.length === 0) return false;
|
|
110
100
|
|
|
@@ -119,21 +109,27 @@ export abstract class EventClass<T extends string, D> {
|
|
|
119
109
|
* Remove all specific type events
|
|
120
110
|
* @param type Type
|
|
121
111
|
*/
|
|
122
|
-
off(type: T): void;
|
|
112
|
+
off<T extends keyof D>(type: T): void;
|
|
123
113
|
|
|
124
114
|
/**
|
|
125
115
|
* Remove specific type and callback event
|
|
126
116
|
* @param type Type
|
|
127
117
|
* @param callback Callback
|
|
128
118
|
*/
|
|
129
|
-
off
|
|
119
|
+
off<T extends keyof D>(
|
|
120
|
+
type: T,
|
|
121
|
+
callback: (event: EventBase<T, D[T]>) => void
|
|
122
|
+
): void;
|
|
130
123
|
|
|
131
124
|
/**
|
|
132
125
|
* Remove specific type and callback event
|
|
133
126
|
* @param type Type
|
|
134
127
|
* @param callback Callback
|
|
135
128
|
*/
|
|
136
|
-
off
|
|
129
|
+
off<T extends keyof D>(
|
|
130
|
+
type: T,
|
|
131
|
+
callback?: (event: EventBase<T, D[T]>) => void
|
|
132
|
+
) {
|
|
137
133
|
if (callback == null) {
|
|
138
134
|
this.listeners.delete(type);
|
|
139
135
|
return;
|
|
@@ -153,7 +149,9 @@ export abstract class EventClass<T extends string, D> {
|
|
|
153
149
|
* Add event listeners
|
|
154
150
|
* @param collection Collection of events
|
|
155
151
|
*/
|
|
156
|
-
on(collection:
|
|
152
|
+
on(collection: {
|
|
153
|
+
[type in keyof D]: (event: EventBase<type, D[type]>) => void;
|
|
154
|
+
}): void;
|
|
157
155
|
|
|
158
156
|
/**
|
|
159
157
|
* Add event listener
|
|
@@ -161,9 +159,9 @@ export abstract class EventClass<T extends string, D> {
|
|
|
161
159
|
* @param callback Callback
|
|
162
160
|
* @param options Options
|
|
163
161
|
*/
|
|
164
|
-
on(
|
|
162
|
+
on<T extends keyof D>(
|
|
165
163
|
type: T,
|
|
166
|
-
callback:
|
|
164
|
+
callback: (event: EventBase<T, D[T]>) => void,
|
|
167
165
|
options?: EventOptions
|
|
168
166
|
): void;
|
|
169
167
|
|
|
@@ -173,14 +171,18 @@ export abstract class EventClass<T extends string, D> {
|
|
|
173
171
|
* @param callback Callback
|
|
174
172
|
* @param options Options
|
|
175
173
|
*/
|
|
176
|
-
on(
|
|
177
|
-
type:
|
|
178
|
-
|
|
174
|
+
on<T extends keyof D>(
|
|
175
|
+
type:
|
|
176
|
+
| {
|
|
177
|
+
[type in keyof D]: (event: EventBase<type, D[type]>) => void;
|
|
178
|
+
}
|
|
179
|
+
| T,
|
|
180
|
+
callback?: (event: EventBase<T, D[T]>) => void,
|
|
179
181
|
options?: EventOptions
|
|
180
182
|
) {
|
|
181
183
|
if (typeof type === 'object') {
|
|
182
184
|
for (const key in type) {
|
|
183
|
-
const item = key as
|
|
185
|
+
const item = key as keyof D;
|
|
184
186
|
const itemCallback = type[item] ?? callback;
|
|
185
187
|
if (itemCallback) this.on(item, itemCallback, options);
|
|
186
188
|
}
|
|
@@ -189,14 +191,17 @@ export abstract class EventClass<T extends string, D> {
|
|
|
189
191
|
|
|
190
192
|
if (callback == null) return;
|
|
191
193
|
this.listeners.has(type) || this.listeners.set(type, []);
|
|
192
|
-
|
|
194
|
+
|
|
195
|
+
// String to T conversion problem
|
|
196
|
+
// "keyofStringsOnly": true could solve part of it
|
|
197
|
+
this.listeners.get(type)?.push([callback as any, options]);
|
|
193
198
|
}
|
|
194
199
|
|
|
195
200
|
/**
|
|
196
201
|
* Trigger event
|
|
197
202
|
* @param event Event
|
|
198
203
|
*/
|
|
199
|
-
trigger(event: EventBase<T, D>) {
|
|
204
|
+
trigger<T extends keyof D>(event: EventBase<T, D[T]>) {
|
|
200
205
|
const items = this.listeners.get(event.type);
|
|
201
206
|
if (items == null) return;
|
|
202
207
|
|
package/tsconfig.cjs.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
/* Visit https://aka.ms/tsconfig.json to read more about this file */
|
|
4
|
-
"target": "
|
|
4
|
+
"target": "ES6",
|
|
5
5
|
"module": "commonjs",
|
|
6
6
|
"moduleResolution": "node",
|
|
7
7
|
"isolatedModules": true,
|
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
"strict": true,
|
|
12
12
|
"esModuleInterop": true,
|
|
13
13
|
"skipLibCheck": true,
|
|
14
|
+
"importHelpers": true,
|
|
15
|
+
"downlevelIteration": true,
|
|
14
16
|
"forceConsistentCasingInFileNames": true,
|
|
15
17
|
"lib": ["dom", "dom.iterable", "esnext"]
|
|
16
18
|
},
|
package/tsconfig.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
/* Visit https://aka.ms/tsconfig.json to read more about this file */
|
|
4
|
-
"target": "
|
|
4
|
+
"target": "ES6",
|
|
5
5
|
"module": "ESNext",
|
|
6
6
|
"moduleResolution": "node",
|
|
7
7
|
"isolatedModules": true,
|
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
"strict": true,
|
|
12
12
|
"esModuleInterop": true,
|
|
13
13
|
"skipLibCheck": true,
|
|
14
|
+
"importHelpers": true,
|
|
15
|
+
"downlevelIteration": true,
|
|
14
16
|
"forceConsistentCasingInFileNames": true,
|
|
15
17
|
"lib": ["dom", "dom.iterable", "esnext"]
|
|
16
18
|
},
|