[Perl] DAQ system for the FAPG.
1
const=Array.fromdocument.querySelectorAll"[data-download-range]";
2
const=document.querySelector"[data-custom-date-fields]";
3
const=customDateFields?new bootstrap.CollapsecustomDateFields, {false}:null;
4
const=document.querySelector"#download-from";
5
const=document.querySelector"#download-to";
6
const=document.querySelector"[data-downloads-error]";
7
const=document.querySelector"form.downloads-form";
8
let=null;
9
10
downloadRangeButtons.forEachoption=>{
11
option.addEventListener"change",()=>{
12
const=option.value==="custom";
13
14
ifcustomDateCollapse{
15
custom?customDateCollapse.show:customDateCollapse.hide;
16
}
17
18
ifcustom{
19
downloadFrom?.focus;
20
return;
21
}
22
23
ifdownloadFrom{
24
downloadFrom.value=option.dataset.from;
25
}
26
27
ifdownloadTo{
28
downloadTo.value=option.dataset.to;
29
}
30
};
31
};
32
33
function(message){
34
if!errorBannerreturn;
35
errorBanner.textContent=message;
36
errorBanner.classList.remove"d-none";
37
}
38
39
function(){
40
if!errorBannerreturn;
41
errorBanner.textContent="";
42
errorBanner.classList.add"d-none";
43
}
44
45
function(blob,filename){
46
const=URL.createObjectURLblob;
47
const=document.createElement"a";
48
link.href=url;
49
link.download=filename;
50
document.body.appendChildlink;
51
link.click;
52
link.remove;
53
URL.revokeObjectURLurl;
54
}
55
56
function(response){
57
const=response.headers.get"Content-Disposition"||"";
58
const=header.match/filename="([^"]+)"/;
59
returnmatch?match1:"readings.csv";
60
}
61
62
function(button,pending){
63
if!buttonreturn;
64
65
button.disabled=pending;
66
button.setAttribute"aria-busy",pending?"true":"false";
67
button.querySelector"[data-download-spinner]"?.classList.toggle"d-none",!pending;
68
}
69
70
if form {
71
form.querySelectorAll"[data-download-button]".forEachbutton=>{
72
button.addEventListener"click",()=>{
73
submittedButton=button;
74
};
75
};
76
77
form.addEventListener"submit",asyncevent=>{
78
event.preventDefault;
79
clearError;
80
81
const=event.submitter||submittedButton||form.querySelector"[data-download-button]";
82
const=new URLSearchParamsnew FormDataform;
83
const=button?.getAttribute"formaction"||form.action;
84
const=new URLaction, window.location.origin;
85
url.search=params;
86
setDownloadPendingbutton,true;
87
88
try{
89
const=awaitfetchurl;
90
91
if!response.ok{
92
const=awaitresponse.text;
93
showErrortext||`Download failed (HTTP ${response.status}).`;
94
return;
95
}
96
97
const=awaitresponse.blob;
98
triggerDownloadblob,filenameFromResponseresponse;
99
}catcherr{
100
showError"Could not reach the server. Check your connection and try again.";
101
}finally{
102
setDownloadPendingbutton,false;
103
submittedButton=null;
104
}
105
};
106
}
107