-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
96 lines (88 loc) · 2.94 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
import { toast } from 'tailwind-toast'
import { registerSW } from 'virtual:pwa-register'
import createFile from './createFile'
// import './index.css'
let csv, loadFiles, dlBtn, classe, sexe, distance, nom
window.onload = function () {
csv = document.getElementById('csv')
loadFiles = document.getElementById('loadFile')
dlBtn = document.getElementById('dlBtn')
classe = document.getElementById('classe')
sexe = document.getElementById('sexe')
distance = document.getElementById('distance')
nom = document.getElementById('name')
csv.onchange = handleFiles
loadFiles.onclick = () => {
csv.click()
}
dlBtn.onclick = process
registerSW({
onOfflineReady() {
toast().success('Page disponible hors connexion', 'Vous pouvez revenir ici sans connexion internet').as('pill').from('bottom', 'center').show()
}
})
}
function saveFile(name, file) {
download(file, name)
classe.value = ''
sexe.value = ''
distance.value = ''
nom.value = ''
csv.value = ''
dlBtn.disabled = true
}
function download(file, name) {
const blob = new window.Blob([file], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
const element = document.createElement('a')
const url = URL.createObjectURL(blob)
element.setAttribute('href', url)
element.setAttribute('download', name)
element.style.visibility = 'hidden'
element.style.display = 'none'
document.body.appendChild(element)
element.click()
document.body.removeChild(element)
}
async function process(e) {
e.preventDefault()
try {
const cla = classe.value
const type = sexe.value.slice(0, 1).toUpperCase()
const long = distance.value
const name = (nom.value ?? 'fichier') + '.xlsx'
if (isNaN(parseInt(long))) {
toast().warning('Erreur:', "La distance n'est pas un nombre valide").show()
}
if (cla && type && long) {
const file = csv.files[0]
const reader = new window.FileReader()
reader.onload = async () => {
const data = reader.result
const result = await createFile(name, data, cla, type, long)
saveFile(name, result)
}
reader.readAsText(file)
} else {
toast().danger('Erreur', "Manque d'informations (classe, sexe, distance)").show()
}
} catch (e) {
console.error('read text')
console.error(e)
toast().danger('Erreur:', "besoin d'un fichier .csv").show()
}
}
const handleFiles = async () => {
const files = csv.files
if (files && files.length > 0) {
const file = files[0]
const name = file.name
const cla = name.match(/^\d{1}/) === null ? '' : name.match(/^\d{1}/)[0]
const type = name.match(/f|g/) === null ? '' : name.match(/f|g/)[0].toUpperCase()
const long = name.match(/(\d+m)/) === null ? '' : name.match(/(\d+m)/)[0].substring(0, name.match(/(\d+m)/)[0].length - 1)
classe.value = cla
sexe.value = type
distance.value = long
nom.value = name.slice(0, name.length - 4)
dlBtn.disabled = false
}
}