mapcomplete/preferences.ts

112 lines
3.2 KiB
TypeScript
Raw Normal View History

2020-07-31 21:54:30 +02:00
import {OsmConnection} from "./Logic/Osm/OsmConnection";
2020-08-07 16:01:18 +02:00
import Combine from "./UI/Base/Combine";
import {Button} from "./UI/Base/Button";
import {TextField} from "./UI/Input/TextField";
import {FixedUiElement} from "./UI/Base/FixedUiElement";
import {UIElement} from "./UI/UIElement";
import {UIEventSource} from "./Logic/UIEventSource";
2021-05-07 13:17:42 +02:00
import {Utils} from "./Utils";
import {SubtleButton} from "./UI/Base/SubtleButton";
2020-07-31 21:54:30 +02:00
const connection = new OsmConnection(false, new UIEventSource<string>(undefined), "");
2020-08-07 16:01:18 +02:00
let rendered = false;
2021-05-07 13:17:42 +02:00
function salvageThemes(preferences: any) {
const knownThemeNames = new Set<string>();
const correctThemeNames = []
for (const key in preferences) {
if (!(typeof key === "string")) {
continue;
}
const prefix = "mapcomplete-installed-theme-";
// mapcomplete-installed-theme-arbres_llefia-combined-11
//mapcomplete-installed-theme-1roadAlllanes-combined-length
if (!key.startsWith(prefix)) {
continue;
}
const theme = key.substring(prefix.length, key.indexOf("-combined-"))
if (key.endsWith("-length")) {
correctThemeNames.push(theme)
} else {
knownThemeNames.add(theme);
}
}
for (const correctThemeName of correctThemeNames) {
knownThemeNames.delete(correctThemeName);
}
const missingValues = Array.from(knownThemeNames).map(failedTheme => {
let i = 0;
let foundValue = undefined
let combined = ""
do {
const key = prefix + failedTheme + "-combined-" + i;
foundValue = preferences[key]
i++;
combined += foundValue ?? ""
} while (foundValue !== undefined);
const json = Utils.UnMinify(combined);
return {
themeName: failedTheme,
contents: json
}
})
return missingValues;
}
function SalvageButton(theme: {themeName: string, contents: string}){
return new SubtleButton("bug.svg", "Download broken theme "+theme.themeName).onClick(
() => {
Utils.downloadTxtFile(theme.contents, theme.themeName+".json")
}
)
}
2020-08-07 16:01:18 +02:00
function createTable(preferences: any) {
if (rendered) {
return;
}
rendered = true;
const prefs = [];
for (const key in preferences) {
const pref = connection.GetPreference(key, "");
let value: UIElement = new FixedUiElement(pref.data);
if (connection.userDetails.data.csCount > 500 &&
(key.startsWith("mapcomplete") || connection.userDetails.data.csCount > 2500)) {
value = new TextField({
2020-08-07 16:01:18 +02:00
value: pref
});
}
const c = [
"<tr><td>",
key,
"</td><td>",
new Button("delete", () => pref.setData("")),
"</td><td>",
2021-04-10 03:50:44 +02:00
value,
2020-08-07 16:01:18 +02:00
"</td></tr>"
];
prefs.push(...c);
}
2021-04-10 03:50:44 +02:00
new Combine(
2021-05-07 13:17:42 +02:00
[
...salvageThemes(preferences).map(theme => SalvageButton(theme)),
"<table>",
2020-08-07 16:01:18 +02:00
...prefs,
"</table>"]
2021-04-10 03:50:44 +02:00
).AttachTo("maindiv");
2020-08-07 16:01:18 +02:00
}
connection.preferencesHandler.preferences.addCallback((prefs) => createTable(prefs))
2020-08-07 16:01:18 +02:00