|
1 | 1 | import { isEscapeKey } from './utils.js'; |
2 | 2 |
|
3 | 3 | let currentDialog = null; |
4 | | -let currentEscapeHandler = null; |
5 | | -let currentOutsideClickHandler = null; |
6 | 4 |
|
7 | 5 | const errorTemplate = document.querySelector('#error'); |
8 | 6 | const successTemplate = document.querySelector('#success'); |
9 | 7 | const dataErrorTemplate = document.querySelector('#data-error'); |
10 | 8 |
|
11 | | -const descriptionInput = document.querySelector('.text__description'); |
12 | | -const imgHashtags = document.querySelector('.text__hashtags'); |
13 | | - |
14 | | -const canCloseModal = () => { |
15 | | - const activeElement = document.activeElement; |
16 | | - const isInputFocused = activeElement === descriptionInput || activeElement === imgHashtags; |
17 | | - return !isInputFocused && !document.querySelector('.error'); |
18 | | -}; |
19 | | - |
20 | 9 | const createEscapeHandler = (closeCallback) => (evt) => { |
21 | | - if (isEscapeKey(evt.key) && canCloseModal()) { |
| 10 | + if (isEscapeKey(evt.key) && currentDialog) { |
22 | 11 | evt.preventDefault(); |
23 | 12 | closeCallback(); |
| 13 | + evt.stopPropagation(); |
24 | 14 | } |
25 | 15 | }; |
26 | 16 |
|
27 | 17 | const createOutsideClickHandler = (closeCallback) => (evt) => { |
28 | | - if (currentDialog && !currentDialog.contains(evt.target)) { |
| 18 | + if (evt.target.closest('[data-dialog-close]') || !evt.target.closest('[data-dialog-content]')) { |
29 | 19 | closeCallback(); |
30 | 20 | } |
31 | 21 | }; |
32 | 22 |
|
33 | | -const cleanupEventListeners = () => { |
34 | | - if (currentEscapeHandler) { |
35 | | - document.removeEventListener('keydown', currentEscapeHandler); |
36 | | - currentEscapeHandler = null; |
37 | | - } |
38 | | - |
39 | | - if (currentOutsideClickHandler) { |
40 | | - document.removeEventListener('click', currentOutsideClickHandler); |
41 | | - currentOutsideClickHandler = null; |
42 | | - } |
43 | | -}; |
44 | | - |
45 | 23 | const closeDialog = () => { |
46 | 24 | if (currentDialog) { |
47 | 25 | currentDialog.remove(); |
48 | 26 | currentDialog = null; |
49 | | - cleanupEventListeners(); |
50 | 27 | } |
51 | 28 | }; |
52 | 29 |
|
53 | | -const openDialog = (template, className, closeCallback = closeDialog) => { |
54 | | - closeDialog(); |
55 | | - |
| 30 | +const openDialog = (template) => { |
56 | 31 | const dialogElement = template.content.cloneNode(true); |
57 | | - document.body.appendChild(dialogElement); |
58 | | - currentDialog = document.querySelector(`.${className}`); |
| 32 | + const dialogMainElement = dialogElement.children[0]; |
59 | 33 |
|
60 | | - const dialogButton = currentDialog.querySelector(`.${className}__button`); |
61 | | - |
62 | | - currentEscapeHandler = createEscapeHandler(closeCallback); |
63 | | - currentOutsideClickHandler = createOutsideClickHandler(closeCallback); |
| 34 | + document.body.appendChild(dialogElement); |
64 | 35 |
|
65 | | - document.addEventListener('keydown', currentEscapeHandler); |
66 | | - document.addEventListener('click', currentOutsideClickHandler); |
67 | | - dialogButton.addEventListener('click', closeCallback); |
| 36 | + currentDialog = dialogMainElement; |
68 | 37 | }; |
| 38 | +document.addEventListener('keydown', createEscapeHandler(closeDialog), { capture: true }); |
| 39 | +document.addEventListener('click', createOutsideClickHandler(closeDialog)); |
69 | 40 |
|
70 | 41 | export const showError = () => { |
71 | | - openDialog(errorTemplate, 'error'); |
| 42 | + openDialog(errorTemplate); |
72 | 43 | }; |
73 | 44 |
|
74 | 45 | export const showSuccess = () => { |
75 | | - openDialog(successTemplate, 'success'); |
| 46 | + openDialog(successTemplate); |
76 | 47 | }; |
77 | 48 |
|
78 | 49 | export const showDataError = () => { |
79 | 50 | const errorElement = dataErrorTemplate.content.cloneNode(true); |
| 51 | + const errorMainElement = errorElement.children[0]; |
| 52 | + |
80 | 53 | document.body.appendChild(errorElement); |
81 | 54 |
|
82 | | - const errorMessage = document.querySelector('.data-error'); |
| 55 | + const errorMessage = errorMainElement; |
83 | 56 |
|
84 | 57 | setTimeout(() => { |
85 | 58 | if (errorMessage) { |
|
0 commit comments