mapcomplete/UI/BigComponents/LayerSelection.ts

81 lines
3.3 KiB
TypeScript
Raw Normal View History

import {UIEventSource} from "../../Logic/UIEventSource";
import {VariableUiElement} from "../Base/VariableUIElement";
import State from "../../State";
2021-06-10 01:36:20 +02:00
import Toggle from "../Input/Toggle";
import Combine from "../Base/Combine";
import Translations from "../i18n/Translations";
import LayerConfig from "../../Customizations/JSON/LayerConfig";
2021-06-12 02:58:32 +02:00
import BaseUIElement from "../BaseUIElement";
import {Translation} from "../i18n/Translation";
2020-07-22 11:49:01 +02:00
/**
* Shows the panel with all layers and a toggle for each of them
*/
2021-06-12 02:58:32 +02:00
export default class LayerSelection extends Combine {
2020-07-22 11:49:01 +02:00
constructor(activeLayers: UIEventSource<{
readonly isDisplayed: UIEventSource<boolean>,
readonly layerDef: LayerConfig;
}[]>) {
2021-06-12 02:58:32 +02:00
if (activeLayers === undefined) {
throw "ActiveLayers should be defined..."
}
2021-06-12 02:58:32 +02:00
const checkboxes: BaseUIElement[] = [];
2020-07-22 11:49:01 +02:00
2021-06-12 02:58:32 +02:00
for (const layer of activeLayers.data) {
const leafletStyle = layer.layerDef.GenerateLeafletStyle(
new UIEventSource<any>({id: "node/-1"}),
false)
const leafletStyleNa = layer.layerDef.GenerateLeafletStyle(
new UIEventSource<any>({id: "node/-1"}),
false)
2021-06-12 02:58:32 +02:00
const icon = new Combine([leafletStyle.icon.html]).SetClass("single-layer-selection-toggle")
let iconUnselected: BaseUIElement = new Combine([leafletStyleNa.icon.html])
.SetClass("single-layer-selection-toggle")
.SetStyle("opacity:0.2;");
2020-09-17 19:11:16 +02:00
if (layer.layerDef.name === undefined) {
continue;
}
2020-09-17 20:59:05 +02:00
const name: Translation = Translations.WT(layer.layerDef.name)?.Clone()
name.SetStyle("font-size:large;margin-left: 0.5em;");
2020-09-17 19:11:16 +02:00
const zoomStatus = new VariableUiElement(State.state.locationControl.map(location => {
2020-09-17 20:59:05 +02:00
if (location.zoom < layer.layerDef.minzoom) {
2021-06-14 17:42:26 +02:00
return Translations.t.general.layerSelection.zoomInToSeeThisLayer.Clone()
2020-09-17 19:11:16 +02:00
.SetClass("alert")
2020-09-17 20:59:05 +02:00
.SetStyle("display: block ruby;width:min-content;")
2020-09-17 19:11:16 +02:00
}
return ""
}))
const zoomStatusNonActive = new VariableUiElement(State.state.locationControl.map(location => {
if (location.zoom < layer.layerDef.minzoom) {
return Translations.t.general.layerSelection.zoomInToSeeThisLayer.Clone()
.SetClass("alert")
.SetStyle("display: block ruby;width:min-content;")
}
return ""
}))
2021-01-08 14:23:12 +01:00
const style = "display:flex;align-items:center;"
const styleWhole = "display:flex; flex-wrap: wrap"
2021-06-12 02:58:32 +02:00
checkboxes.push(new Toggle(
new Combine([new Combine([icon, name.Clone()]).SetStyle(style), zoomStatus])
2021-01-08 14:23:12 +01:00
.SetStyle(styleWhole),
new Combine([new Combine([iconUnselected, "<del>", name.Clone(), "</del>"]).SetStyle(style), zoomStatusNonActive])
2021-01-08 14:23:12 +01:00
.SetStyle(styleWhole),
2021-06-14 02:39:23 +02:00
layer.isDisplayed).ToggleOnClick()
.SetStyle("margin:0.3em;")
);
}
2020-07-22 11:49:01 +02:00
2021-06-12 02:58:32 +02:00
super(checkboxes)
this.SetStyle("display:flex;flex-direction:column;")
2020-07-22 11:49:01 +02:00
2021-06-12 02:58:32 +02:00
}
2020-07-22 11:49:01 +02:00
}