forked from MapComplete/MapComplete
		
	Add 'catch' to onDestory functie
This commit is contained in:
		
							parent
							
								
									bcfea8b1c1
								
							
						
					
					
						commit
						d60f098898
					
				
					 1 changed files with 18 additions and 13 deletions
				
			
		| 
						 | 
					@ -22,7 +22,7 @@ export class Translation extends BaseUIElement {
 | 
				
			||||||
    constructor(
 | 
					    constructor(
 | 
				
			||||||
        translations: string | Record<string, string>,
 | 
					        translations: string | Record<string, string>,
 | 
				
			||||||
        context?: string,
 | 
					        context?: string,
 | 
				
			||||||
        strictLanguages?: boolean
 | 
					        strictLanguages?: boolean,
 | 
				
			||||||
    ) {
 | 
					    ) {
 | 
				
			||||||
        super()
 | 
					        super()
 | 
				
			||||||
        this._strictLanguages = strictLanguages
 | 
					        this._strictLanguages = strictLanguages
 | 
				
			||||||
| 
						 | 
					@ -62,7 +62,7 @@ export class Translation extends BaseUIElement {
 | 
				
			||||||
                    `. The offending object is: `,
 | 
					                    `. The offending object is: `,
 | 
				
			||||||
                    translations[translationsKey],
 | 
					                    translations[translationsKey],
 | 
				
			||||||
                    "\n    current translations are: ",
 | 
					                    "\n    current translations are: ",
 | 
				
			||||||
                    translations
 | 
					                    translations,
 | 
				
			||||||
                )
 | 
					                )
 | 
				
			||||||
                throw (
 | 
					                throw (
 | 
				
			||||||
                    "Error in an object depicting a translation: a non-string object was found. (" +
 | 
					                    "Error in an object depicting a translation: a non-string object was found. (" +
 | 
				
			||||||
| 
						 | 
					@ -75,7 +75,7 @@ export class Translation extends BaseUIElement {
 | 
				
			||||||
        if (count === 0) {
 | 
					        if (count === 0) {
 | 
				
			||||||
            console.error(
 | 
					            console.error(
 | 
				
			||||||
                "Constructing a translation, but the object containing translations is empty " +
 | 
					                "Constructing a translation, but the object containing translations is empty " +
 | 
				
			||||||
                    (context ?? "No context given")
 | 
					                (context ?? "No context given"),
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					@ -94,7 +94,7 @@ export class Translation extends BaseUIElement {
 | 
				
			||||||
                [],
 | 
					                [],
 | 
				
			||||||
                (f) => {
 | 
					                (f) => {
 | 
				
			||||||
                    this.onDestroy = f
 | 
					                    this.onDestroy = f
 | 
				
			||||||
                }
 | 
					                },
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return this._currentLanguage
 | 
					        return this._currentLanguage
 | 
				
			||||||
| 
						 | 
					@ -113,7 +113,7 @@ export class Translation extends BaseUIElement {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    static ExtractAllTranslationsFrom(
 | 
					    static ExtractAllTranslationsFrom(
 | 
				
			||||||
        object: any,
 | 
					        object: any,
 | 
				
			||||||
        context = ""
 | 
					        context = "",
 | 
				
			||||||
    ): { context: string; tr: Translation }[] {
 | 
					    ): { context: string; tr: Translation }[] {
 | 
				
			||||||
        const allTranslations: { context: string; tr: Translation }[] = []
 | 
					        const allTranslations: { context: string; tr: Translation }[] = []
 | 
				
			||||||
        for (const key in object) {
 | 
					        for (const key in object) {
 | 
				
			||||||
| 
						 | 
					@ -127,7 +127,7 @@ export class Translation extends BaseUIElement {
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            if (typeof v === "object") {
 | 
					            if (typeof v === "object") {
 | 
				
			||||||
                allTranslations.push(
 | 
					                allTranslations.push(
 | 
				
			||||||
                    ...Translation.ExtractAllTranslationsFrom(v, context + "." + key)
 | 
					                    ...Translation.ExtractAllTranslationsFrom(v, context + "." + key),
 | 
				
			||||||
                )
 | 
					                )
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					@ -153,7 +153,11 @@ export class Translation extends BaseUIElement {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Destroy() {
 | 
					    Destroy() {
 | 
				
			||||||
        super.Destroy()
 | 
					        super.Destroy()
 | 
				
			||||||
        this.onDestroy()
 | 
					        try {
 | 
				
			||||||
 | 
					            this.onDestroy()
 | 
				
			||||||
 | 
					        } catch (e) {
 | 
				
			||||||
 | 
					            console.error("Could not call this.onDestroy", e)
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        this.isDestroyed = true
 | 
					        this.isDestroyed = true
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -180,6 +184,7 @@ export class Translation extends BaseUIElement {
 | 
				
			||||||
        console.error("Missing language ", Locale.language.data, "for", this.translations)
 | 
					        console.error("Missing language ", Locale.language.data, "for", this.translations)
 | 
				
			||||||
        return undefined
 | 
					        return undefined
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public textFor(language: string): string | undefined {
 | 
					    public textFor(language: string): string | undefined {
 | 
				
			||||||
        return this.translations[this.actualLanguage(language)]
 | 
					        return this.translations[this.actualLanguage(language)]
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					@ -249,7 +254,7 @@ export class Translation extends BaseUIElement {
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public OnEveryLanguage(
 | 
					    public OnEveryLanguage(
 | 
				
			||||||
        f: (s: string, language: string) => string,
 | 
					        f: (s: string, language: string) => string,
 | 
				
			||||||
        context?: string
 | 
					        context?: string,
 | 
				
			||||||
    ): Translation {
 | 
					    ): Translation {
 | 
				
			||||||
        const newTranslations = {}
 | 
					        const newTranslations = {}
 | 
				
			||||||
        for (const lang in this.translations) {
 | 
					        for (const lang in this.translations) {
 | 
				
			||||||
| 
						 | 
					@ -332,7 +337,7 @@ export class Translation extends BaseUIElement {
 | 
				
			||||||
                const htmlElement = document.createElement("div")
 | 
					                const htmlElement = document.createElement("div")
 | 
				
			||||||
                htmlElement.innerHTML = render
 | 
					                htmlElement.innerHTML = render
 | 
				
			||||||
                const images = Array.from(htmlElement.getElementsByTagName("img")).map(
 | 
					                const images = Array.from(htmlElement.getElementsByTagName("img")).map(
 | 
				
			||||||
                    (img) => img.src
 | 
					                    (img) => img.src,
 | 
				
			||||||
                )
 | 
					                )
 | 
				
			||||||
                allIcons.push(...images)
 | 
					                allIcons.push(...images)
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
| 
						 | 
					@ -345,7 +350,7 @@ export class Translation extends BaseUIElement {
 | 
				
			||||||
                            .map((img) => img.match(/src=("[^"]+"|'[^']+'|[^/ ]+)/))
 | 
					                            .map((img) => img.match(/src=("[^"]+"|'[^']+'|[^/ ]+)/))
 | 
				
			||||||
                            .filter((match) => match != null)
 | 
					                            .filter((match) => match != null)
 | 
				
			||||||
                            .map((match) =>
 | 
					                            .map((match) =>
 | 
				
			||||||
                                match[1].trim().replace(/^['"]/, "").replace(/['"]$/, "")
 | 
					                                match[1].trim().replace(/^['"]/, "").replace(/['"]$/, ""),
 | 
				
			||||||
                            )
 | 
					                            )
 | 
				
			||||||
                        allIcons.push(...sources)
 | 
					                        allIcons.push(...sources)
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
| 
						 | 
					@ -393,7 +398,7 @@ export class TypedTranslation<T extends Record<string, any>> extends Translation
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    PartialSubs<X extends string>(
 | 
					    PartialSubs<X extends string>(
 | 
				
			||||||
        text: Partial<T> & Record<X, string>
 | 
					        text: Partial<T> & Record<X, string>,
 | 
				
			||||||
    ): TypedTranslation<Omit<T, X>> {
 | 
					    ): TypedTranslation<Omit<T, X>> {
 | 
				
			||||||
        const newTranslations: Record<string, string> = {}
 | 
					        const newTranslations: Record<string, string> = {}
 | 
				
			||||||
        for (const lang in this.translations) {
 | 
					        for (const lang in this.translations) {
 | 
				
			||||||
| 
						 | 
					@ -410,7 +415,7 @@ export class TypedTranslation<T extends Record<string, any>> extends Translation
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    PartialSubsTr<K extends string>(
 | 
					    PartialSubsTr<K extends string>(
 | 
				
			||||||
        key: string,
 | 
					        key: string,
 | 
				
			||||||
        replaceWith: Translation
 | 
					        replaceWith: Translation,
 | 
				
			||||||
    ): TypedTranslation<Omit<T, K>> {
 | 
					    ): TypedTranslation<Omit<T, K>> {
 | 
				
			||||||
        const newTranslations: Record<string, string> = {}
 | 
					        const newTranslations: Record<string, string> = {}
 | 
				
			||||||
        const toSearch = "{" + key + "}"
 | 
					        const toSearch = "{" + key + "}"
 | 
				
			||||||
| 
						 | 
					@ -429,7 +434,7 @@ export class TypedTranslation<T extends Record<string, any>> extends Translation
 | 
				
			||||||
        for (const missingLanguage of missingLanguages) {
 | 
					        for (const missingLanguage of missingLanguages) {
 | 
				
			||||||
            newTranslations[missingLanguage] = baseTemplate.replaceAll(
 | 
					            newTranslations[missingLanguage] = baseTemplate.replaceAll(
 | 
				
			||||||
                toSearch,
 | 
					                toSearch,
 | 
				
			||||||
                replaceWith.textFor(missingLanguage)
 | 
					                replaceWith.textFor(missingLanguage),
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue