1 /* eslint-disable no-unused-vars */
2 /* eslint-disable comma-dangle */
3 /* eslint-disable require-jsdoc */
5 document.addEventListener('DOMContentLoaded', onReady, false);
8 return document.getElementById(el);
12 if (window.File && window.FileList && window.FileReader) {
18 function dec2hex(i, len) {
19 return "0x" + (i+0x10000).toString(16).substr(-len).toUpperCase();
23 xmlhttp = new XMLHttpRequest();
24 xmlhttp.onreadystatechange = function() {
25 if (this.readyState === 4 && this.status === 200) {
26 const data = JSON.parse(this.responseText);
27 _('radio_type1').textContent = dec2hex(data['radio1']['type'], 2)
28 _('radio_hardware1').textContent = dec2hex(data['radio1']['hardware'], 2)
29 _('radio_firmware1').textContent = dec2hex(data['radio1']['firmware'], 4)
31 _('radios').style.display='block'
32 _('radio_type2').textContent = dec2hex(data['radio2']['type'], 2)
33 _('radio_hardware2').textContent = dec2hex(data['radio2']['hardware'], 2)
34 _('radio_firmware2').textContent = dec2hex(data['radio2']['firmware'], 4)
38 xmlhttp.open('GET', '/lr1121.json', true);
39 xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
43 function initFiledrag() {
44 const fileselect = _('firmware_file');
45 const filedrag = _('filedrag');
47 fileselect.addEventListener('change', fileSelectHandler, false);
49 const xhr = new XMLHttpRequest();
51 filedrag.addEventListener('dragover', fileDragHover, false);
52 filedrag.addEventListener('dragleave', fileDragHover, false);
53 filedrag.addEventListener('drop', fileSelectHandler, false);
54 filedrag.style.display = 'block';
58 function fileDragHover(e) {
61 if (e.target === _('filedrag')) e.target.className = (e.type === 'dragover' ? 'hover' : '');
64 function fileSelectHandler(e) {
66 const files = e.target.files || e.dataTransfer.files;
70 function uploadFile(file) {
71 _('upload_btn').disabled = true
73 const formdata = new FormData();
74 formdata.append('upload', file, file.name);
75 const ajax = new XMLHttpRequest();
76 ajax.upload.addEventListener('progress', progressHandler, false);
77 ajax.addEventListener('load', completeHandler, false);
78 ajax.addEventListener('error', errorHandler, false);
79 ajax.addEventListener('abort', abortHandler, false);
80 ajax.open('POST', '/lr1121');
81 ajax.setRequestHeader('X-FileSize', file.size);
82 ajax.setRequestHeader('X-Radio', document.querySelector("input[name=optionsRadio]:checked").value);
86 _('upload_btn').disabled = false
90 function progressHandler(event) {
91 // _("loaded_n_total").innerHTML = "Uploaded " + event.loaded + " bytes of " + event.total;
92 const percent = Math.round((event.loaded / event.total) * 100);
93 _('progressBar').value = percent;
94 _('status').innerHTML = percent + '% uploaded... please wait';
97 function completeHandler(event) {
98 _('status').innerHTML = '';
99 _('progressBar').value = 0;
100 _('upload_btn').disabled = false
101 const data = JSON.parse(event.target.responseText);
102 if (data.status === 'ok') {
103 function showMessage() {
106 title: 'Update Succeeded',
110 // This is basically a delayed display of the success dialog with a fake progress
112 const interval = setInterval(()=>{
113 percent = percent + 2;
114 _('progressBar').value = percent;
115 _('status').innerHTML = percent + '% flashed... please wait';
116 if (percent === 100) {
117 clearInterval(interval);
118 _('status').innerHTML = '';
119 _('progressBar').value = 0;
123 } else if (data.status === 'mismatch') {
126 title: 'Targets Mismatch',
128 confirmText: 'Flash anyway',
131 const xmlhttp = new XMLHttpRequest();
132 xmlhttp.onreadystatechange = function() {
133 if (this.readyState === 4) {
134 _('status').innerHTML = '';
135 _('progressBar').value = 0;
136 if (this.status === 200) {
137 const data = JSON.parse(this.responseText);
140 title: 'Force Update',
146 title: 'Force Update',
147 message: 'An error occurred trying to force the update'
152 xmlhttp.open('POST', '/forceupdate', true);
153 const data = new FormData();
154 data.append('action', e);
160 title: 'Update Failed',
166 function errorHandler(event) {
167 _('status').innerHTML = '';
168 _('progressBar').value = 0;
169 _('upload_btn').disabled = false
172 title: 'Update Failed',
173 message: event.target.responseText
177 function abortHandler(event) {
178 _('status').innerHTML = '';
179 _('progressBar').value = 0;
180 _('upload_btn').disabled = false
183 title: 'Update Aborted',
184 message: event.target.responseText