mapcomplete/Logic/Web/QueryParameters.ts

92 lines
3 KiB
TypeScript
Raw Normal View History

2020-07-22 21:47:04 +00:00
/**
* Wraps the query parameters into UIEventSources
*/
import {UIEventSource} from "../UIEventSource";
2020-11-17 01:22:48 +00:00
import Hash from "./Hash";
2020-07-22 21:47:04 +00:00
export class QueryParameters {
2020-11-13 22:58:11 +00:00
private static order: string [] = ["layout", "test", "z", "lat", "lon"];
2020-07-31 15:11:44 +00:00
private static knownSources = {};
private static initialized = false;
2020-07-29 13:05:19 +00:00
private static defaults = {}
2020-11-13 22:58:11 +00:00
private static documentation = {}
private static addOrder(key) {
if (this.order.indexOf(key) < 0) {
2020-07-22 21:47:04 +00:00
this.order.push(key)
}
}
private static init() {
2020-11-13 22:58:11 +00:00
if (this.initialized) {
2020-07-25 16:00:08 +00:00
return;
}
2020-07-31 15:11:44 +00:00
this.initialized = true;
if (window?.location?.search) {
2020-07-22 21:47:04 +00:00
const params = window.location.search.substr(1).split("&");
for (const param of params) {
const kv = param.split("=");
2020-10-17 01:56:08 +00:00
const key = decodeURIComponent(kv[0]);
2020-07-22 21:47:04 +00:00
QueryParameters.addOrder(key)
2020-10-17 01:56:08 +00:00
const v = decodeURIComponent(kv[1]);
2020-07-22 21:47:04 +00:00
const source = new UIEventSource<string>(v);
source.addCallback(() => QueryParameters.Serialize())
2020-07-31 15:11:44 +00:00
QueryParameters.knownSources[key] = source;
2020-07-22 21:47:04 +00:00
}
}
}
private static Serialize() {
const parts = []
for (const key of QueryParameters.order) {
2021-01-08 17:02:07 +00:00
if (QueryParameters.knownSources[key]?.data === undefined) {
2020-07-22 21:47:04 +00:00
continue;
}
2020-11-02 19:15:55 +00:00
2020-12-04 20:30:35 +00:00
if (QueryParameters.knownSources[key].data === "undefined") {
continue;
}
2021-01-08 17:02:07 +00:00
if (QueryParameters.knownSources[key].data === QueryParameters.defaults[key]) {
2020-07-29 13:05:19 +00:00
continue;
}
2020-11-02 19:15:55 +00:00
2020-07-22 21:47:04 +00:00
parts.push(encodeURIComponent(key) + "=" + encodeURIComponent(QueryParameters.knownSources[key].data))
}
2021-01-08 15:49:42 +00:00
// Don't pollute the history every time a parameter changes
2021-01-08 17:02:07 +00:00
history.replaceState(null, "", "?" + parts.join("&") + Hash.Current());
2020-07-22 21:47:04 +00:00
}
public static GetQueryParameter(key: string, deflt: string, documentation?: string): UIEventSource<string> {
2020-07-31 15:11:44 +00:00
if(!this.initialized){
this.init();
}
2020-11-13 22:58:11 +00:00
QueryParameters.documentation[key] = documentation;
if (deflt !== undefined) {
QueryParameters.defaults[key] = deflt;
}
2020-07-22 21:47:04 +00:00
if (QueryParameters.knownSources[key] !== undefined) {
return QueryParameters.knownSources[key];
}
QueryParameters.addOrder(key);
2020-07-29 13:05:19 +00:00
const source = new UIEventSource<string>(deflt);
2020-07-22 21:47:04 +00:00
QueryParameters.knownSources[key] = source;
source.addCallback(() => QueryParameters.Serialize())
return source;
}
2020-11-13 22:58:11 +00:00
public static GenerateQueryParameterDocs(): string {
const docs = [];
for (const key in QueryParameters.documentation) {
docs.push("**" + key + "**: " + QueryParameters.documentation[key] + " (default value: _" + QueryParameters.defaults[key] + "_)")
}
return docs.join("\n\n");
}
2020-07-22 21:47:04 +00:00
}