Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check for invalid speedometer names #110

Merged
merged 1 commit into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_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;
tsa96 marked this conversation as resolved.
Show resolved Hide resolved
}

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
6 changes: 5 additions & 1 deletion scripts/pages/settings/speedometer.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,11 @@ 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)
.join(',')}&callback=${UiToolkitAPI.RegisterJSCallback((type, name) =>
this.addSpeedometerByType(type, name)
)}`
);
}

Expand Down
Loading