Clean up MSP_STATUS for DJI O4 (#3107)
[ExpressLRS.git] / src / html / hardware.js
blobc05b4aa51e902a344813ac6f9bf36ab48c55b916
1 /* eslint-disable no-unused-vars */
2 /* eslint-disable comma-dangle */
3 /* eslint-disable require-jsdoc */
5 document.addEventListener('DOMContentLoaded', onReady, false);
7 function _(el) {
8 return document.getElementById(el);
11 function onReady() {
12 // Add some tooltips to the pin type icons [CSS class name, Label]
13 [['icon-input', 'Digital Input'], ['icon-output', 'Digital Output'], ['icon-analog', 'Analog Input'], ['icon-pwm', 'PWM Output']].forEach((t) =>
15 let imgs = document.getElementsByClassName(t[0]);
16 [...imgs].forEach(i => i.title = t[1]);
17 });
19 if (window.File && window.FileList && window.FileReader) {
20 initFiledrag();
23 loadData();
26 function loadData() {
27 xmlhttp = new XMLHttpRequest();
28 xmlhttp.onreadystatechange = function() {
29 if (this.readyState === 4 && this.status === 200) {
30 const data = JSON.parse(this.responseText);
31 updateHardwareSettings(data);
34 xmlhttp.open('GET', '/hardware.json', true);
35 xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
36 xmlhttp.send();
39 function submitHardwareSettings() {
40 const xhr = new XMLHttpRequest();
41 xhr.open('POST', '/hardware.json');
42 xhr.setRequestHeader('Content-Type', 'application/json');
43 const formData = new FormData(_('upload_hardware'));
44 xhr.send(JSON.stringify(Object.fromEntries(formData), function(k, v) {
45 if (v === '') return undefined;
46 if (_(k) && _(k).type === 'checkbox') {
47 return v === 'on';
49 if (_(k) && _(k).classList.contains('array')) {
50 const arr = v.split(',').map((element) => {
51 return Number(element);
52 });
53 return arr.length === 0 ? undefined : arr;
55 return isNaN(v) ? v : +v;
56 }));
57 xhr.onreadystatechange = function() {
58 if (this.readyState === 4 && this.status === 200) {
59 cuteAlert({
60 type: 'question',
61 title: 'Upload Succeeded',
62 message: 'Reboot to take effect',
63 confirmText: 'Reboot',
64 cancelText: 'Close'
65 }).then((e) => {
66 if (e === 'confirm') {
67 const xhr = new XMLHttpRequest();
68 xhr.open('POST', '/reboot');
69 xhr.setRequestHeader('Content-Type', 'application/json');
70 xhr.onreadystatechange = function() {};
71 xhr.send();
73 });
76 return false;
79 function updateHardwareSettings(data) {
80 for (const [key, value] of Object.entries(data)) {
81 if (_(key)) {
82 if (_(key).type === 'checkbox') {
83 _(key).checked = value;
84 } else {
85 if (Array.isArray(value)) _(key).value = value.toString();
86 else _(key).value = value;
90 if (data.customised) _('custom_config').style.display = 'block';
93 function fileDragHover(e) {
94 e.stopPropagation();
95 e.preventDefault();
96 if (e.target === _('filedrag')) e.target.className = (e.type === 'dragover' ? 'hover' : '');
99 function fileSelectHandler(e) {
100 fileDragHover(e);
101 const files = e.target.files || e.dataTransfer.files;
102 _('upload_hardware').reset();
103 for (const f of files) {
104 parseFile(f);
108 function parseFile(file) {
109 const reader = new FileReader();
110 reader.onload = function(e) {
111 const data = JSON.parse(e.target.result);
112 updateHardwareSettings(data);
114 reader.readAsText(file);
117 function initFiledrag() {
118 const fileselect = _('fileselect');
119 const filedrag = _('filedrag');
121 fileselect.addEventListener('change', fileSelectHandler, false);
123 const xhr = new XMLHttpRequest();
124 if (xhr.upload) {
125 filedrag.addEventListener('dragover', fileDragHover, false);
126 filedrag.addEventListener('dragleave', fileDragHover, false);
127 filedrag.addEventListener('drop', fileSelectHandler, false);
128 filedrag.style.display = 'block';
132 @@include("libs.js")