mapcomplete/Customizations/OnlyShowIf.ts

98 lines
2.6 KiB
TypeScript
Raw Normal View History

2020-07-12 23:19:05 +02:00
import {TagDependantUIElement, TagDependantUIElementConstructor} from "./UIElementConstructor";
import {TagsFilter, TagUtils} from "../Logic/Tags";
2020-07-12 23:19:05 +02:00
import {UIElement} from "../UI/UIElement";
import {UIEventSource} from "../Logic/UIEventSource";
import Translation from "../UI/i18n/Translation";
2020-07-12 23:19:05 +02:00
/**
* Wrapper around another TagDependandElement, which only shows if the filters match
*/
2020-07-12 23:19:05 +02:00
export class OnlyShowIfConstructor implements TagDependantUIElementConstructor{
private readonly _tagsFilter: TagsFilter;
private readonly _embedded: TagDependantUIElementConstructor;
constructor(tagsFilter: TagsFilter, embedded: TagDependantUIElementConstructor) {
2020-07-12 23:19:05 +02:00
this._tagsFilter = tagsFilter;
this._embedded = embedded;
}
2020-10-14 12:15:09 +02:00
construct(tags: UIEventSource<any>): TagDependantUIElement {
return new OnlyShowIf(tags,
this._embedded.construct(tags),
this._tagsFilter);
2020-07-12 23:19:05 +02:00
}
IsKnown(properties: any): boolean {
if(!this.Matches(properties)){
return true;
}
return this._embedded.IsKnown(properties);
}
IsQuestioning(properties: any): boolean {
if(!this.Matches(properties)){
return false;
}
return this._embedded.IsQuestioning(properties);
}
GetContent(tags: any): Translation {
2020-09-09 18:42:13 +02:00
if(!this.IsKnown(tags)){
return undefined;
}
return this._embedded.GetContent(tags);
}
private Matches(properties: any) : boolean{
return this._tagsFilter.matches(TagUtils.proprtiesToKV(properties));
}
2020-07-12 23:19:05 +02:00
}
class OnlyShowIf extends UIElement implements TagDependantUIElement {
private readonly _embedded: TagDependantUIElement;
private readonly _filter: TagsFilter;
2020-07-12 23:19:05 +02:00
constructor(
tags: UIEventSource<any>,
embedded: TagDependantUIElement,
filter: TagsFilter) {
2020-07-12 23:19:05 +02:00
super(tags);
this._filter = filter;
this._embedded = embedded;
}
private Matches() : boolean{
return this._filter.matches(TagUtils.proprtiesToKV(this._source.data));
2020-07-12 23:19:05 +02:00
}
2020-07-25 01:07:02 +02:00
InnerRender(): string {
2020-07-12 23:19:05 +02:00
if (this.Matches()) {
return this._embedded.Render();
} else {
return "";
}
}
IsKnown(): boolean {
if(!this.Matches()){
return false;
}
return this._embedded.IsKnown();
}
2020-07-25 01:07:02 +02:00
IsSkipped(): boolean {
if(!this.Matches()){
return false;
}
return this._embedded.IsSkipped();
}
2020-07-12 23:19:05 +02:00
IsQuestioning(): boolean {
if(!this.Matches()){
return false;
}
return this._embedded.IsQuestioning();
}
}