-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
160 lines (125 loc) · 4.5 KB
/
main.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const fileInput = document.querySelector('#file-input')
const container = document.querySelector('.container')
const description = document.createElement('p')
fileInput.addEventListener('change', function createNewBox() {
const file = fileInput.files[0];
let fileName = file.name
let fileSize = parseFloat((file.size / (1024 * 1024))).toFixed(2)
let fileNameFormatted = formateFileName(fileName)
const newBox = document.createElement('div')
newBox.classList.add('box')
newBox.setAttribute('draggable', true)
newBox.innerHTML = `
<img src="./assets/iconBlue.svg" alt="" class="icon">
<div class="info">
<div class="top">
<h2 class="name">${fileNameFormatted}</h2>
<img
src="./assets/cancel-icon.svg"
alt="Ícone de excluir o arquivo correspondente"
class="removeBoxBtn"
title="Remover arquivo"
>
</div>
<p>
<span class="loaded"></span>
<span class="total">${fileSize}MB</span>
</p>
<div class="bottom">
<div class="progress-bar">
<div class="bar"></div>
</div>
<span class="porcentage">48%</span>
</div>
</div>
`
showProgress(newBox, file.size)
turnBoxDraggable(newBox)
removeDescription()
})
function showProgress(newBox, fileSize) {
const loadedSpan = newBox.querySelector('.loaded')
const progressBar = newBox.querySelector('.bar')
const porcentageSpan = newBox.querySelector('.porcentage')
const icon = newBox.querySelector('.icon')
let loaded = 0
let porcentage = 0
let total = fileSize
const intervalTime = 100 // tempo entre cada execução do setInterval em milissegundos
const totalTime = 4000 // tempo total que você deseja que o arquivo demore para carregar em milissegundos
const loadedIncrement = total * intervalTime / totalTime
const porcentageIncrement = 100 * intervalTime / totalTime
const updateValues = setInterval(() => {
loaded += loadedIncrement
porcentage += porcentageIncrement
if (loaded >= total && porcentage >= 100) {
porcentage = 100
loaded = total
icon.src = './assets/iconGreen.svg'
progressBar.classList.add('upload-completed')
porcentageSpan.classList.add('upload-completed')
clearInterval(updateValues)
}
loadedSpan.textContent = `${(loaded / (1024 * 1024)).toFixed(2)}MB / `
progressBar.style.width = `${porcentage}%`
porcentageSpan.textContent = `${Math.round(porcentage)}%`
}, intervalTime)
container.appendChild(newBox)
return newBox
}
container.addEventListener('click', removeBox)
function removeBox(event) {
if(event.target.classList.contains('removeBoxBtn')) {
const box = event.target.closest('.box')
box.classList.add('box-leave')
setTimeout(() => {
container.removeChild(box);
addDescriptionIfContainerIsEmpty()
}, 500)
}
}
function addDescriptionIfContainerIsEmpty() {
if(container.children.length === 0) {
description.classList.add('description')
description.innerHTML = `
Nenhum arquivo foi importado no momento. Por favor, selecione um arquivo.
`
container.appendChild(description)
}
}
function removeDescription() {
if(container.contains(description)) {
container.removeChild(description)
}
}
const limit = 34
function formateFileName(fileName) {
const aboveLimit = fileName.length > limit
const dotsOrEmpty = aboveLimit ? '...' : ''
const fileNameFormatted = fileName.substring(0, limit) + dotsOrEmpty
return fileNameFormatted
}
const toggleTheme = document.querySelector('.toggle')
toggleTheme.addEventListener('click', toggleThemes)
function toggleThemes() {
const body = document.querySelector('body')
const ball = document.querySelector('.ball')
body.classList.toggle('dark')
ball.classList.toggle('dark')
}
let boxBeingDragged = null
function turnBoxDraggable(newbox) {
newbox.addEventListener('dragstart', () => {
boxBeingDragged = newbox
})
newbox.addEventListener('dragend', () => {
boxBeingDragged = null
})
container.addEventListener('dragover', (e) => {
e.preventDefault()
})
container.addEventListener('drop', (e) => {
e.preventDefault()
container.appendChild(boxBeingDragged)
})
}