Refactored out 'layout.ts'

This commit is contained in:
Pieter Vander Vennet 2020-11-11 16:23:49 +01:00
parent 36f5e896df
commit 73f32e0ecf
30 changed files with 465 additions and 400 deletions

View file

@ -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]);

View 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;
}
}

View file

@ -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;
}