-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
109 lines (93 loc) · 3.65 KB
/
index.js
File metadata and controls
109 lines (93 loc) · 3.65 KB
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
105
106
107
108
109
const phoneRegex = /^(8|\+7)(\s|\(|-)?(\d{3})(\s|\)|-)?(\d{3})(\s|-)?(\d{2})(\s|-)?(\d{2})$/;
// Проверяет, является ли файл изображением формата JPEG или PNG
function checkFileValid(file) {
const validTypes = ['image/jpeg', 'image/png'];
return validTypes.includes(file.type);
}
// Создаёт элемент для отображения выбранного файла с кнопкой удаления
function createFileBox(filename) {
const box = document.createElement('div');
box.classList.add('file-box');
const name = document.createElement('p')
name.textContent = filename;
const deleteButton = document.createElement('button');
deleteButton.textContent = 'X';
deleteButton.onclick = () => box.remove();
box.appendChild(name);
box.appendChild(deleteButton);
return box;
}
// Добавляет визуальный элемент выбранного файла в форму
function appendFile(file){
const newNode = createFileBox(file.name);
document.getElementById('form').insertBefore(newNode, document.getElementById('submitButton'))
}
// Проверяет корректность номера телефона и выводит сообщение об ошибке
function checkPhoneValid() {
if (!phoneRegex.test(phone.value)){
phone.setCustomValidity('Указан некорректный номер телефона')
} else{
phone.setCustomValidity('');
}
}
// Открывает диалог выбора файла и добавляет файл, если он корректный
function chooseFile() {
const input = document.createElement('input');
input.type = 'file';
input.onchange = function (e) {
const file = e.target.files[0];
if (!checkFileValid(file)){
alert('Некорректный тип файла. Пожалуйста, выберите изображение в формате JPEG или PNG.');
}
else{
appendFile(file);
}
}
input.click();
}
function initDropzone() {
const dropzone = document.getElementById('dropzone');
dropzone.addEventListener('dragover', (e) => {
e.preventDefault();
e.stopPropagation();
})
dropzone.addEventListener('drop', (e) => {
e.preventDefault();
e.stopPropagation();
const file = e.dataTransfer.files[0];
if (!checkFileValid(file)){
alert('Некорректный тип файла. Пожалуйста, выберите изображение в формате JPEG или PNG.');
}
else{
appendFile(file);
}
})
}
function appendFile(file){
const newNode = createFileBox(file.name);
document.getElementById('form').insertBefore(newNode, document.getElementById('submitButton'))
}
function sendForm(event){
event.preventDefault();
form.reset()
document.querySelectorAll('.file-box').forEach(box => box.remove());
}
function createFileBox(filename) {
const box = document.createElement('div');
box.classList.add('file-box');
const name = document.createElement('p')
name.textContent = filename;
const deleteButton = document.createElement('button');
deleteButton.textContent = 'X';
deleteButton.onclick = () => box.remove();
box.appendChild(name);
box.appendChild(deleteButton);
return box;
}
window.onload = () => {
initDropzone();
const phone = document.getElementById('phone');
const form = document.getElementById('form');
phone.addEventListener('keyup', checkPhoneValid)
form.addEventListener('submit', sendForm)
}