Skip to content

Commit

Permalink
fix: check for invalid speedometer names
Browse files Browse the repository at this point in the history
  • Loading branch information
Trainermitch committed Dec 9, 2023
1 parent 41d9500 commit 34c85b5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
3 changes: 2 additions & 1 deletion layout/modals/popups/speedometer-select.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
<Panel id="SpeedometerSelectContainer" class="flow-down horizontal-align-center">
</Panel>
<Panel class="row horizontal-align-center mt-4">
<TextEntry id="SpeedometerName" class="textentry" maxchars="255" placeholder="#Settings_Speedometer_Customname_Placeholder" text="" />
<TextEntry id="SpeedometerName" class="textentry" maxchars="255" placeholder="#Settings_Speedometer_Customname_Placeholder" text="" ontextentrysubmit="SpeedometerSelectPopup.onTextSubmitted()"/>
</Panel>
<Label id="InvalidNameLabel" class="text-m horizontal-align-center" text="#Settings_Speedometer_Customename_InvalidName" />
<Panel class="row horizontal-align-center mt-4 generic-popup__button-row">
<Button id="CancelButton" class="button mr-4" onactivate="UiToolkitAPI.CloseAllVisiblePopups();">
<Label class="button__text" text="#Common_Cancel" />
Expand Down
38 changes: 32 additions & 6 deletions scripts/modals/popups/speedometer-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,46 @@ class SpeedometerSelectPopup {
static container = $('#SpeedometerSelectContainer');
/** @static @type {TextEntry} */
static textEntry = $('#SpeedometerName');
/** @type {Label} @static */
static invalidNameLabel = $('#InvalidNameLabel');
static selected = 0;
static speedometerNames = [];

static onTextSubmitted() {
if (!this.validateSpeedometerNames()) {
this.invalidNameLabel.visible = true;
return;
}

static onAddButtonPressed() {
const callbackHandle = $.GetContextPanel().GetAttributeInt('callback', -1);

if (callbackHandle !== -1)
UiToolkitAPI.InvokeJSCallback(
callbackHandle,
SpeedometerSelectPopup.selected,
SpeedometerSelectPopup.textEntry.text
);
UiToolkitAPI.InvokeJSCallback(callbackHandle, SpeedometerSelectPopup.selected, this.textEntry.text);
UiToolkitAPI.CloseAllVisiblePopups();
}

static validateSpeedometerNames() {
const text = this.textEntry.text;
if (text === '' || text.includes(',')) return false;

if (this.speedometerNames.some((name) => text.toUpperCase() === name.toUpperCase())) return false;

return true;
}

static invalidNameSubmitted() {
SpeedometerSelectPopup.invalidNameLabel.visible = true;
}

static onAddButtonPressed() {
this.textEntry.Submit();
}

static init() {
SpeedometerSelectPopup.invalidNameLabel.visible = false;
SpeedometerSelectPopup.speedometerNames = $.GetContextPanel()
.GetAttributeString('speedometerNames', '')
.split(',');
for (const typeNum of Object.values(SpeedometerTypes)) {
const speedometer = $.CreatePanel('Panel', SpeedometerSelectPopup.container, '');
speedometer.LoadLayoutSnippet('speedometer-radiobutton');
Expand Down
4 changes: 3 additions & 1 deletion scripts/pages/settings/speedometer.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ class Speedometers {
UiToolkitAPI.ShowCustomLayoutPopupParameters(
'',
'file://{resources}/layout/modals/popups/speedometer-select.xml',
`callback=${UiToolkitAPI.RegisterJSCallback((type, name) => this.addSpeedometerByType(type, name))}`
`speedometerNames=${this.detailObjectList.map((x) => x.name)}&callback=${UiToolkitAPI.RegisterJSCallback(
(type, name) => this.addSpeedometerByType(type, name)
)}`
);
}

Expand Down

0 comments on commit 34c85b5

Please sign in to comment.