-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
104 lines (87 loc) · 2.99 KB
/
util.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import {formElement, mapFiltersElement} from './form.js';
import {map, resetMarker, markerGroup, createMarker} from './map.js';
import {getData} from './api.js';
const DISPLAY_ERROR_TIME = 1500;
const ALERT_SHOW_TIME = 1000;
const DEFAULT_PRICE_VALUE = '1000';
const DEFAULT_PREVIEW_AVATAR = 'img/muffin-grey.svg';
const DEFAULT_PREVIEW_PHOTO = '';
const priceResetForm = document.querySelector('#price');
const previewAvatarElement = document.querySelector('.ad-form-header__preview img');
const previewPhotoElement = document.querySelector('.ad-form__photo');
const resetElements = () => {
priceResetForm.placeholder = DEFAULT_PRICE_VALUE;
priceResetForm.min = DEFAULT_PRICE_VALUE;
previewAvatarElement.src = DEFAULT_PREVIEW_AVATAR;
previewPhotoElement.innerHTML = DEFAULT_PREVIEW_PHOTO;
};
const resetMapShow = () => {
map.closePopup();
resetMarker();
markerGroup.clearLayers();
};
const getDataOutput = () => {
getData((data) => {
const slicedData = data.slice(0, 10);
slicedData.forEach((point) => {
createMarker(point);
});
});
};
const getError = () => {
const errorElement = document.querySelector('#error')
.content
.querySelector('.error');
const errorNode = errorElement.cloneNode(true);
document.body.append(errorNode);
const documentErrorClickHeandler = (evt) => {
evt.preventDefault();
errorNode.remove();
document.removeEventListener('click', documentErrorClickHeandler);
};
document.addEventListener('click', documentErrorClickHeandler);
const documentErrorClickHeandlerEscape = (evt) => {
if (evt.key === 'Escape' || evt.key === 'Esc') {
evt.preventDefault();
errorNode.remove();
document.removeEventListener('click', documentErrorClickHeandlerEscape);
}
};
document.addEventListener('keydown', documentErrorClickHeandlerEscape);
};
const getSuccess = () => {
const successElement = document.querySelector('#success')
.content
.querySelector('.success');
const successNode = successElement.cloneNode(true);
document.body.append(successNode);
setTimeout(() => {
successNode.remove();
mapFiltersElement.reset();
formElement.reset();
map.closePopup();
getDataOutput();
resetMapShow();
resetElements();
getDataOutput();
}, DISPLAY_ERROR_TIME);
};
const showAlert = (message) => {
const alertContainer = document.createElement('div');
alertContainer.style.zIndex = 100;
alertContainer.style.position = 'absolute';
alertContainer.style.left = 0;
alertContainer.style.top = '25px';
alertContainer.style.right = 0;
alertContainer.style.padding = '10px 3px';
alertContainer.style.fontSize = '25px';
alertContainer.style.textAlign = 'center';
alertContainer.style.backgroundColor = 'red';
alertContainer.style.color = 'white';
alertContainer.textContent = message;
document.body.append(alertContainer);
setTimeout(() => {
alertContainer.remove();
}, ALERT_SHOW_TIME);
};
export {getError, getSuccess, resetMapShow, showAlert, resetElements, getDataOutput};