@4i/modal-manager 1.1.9 → 1.1.20

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,94 +1,94 @@
1
- import Manager from "./Manager";
2
-
3
- function uniqueID() {
4
- return Math.floor(Math.random() * Date.now());
5
- }
6
-
7
- export const constants = {
8
- CHANGE: "change",
9
- CLOSE: "close",
10
- };
11
-
12
- interface QueueState {
13
- queue: string[];
14
- closedModalName?: string | undefined;
15
- lastOpenedModal?: string | undefined;
16
- }
17
-
18
- interface ModalState {
19
- isHaveOpenModals: boolean;
20
- queue: string[];
21
- closedModalName?: string | undefined;
22
- lastOpenedModal?: string | undefined;
23
- }
24
-
25
- export interface Options {
26
- hideBackdrop?: boolean;
27
- extraClass?: string;
28
- openMinimized?: boolean;
29
- }
30
-
31
- export class ModalManager extends Manager {
32
- queue: string[] = [];
33
- _openModalStateCallback: null | ((props: ModalState) => void);
34
-
35
- constructor() {
36
- super();
37
- this.create = this.create.bind(this);
38
- this.call = this.call.bind(this);
39
- this.close = this.close.bind(this);
40
- this._openModalStateCallback = null;
41
- }
42
-
43
- create<T>(
44
- name: string,
45
- payload: { modalId: number; data?: T },
46
- options?: Options
47
- ) {
48
- this.name = name;
49
- this.data = payload;
50
- this.emitter.emit(constants.CHANGE, this.name, this.data, options);
51
- }
52
-
53
- call<T>(name: string, data?: T, options?: Options) {
54
- this.create<T>(name, { modalId: uniqueID(), data }, options);
55
- const lastOpenedModal = name;
56
- this.queue.push(name);
57
-
58
- this._openModalStateCallback?.(
59
- this.getQueueState({
60
- queue: this.queue,
61
- lastOpenedModal,
62
- })
63
- );
64
- }
65
-
66
- close<T>(position?: T) {
67
- this.emitter.emit(constants.CLOSE, position ?? this.queue?.length - 1);
68
- const closedModalName = this.queue[this.queue.length - 1];
69
- this.queue.pop();
70
-
71
- this._openModalStateCallback?.(
72
- this.getQueueState({
73
- queue: this.queue,
74
- closedModalName,
75
- })
76
- );
77
- }
78
-
79
- getQueueState({ queue, closedModalName, lastOpenedModal }: QueueState) {
80
- return {
81
- isHaveOpenModals: queue.length > 0,
82
- queue,
83
- lastOpenedModal: lastOpenedModal,
84
- closedModalName,
85
- };
86
- }
87
-
88
- onOpenModalState(callback: (state: ModalState) => void) {
89
- this._openModalStateCallback = callback;
90
- }
91
- }
92
-
93
- const modal = new ModalManager();
94
- export default modal;
1
+ import Manager from "./Manager";
2
+
3
+ function uniqueID() {
4
+ return Math.floor(Math.random() * Date.now());
5
+ }
6
+
7
+ export const constants = {
8
+ CHANGE: "change",
9
+ CLOSE: "close",
10
+ };
11
+
12
+ interface QueueState {
13
+ queue: string[];
14
+ closedModalName?: string | undefined;
15
+ lastOpenedModal?: string | undefined;
16
+ }
17
+
18
+ interface ModalState {
19
+ isHaveOpenModals: boolean;
20
+ queue: string[];
21
+ closedModalName?: string | undefined;
22
+ lastOpenedModal?: string | undefined;
23
+ }
24
+
25
+ export interface Options {
26
+ hideBackdrop?: boolean;
27
+ extraClass?: string;
28
+ openMinimized?: boolean;
29
+ }
30
+
31
+ export class ModalManager extends Manager {
32
+ queue: string[] = [];
33
+ _openModalStateCallback: null | ((props: ModalState) => void);
34
+
35
+ constructor() {
36
+ super();
37
+ this.create = this.create.bind(this);
38
+ this.call = this.call.bind(this);
39
+ this.close = this.close.bind(this);
40
+ this._openModalStateCallback = null;
41
+ }
42
+
43
+ create<T>(
44
+ name: string,
45
+ payload: { modalId: number; data?: T },
46
+ options?: Options
47
+ ) {
48
+ this.name = name;
49
+ this.data = payload;
50
+ this.emitter.emit(constants.CHANGE, this.name, this.data, options);
51
+ }
52
+
53
+ call<T>(name: string, data?: T, options?: Options) {
54
+ this.create<T>(name, { modalId: uniqueID(), data }, options);
55
+ const lastOpenedModal = name;
56
+ this.queue.push(name);
57
+
58
+ this._openModalStateCallback?.(
59
+ this.getQueueState({
60
+ queue: this.queue,
61
+ lastOpenedModal,
62
+ })
63
+ );
64
+ }
65
+
66
+ close<T>(position?: T) {
67
+ this.emitter.emit(constants.CLOSE, position ?? this.queue?.length - 1);
68
+ const closedModalName = this.queue[this.queue.length - 1];
69
+ this.queue.pop();
70
+
71
+ this._openModalStateCallback?.(
72
+ this.getQueueState({
73
+ queue: this.queue,
74
+ closedModalName,
75
+ })
76
+ );
77
+ }
78
+
79
+ getQueueState({ queue, closedModalName, lastOpenedModal }: QueueState) {
80
+ return {
81
+ isHaveOpenModals: queue.length > 0,
82
+ queue,
83
+ lastOpenedModal: lastOpenedModal,
84
+ closedModalName,
85
+ };
86
+ }
87
+
88
+ onOpenModalState(callback: (state: ModalState) => void) {
89
+ this._openModalStateCallback = callback;
90
+ }
91
+ }
92
+
93
+ const modal = new ModalManager();
94
+ export default modal;
package/tsconfig.json CHANGED
@@ -1,16 +1,17 @@
1
- {
2
- "compilerOptions": {
3
- "target": "es5",
4
- "module": "commonjs",
5
- "esModuleInterop": true,
6
- "jsx": "react",
7
- "strict": true,
8
- "skipLibCheck": true,
9
- "forceConsistentCasingInFileNames": true,
10
- "moduleResolution": "node",
11
- "declaration": true,
12
- "outDir": "./src"
13
- },
14
- "include": ["src/**/*.ts", "src/**/*.tsx"],
15
- "exclude": ["node_modules"]
16
- }
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es5",
4
+ "module": "commonjs",
5
+ "esModuleInterop": true,
6
+ "jsx": "react",
7
+ "strict": true,
8
+ "skipLibCheck": true,
9
+ "forceConsistentCasingInFileNames": true,
10
+ "moduleResolution": "node",
11
+ "declaration": true,
12
+ "outDir": "./src",
13
+ "allowJs": true
14
+ },
15
+ "include": ["src/**/*.ts", "src/**/*.tsx"],
16
+ "exclude": ["node_modules"]
17
+ }