feat(drivers): country flag before the driver's name in timing, overlay, results, printouts and PDF (1.89.20)
This commit is contained in:
1 parent
da20a7e978
commit
6e45918e74
101 files changed
+2898
-45
No files matched your search
@@ -0,0 +1,84 @@
|
||||
// Country flags for drivers. The driver's "country" is free text ("Sverige", "Sweden", "SE",
|
||||
// "SWE" all work); countryCode() turns it into a code, flagHtml() into a small flag image from
|
||||
// src/flags (flag-icons, MIT - local, so it works offline and in OBS on Windows, which shows
|
||||
// emoji flags as letters). No country, or one not in the list: no flag.
|
||||
|
||||
// [code, IOC/3-letter, Swedish name, English name]
|
||||
export const COUNTRIES = [
|
||||
["SE", "SWE", "Sverige", "Sweden"], ["NO", "NOR", "Norge", "Norway"], ["DK", "DEN", "Danmark", "Denmark"],
|
||||
["FI", "FIN", "Finland", "Finland"], ["IS", "ISL", "Island", "Iceland"], ["EE", "EST", "Estland", "Estonia"],
|
||||
["LV", "LAT", "Lettland", "Latvia"], ["LT", "LTU", "Litauen", "Lithuania"], ["DE", "GER", "Tyskland", "Germany"],
|
||||
["AT", "AUT", "Österrike", "Austria"], ["CH", "SUI", "Schweiz", "Switzerland"], ["LI", "LIE", "Liechtenstein", "Liechtenstein"],
|
||||
["NL", "NED", "Nederländerna", "Netherlands"], ["BE", "BEL", "Belgien", "Belgium"], ["LU", "LUX", "Luxemburg", "Luxembourg"],
|
||||
["FR", "FRA", "Frankrike", "France"], ["IT", "ITA", "Italien", "Italy"], ["ES", "ESP", "Spanien", "Spain"],
|
||||
["PT", "POR", "Portugal", "Portugal"], ["AD", "AND", "Andorra", "Andorra"], ["MC", "MON", "Monaco", "Monaco"],
|
||||
["SM", "SMR", "San Marino", "San Marino"], ["GB", "GBR", "Storbritannien", "United Kingdom"], ["IE", "IRL", "Irland", "Ireland"],
|
||||
["GB-ENG", "ENG", "England", "England"], ["GB-SCT", "SCO", "Skottland", "Scotland"], ["GB-WLS", "WAL", "Wales", "Wales"],
|
||||
["PL", "POL", "Polen", "Poland"], ["CZ", "CZE", "Tjeckien", "Czechia"], ["SK", "SVK", "Slovakien", "Slovakia"],
|
||||
["HU", "HUN", "Ungern", "Hungary"], ["SI", "SLO", "Slovenien", "Slovenia"], ["HR", "CRO", "Kroatien", "Croatia"],
|
||||
["BA", "BIH", "Bosnien och Hercegovina", "Bosnia and Herzegovina"], ["RS", "SRB", "Serbien", "Serbia"], ["ME", "MNE", "Montenegro", "Montenegro"],
|
||||
["MK", "MKD", "Nordmakedonien", "North Macedonia"], ["AL", "ALB", "Albanien", "Albania"], ["GR", "GRE", "Grekland", "Greece"],
|
||||
["BG", "BUL", "Bulgarien", "Bulgaria"], ["RO", "ROU", "Rumänien", "Romania"], ["MD", "MDA", "Moldavien", "Moldova"],
|
||||
["UA", "UKR", "Ukraina", "Ukraine"], ["BY", "BLR", "Belarus", "Belarus"], ["RU", "RUS", "Ryssland", "Russia"],
|
||||
["MT", "MLT", "Malta", "Malta"], ["CY", "CYP", "Cypern", "Cyprus"], ["TR", "TUR", "Turkiet", "Türkiye"],
|
||||
["US", "USA", "USA", "United States"], ["CA", "CAN", "Kanada", "Canada"], ["MX", "MEX", "Mexiko", "Mexico"],
|
||||
["BR", "BRA", "Brasilien", "Brazil"], ["AR", "ARG", "Argentina", "Argentina"], ["CL", "CHI", "Chile", "Chile"],
|
||||
["UY", "URU", "Uruguay", "Uruguay"], ["AU", "AUS", "Australien", "Australia"], ["NZ", "NZL", "Nya Zeeland", "New Zealand"],
|
||||
["ZA", "RSA", "Sydafrika", "South Africa"], ["JP", "JPN", "Japan", "Japan"], ["CN", "CHN", "Kina", "China"],
|
||||
["KR", "KOR", "Sydkorea", "South Korea"], ["TW", "TPE", "Taiwan", "Taiwan"], ["HK", "HKG", "Hongkong", "Hong Kong"],
|
||||
["TH", "THA", "Thailand", "Thailand"], ["MY", "MAS", "Malaysia", "Malaysia"], ["SG", "SGP", "Singapore", "Singapore"],
|
||||
["IN", "IND", "Indien", "India"], ["ID", "INA", "Indonesien", "Indonesia"], ["PH", "PHI", "Filippinerna", "Philippines"],
|
||||
["AE", "UAE", "Förenade Arabemiraten", "United Arab Emirates"], ["IL", "ISR", "Israel", "Israel"], ["SA", "KSA", "Saudiarabien", "Saudi Arabia"],
|
||||
["QA", "QAT", "Qatar", "Qatar"],
|
||||
];
|
||||
|
||||
const lookup = new Map();
|
||||
COUNTRIES.forEach(([code, ioc, sv, en]) => [code, ioc, sv, en].forEach((key) => lookup.set(key.toLowerCase(), code)));
|
||||
// Common other spellings.
|
||||
[["sweden", "SE"], ["swe", "SE"], ["sverige", "SE"], ["norway", "NO"], ["noreg", "NO"], ["dnk", "DK"], ["deu", "DE"], ["deutschland", "DE"],
|
||||
["uk", "GB"], ["great britain", "GB"], ["storbritannien", "GB"], ["holland", "NL"], ["nld", "NL"], ["che", "CH"], ["usa", "US"],
|
||||
["united states of america", "US"], ["suomi", "FI"], ["danmark", "DK"], ["österreich", "AT"], ["schweiz", "CH"], ["turkey", "TR"]]
|
||||
.forEach(([key, code]) => lookup.set(key, code));
|
||||
|
||||
export function countryCode(value) {
|
||||
return lookup.get(String(value || "").trim().toLowerCase()) || "";
|
||||
}
|
||||
|
||||
export function countryName(code, language = "sv") {
|
||||
const country = COUNTRIES.find((item) => item[0] === countryCode(code));
|
||||
return country ? (language === "en" ? country[3] : country[2]) : "";
|
||||
}
|
||||
|
||||
// The names the driver form offers (in the app's language), sorted.
|
||||
export function countryChoices(language = "sv") {
|
||||
const index = language === "en" ? 3 : 2;
|
||||
return COUNTRIES.map((item) => item[index]).sort((a, b) => a.localeCompare(b, language));
|
||||
}
|
||||
|
||||
// A small flag before a name; "" without a known country. The title gives the name on hover.
|
||||
export function flagHtml(value, language = "sv") {
|
||||
const code = countryCode(value);
|
||||
if (!code) return "";
|
||||
return `<img class="country-flag" src="/src/flags/${code.toLowerCase()}.svg" alt="${code}" title="${countryName(code, language)}" width="16" height="12" loading="lazy" decoding="async" />`;
|
||||
}
|
||||
|
||||
// A driver's country: their own, else one set on another class entry of the same person
|
||||
// (same profile), so it only has to be filled in once.
|
||||
export function driverCountryCode(drivers, driverId, getProfileId = (driver) => driver?.profileId || "") {
|
||||
const list = Array.isArray(drivers) ? drivers : [];
|
||||
const driver = list.find((item) => item.id === driverId);
|
||||
if (!driver) return "";
|
||||
if (countryCode(driver.country)) return countryCode(driver.country);
|
||||
const profileId = getProfileId(driver);
|
||||
return countryCode(list.find((item) => item.country && profileId && getProfileId(item) === profileId)?.country);
|
||||
}
|
||||
|
||||
// The app tells this module how to find a driver's country once; renderers then call rowFlag(row)
|
||||
// for any row with a driverId (leaderboard, standings, heat lists) without passing it around.
|
||||
let resolveDriverCountry = () => "";
|
||||
export function setDriverCountryResolver(fn) {
|
||||
resolveDriverCountry = typeof fn === "function" ? fn : () => "";
|
||||
}
|
||||
export function rowFlag(row, language = "sv") {
|
||||
return flagHtml(row?.countryCode || (row?.driverId ? resolveDriverCountry(row.driverId) : ""), language);
|
||||
}
|
||||
Reference in new issue
Block a user