| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  | import {Translation} from "../UI/i18n/Translation"; | 
					
						
							| 
									
										
										
										
											2022-08-18 19:17:15 +02:00
										 |  |  | import {DenominationConfigJson} from "./ThemeConfig/Json/UnitConfigJson"; | 
					
						
							| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  | import Translations from "../UI/i18n/Translations"; | 
					
						
							| 
									
										
										
										
											2022-08-18 19:17:15 +02:00
										 |  |  | import {Store} from "../Logic/UIEventSource"; | 
					
						
							| 
									
										
										
										
											2021-09-13 02:38:20 +02:00
										 |  |  | import BaseUIElement from "../UI/BaseUIElement"; | 
					
						
							|  |  |  | import Toggle from "../UI/Input/Toggle"; | 
					
						
							| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export class Denomination { | 
					
						
							|  |  |  |     public readonly canonical: string; | 
					
						
							| 
									
										
										
										
											2021-09-13 02:38:20 +02:00
										 |  |  |     public readonly _canonicalSingular: string; | 
					
						
							| 
									
										
										
										
											2022-08-18 19:17:15 +02:00
										 |  |  |     public readonly useAsDefaultInput: boolean | string[] | 
					
						
							|  |  |  |     public readonly useIfNoUnitGiven : boolean | string[] | 
					
						
							| 
									
										
										
										
											2021-09-13 02:38:20 +02:00
										 |  |  |     public readonly prefix: boolean; | 
					
						
							| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  |     public readonly alternativeDenominations: string []; | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |     private readonly _human: Translation; | 
					
						
							| 
									
										
										
										
											2021-09-13 02:38:20 +02:00
										 |  |  |     private readonly _humanSingular?: Translation; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-18 19:17:15 +02:00
										 |  |  |     constructor(json: DenominationConfigJson, context: string) { | 
					
						
							| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  |         context = `${context}.unit(${json.canonicalDenomination})` | 
					
						
							|  |  |  |         this.canonical = json.canonicalDenomination.trim() | 
					
						
							|  |  |  |         if (this.canonical === undefined) { | 
					
						
							|  |  |  |             throw `${context}: this unit has no decent canonical value defined` | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-13 02:38:20 +02:00
										 |  |  |         this._canonicalSingular = json.canonicalDenominationSingular?.trim() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         json.alternativeDenomination.forEach((v, i) => { | 
					
						
							|  |  |  |             if (((v?.trim() ?? "") === "")) { | 
					
						
							|  |  |  |                 throw `${context}.alternativeDenomination.${i}: invalid alternative denomination: undefined, null or only whitespace` | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.alternativeDenominations = json.alternativeDenomination?.map(v => v.trim()) ?? [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-18 19:17:15 +02:00
										 |  |  |         if(json["default"] !== undefined) { | 
					
						
							|  |  |  |             throw `${context} uses the old 'default'-key. Use "useIfNoUnitGiven" or "useAsDefaultInput" instead` | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         this.useIfNoUnitGiven = json.useIfNoUnitGiven | 
					
						
							|  |  |  |         this.useAsDefaultInput = json.useAsDefaultInput ?? json.useIfNoUnitGiven | 
					
						
							|  |  |  |          | 
					
						
							| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  |         this._human = Translations.T(json.human, context + "human") | 
					
						
							| 
									
										
										
										
											2021-09-13 02:38:20 +02:00
										 |  |  |         this._humanSingular = Translations.T(json.humanSingular, context + "humanSingular") | 
					
						
							| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         this.prefix = json.prefix ?? false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     get human(): Translation { | 
					
						
							|  |  |  |         return this._human.Clone() | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-13 02:38:20 +02:00
										 |  |  |     get humanSingular(): Translation { | 
					
						
							|  |  |  |         return (this._humanSingular ?? this._human).Clone() | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-05 02:24:14 +02:00
										 |  |  |     getToggledHuman(isSingular: Store<boolean>): BaseUIElement { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         if (this._humanSingular === undefined) { | 
					
						
							| 
									
										
										
										
											2021-09-13 02:38:20 +02:00
										 |  |  |             return this.human | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return new Toggle( | 
					
						
							|  |  |  |             this.humanSingular, | 
					
						
							|  |  |  |             this.human, | 
					
						
							|  |  |  |             isSingular | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 01:53:08 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Create a representation of the given value | 
					
						
							|  |  |  |      * @param value: the value from OSM | 
					
						
							|  |  |  |      * @param actAsDefault: if set and the value can be parsed as number, will be parsed and trimmed | 
					
						
							|  |  |  |      *  | 
					
						
							|  |  |  |      * const unit = new Denomination({ | 
					
						
							|  |  |  |      *               canonicalDenomination: "m", | 
					
						
							|  |  |  |      *               alternativeDenomination: ["meter"], | 
					
						
							|  |  |  |      *               human: { | 
					
						
							|  |  |  |      *                   en: "meter" | 
					
						
							|  |  |  |      *               } | 
					
						
							|  |  |  |      *           }, "test") | 
					
						
							| 
									
										
										
										
											2022-08-18 19:17:15 +02:00
										 |  |  |      * unit.canonicalValue("42m", true) // =>"42 m"
 | 
					
						
							|  |  |  |      * unit.canonicalValue("42", true) // =>"42 m"
 | 
					
						
							|  |  |  |      * unit.canonicalValue("42 m", true) // =>"42 m"
 | 
					
						
							|  |  |  |      * unit.canonicalValue("42 meter", true) // =>"42 m"
 | 
					
						
							|  |  |  |      * unit.canonicalValue("42m", true) // =>"42 m"
 | 
					
						
							|  |  |  |      * unit.canonicalValue("42", true) // =>"42 m"
 | 
					
						
							| 
									
										
										
										
											2022-03-15 01:53:08 +01:00
										 |  |  |      *  | 
					
						
							| 
									
										
										
										
											2022-04-06 19:16:55 +02:00
										 |  |  |      * // Should be trimmed if canonical is empty
 | 
					
						
							|  |  |  |      * const unit = new Denomination({ | 
					
						
							|  |  |  |      *               canonicalDenomination: "", | 
					
						
							| 
									
										
										
										
											2022-04-07 02:55:24 +02:00
										 |  |  |      *               alternativeDenomination: ["meter","m"], | 
					
						
							| 
									
										
										
										
											2022-04-06 19:16:55 +02:00
										 |  |  |      *               human: { | 
					
						
							|  |  |  |      *                   en: "meter" | 
					
						
							|  |  |  |      *               } | 
					
						
							|  |  |  |      *           }, "test") | 
					
						
							| 
									
										
										
										
											2022-08-18 19:17:15 +02:00
										 |  |  |      * unit.canonicalValue("42m", true) // =>"42"
 | 
					
						
							|  |  |  |      * unit.canonicalValue("42", true) // =>"42"
 | 
					
						
							|  |  |  |      * unit.canonicalValue("42 m", true) // =>"42"
 | 
					
						
							|  |  |  |      * unit.canonicalValue("42 meter", true) // =>"42"
 | 
					
						
							| 
									
										
										
										
											2022-03-15 01:53:08 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-08-18 19:17:15 +02:00
										 |  |  |     public canonicalValue(value: string, actAsDefault: boolean) : string { | 
					
						
							| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  |         if (value === undefined) { | 
					
						
							|  |  |  |             return undefined; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         const stripped = this.StrippedValue(value, actAsDefault) | 
					
						
							|  |  |  |         if (stripped === null) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         if (stripped === "1" && this._canonicalSingular !== undefined) { | 
					
						
							| 
									
										
										
										
											2022-04-06 19:16:55 +02:00
										 |  |  |             return ("1 " + this._canonicalSingular).trim() | 
					
						
							| 
									
										
										
										
											2021-09-13 02:38:20 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-04-06 19:16:55 +02:00
										 |  |  |         return (stripped + " " + this.canonical).trim(); | 
					
						
							| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Returns the core value (without unit) if: | 
					
						
							|  |  |  |      * - the value ends with the canonical or an alternative value (or begins with if prefix is set) | 
					
						
							|  |  |  |      * - the value is a Number (without unit) and default is set | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Returns null if it doesn't match this unit | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-08-18 19:17:15 +02:00
										 |  |  |     public StrippedValue(value: string, actAsDefault: boolean): string { | 
					
						
							| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (value === undefined) { | 
					
						
							|  |  |  |             return undefined; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         value = value.toLowerCase() | 
					
						
							| 
									
										
										
										
											2021-09-13 02:38:20 +02:00
										 |  |  |         const self = this; | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         function startsWith(key) { | 
					
						
							|  |  |  |             if (self.prefix) { | 
					
						
							| 
									
										
										
										
											2021-09-13 02:38:20 +02:00
										 |  |  |                 return value.startsWith(key) | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2021-09-13 02:38:20 +02:00
										 |  |  |                 return value.endsWith(key) | 
					
						
							| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-09-13 02:38:20 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         function substr(key) { | 
					
						
							|  |  |  |             if (self.prefix) { | 
					
						
							| 
									
										
										
										
											2021-09-13 02:38:20 +02:00
										 |  |  |                 return value.substr(key.length).trim() | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2021-09-13 02:38:20 +02:00
										 |  |  |                 return value.substring(0, value.length - key.length).trim() | 
					
						
							| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-09-13 02:38:20 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (this.canonical !== "" && startsWith(this.canonical.toLowerCase())) { | 
					
						
							| 
									
										
										
										
											2021-09-13 02:38:20 +02:00
										 |  |  |             return substr(this.canonical) | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (this._canonicalSingular !== undefined && this._canonicalSingular !== "" && startsWith(this._canonicalSingular)) { | 
					
						
							| 
									
										
										
										
											2021-09-13 02:38:20 +02:00
										 |  |  |             return substr(this._canonicalSingular) | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-13 02:38:20 +02:00
										 |  |  |         for (const alternativeValue of this.alternativeDenominations) { | 
					
						
							|  |  |  |             if (startsWith(alternativeValue)) { | 
					
						
							|  |  |  |                 return substr(alternativeValue); | 
					
						
							| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-18 19:17:15 +02:00
										 |  |  |         if (!actAsDefault) { | 
					
						
							|  |  |  |             return null | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         const parsed = Number(value.trim()) | 
					
						
							|  |  |  |         if (!isNaN(parsed)) { | 
					
						
							|  |  |  |             return value.trim(); | 
					
						
							| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-18 19:17:15 +02:00
										 |  |  |     isDefaultUnit(country: () => string) { | 
					
						
							|  |  |  |         if(this.useIfNoUnitGiven === true){ | 
					
						
							|  |  |  |             return true | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if(this.useIfNoUnitGiven === false){ | 
					
						
							|  |  |  |             return false | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return this.useIfNoUnitGiven.indexOf(country()) >= 0 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  | } |