brakeconf/gui/app.js

212 lines
6.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const ENABLE_AUTO_CHECK_THEME = true;
// Перечень кнопок переключения окон
const enumWindowButtons = [
'btnWindowDevice',
'btnWindowTesting',
'btnWindowSettings',
'btnWindowFirmwareUpdate',
'btnWindowLog',
'btnWindowAbout',
];
// Перечень окон программы
const enumWindow = [
'appWindowDevice',
'appWindowTesting',
'appWindowSettings',
'appWindowFirmwareUpdate',
'appWindowLog',
'appWindowAbout',
];
/* main */
window.onload = function () {
/* Автоматически активирует тему в зависимости от настроек системы */
if (ENABLE_AUTO_CHECK_THEME) {
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
switch_theme('dark');
} else {
switch_theme('light');
}
}
document.getElementById('btn_theme_switch').addEventListener('click', switch_theme, false);
enumWindowButtons.forEach((btn) => {
document.getElementById(btn).addEventListener('click', ui_window_btn_active, false);
});
document.getElementById('btn_app_close').addEventListener('click', close_app);
document.getElementById('btn_port_refresh').addEventListener('click', refresh_ports);
document.getElementById('btn_device_connect').addEventListener('click', connect_to_device);
document.getElementById('option_zone_reset').addEventListener('click', reset_test_zones);
};
/**
* Изменяет тему приложения
*/
function switch_theme() {
let app = document.documentElement;
let theme = app.getAttribute('data-bs-theme');
if (theme == 'auto' || theme == 'light') {
app.setAttribute('data-bs-theme', 'dark');
} else {
app.setAttribute('data-bs-theme', 'light');
}
}
/**
* ...
*/
function ui_window_btn_active(clicked_btn) {
enumWindowButtons.forEach((btn) => {
const button = document.getElementById(btn);
button.classList.remove('active');
});
clicked_btn.target.classList.add('active');
ui_show_window(clicked_btn.target.getAttribute('id').replace('btn', 'app'));
}
/**
* ...
*/
function ui_show_window(window_name) {
const current_window = document.getElementById(window_name);
enumWindow.forEach((w) => {
let each_window = document.getElementById(w);
each_window.classList.remove('d-flex');
each_window.classList.add('d-none');
});
current_window.classList.remove('d-none');
}
/**
* Обновляет список доступных портов
*/
function refresh_ports() {
const port_selector = document.getElementById('port_selector');
webui.call('webui_refresh_ports').then((response) => {
const ports_array = response;
// Выводит уведомление о доступных устройствах
if (ports_array.ports.length > 0) {
port_selector.innerHTML = '';
// BUG: Нет пробела между двоеточием и количеством устройств
alert(`Доступных портов: ${ports_array.ports.length}`);
} else {
alert('Отсутсвуют подключённые устройства!');
return 1;
}
ports_array.ports.forEach((port, index) => {
if (index == 0) {
const default_option = document.createElement('option');
default_option.innerText = 'Выберите порт...';
default_option.value = 'null';
default_option.selected = true;
port_selector.appendChild(default_option);
}
const option = document.createElement('option');
option.innerText = port;
option.value = port;
port_selector.appendChild(option);
});
});
}
/**
* Подключается к выбранному устройству
*/
function connect_to_device() {
const port_selector = document.getElementById('port_selector');
const btn_port_refresh = document.getElementById('btn_port_refresh');
const btn_device_connect = document.getElementById('btn_device_connect');
if (port_selector.value == 'null') {
alert('Необходимо выбрать порт!');
return;
}
webui.call('webui_connect_device', port_selector.value).then((response) => {
alert(`!: ${response}`);
});
if (btn_device_connect.innerText == 'Подключиться') {
port_selector.disabled = true;
btn_port_refresh.disabled = true;
btn_device_connect.classList.add('btn-success');
btn_device_connect.classList.remove('btn-primary');
btn_device_connect.innerText = 'Отключиться';
} else {
port_selector.disabled = false;
btn_port_refresh.disabled = false;
btn_device_connect.classList.add('btn-primary');
btn_device_connect.classList.remove('btn-success');
btn_device_connect.innerText = 'Подключиться';
}
}
/**
* Добавляет строку в окно лога
*/
function add_line_log(text) {
const connection_log_console = document.getElementById('connection_log_console');
connection_log_console.innerHTML += `${text}\n`;
/* Автоматически прокручивает лог в конец */
const sw_connection_log_autoscroll = document.getElementById('sw_connection_log_autoscroll');
if (sw_connection_log_autoscroll.checked) {
connection_log_console.scrollTop = connection_log_console.scrollHeight;
}
}
/**
* Меняет состояние checkbox'а статуса режимов в окне `Тестирование`
*/
function set_mode_status(mode, status) {
const set_value = (element, status) => (element.checked = status);
if (mode == 'emergency') {
const element = document.getElementById('mode_status_emergency');
status ? set_value(element, status) : set_value(element, status);
} else if (mode == 'reverse') {
const element = document.getElementById('mode_status_reverse');
status ? set_value(element, status) : set_value(element, status);
} else {
return -1;
}
return 0;
}
/**
* Сбрасывает состояние переключателей зон в окне `Тестирование`
*/
function reset_test_zones() {
const option_zones = document.getElementsByName('option-zone');
for (let checkbox of option_zones) {
checkbox.checked = false;
}
}
/**
*
*/
function close_app() {
webui.call('close_app', 'a').then((response) => {});
}