2021-07-27 17:00:05 +02:00
|
|
|
import {Utils} from "../../Utils";
|
|
|
|
import {FixedInputElement} from "../Input/FixedInputElement";
|
|
|
|
import {RadioButton} from "../Input/RadioButton";
|
|
|
|
import {VariableUiElement} from "../Base/VariableUIElement";
|
2021-07-20 15:14:51 +02:00
|
|
|
import Toggle from "../Input/Toggle";
|
|
|
|
import Combine from "../Base/Combine";
|
|
|
|
import Translations from "../i18n/Translations";
|
2021-07-27 17:00:05 +02:00
|
|
|
import {Translation} from "../i18n/Translation";
|
2021-07-20 16:29:48 +02:00
|
|
|
import Svg from "../../Svg";
|
2021-07-27 17:00:05 +02:00
|
|
|
import {UIEventSource} from "../../Logic/UIEventSource";
|
|
|
|
import BaseUIElement from "../BaseUIElement";
|
2021-07-27 19:39:57 +02:00
|
|
|
import State from "../../State";
|
2021-07-27 20:29:34 +02:00
|
|
|
import FilteredLayer from "../../Models/FilteredLayer";
|
2021-07-28 16:48:59 +02:00
|
|
|
import BackgroundSelector from "./BackgroundSelector";
|
2021-08-07 23:11:34 +02:00
|
|
|
import FilterConfig from "../../Models/ThemeConfig/FilterConfig";
|
2021-10-14 21:43:14 +02:00
|
|
|
import TilesourceConfig from "../../Models/ThemeConfig/TilesourceConfig";
|
2021-07-27 19:39:57 +02:00
|
|
|
|
2021-07-26 12:26:41 +02:00
|
|
|
export default class FilterView extends VariableUiElement {
|
2021-10-14 21:43:14 +02:00
|
|
|
constructor(filteredLayer: UIEventSource<FilteredLayer[]>, tileLayers: { config: TilesourceConfig, isDisplayed: UIEventSource<boolean> }[]) {
|
2021-09-07 01:49:18 +02:00
|
|
|
const backgroundSelector = new Toggle(
|
2021-07-28 16:48:59 +02:00
|
|
|
new BackgroundSelector(),
|
|
|
|
undefined,
|
2021-12-04 12:20:24 +01:00
|
|
|
State.state.featureSwitchBackgroundSelection
|
2021-07-28 16:48:59 +02:00
|
|
|
)
|
2021-07-27 17:00:05 +02:00
|
|
|
super(
|
2021-10-14 21:43:14 +02:00
|
|
|
filteredLayer.map((filteredLayers) => {
|
|
|
|
let elements = filteredLayers?.map(l => FilterView.createOneFilteredLayerElement(l))
|
|
|
|
elements = elements.concat(tileLayers.map(tl => FilterView.createOverlayToggle(tl)))
|
|
|
|
return elements.concat(backgroundSelector);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static createOverlayToggle(config: { config: TilesourceConfig, isDisplayed: UIEventSource<boolean> }) {
|
|
|
|
|
|
|
|
const iconStyle = "width:1.5rem;height:1.5rem;margin-left:1.25rem;flex-shrink: 0;";
|
|
|
|
|
|
|
|
const icon = new Combine([Svg.checkbox_filled]).SetStyle(iconStyle);
|
|
|
|
const iconUnselected = new Combine([Svg.checkbox_empty]).SetStyle(
|
|
|
|
iconStyle
|
|
|
|
);
|
2021-10-25 21:50:38 +02:00
|
|
|
const name: Translation = config.config.name;
|
2021-10-14 21:43:14 +02:00
|
|
|
|
2021-10-30 02:34:16 +02:00
|
|
|
const styledNameChecked = name.Clone().SetStyle("font-size:large").SetClass("ml-2");
|
|
|
|
const styledNameUnChecked = name.Clone().SetStyle("font-size:large").SetClass("ml-2");
|
2021-10-14 21:43:14 +02:00
|
|
|
|
|
|
|
const zoomStatus =
|
|
|
|
new Toggle(
|
|
|
|
undefined,
|
2021-10-25 21:50:38 +02:00
|
|
|
Translations.t.general.layerSelection.zoomInToSeeThisLayer
|
2021-10-14 21:43:14 +02:00
|
|
|
.SetClass("alert")
|
|
|
|
.SetStyle("display: block ruby;width:min-content;"),
|
|
|
|
State.state.locationControl.map(location => location.zoom >= config.config.minzoom)
|
2021-07-27 17:00:05 +02:00
|
|
|
)
|
2021-10-14 21:43:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
const style =
|
|
|
|
"display:flex;align-items:center;padding:0.5rem 0;";
|
|
|
|
const layerChecked = new Combine([icon, styledNameChecked, zoomStatus])
|
|
|
|
.SetStyle(style)
|
|
|
|
.onClick(() => config.isDisplayed.setData(false));
|
|
|
|
|
|
|
|
const layerNotChecked = new Combine([iconUnselected, styledNameUnChecked])
|
|
|
|
.SetStyle(style)
|
|
|
|
.onClick(() => config.isDisplayed.setData(true));
|
|
|
|
|
|
|
|
|
|
|
|
return new Toggle(
|
|
|
|
layerChecked,
|
|
|
|
layerNotChecked,
|
|
|
|
config.isDisplayed
|
2021-07-27 17:00:05 +02:00
|
|
|
);
|
2021-07-26 12:26:41 +02:00
|
|
|
}
|
2021-07-20 16:29:48 +02:00
|
|
|
|
2021-09-07 01:49:18 +02:00
|
|
|
private static createOneFilteredLayerElement(filteredLayer: FilteredLayer) {
|
|
|
|
if (filteredLayer.layerDef.name === undefined) {
|
2021-07-27 19:39:57 +02:00
|
|
|
// Name is not defined: we hide this one
|
|
|
|
return undefined;
|
|
|
|
}
|
2021-10-04 19:47:23 +02:00
|
|
|
const iconStyle = "width:1.5rem;height:1.5rem;margin-left:1.25rem;flex-shrink: 0;";
|
2021-07-27 17:00:05 +02:00
|
|
|
|
|
|
|
const icon = new Combine([Svg.checkbox_filled]).SetStyle(iconStyle);
|
2021-10-30 02:34:16 +02:00
|
|
|
const layer = filteredLayer.layerDef
|
2021-10-31 02:08:39 +01:00
|
|
|
|
2021-07-27 17:00:05 +02:00
|
|
|
const iconUnselected = new Combine([Svg.checkbox_empty]).SetStyle(
|
|
|
|
iconStyle
|
|
|
|
);
|
|
|
|
|
|
|
|
if (filteredLayer.layerDef.name === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const name: Translation = Translations.WT(
|
|
|
|
filteredLayer.layerDef.name
|
2021-10-25 21:50:38 +02:00
|
|
|
);
|
2021-07-27 17:00:05 +02:00
|
|
|
|
2021-10-30 02:34:16 +02:00
|
|
|
const styledNameChecked = name.Clone().SetStyle("font-size:large").SetClass("ml-3");
|
2021-07-27 17:00:05 +02:00
|
|
|
|
2021-10-30 02:34:16 +02:00
|
|
|
const styledNameUnChecked = name.Clone().SetStyle("font-size:large").SetClass("ml-3");
|
2021-07-27 17:00:05 +02:00
|
|
|
|
2021-07-27 19:39:57 +02:00
|
|
|
const zoomStatus =
|
|
|
|
new Toggle(
|
|
|
|
undefined,
|
2021-10-25 21:50:38 +02:00
|
|
|
Translations.t.general.layerSelection.zoomInToSeeThisLayer
|
2021-07-27 19:39:57 +02:00
|
|
|
.SetClass("alert")
|
|
|
|
.SetStyle("display: block ruby;width:min-content;"),
|
2021-09-07 01:49:18 +02:00
|
|
|
State.state.locationControl.map(location => location.zoom >= filteredLayer.layerDef.minzoom)
|
2021-07-27 19:39:57 +02:00
|
|
|
)
|
2021-09-07 01:49:18 +02:00
|
|
|
|
2021-07-27 19:39:57 +02:00
|
|
|
|
2021-07-27 17:00:05 +02:00
|
|
|
const style =
|
2021-07-27 21:25:00 +02:00
|
|
|
"display:flex;align-items:center;padding:0.5rem 0;";
|
2021-10-31 02:08:39 +01:00
|
|
|
const layerIcon = layer.defaultIcon()?.SetClass("w-8 h-8 ml-2")
|
|
|
|
const layerIconUnchecked = layer.defaultIcon()?.SetClass("opacity-50 w-8 h-8 ml-2")
|
2021-10-30 02:34:16 +02:00
|
|
|
|
|
|
|
const layerChecked = new Combine([icon, layerIcon, styledNameChecked, zoomStatus])
|
2021-07-27 17:00:05 +02:00
|
|
|
.SetStyle(style)
|
|
|
|
.onClick(() => filteredLayer.isDisplayed.setData(false));
|
|
|
|
|
2021-10-30 02:34:16 +02:00
|
|
|
const layerNotChecked = new Combine([iconUnselected, layerIconUnchecked, styledNameUnChecked])
|
2021-07-27 17:00:05 +02:00
|
|
|
.SetStyle(style)
|
|
|
|
.onClick(() => filteredLayer.isDisplayed.setData(true));
|
|
|
|
|
2021-07-27 19:39:57 +02:00
|
|
|
|
2021-07-27 17:00:05 +02:00
|
|
|
const filterPanel: BaseUIElement = FilterView.createFilterPanel(filteredLayer)
|
2021-07-27 19:39:57 +02:00
|
|
|
|
|
|
|
|
2021-07-27 17:00:05 +02:00
|
|
|
return new Toggle(
|
|
|
|
new Combine([layerChecked, filterPanel]),
|
|
|
|
layerNotChecked,
|
|
|
|
filteredLayer.isDisplayed
|
|
|
|
);
|
2021-07-26 12:26:41 +02:00
|
|
|
}
|
2021-07-27 19:39:57 +02:00
|
|
|
|
2021-09-07 01:49:18 +02:00
|
|
|
private static createFilterPanel(flayer: FilteredLayer): BaseUIElement {
|
2021-07-27 17:00:05 +02:00
|
|
|
const layer = flayer.layerDef
|
2021-07-27 19:39:57 +02:00
|
|
|
if (layer.filters.length === 0) {
|
2021-07-27 17:00:05 +02:00
|
|
|
return undefined;
|
|
|
|
}
|
2021-07-27 19:39:57 +02:00
|
|
|
|
2021-09-27 18:35:32 +02:00
|
|
|
const filterIndexes = new Map<string, number>()
|
|
|
|
layer.filters.forEach((f, i) => filterIndexes.set(f.id, i))
|
|
|
|
|
|
|
|
let listFilterElements: [BaseUIElement, UIEventSource<{ filter: FilterConfig, selected: number }>][] = layer.filters.map(
|
2021-07-27 17:00:05 +02:00
|
|
|
FilterView.createFilter
|
|
|
|
);
|
|
|
|
|
2021-09-27 18:35:32 +02:00
|
|
|
listFilterElements.forEach((inputElement, i) =>
|
|
|
|
inputElement[1].addCallback((changed) => {
|
|
|
|
const oldValue = flayer.appliedFilters.data
|
2021-10-14 21:43:14 +02:00
|
|
|
|
|
|
|
if (changed === undefined) {
|
2021-09-27 18:35:32 +02:00
|
|
|
// Lets figure out which filter should be removed
|
|
|
|
// We know this inputElement corresponds with layer.filters[i]
|
|
|
|
// SO, if there is a value in 'oldValue' with this filter, we have to recalculated
|
2021-10-14 21:43:14 +02:00
|
|
|
if (!oldValue.some(f => f.filter === layer.filters[i])) {
|
2021-09-27 18:35:32 +02:00
|
|
|
// The filter to remove is already gone, we can stop
|
|
|
|
return;
|
|
|
|
}
|
2021-10-14 21:43:14 +02:00
|
|
|
} else if (oldValue.some(f => f.filter === changed.filter && f.selected === changed.selected)) {
|
2021-09-27 18:35:32 +02:00
|
|
|
// The changed value is already there
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const listTagsFilters = Utils.NoNull(
|
|
|
|
listFilterElements.map((input) => input[1].data)
|
|
|
|
);
|
|
|
|
|
|
|
|
flayer.appliedFilters.setData(listTagsFilters);
|
|
|
|
})
|
2021-07-27 17:00:05 +02:00
|
|
|
);
|
|
|
|
|
2021-09-07 01:49:18 +02:00
|
|
|
flayer.appliedFilters.addCallbackAndRun(appliedFilters => {
|
2021-10-14 21:43:14 +02:00
|
|
|
for (let i = 0; i < layer.filters.length; i++) {
|
2021-09-27 18:35:32 +02:00
|
|
|
const filter = layer.filters[i];
|
|
|
|
let foundMatch = undefined
|
|
|
|
for (const appliedFilter of appliedFilters) {
|
2021-10-14 21:43:14 +02:00
|
|
|
if (appliedFilter.filter === filter) {
|
2021-09-27 18:35:32 +02:00
|
|
|
foundMatch = appliedFilter
|
2021-10-14 21:43:14 +02:00
|
|
|
break;
|
2021-09-27 18:35:32 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-14 21:43:14 +02:00
|
|
|
|
2021-09-27 18:35:32 +02:00
|
|
|
listFilterElements[i][1].setData(foundMatch)
|
2021-09-07 01:49:18 +02:00
|
|
|
}
|
2021-09-27 18:35:32 +02:00
|
|
|
|
2021-09-07 01:49:18 +02:00
|
|
|
})
|
|
|
|
|
2021-07-27 17:00:05 +02:00
|
|
|
return new Combine(listFilterElements.map(input => input[0].SetClass("mt-3")))
|
|
|
|
.SetClass("flex flex-col ml-8 bg-gray-300 rounded-xl p-2")
|
2021-07-20 16:29:48 +02:00
|
|
|
|
2021-07-27 17:00:05 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 18:35:32 +02:00
|
|
|
private static createFilter(filterConfig: FilterConfig): [BaseUIElement, UIEventSource<{ filter: FilterConfig, selected: number }>] {
|
2021-07-27 17:00:05 +02:00
|
|
|
if (filterConfig.options.length === 1) {
|
|
|
|
let option = filterConfig.options[0];
|
|
|
|
|
|
|
|
const icon = Svg.checkbox_filled_svg().SetClass("block mr-2");
|
|
|
|
const iconUnselected = Svg.checkbox_empty_svg().SetClass("block mr-2");
|
|
|
|
|
|
|
|
const toggle = new Toggle(
|
2021-09-30 18:50:08 +02:00
|
|
|
new Combine([icon, option.question.Clone().SetClass("block")]).SetClass("flex"),
|
|
|
|
new Combine([iconUnselected, option.question.Clone().SetClass("block")]).SetClass("flex")
|
2021-07-27 17:00:05 +02:00
|
|
|
)
|
|
|
|
.ToggleOnClick()
|
|
|
|
.SetClass("block m-1")
|
|
|
|
|
2021-09-27 18:35:32 +02:00
|
|
|
const selected = {
|
|
|
|
filter: filterConfig,
|
|
|
|
selected: 0
|
|
|
|
}
|
|
|
|
return [toggle, toggle.isEnabled.map(enabled => enabled ? selected : undefined, [],
|
|
|
|
f => f?.filter === filterConfig && f?.selected === 0)
|
|
|
|
]
|
2021-07-27 17:00:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let options = filterConfig.options;
|
|
|
|
|
2021-09-27 18:35:32 +02:00
|
|
|
const values = options.map((f, i) => ({
|
2021-10-14 21:43:14 +02:00
|
|
|
filter: filterConfig, selected: i
|
2021-09-27 18:35:32 +02:00
|
|
|
}))
|
2021-07-27 17:00:05 +02:00
|
|
|
const radio = new RadioButton(
|
|
|
|
options.map(
|
2021-09-27 18:35:32 +02:00
|
|
|
(option, i) =>
|
2021-09-30 18:50:08 +02:00
|
|
|
new FixedInputElement(option.question.Clone().SetClass("block"), i)
|
2021-07-27 17:00:05 +02:00
|
|
|
),
|
|
|
|
{
|
|
|
|
dontStyle: true
|
|
|
|
}
|
|
|
|
);
|
2021-10-14 21:43:14 +02:00
|
|
|
return [radio,
|
2021-09-27 18:35:32 +02:00
|
|
|
radio.GetValue().map(
|
2021-10-14 21:43:14 +02:00
|
|
|
i => values[i],
|
2021-09-27 18:35:32 +02:00
|
|
|
[],
|
2021-10-14 21:43:14 +02:00
|
|
|
selected => {
|
|
|
|
return selected?.selected
|
|
|
|
}
|
|
|
|
)]
|
2021-07-27 17:00:05 +02:00
|
|
|
}
|
2021-07-20 15:14:51 +02:00
|
|
|
}
|