2020-10-27 01:01:34 +01:00
import { UIEventSource } from "../../Logic/UIEventSource" ;
import TagRenderingConfig from "../../Customizations/JSON/TagRenderingConfig" ;
import { UIElement } from "../UIElement" ;
2021-01-06 01:11:07 +01:00
import { Utils } from "../../Utils" ;
import Combine from "../Base/Combine" ;
import { TagUtils } from "../../Logic/Tags" ;
2021-02-05 16:32:37 +01:00
import { SubstitutedTranslation } from "../SubstitutedTranslation" ;
2021-03-13 17:25:44 +01:00
import { Translation } from "../i18n/Translation" ;
2020-10-27 01:01:34 +01:00
/ * * *
* Displays the correct value for a known tagrendering
* /
export default class TagRenderingAnswer extends UIElement {
2021-01-04 04:06:21 +01:00
private readonly _tags : UIEventSource < any > ;
2020-10-27 01:01:34 +01:00
private _configuration : TagRenderingConfig ;
2020-11-15 01:16:35 +01:00
private _content : UIElement ;
2021-02-21 01:36:31 +01:00
private readonly _contentClass : string ;
private _contentStyle : string ;
2020-10-27 01:01:34 +01:00
2021-02-21 01:36:31 +01:00
constructor ( tags : UIEventSource < any > , configuration : TagRenderingConfig , contentClasses : string = "" , contentStyle : string = "" ) {
2020-10-27 01:01:34 +01:00
super ( tags ) ;
this . _tags = tags ;
this . _configuration = configuration ;
2021-02-21 01:36:31 +01:00
this . _contentClass = contentClasses ;
this . _contentStyle = contentStyle ;
2021-01-06 01:11:07 +01:00
if ( configuration === undefined ) {
2020-11-17 02:22:48 +01:00
throw "Trying to generate a tagRenderingAnswer without configuration..."
}
2021-01-25 03:12:09 +01:00
this . SetClass ( "flex items-center flex-row text-lg" )
2021-02-21 01:36:31 +01:00
this . SetStyle ( "word-wrap: anywhere;" ) ;
2020-10-27 01:01:34 +01:00
}
InnerRender ( ) : string {
2020-11-02 18:59:21 +01:00
if ( this . _configuration . condition !== undefined ) {
if ( ! this . _configuration . condition . matchesProperties ( this . _tags . data ) ) {
2020-10-28 11:19:47 +01:00
return "" ;
}
}
2020-11-13 23:58:11 +01:00
const tags = this . _tags . data ;
if ( tags === undefined ) {
return "" ;
}
2021-01-06 01:11:07 +01:00
// The render value doesn't work well with multi-answers (checkboxes), so we have to check for them manually
if ( this . _configuration . multiAnswer ) {
2021-03-14 03:14:45 +01:00
let freeformKeyUsed = this . _configuration . freeform ? . key === undefined ; // If it is undefined, it is "used" already, or at least we don't have to check for it anymore
2021-03-13 17:25:44 +01:00
const applicableThens : Translation [ ] = Utils . NoNull ( this . _configuration . mappings . map ( mapping = > {
2021-01-06 01:11:07 +01:00
if ( mapping . if === undefined ) {
return mapping . then ;
}
if ( TagUtils . MatchesMultiAnswer ( mapping . if , tags ) ) {
2021-03-14 03:14:45 +01:00
if ( ! freeformKeyUsed ) {
if ( mapping . if . usedKeys ( ) . indexOf ( this . _configuration . freeform . key ) >= 0 ) {
freeformKeyUsed = true ;
}
}
2021-01-06 01:11:07 +01:00
return mapping . then ;
}
return undefined ;
} ) )
2021-03-13 17:25:44 +01:00
2021-03-14 03:14:45 +01:00
if ( ! freeformKeyUsed
&& tags [ this . _configuration . freeform . key ] !== undefined ) {
2021-03-13 17:25:44 +01:00
applicableThens . push ( this . _configuration . render )
}
const self = this
const valuesToRender : UIElement [ ] = applicableThens . map ( tr = > SubstitutedTranslation . construct ( tr , self . _tags ) )
if ( valuesToRender . length >= 0 ) {
if ( valuesToRender . length === 1 ) {
this . _content = valuesToRender [ 0 ] ;
2021-01-06 01:11:07 +01:00
} else {
this . _content = new Combine ( [ "<ul>" ,
2021-03-13 17:25:44 +01:00
. . . valuesToRender . map ( tr = > new Combine ( [ "<li>" , tr , "</li>" ] ) )
2021-01-06 01:11:07 +01:00
,
"</ul>"
] )
}
2021-02-21 01:36:31 +01:00
return this . _content . SetClass ( this . _contentClass ) . SetStyle ( this . _contentStyle ) . Render ( ) ;
2021-01-06 01:11:07 +01:00
}
2020-10-27 01:01:34 +01:00
}
2021-03-13 17:25:44 +01:00
2021-01-21 23:39:31 +01:00
const tr = this . _configuration . GetRenderValue ( tags ) ;
if ( tr !== undefined ) {
2021-02-05 16:32:37 +01:00
this . _content = SubstitutedTranslation . construct ( tr , this . _tags ) ;
2021-02-21 01:36:31 +01:00
return this . _content . SetClass ( this . _contentClass ) . SetStyle ( this . _contentStyle ) . Render ( ) ;
2021-01-21 23:39:31 +01:00
}
2021-03-13 17:25:44 +01:00
2021-01-06 01:11:07 +01:00
return "" ;
2020-10-27 01:01:34 +01:00
}
}