2021-02-05 16:32:37 +01:00
import { UIEventSource } from "../Logic/UIEventSource" ;
import { Translation } from "./i18n/Translation" ;
import Locale from "./i18n/Locale" ;
import State from "../State" ;
import { FixedUiElement } from "./Base/FixedUiElement" ;
2021-06-27 19:21:31 +02:00
import SpecialVisualizations , { SpecialVisualization } from "./SpecialVisualizations" ;
2021-06-14 02:39:23 +02:00
import { Utils } from "../Utils" ;
import { VariableUiElement } from "./Base/VariableUIElement" ;
import Combine from "./Base/Combine" ;
2021-07-11 15:44:17 +02:00
import BaseUIElement from "./BaseUIElement" ;
2021-02-05 16:32:37 +01:00
2021-06-14 02:39:23 +02:00
export class SubstitutedTranslation extends VariableUiElement {
2021-02-05 16:32:37 +01:00
2021-06-14 02:39:23 +02:00
public constructor (
2021-02-05 16:32:37 +01:00
translation : Translation ,
2021-07-11 15:44:17 +02:00
tagsSource : UIEventSource < any > ,
mapping : Map < string , BaseUIElement > = undefined ) {
const extraMappings : SpecialVisualization [ ] = [ ] ;
mapping ? . forEach ( ( value , key ) = > {
console . log ( "KV:" , key , value )
extraMappings . push (
{
funcName : key ,
constr : ( ( ) = > {
return value
} ) ,
docs : "Dynamically injected input element" ,
args : [ ] ,
example : ""
}
)
} )
2021-06-14 02:39:23 +02:00
super (
2021-06-27 19:21:31 +02:00
Locale . language . map ( language = > {
2021-07-11 15:44:17 +02:00
let txt = translation . textFor ( language ) ;
2021-06-23 02:15:28 +02:00
if ( txt === undefined ) {
2021-06-15 01:24:04 +02:00
return undefined
2021-06-14 02:39:23 +02:00
}
2021-07-11 15:44:17 +02:00
mapping ? . forEach ( ( _ , key ) = > {
txt = txt . replace ( new RegExp ( ` { ${ key } } ` , "g" ) , ` { ${ key } ()} ` )
} )
return new Combine ( SubstitutedTranslation . ExtractSpecialComponents ( txt , extraMappings ) . map (
2021-06-27 19:21:31 +02:00
proto = > {
if ( proto . fixed !== undefined ) {
return new VariableUiElement ( tagsSource . map ( tags = > Utils . SubstituteKeys ( proto . fixed , tags ) ) ) ;
}
const viz = proto . special ;
try {
return viz . func . constr ( State . state , tagsSource , proto . special . args ) . SetStyle ( proto . special . style ) ;
} catch ( e ) {
console . error ( "SPECIALRENDERING FAILED for" , tagsSource . data ? . id , e )
return new FixedUiElement ( ` Could not generate special rendering for ${ viz . func } ( ${ viz . args . join ( ", " ) } ) ${ e } ` ) . SetStyle ( "alert" )
}
}
) )
} )
2021-06-14 02:39:23 +02:00
)
2021-06-23 02:15:28 +02:00
2021-02-06 00:05:38 +01:00
this . SetClass ( "w-full" )
2021-02-05 16:32:37 +01:00
}
2021-07-11 15:44:17 +02:00
public static ExtractSpecialComponents ( template : string , extraMappings : SpecialVisualization [ ] = [ ] ) : {
fixed? : string ,
special ? : {
2021-06-27 19:21:31 +02:00
func : SpecialVisualization ,
args : string [ ] ,
style : string
}
} [ ] {
2021-02-05 16:32:37 +01:00
2021-07-11 15:44:17 +02:00
if ( extraMappings . length > 0 ) {
console . log ( "Extra mappings are" , extraMappings )
}
for ( const knownSpecial of SpecialVisualizations . specialVisualizations . concat ( extraMappings ) ) {
2021-02-05 16:32:37 +01:00
// Note: the '.*?' in the regex reads as 'any character, but in a non-greedy way'
2021-06-23 02:15:28 +02:00
const matched = template . match ( ` (.*){ ${ knownSpecial . funcName } \\ ((.*?) \\ )(:.*)?}(.*) ` ) ;
2021-02-05 16:32:37 +01:00
if ( matched != null ) {
// We found a special component that should be brought to live
2021-07-11 15:44:17 +02:00
const partBefore = SubstitutedTranslation . ExtractSpecialComponents ( matched [ 1 ] , extraMappings ) ;
2021-02-05 16:32:37 +01:00
const argument = matched [ 2 ] . trim ( ) ;
2021-06-23 02:41:30 +02:00
const style = matched [ 3 ] ? . substring ( 1 ) ? ? ""
2021-07-11 15:44:17 +02:00
const partAfter = SubstitutedTranslation . ExtractSpecialComponents ( matched [ 4 ] , extraMappings ) ;
2021-06-27 19:21:31 +02:00
const args = knownSpecial . args . map ( arg = > arg . defaultValue ? ? "" ) ;
if ( argument . length > 0 ) {
const realArgs = argument . split ( "," ) . map ( str = > str . trim ( ) ) ;
for ( let i = 0 ; i < realArgs . length ; i ++ ) {
if ( args . length <= i ) {
args . push ( realArgs [ i ] ) ;
} else {
args [ i ] = realArgs [ i ] ;
2021-02-05 16:32:37 +01:00
}
}
}
2021-06-27 19:21:31 +02:00
let element ;
2021-07-11 15:44:17 +02:00
element = {
special : {
args : args ,
style : style ,
func : knownSpecial
}
}
2021-06-27 19:21:31 +02:00
return [ . . . partBefore , element , . . . partAfter ]
2021-02-05 16:32:37 +01:00
}
}
2021-04-23 17:22:01 +02:00
// Let's to a small sanity check to help the theme designers:
2021-06-10 01:36:20 +02:00
if ( template . search ( /{[^}]+\([^}]*\)}/ ) >= 0 ) {
2021-04-23 17:22:01 +02:00
// Hmm, we might have found an invalid rendering name
2021-06-10 01:36:20 +02:00
console . warn ( "Found a suspicious special rendering value in: " , template , " did you mean one of: " , SpecialVisualizations . specialVisualizations . map ( sp = > sp . funcName + "()" ) . join ( ", " ) )
2021-04-23 17:22:01 +02:00
}
2021-06-10 01:36:20 +02:00
2021-04-06 21:10:18 +02:00
// IF we end up here, no changes have to be made - except to remove any resting {}
2021-06-27 19:21:31 +02:00
return [ { fixed : template } ] ;
2021-02-05 16:32:37 +01:00
}
}