Merge pull request #471 from Pax1601/470-spawn-menu-qty-sorted-alphabetically-not-numerically

Unit spawn qty now sorted numerically.
This commit is contained in:
Pax1601 2023-10-19 01:29:45 +02:00 committed by GitHub
commit d14741f3b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 6 deletions

View File

@ -36,8 +36,21 @@ export class Dropdown {
return this.#container;
}
setOptions(optionsList: string[], sortAlphabetically: boolean = true) {
this.#optionsList = optionsList.sort();
setOptions(optionsList: string[], sort:""|"string"|"number" = "string") {
if ( sort === "number" ) {
this.#optionsList = optionsList.sort( (optionA:string, optionB:string) => {
const a = parseInt( optionA );
const b = parseInt( optionB );
if ( a > b )
return 1;
else
return ( b > a ) ? -1 : 0;
});
} else if ( sort === "string" ) {
this.#optionsList = optionsList.sort();
}
if (this.#optionsList.length == 0) {
optionsList = ["No options available"]
this.#value.innerText = "No options available";

View File

@ -343,10 +343,7 @@ export class UnitSpawnMenu {
setMaxUnitCount(maxUnitCount: number) {
/* Create the unit count options */
var countOptions: string[] = [];
for (let i = 1; i <= maxUnitCount; i++)
countOptions.push(i.toString());
this.#unitCountDropdown.setOptions(countOptions);
this.#unitCountDropdown.setOptions( [...Array(maxUnitCount).keys()].map( n => (n+1).toString() ), "number");
this.#unitCountDropdown.selectValue(0);
}