/
actions.ts
72 lines (56 loc) · 1.56 KB
/
actions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { getNode } from './utils';
import { CANCEL_KEY } from './options/buttons';
import CLASS_NAMES from './class-list';
const {
OVERLAY,
SHOW_MODAL,
BUTTON,
BUTTON_LOADING,
} = CLASS_NAMES;
import state, { SwalState } from './state';
export const openModal = (): void => {
let overlay = getNode(OVERLAY);
overlay.classList.add(SHOW_MODAL);
state.isOpen = true;
};
const hideModal = (): void => {
let overlay = getNode(OVERLAY);
overlay.classList.remove(SHOW_MODAL);
state.isOpen = false;
};
/*
* Triggers when the user presses any button, or
* hits Enter inside the input:
*/
export const onAction = (namespace: string = CANCEL_KEY): void => {
const { value, closeModal } = state.actions[namespace];
if (closeModal === false) {
const buttonClass = `${BUTTON}--${namespace}`;
const button = getNode(buttonClass);
button.classList.add(BUTTON_LOADING);
} else {
hideModal();
}
state.promise.resolve(value);
};
/*
* Filter the state object. Remove the stuff
* that's only for internal use
*/
export const getState = (): SwalState => {
const publicState = Object.assign({}, state);
delete publicState.promise;
delete publicState.timer;
return publicState;
};
/*
* Stop showing loading animation on button
* (to display error message in input for example)
*/
export const stopLoading = (): void => {
const buttons: NodeListOf<Element> = document.querySelectorAll(`.${BUTTON}`);
for (let i = 0; i < buttons.length; i++) {
const button: Element = buttons[i];
button.classList.remove(BUTTON_LOADING);
}
};