forked from MapComplete/MapComplete
Refactored out 'layout.ts'
This commit is contained in:
parent
36f5e896df
commit
73f32e0ecf
30 changed files with 465 additions and 400 deletions
|
@ -1,4 +1,3 @@
|
|||
import {Layout} from "./Layout";
|
||||
import * as bookcases from "../assets/themes/bookcases/Bookcases.json";
|
||||
import * as aed from "../assets/themes/aed/aed.json";
|
||||
import * as toilets from "../assets/themes/toilets/toilets.json";
|
||||
|
@ -15,17 +14,18 @@ import * as fritures from "../assets/themes/fritures/fritures.json"
|
|||
import * as benches from "../assets/themes/benches/benches.json";
|
||||
import * as charging_stations from "../assets/themes/charging_stations/charging_stations.json"
|
||||
import * as widths from "../assets/themes/widths/width.json"
|
||||
|
||||
import {PersonalLayout} from "../Logic/PersonalLayout";
|
||||
import * as drinking_water from "../assets/themes/drinking_water/drinking_water.json"
|
||||
import LayerConfig from "./JSON/LayerConfig";
|
||||
import SharedLayers from "./SharedLayers";
|
||||
import * as personal from "../assets/themes/personalLayout/personalLayout.json"
|
||||
import LayoutConfig from "./JSON/LayoutConfig";
|
||||
|
||||
export class AllKnownLayouts {
|
||||
|
||||
public static allLayers: Map<string, LayerConfig> = undefined;
|
||||
|
||||
private static GenerateCycloFix(): Layout {
|
||||
const layout = Layout.LayoutFromJSON(cyclofix, SharedLayers.sharedLayers)
|
||||
private static GenerateCycloFix(): LayoutConfig {
|
||||
const layout = new LayoutConfig(cyclofix)
|
||||
const now = new Date();
|
||||
const m = now.getMonth() + 1;
|
||||
const day = new Date().getDate() + 1;
|
||||
|
@ -41,63 +41,31 @@ export class AllKnownLayouts {
|
|||
return layout;
|
||||
|
||||
}
|
||||
|
||||
private static GenerateWidths(): Layout {
|
||||
const layout = Layout.LayoutFromJSON(widths, SharedLayers.sharedLayers);
|
||||
|
||||
layout.enableUserBadge = false;
|
||||
layout.enableShareScreen = false;
|
||||
layout.enableMoreQuests = false;
|
||||
layout.enableLayers = false;
|
||||
layout.hideFromOverview = true;
|
||||
layout.enableSearch = false;
|
||||
layout.enableGeolocation = false;
|
||||
return layout;
|
||||
}
|
||||
|
||||
private static GenerateBuurtNatuur(): Layout {
|
||||
const layout = Layout.LayoutFromJSON(buurtnatuur, SharedLayers.sharedLayers);
|
||||
layout.enableMoreQuests = false;
|
||||
layout.enableShareScreen = false;
|
||||
layout.hideFromOverview = true;
|
||||
console.log("Buurtnatuur:",layout)
|
||||
return layout;
|
||||
}
|
||||
|
||||
private static GenerateBikeMonitoringStations(): Layout {
|
||||
const layout = Layout.LayoutFromJSON(bike_monitoring_stations, SharedLayers.sharedLayers);
|
||||
layout.hideFromOverview = true;
|
||||
return layout;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static layoutsList: Layout[] = [
|
||||
new PersonalLayout(),
|
||||
|
||||
Layout.LayoutFromJSON(shops, SharedLayers.sharedLayers),
|
||||
Layout.LayoutFromJSON(bookcases, SharedLayers.sharedLayers),
|
||||
Layout.LayoutFromJSON(aed, SharedLayers.sharedLayers),
|
||||
Layout.LayoutFromJSON(toilets, SharedLayers.sharedLayers),
|
||||
Layout.LayoutFromJSON(artworks, SharedLayers.sharedLayers),
|
||||
public static layoutsList: LayoutConfig[] = [
|
||||
new LayoutConfig(personal),
|
||||
AllKnownLayouts.GenerateCycloFix(),
|
||||
Layout.LayoutFromJSON(ghostbikes, SharedLayers.sharedLayers),
|
||||
Layout.LayoutFromJSON(nature, SharedLayers.sharedLayers),
|
||||
Layout.LayoutFromJSON(cyclestreets, SharedLayers.sharedLayers),
|
||||
Layout.LayoutFromJSON(maps, SharedLayers.sharedLayers),
|
||||
Layout.LayoutFromJSON(fritures, SharedLayers.sharedLayers),
|
||||
Layout.LayoutFromJSON(benches, SharedLayers.sharedLayers),
|
||||
Layout.LayoutFromJSON(charging_stations, SharedLayers.sharedLayers),
|
||||
AllKnownLayouts.GenerateWidths(),
|
||||
AllKnownLayouts.GenerateBuurtNatuur(),
|
||||
AllKnownLayouts.GenerateBikeMonitoringStations(),
|
||||
|
||||
new LayoutConfig(aed),
|
||||
new LayoutConfig(bookcases),
|
||||
new LayoutConfig(toilets),
|
||||
new LayoutConfig(artworks),
|
||||
new LayoutConfig(ghostbikes),
|
||||
new LayoutConfig(shops),
|
||||
new LayoutConfig(drinking_water),
|
||||
new LayoutConfig(nature),
|
||||
new LayoutConfig(cyclestreets),
|
||||
new LayoutConfig(maps),
|
||||
new LayoutConfig(fritures),
|
||||
new LayoutConfig(benches),
|
||||
new LayoutConfig(charging_stations),
|
||||
new LayoutConfig(widths),
|
||||
new LayoutConfig(buurtnatuur),
|
||||
new LayoutConfig(bike_monitoring_stations),
|
||||
];
|
||||
|
||||
|
||||
public static allSets: Map<string, Layout> = AllKnownLayouts.AllLayouts();
|
||||
public static allSets: Map<string, LayoutConfig> = AllKnownLayouts.AllLayouts();
|
||||
|
||||
private static AllLayouts(): Map<string, Layout> {
|
||||
private static AllLayouts(): Map<string, LayoutConfig> {
|
||||
this.allLayers = new Map<string, LayerConfig>();
|
||||
for (const layout of this.layoutsList) {
|
||||
for (let i = 0; i < layout.layers.length; i++) {
|
||||
|
@ -118,7 +86,7 @@ export class AllKnownLayouts {
|
|||
}
|
||||
}
|
||||
|
||||
const allSets: Map<string, Layout> = new Map();
|
||||
const allSets: Map<string, LayoutConfig> = new Map();
|
||||
for (const layout of this.layoutsList) {
|
||||
allSets[layout.id] = layout;
|
||||
allSets[layout.id.toLowerCase()] = layout;
|
||||
|
|
|
@ -2,35 +2,9 @@ import {AndOrTagConfigJson} from "./TagConfigJson";
|
|||
import {And, Or, RegexTag, Tag, TagsFilter} from "../../Logic/Tags";
|
||||
|
||||
import {Utils} from "../../Utils";
|
||||
import {Translation} from "../../UI/i18n/Translation";
|
||||
|
||||
export class FromJSON {
|
||||
|
||||
|
||||
public static Translation(json: string | any): Translation {
|
||||
if (json === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
if (typeof (json) === "string") {
|
||||
return new Translation({"*": json});
|
||||
}
|
||||
if(json.render !== undefined){
|
||||
console.error("Using a 'render' where a translation is expected. Content is", json.render);
|
||||
throw "ERROR: using a 'render' where none is expected"
|
||||
}
|
||||
const tr = {};
|
||||
let keyCount = 0;
|
||||
for (let key in json) {
|
||||
keyCount++;
|
||||
tr[key] = json[key]; // I'm doing this wrong, I know
|
||||
}
|
||||
if(keyCount == 0){
|
||||
return undefined;
|
||||
}
|
||||
const transl = new Translation(tr);
|
||||
return transl;
|
||||
}
|
||||
|
||||
public static SimpleTag(json: string): Tag {
|
||||
const tag = Utils.SplitFirst(json, "=");
|
||||
return new Tag(tag[0], tag[1]);
|
||||
|
|
98
Customizations/JSON/LayoutConfig.ts
Normal file
98
Customizations/JSON/LayoutConfig.ts
Normal file
|
@ -0,0 +1,98 @@
|
|||
import {Translation} from "../../UI/i18n/Translation";
|
||||
import TagRenderingConfig from "./TagRenderingConfig";
|
||||
import LayerConfig from "./LayerConfig";
|
||||
import {LayoutConfigJson} from "./LayoutConfigJson";
|
||||
import SharedLayers from "../SharedLayers";
|
||||
import SharedTagRenderings from "../SharedTagRenderings";
|
||||
|
||||
export default class LayoutConfig {
|
||||
public readonly id: string;
|
||||
public readonly maintainer: string;
|
||||
public readonly changesetmessage?: string;
|
||||
public readonly version: string;
|
||||
public readonly language: string[];
|
||||
public readonly title: Translation;
|
||||
public readonly shortDescription?: Translation;
|
||||
public readonly description: Translation;
|
||||
public readonly descriptionTail?: Translation;
|
||||
public readonly icon: string;
|
||||
public readonly socialImage?: string;
|
||||
public readonly startZoom: number;
|
||||
public readonly startLat: number;
|
||||
public readonly startLon: number;
|
||||
public readonly widenFactor: number;
|
||||
public readonly roamingRenderings: TagRenderingConfig[];
|
||||
public readonly defaultBackgroundId?: string;
|
||||
public readonly layers: LayerConfig[];
|
||||
public readonly hideFromOverview: boolean;
|
||||
public readonly enableUserBadge: boolean;
|
||||
public readonly enableShareScreen: boolean;
|
||||
public readonly enableMoreQuests: boolean;
|
||||
public readonly enableAddNewPoints: boolean;
|
||||
public readonly enableLayers: boolean;
|
||||
public readonly enableSearch: boolean;
|
||||
public readonly enableGeolocation: boolean;
|
||||
public readonly enableBackgroundLayerSelection: boolean;
|
||||
public readonly customCss?: string;
|
||||
|
||||
constructor(json: LayoutConfigJson, context?:string) {
|
||||
this.id = json.id;
|
||||
context = (context ?? "")+"."+this.id;
|
||||
this.maintainer = json.maintainer;
|
||||
this.changesetmessage = json.changesetmessage;
|
||||
this.version = json.version;
|
||||
this.language = [];
|
||||
if (typeof json.language === "string") {
|
||||
this.language = [json.language];
|
||||
} else {
|
||||
this.language = json.language;
|
||||
}
|
||||
if(json.title === undefined){
|
||||
throw "Title not defined in "+this.id;
|
||||
}
|
||||
if(json.description === undefined){
|
||||
throw "Description not defined in "+this.id;
|
||||
}
|
||||
this.title = new Translation(json.title, context+".title");
|
||||
this.description = new Translation(json.description, context+".description");
|
||||
this.shortDescription = json.shortDescription === undefined ? this.description.FirstSentence() : new Translation(json.shortDescription, context+".shortdescription");
|
||||
this.descriptionTail = json.descriptionTail === undefined ? new Translation({"*":""}, context) : new Translation(json.descriptionTail, context+".descriptionTail");
|
||||
this.icon = json.icon;
|
||||
this.socialImage = json.socialImage;
|
||||
this.startZoom = json.startZoom;
|
||||
this.startLat = json.startLat;
|
||||
this.startLon = json.startLon;
|
||||
this.widenFactor = json.widenFactor ?? 0.05;
|
||||
this.roamingRenderings = (json.roamingRenderings ?? []).map((tr, i) => {
|
||||
if (typeof tr === "string") {
|
||||
if (SharedTagRenderings.SharedTagRendering[tr] !== undefined) {
|
||||
return SharedTagRenderings.SharedTagRendering[tr];
|
||||
}
|
||||
}
|
||||
return new TagRenderingConfig(tr, `${this.id}.roaming_renderings[${i}]`);
|
||||
}
|
||||
);
|
||||
this.defaultBackgroundId = json.defaultBackgroundId;
|
||||
this.layers = json.layers.map((layer, i) => {
|
||||
if (typeof layer === "string")
|
||||
if (SharedLayers.sharedLayers[layer] !== undefined) {
|
||||
return SharedLayers.sharedLayers[layer];
|
||||
} else {
|
||||
throw "Unkown fixed layer " + layer;
|
||||
}
|
||||
return new LayerConfig(layer, `${this.id}.layers[${i}]`);
|
||||
});
|
||||
this.hideFromOverview = json.hideFromOverview ?? false;
|
||||
|
||||
this.enableUserBadge = json.enableUserBadge ?? true;
|
||||
this.enableShareScreen = json.enableShareScreen ?? true;
|
||||
this.enableMoreQuests = json.enableMoreQuests ?? true;
|
||||
this.enableLayers = json.enableLayers ?? true;
|
||||
this.enableSearch = json.enableSearch ?? true;
|
||||
this.enableGeolocation = json.enableGeolocation ?? true;
|
||||
this.enableAddNewPoints = json.enableAddNewPoints ?? true;
|
||||
this.enableBackgroundLayerSelection = json.enableBackgroundLayerSelection ?? true;
|
||||
this.customCss = json.customCss;
|
||||
}
|
||||
|
||||
}
|
|
@ -106,9 +106,21 @@ export interface LayoutConfigJson {
|
|||
*/
|
||||
layers: (LayerConfigJson | string)[],
|
||||
|
||||
|
||||
/**
|
||||
* The URL of a custom CSS stylesheet to modify the layout
|
||||
*/
|
||||
customCss?: string;
|
||||
/**
|
||||
* If set to true, this layout will not be shown in the overview with more themes
|
||||
*/
|
||||
hideFromOverview?: boolean;
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
enableUserBadge?: boolean;
|
||||
enableShareScreen?: boolean;
|
||||
enableMoreQuests?: boolean;
|
||||
enableLayers?: boolean;
|
||||
enableSearch?: boolean;
|
||||
enableAddNewPoints?: boolean;
|
||||
enableGeolocation?: boolean;
|
||||
enableBackgroundLayerSelection?: boolean;
|
||||
}
|
||||
|
|
|
@ -1,131 +0,0 @@
|
|||
import {UIElement} from "../UI/UIElement";
|
||||
import Translations from "../UI/i18n/Translations";
|
||||
import Combine from "../UI/Base/Combine";
|
||||
import State from "../State";
|
||||
import LayerConfig from "./JSON/LayerConfig";
|
||||
import {LayoutConfigJson} from "./JSON/LayoutConfigJson";
|
||||
import TagRenderingConfig from "./JSON/TagRenderingConfig";
|
||||
import {FromJSON} from "./JSON/FromJSON";
|
||||
import {Translation} from "../UI/i18n/Translation";
|
||||
import Svg from "../Svg";
|
||||
import {Img} from "../UI/Img";
|
||||
|
||||
/**
|
||||
* A layout is a collection of settings of the global view (thus: welcome text, title, selection of layers).
|
||||
*/
|
||||
export class Layout {
|
||||
|
||||
public id: string;
|
||||
public icon: string = Img.AsData(Svg.bug);
|
||||
public title: UIElement;
|
||||
public maintainer: string;
|
||||
public version: string;
|
||||
public description: string | UIElement;
|
||||
public changesetMessage: string;
|
||||
public socialImage: string = "";
|
||||
/**
|
||||
* Custom CSS link
|
||||
*/
|
||||
public customCss: string = undefined;
|
||||
|
||||
public layers: LayerConfig[];
|
||||
public welcomeMessage: UIElement;
|
||||
public gettingStartedPlzLogin: UIElement;
|
||||
public welcomeBackMessage: UIElement;
|
||||
public welcomeTail: UIElement;
|
||||
|
||||
public supportedLanguages: string[];
|
||||
|
||||
public startzoom: number;
|
||||
public startLon: number;
|
||||
public startLat: number;
|
||||
|
||||
public enableAdd: boolean = true;
|
||||
public enableUserBadge: boolean = true;
|
||||
public enableSearch: boolean = true;
|
||||
public enableLayers: boolean = true;
|
||||
public enableBackgroundLayers: boolean = true;
|
||||
public enableMoreQuests: boolean = true;
|
||||
public enableShareScreen: boolean = true;
|
||||
public enableGeolocation: boolean = true;
|
||||
public hideFromOverview: boolean = false;
|
||||
|
||||
/**
|
||||
* The BBOX of the currently visible map are widened by this factor, in order to make some panning possible.
|
||||
* This number influences this
|
||||
*/
|
||||
public widenFactor: number = 0.07;
|
||||
public defaultBackground: string = "osm";
|
||||
|
||||
public static LayoutFromJSON(json: LayoutConfigJson, sharedLayers): Layout {
|
||||
const tr = FromJSON.Translation;
|
||||
const layers = json.layers.map(jsonLayer => {
|
||||
if(typeof jsonLayer === "string"){
|
||||
return sharedLayers[jsonLayer];
|
||||
}
|
||||
return new LayerConfig(jsonLayer, "theme."+json.id);
|
||||
});
|
||||
const roaming: TagRenderingConfig[] = json.roamingRenderings?.map((tr, i) =>
|
||||
new TagRenderingConfig(tr, `theme.${json.id}.roamingRendering[${i}]`)) ?? [];
|
||||
for (const layer of layers) {
|
||||
layer.tagRenderings.push(...roaming);
|
||||
}
|
||||
|
||||
const layout = new Layout(
|
||||
json.id,
|
||||
typeof (json.language) === "string" ? [json.language] : json.language,
|
||||
tr(json.title ?? "Title not defined"),
|
||||
layers,
|
||||
json.startZoom,
|
||||
json.startLat,
|
||||
json.startLon,
|
||||
new Combine(["<h3>", tr(json.title), "</h3>", tr(json.description)]),
|
||||
undefined,
|
||||
undefined,
|
||||
tr(json.descriptionTail)
|
||||
|
||||
);
|
||||
|
||||
layout.defaultBackground = json.defaultBackgroundId ?? "osm";
|
||||
layout.widenFactor = json.widenFactor ?? 0.07;
|
||||
layout.icon = json.icon;
|
||||
layout.maintainer = json.maintainer;
|
||||
layout.version = json.version;
|
||||
layout.socialImage = json.socialImage;
|
||||
layout.description = tr(json.shortDescription) ?? tr(json.description)?.FirstSentence();
|
||||
layout.changesetMessage = json.changesetmessage;
|
||||
return layout;
|
||||
}
|
||||
|
||||
constructor(
|
||||
id: string,
|
||||
supportedLanguages: string[],
|
||||
title: Translation | string,
|
||||
layers: LayerConfig[],
|
||||
startzoom: number,
|
||||
startLat: number,
|
||||
startLon: number,
|
||||
welcomeMessage: UIElement | string,
|
||||
gettingStartedPlzLogin: UIElement | string = new Combine([
|
||||
Translations.t.general.getStartedLogin
|
||||
.SetClass("soft")
|
||||
.onClick(() => {State.state.osmConnection.AttemptLogin()}),
|
||||
Translations.t.general.getStartedNewAccount
|
||||
]),
|
||||
welcomeBackMessage: UIElement | string = Translations.t.general.welcomeBack,
|
||||
welcomeTail: UIElement | string = "",
|
||||
) {
|
||||
this.supportedLanguages = supportedLanguages;
|
||||
this.title = Translations.WT(title)
|
||||
this.startLon = startLon;
|
||||
this.startLat = startLat;
|
||||
this.startzoom = startzoom;
|
||||
this.id = id;
|
||||
this.layers = layers;
|
||||
this.welcomeMessage = Translations.W(welcomeMessage)
|
||||
this.gettingStartedPlzLogin = Translations.W(gettingStartedPlzLogin);
|
||||
this.welcomeBackMessage = Translations.W(welcomeBackMessage);
|
||||
this.welcomeTail = Translations.W(welcomeTail);
|
||||
}
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue