2022-08-20 12:46:33 +02:00
import { UIEventSource } from "../Logic/UIEventSource"
2021-02-05 16:32:37 +01:00
import { Translation } from "./i18n/Translation"
import Locale from "./i18n/Locale"
import { FixedUiElement } from "./Base/FixedUiElement"
2022-10-28 04:33:05 +02:00
import SpecialVisualizations 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-11-03 00:44:53 +01:00
import { DefaultGuiState } from "./DefaultGuiState"
2022-01-12 02:31:51 +01:00
import FeaturePipelineState from "../Logic/State/FeaturePipelineState"
2022-04-01 12:51:55 +02:00
import LinkToWeblate from "./Base/LinkToWeblate"
2022-11-02 13:47:34 +01:00
import { SpecialVisualization } from "./SpecialVisualization"
2021-02-05 16:32:37 +01:00
2021-06-14 02:39:23 +02:00
export class SubstitutedTranslation extends VariableUiElement {
public constructor (
2021-02-05 16:32:37 +01:00
translation : Translation ,
2022-08-20 12:46:33 +02:00
tagsSource : UIEventSource < Record < string , string > > ,
2022-01-12 02:31:51 +01:00
state : FeaturePipelineState ,
2022-05-27 05:49:21 +02:00
mapping : Map <
string ,
| BaseUIElement
2022-08-20 12:46:33 +02:00
| ( (
state : FeaturePipelineState ,
tagSource : UIEventSource < Record < string , string > > ,
argument : string [ ] ,
guistate : DefaultGuiState
) = > BaseUIElement )
> = undefined
) {
2021-07-11 15:44:17 +02:00
const extraMappings : SpecialVisualization [ ] = [ ]
mapping ? . forEach ( ( value , key ) = > {
extraMappings . push ( {
funcName : key ,
2022-05-27 05:49:21 +02:00
constr : typeof value === "function" ? value : ( ) = > value ,
2021-07-11 15:44:17 +02:00
docs : "Dynamically injected input element" ,
args : [ ] ,
example : "" ,
} )
} )
2022-04-06 19:16:55 +02:00
const linkToWeblate =
translation !== undefined
? new LinkToWeblate ( translation . context , translation . translations )
: undefined
2022-09-08 21:40:48 +02:00
2021-06-14 02:39:23 +02:00
super (
2021-06-27 19:21:31 +02:00
Locale . language . map ( ( language ) = > {
2022-02-01 04:14:54 +01: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 } ()} ` )
} )
2022-04-01 12:51:55 +02:00
const allElements = SubstitutedTranslation . ExtractSpecialComponents (
txt ,
extraMappings
2021-06-27 19:21:31 +02:00
) . map ( ( proto ) = > {
if ( proto . fixed !== undefined ) {
2022-07-08 03:14:55 +02:00
if ( tagsSource === undefined ) {
return Utils . SubstituteKeys ( proto . fixed , undefined )
2021-06-27 19:21:31 +02:00
}
return new VariableUiElement (
tagsSource . map ( ( tags ) = > Utils . SubstituteKeys ( proto . fixed , tags ) )
2022-09-08 21:40:48 +02:00
)
}
2021-06-27 19:21:31 +02:00
const viz = proto . special
2022-07-27 23:59:04 +02:00
if ( viz === undefined ) {
console . error (
"SPECIALRENDERING UNDEFINED for" ,
tagsSource . data ? . id ,
"THIS IS REALLY WEIRD"
)
return undefined
}
2021-06-27 19:21:31 +02:00
try {
2022-04-28 00:32:15 +02:00
return viz . func
. constr ( state , tagsSource , proto . special . args , DefaultGuiState . state )
? . SetStyle ( proto . special . style )
2021-06-27 19:21:31 +02:00
} catch ( e ) {
console . error ( "SPECIALRENDERING FAILED for" , tagsSource . data ? . id , e )
2022-01-01 01:59:50 +01:00
return new FixedUiElement (
` Could not generate special rendering for ${
viz . func . funcName
} ( $ { viz . args . join ( ", " ) } ) $ { e } `
) . SetStyle ( "alert" )
2021-06-27 19:21:31 +02:00
}
2022-04-01 12:51:55 +02:00
} )
allElements . push ( linkToWeblate )
2022-09-08 21:40:48 +02:00
2022-04-01 12:51:55 +02:00
return new Combine ( allElements )
2021-06-27 19:21:31 +02:00
} )
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
}
2022-07-27 23:59:04 +02:00
/ * *
*
* // Return empty list on empty input
2022-07-28 10:00:11 +02:00
* SubstitutedTranslation . ExtractSpecialComponents ( "" ) // => []
2022-07-27 23:59:04 +02:00
*
* // Advanced cases with commas, braces and newlines should be handled without problem
* const templates = SubstitutedTranslation . ExtractSpecialComponents ( "{send_email(&LBRACEemail&RBRACE,Broken bicycle pump,Hello&COMMA\n\nWith this email&COMMA I'd like to inform you that the bicycle pump located at https://mapcomplete.osm.be/cyclofix?lat=&LBRACE_lat&RBRACE&lon=&LBRACE_lon&RBRACE&z=18#&LBRACEid&RBRACE is broken.\n\n Kind regards,Report this bicycle pump as broken)}" )
* const templ = templates [ 0 ]
* templ . special . func . funcName // => "send_email"
* templ . special . args [ 0 ] = "{email}"
* /
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
}
} [ ] {
2022-07-27 23:59:04 +02:00
if ( template === "" ) {
return [ ]
}
2022-01-26 21:40:38 +01:00
2022-04-28 00:32:15 +02:00
for ( const knownSpecial of extraMappings . concat (
SpecialVisualizations . specialVisualizations
) ) {
2021-02-05 16:32:37 +01:00
// Note: the '.*?' in the regex reads as 'any character, but in a non-greedy way'
2022-07-27 23:59:04 +02:00
const matched = template . match (
new RegExp ( ` (.*){ ${ knownSpecial . funcName } \\ ((.*?) \\ )(:.*)?}(.*) ` , "s" )
2022-09-08 21:40:48 +02:00
)
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
2022-09-08 21:40:48 +02:00
)
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
2022-09-08 21:40:48 +02:00
)
2021-06-27 19:21:31 +02:00
const args = knownSpecial . args . map ( ( arg ) = > arg . defaultValue ? ? "" )
if ( argument . length > 0 ) {
2021-10-29 03:42:33 +02:00
const realArgs = argument . split ( "," ) . map ( ( str ) = >
str
. trim ( )
. replace ( /&LPARENS/g , "(" )
2022-07-27 23:59:04 +02:00
. replace ( /&RPARENS/g , ")" )
. replace ( /&LBRACE/g , "{" )
. replace ( /&RBRACE/g , "}" )
. replace ( /&COMMA/g , "," )
2022-09-08 21:40:48 +02:00
)
2021-06-27 19:21:31 +02:00
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 ( ", " )
2022-09-08 21:40:48 +02:00
)
2021-04-23 17:22:01 +02:00
}
2022-09-08 21:40:48 +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
}
}