1
0
Fork 0
mirror of https://github.com/betaflight/betaflight-configurator.git synced 2025-07-16 21:05:30 +03:00

Chore/move gui to modules (#3001)

Move `GUI` to esm
This commit is contained in:
Tomas Chmelevskij 2022-12-28 21:08:48 +01:00 committed by GitHub
parent 704970e291
commit 483dec9103
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
34 changed files with 559 additions and 546 deletions

1
.gitignore vendored
View file

@ -31,3 +31,4 @@ nbproject/
# Eclipse
.project
.settings/
test-results-junit/

View file

@ -1,4 +1,4 @@
'use strict';
import GUI from './gui.js';
/**
* Encapsulates the Clipboard logic, depending on web or nw
@ -99,3 +99,5 @@ if (GUI.isNWJS()){
} else {
Clipboard._configureClipboardAsOther();
}
export default Clipboard;

View file

@ -1,6 +1,3 @@
'use strict';
window.TABS = {}; // filled by individual tab js file
const GUI_MODES = {
@ -9,7 +6,8 @@ const GUI_MODES = {
Other: "Other",
};
const GuiControl = function () {
class GuiControl {
constructor() {
this.auto_connect = false;
this.connecting_to = false;
this.connected_to = false;
@ -71,19 +69,13 @@ const GuiControl = function () {
this.Mode = GUI_MODES.Other;
}
}
};
function GUI_checkOperatingSystem() {
return navigator?.userAgentData?.platform || 'Android';
}
// Timer managing methods
// name = string
// code = function reference (code to be executed)
// interval = time interval in miliseconds
// first = true/false if code should be ran initially before next timer interval hits
GuiControl.prototype.interval_add = function (name, code, interval, first) {
interval_add(name, code, interval, first) {
const data = { 'name': name, 'timer': null, 'code': code, 'interval': interval, 'fired': 0, 'paused': false };
if (first === true) {
@ -101,14 +93,13 @@ GuiControl.prototype.interval_add = function (name, code, interval, first) {
this.interval_array.push(data); // push to primary interval array
return data;
};
}
// name = string
// code = function reference (code to be executed)
// interval = time interval in miliseconds
// first = true/false if code should be ran initially before next timer interval hits
// condition = function reference with true/false result, a condition to be checked before every interval code execution
GuiControl.prototype.interval_add_condition = function (name, code, interval, first, condition) {
interval_add_condition(name, code, interval, first, condition) {
this.interval_add(name, () => {
if (condition()) {
code();
@ -116,10 +107,9 @@ GuiControl.prototype.interval_add_condition = function (name, code, interval, fi
this.interval_remove(name);
}
}, interval, first);
};
}
// name = string
GuiControl.prototype.interval_remove = function (name) {
interval_remove(name) {
for (let i = 0; i < this.interval_array.length; i++) {
if (this.interval_array[i].name === name) {
clearInterval(this.interval_array[i].timer); // stop timer
@ -131,10 +121,9 @@ GuiControl.prototype.interval_remove = function (name) {
}
return false;
};
}
// name = string
GuiControl.prototype.interval_pause = function (name) {
interval_pause(name) {
for (let i = 0; i < this.interval_array.length; i++) {
if (this.interval_array[i].name === name) {
clearInterval(this.interval_array[i].timer);
@ -145,10 +134,9 @@ GuiControl.prototype.interval_pause = function (name) {
}
return false;
};
}
// name = string
GuiControl.prototype.interval_resume = function (name) {
interval_resume(name) {
function executeCode(obj) {
obj.code(); // execute code
@ -168,11 +156,10 @@ GuiControl.prototype.interval_resume = function (name) {
}
return false;
};
}
// input = array of timers thats meant to be kept, or nothing
// return = returns timers killed in last call
GuiControl.prototype.interval_kill_all = function (keepArray) {
interval_kill_all(keepArray) {
const self = this;
let timersKilled = 0;
@ -196,14 +183,14 @@ GuiControl.prototype.interval_kill_all = function (keepArray) {
}
return timersKilled;
};
}
// name = string
// code = function reference (code to be executed)
// timeout = timeout in miliseconds
GuiControl.prototype.timeout_add = function (name, code, timeout) {
timeout_add(name, code, timeout) {
const self = this;
const data = {'name': name,
const data = {
'name': name,
'timer': null,
'timeout': timeout,
};
@ -212,6 +199,7 @@ GuiControl.prototype.timeout_add = function (name, code, timeout) {
data.timer = setTimeout(function () {
code(); // execute code
// remove object from array
const index = self.timeout_array.indexOf(data);
if (index > -1) {
@ -222,10 +210,9 @@ GuiControl.prototype.timeout_add = function (name, code, timeout) {
this.timeout_array.push(data); // push to primary timeout array
return data;
};
}
// name = string
GuiControl.prototype.timeout_remove = function (name) {
timeout_remove(name) {
for (let i = 0; i < this.timeout_array.length; i++) {
if (this.timeout_array[i].name === name) {
clearTimeout(this.timeout_array[i].timer); // stop timer
@ -237,11 +224,10 @@ GuiControl.prototype.timeout_remove = function (name) {
}
return false;
};
}
// no input parameters
// return = returns timers killed in last call
GuiControl.prototype.timeout_kill_all = function () {
timeout_kill_all() {
let timersKilled = 0;
for (let i = 0; i < this.timeout_array.length; i++) {
@ -253,10 +239,9 @@ GuiControl.prototype.timeout_kill_all = function () {
this.timeout_array = []; // drop objects
return timersKilled;
};
}
// message = string
GuiControl.prototype.log = function (message) {
log(message) {
const commandLog = $('div#log');
const d = new Date();
const year = d.getFullYear();
@ -270,12 +255,11 @@ GuiControl.prototype.log = function (message) {
const formattedDate = `${year}-${month}-${date} @${time}`;
$('div.wrapper', commandLog).append(`<p>${formattedDate} -- ${message}</p>`);
commandLog.scrollTop($('div.wrapper', commandLog).height());
};
}
// Method is called every time a valid tab change event is received
// callback = code to run when cleanup is finished
// default switch doesn't require callback to be set
GuiControl.prototype.tab_switch_cleanup = function (callback) {
tab_switch_cleanup(callback) {
MSP.callbacks_cleanup(); // we don't care about any old data that might or might not arrive
this.interval_kill_all(); // all intervals (mostly data pulling) needs to be removed on tab switch
@ -284,9 +268,8 @@ GuiControl.prototype.tab_switch_cleanup = function (callback) {
} else {
callback();
}
};
GuiControl.prototype.switchery = function() {
}
switchery() {
const COLOR_ACCENT = 'var(--accent)';
const COLOR_SWITCHERY_SECOND = 'var(--switcherysecond)';
@ -325,9 +308,8 @@ GuiControl.prototype.switchery = function() {
});
$(elem).removeClass('togglemedium');
});
};
GuiControl.prototype.content_ready = function (callback) {
}
content_ready(callback) {
this.switchery();
@ -374,28 +356,23 @@ GuiControl.prototype.content_ready = function (callback) {
if (callback) {
callback();
}
};
GuiControl.prototype.selectDefaultTabWhenConnected = function() {
}
selectDefaultTabWhenConnected() {
const result = ConfigStorage.get(['rememberLastTab', 'lastTab']);
const tab = result.rememberLastTab && result.lastTab ? result.lastTab : 'tab_setup';
$(`#tabs ul.mode-connected .${tab} a`).trigger('click');
};
GuiControl.prototype.isNWJS = function () {
}
isNWJS() {
return this.Mode === GUI_MODES.NWJS;
};
GuiControl.prototype.isCordova = function () {
}
isCordova() {
return this.Mode === GUI_MODES.Cordova;
};
GuiControl.prototype.isOther = function () {
}
isOther() {
return this.Mode === GUI_MODES.Other;
};
GuiControl.prototype.showYesNoDialog = function(yesNoDialogSettings) {
}
showYesNoDialog(yesNoDialogSettings) {
// yesNoDialogSettings:
// title, text, buttonYesText, buttonNoText, buttonYesCallback, buttonNoCallback
const dialog = $(".dialogYesNo");
@ -423,9 +400,8 @@ GuiControl.prototype.showYesNoDialog = function(yesNoDialogSettings) {
});
dialog[0].showModal();
};
GuiControl.prototype.showWaitDialog = function(waitDialogSettings) {
}
showWaitDialog(waitDialogSettings) {
// waitDialogSettings:
// title, buttonCancelCallback
const dialog = $(".dialogWait")[0];
@ -444,9 +420,8 @@ GuiControl.prototype.showWaitDialog = function(waitDialogSettings) {
dialog.showModal();
return dialog;
};
GuiControl.prototype.showInformationDialog = function(informationDialogSettings) {
}
showInformationDialog(informationDialogSettings) {
// informationDialogSettings:
// title, text, buttonConfirmText
return new Promise(resolve => {
@ -468,9 +443,8 @@ GuiControl.prototype.showInformationDialog = function(informationDialogSettings)
dialog[0].showModal();
});
};
GuiControl.prototype.saveToTextFileDialog = function(textToSave, suggestedFileName, extension) {
}
saveToTextFileDialog(textToSave, suggestedFileName, extension) {
return new Promise((resolve, reject) => {
const accepts = [{ description: `${extension.toUpperCase()} files`, extensions: [extension] }];
@ -483,9 +457,8 @@ GuiControl.prototype.saveToTextFileDialog = function(textToSave, suggestedFileNa
entry => this._saveToTextFileDialogFileSelected(entry, textToSave, resolve, reject),
);
});
};
GuiControl.prototype._saveToTextFileDialogFileSelected = function(entry, textToSave, resolve, reject) {
}
_saveToTextFileDialogFileSelected(entry, textToSave, resolve, reject) {
checkChromeRuntimeError();
if (!entry) {
@ -515,9 +488,8 @@ GuiControl.prototype._saveToTextFileDialogFileSelected = function(entry, textToS
reject();
console.error('Failed to get file writer');
});
};
GuiControl.prototype.readTextFileDialog = function(extension) {
}
readTextFileDialog(extension) {
const accepts = [{ description: `${extension.toUpperCase()} files`, extensions: [extension] }];
return new Promise(resolve => {
@ -541,22 +513,29 @@ GuiControl.prototype.readTextFileDialog = function(extension) {
});
});
});
};
GuiControl.prototype.escapeHtml = function(unsafe) {
}
escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
};
GuiControl.prototype.addLinksTargetBlank = function(element) {
}
addLinksTargetBlank(element) {
element.find('a').each(function () {
$(this).attr('target', '_blank');
});
};
}
}
function GUI_checkOperatingSystem() {
return navigator?.userAgentData?.platform || 'Android';
}
const GUI = new GuiControl();
// initialize object into GUI variable
window.GUI = new GuiControl();
window.GUI = GUI;
export default GUI;

View file

@ -1,5 +1,6 @@
import i18next from 'i18next';
import i18nextXHRBackend from 'i18next-xhr-backend';
import GUI from './gui.js';
const i18n = {};
/*

View file

@ -1,5 +1,6 @@
import '../components/init.js';
import { i18n } from './localization.js';
import GUI from './gui.js';
$(document).ready(function () {

View file

@ -1,4 +1,5 @@
import { i18n } from '../localization';
import GUI from '../gui';
const adjustments = {};

View file

@ -1,4 +1,5 @@
import { i18n } from '../localization';
import GUI from '../gui';
const auxiliary = {};

View file

@ -1,4 +1,6 @@
import { i18n } from "../localization";
import Clipboard from "../Clipboard";
import GUI from '../gui';
const cli = {
lineDelayMs: 15,

View file

@ -1,5 +1,6 @@
import semver from 'semver';
import { i18n } from '../localization';
import GUI from '../gui';
const configuration = {
// intended

View file

@ -1,4 +1,5 @@
import { i18n } from "../localization";
import GUI from '../gui';
const failsafe = {};

View file

@ -1,4 +1,5 @@
import { i18n } from '../localization';
import GUI from '../gui';
const firmware_flasher = {
targets: null,

View file

@ -1,4 +1,5 @@
import { i18n } from "../localization";
import GUI from '../gui';
const gps = {};
gps.initialize = function (callback) {

View file

@ -1,3 +1,5 @@
import GUI from '../gui';
const help = {};
help.initialize = function (callback) {

View file

@ -1,3 +1,5 @@
import GUI from '../gui';
const landing = {};
landing.initialize = function (callback) {

View file

@ -1,4 +1,5 @@
import { i18n } from "../localization";
import GUI from '../gui';
const led_strip = {
wireMode: false,

View file

@ -1,4 +1,5 @@
import { millitime } from '../utils/common.js';
import GUI from '../gui';
const logging = {};
logging.initialize = function (callback) {

View file

@ -1,4 +1,5 @@
import { i18n } from "../localization";
import GUI from '../gui';
const motors = {
previousDshotBidir: null,

View file

@ -1,4 +1,5 @@
import { i18n } from "../localization";
import GUI from '../gui';
let sdcardTimer;

View file

@ -1,4 +1,5 @@
import { i18n } from '../localization';
import GUI from '../gui';
const options = {};
options.initialize = function (callback) {

View file

@ -1,4 +1,5 @@
import { i18n } from "../localization";
import GUI from '../gui';
const FONT = {};
const SYM = {};

View file

@ -1,5 +1,6 @@
import { i18n } from "../localization";
import { colorTables, getColorForPercentage } from '../utils/css.js';
import GUI from '../gui';
const pid_tuning = {
RATE_PROFILE_MASK: 128,

View file

@ -1,5 +1,6 @@
import semver from 'semver';
import { i18n } from "../localization";
import GUI from '../gui';
const ports = {
// intentional

View file

@ -1,4 +1,5 @@
import { i18n } from '../localization';
import GUI from '../gui';
const power = {
supported: false,

View file

@ -1,4 +1,5 @@
import { i18n } from "../localization";
import GUI from '../gui';
const MD5 = require('md5.js');

View file

@ -1,4 +1,5 @@
import { i18n } from "../localization";
import GUI from '../gui';
const sensors = {};
sensors.initialize = function (callback) {

View file

@ -1,4 +1,5 @@
import { i18n } from "../localization";
import GUI from '../gui';
const servos = {};
servos.initialize = function (callback) {

View file

@ -1,4 +1,5 @@
import { i18n } from '../localization';
import GUI from '../gui';
const setup = {
yaw_fix: 0.0,

View file

@ -1,4 +1,5 @@
import { i18n } from "../localization";
import GUI from '../gui';
const setup_osd = {
};

View file

@ -1,4 +1,5 @@
import { i18n } from '../localization';
import GUI from '../gui';
const staticTab = {};
staticTab.initialize = function (staticTabName, callback) {

View file

@ -1,4 +1,5 @@
import { i18n } from "../localization";
import GUI from '../gui';
const transponder = {
available: false,

View file

@ -1,4 +1,6 @@
import { i18n } from "../localization";
import Clipboard from "../Clipboard";
import GUI from '../gui';
const vtx = {
supported: false,

View file

@ -90,7 +90,6 @@
<script type="text/javascript" src="./js/port_handler.js"></script>
<script type="text/javascript" src="./js/port_usage.js"></script>
<script type="text/javascript" src="./js/serial.js"></script>
<script type="text/javascript" src="./js/gui.js"></script>
<script type="text/javascript" src="./js/mdns_discovery.js"></script>
<script type="text/javascript" src="./js/huffman.js"></script>
<script type="text/javascript" src="./js/default_huffman_tree.js"></script>
@ -113,7 +112,6 @@
<script type="text/javascript" src="./js/ConfigInserter.js"></script>
<script type="text/javascript" src="./js/GitHubApi.js"></script>
<script type="module" src="./js/main.js"></script>
<script type="text/javascript" src="./js/Clipboard.js"></script>
<!-- TODO: might be removed when everythign is in modules -->
<script type="text/javascript" src="./tabs/presets/CliEngine.js"></script>
<script type="text/javascript" src="./tabs/presets/PickedPreset.js"></script>

View file

@ -1,4 +1,4 @@
'use strict';
import GUI from '../../js/gui';
import { favoritePresets } from './FavoritePresets';

View file

@ -17,7 +17,7 @@ module.exports = function(config) {
'./src/js/serial.js',
'./src/js/data_storage.js',
{ pattern: './src/js/localization.js', type: 'module', watched: false },
'./src/js/gui.js',
{ pattern: './src/js/gui.js', type: 'module', watched: false },
'./src/js/CliAutoComplete.js',
{ pattern: './src/js/tabs/cli.js', type: 'module', watched: false },
'./src/js/phones_ui.js',
@ -41,6 +41,7 @@ module.exports = function(config) {
preprocessors: {
'./src/js/localization.js': ['rollup'],
'./src/js/tabs/cli.js': ['rollup'],
'./src/js/gui.js': ['rollup'],
},
rollupPreprocessor: {
plugins: [