mapcomplete/Logic/Web/QueryParameters.ts

137 lines
5 KiB
TypeScript
Raw Normal View History

2020-07-22 23:47:04 +02:00
/**
* Wraps the query parameters into UIEventSources
*/
import {UIEventSource} from "../UIEventSource";
2020-11-17 02:22:48 +01:00
import Hash from "./Hash";
import {Utils} from "../../Utils";
import Title from "../../UI/Base/Title";
import Combine from "../../UI/Base/Combine";
2020-07-22 23:47:04 +02:00
export class QueryParameters {
2020-11-13 23:58:11 +01:00
private static order: string [] = ["layout", "test", "z", "lat", "lon"];
private static _wasInitialized: Set<string> = new Set()
2020-07-31 17:11:44 +02:00
private static knownSources = {};
private static initialized = false;
2020-07-29 15:05:19 +02:00
private static defaults = {}
2020-11-13 23:58:11 +01:00
private static documentation = {}
private static QueryParamDocsIntro = "\n" +
"URL-parameters and URL-hash\n" +
"============================\n" +
"\n" +
"This document gives an overview of which URL-parameters can be used to influence MapComplete.\n" +
"\n" +
"What is a URL parameter?\n" +
"------------------------\n" +
"\n" +
"URL-parameters are extra parts of the URL used to set the state.\n" +
"\n" +
"For example, if the url is `https://mapcomplete.osm.be/cyclofix?lat=51.0&lon=4.3&z=5&test=true#node/1234`,\n" +
"the URL-parameters are stated in the part between the `?` and the `#`. There are multiple, all separated by `&`, namely:\n" +
"\n" +
"- The url-parameter `lat` is `51.0` in this instance\n" +
"- The url-parameter `lon` is `4.3` in this instance\n" +
"- The url-parameter `z` is `5` in this instance\n" +
"- The url-parameter `test` is `true` in this instance\n" +
"\n" +
"Finally, the URL-hash is the part after the `#`. It is `node/1234` in this case."
public static GetQueryParameter(key: string, deflt: string, documentation?: string): UIEventSource<string> {
if (!this.initialized) {
this.init();
}
QueryParameters.documentation[key] = documentation;
if (deflt !== undefined) {
QueryParameters.defaults[key] = deflt;
}
if (QueryParameters.knownSources[key] !== undefined) {
return QueryParameters.knownSources[key];
}
QueryParameters.addOrder(key);
const source = new UIEventSource<string>(deflt, "&" + key);
QueryParameters.knownSources[key] = source;
source.addCallback(() => QueryParameters.Serialize())
return source;
}
public static GetBooleanQueryParameter(key: string, deflt: string, documentation?: string): UIEventSource<boolean>{
return QueryParameters.GetQueryParameter(key, deflt, documentation).map(str => str === "true", [], b => ""+b)
}
public static GenerateQueryParameterDocs(): string {
const docs = [QueryParameters.QueryParamDocsIntro];
for (const key in QueryParameters.documentation) {
const c = new Combine([
new Title(key, 2),
QueryParameters.documentation[key],
QueryParameters.defaults[key] === undefined ? "No default value set" : `The default value is _${QueryParameters.defaults[key]}_`
])
docs.push(c.AsMarkdown())
}
return docs.join("\n\n");
}
2020-11-13 23:58:11 +01:00
public static wasInitialized(key: string): boolean {
return QueryParameters._wasInitialized.has(key)
}
2020-11-13 23:58:11 +01:00
private static addOrder(key) {
if (this.order.indexOf(key) < 0) {
2020-07-22 23:47:04 +02:00
this.order.push(key)
}
}
private static init() {
2020-11-13 23:58:11 +01:00
if (this.initialized) {
2020-07-25 18:00:08 +02:00
return;
}
2020-07-31 17:11:44 +02:00
this.initialized = true;
if (Utils.runningFromConsole) {
return;
}
2020-07-31 17:11:44 +02:00
if (window?.location?.search) {
2020-07-22 23:47:04 +02:00
const params = window.location.search.substr(1).split("&");
for (const param of params) {
const kv = param.split("=");
2020-10-17 03:56:08 +02:00
const key = decodeURIComponent(kv[0]);
2020-07-22 23:47:04 +02:00
QueryParameters.addOrder(key)
QueryParameters._wasInitialized.add(key)
2020-10-17 03:56:08 +02:00
const v = decodeURIComponent(kv[1]);
2020-07-22 23:47:04 +02:00
const source = new UIEventSource<string>(v);
source.addCallback(() => QueryParameters.Serialize())
2020-07-31 17:11:44 +02:00
QueryParameters.knownSources[key] = source;
2020-07-22 23:47:04 +02:00
}
}
2021-04-06 18:34:45 +02:00
window["mapcomplete_query_parameter_overview"] = () => {
console.log(QueryParameters.GenerateQueryParameterDocs())
}
2020-07-22 23:47:04 +02:00
}
private static Serialize() {
const parts = []
for (const key of QueryParameters.order) {
2021-01-08 18:02:07 +01:00
if (QueryParameters.knownSources[key]?.data === undefined) {
2020-07-22 23:47:04 +02:00
continue;
}
2020-12-04 21:30:35 +01:00
if (QueryParameters.knownSources[key].data === "undefined") {
continue;
}
2021-01-08 18:02:07 +01:00
if (QueryParameters.knownSources[key].data === QueryParameters.defaults[key]) {
2020-07-29 15:05:19 +02:00
continue;
}
2020-11-02 20:15:55 +01:00
2020-07-22 23:47:04 +02:00
parts.push(encodeURIComponent(key) + "=" + encodeURIComponent(QueryParameters.knownSources[key].data))
}
2021-01-08 16:49:42 +01:00
// Don't pollute the history every time a parameter changes
history.replaceState(null, "", "?" + parts.join("&") + Hash.Current());
2020-07-22 23:47:04 +02:00
2020-11-13 23:58:11 +01:00
}
2020-07-22 23:47:04 +02:00
}