mapcomplete/Logic/CustomLayersState.ts

88 lines
3 KiB
TypeScript
Raw Normal View History

2020-07-31 04:58:58 +02:00
import {State} from "../State";
export class CustomLayersState {
static RemoveFavouriteLayer(layer: string) {
2020-07-31 16:17:16 +02:00
State.state.GetFilteredLayerFor(layer)?.isDisplayed?.setData(false);
2020-07-31 04:58:58 +02:00
const favs = State.state.favourteLayers.data;
const ind = favs.indexOf(layer);
if (ind < 0) {
return;
}
2020-07-31 16:17:16 +02:00
2020-07-31 04:58:58 +02:00
favs.splice(ind, 1);
2020-07-31 16:17:16 +02:00
2020-07-31 04:58:58 +02:00
const osmConnection = State.state.osmConnection;
const count = osmConnection.GetPreference("mapcomplete-custom-layer-count");
2020-07-31 16:17:16 +02:00
for (let i = 0; i < favs.length; i++) {
2020-07-31 04:58:58 +02:00
const layerIDescr = osmConnection.GetPreference("mapcomplete-custom-layer-" + i);
2020-07-31 16:17:16 +02:00
layerIDescr.setData(favs[i]);
2020-07-31 04:58:58 +02:00
}
2020-07-31 16:17:16 +02:00
count.setData("" + favs.length)
2020-07-31 04:58:58 +02:00
}
static AddFavouriteLayer(layer: string) {
2020-07-31 16:17:16 +02:00
State.state.GetFilteredLayerFor(layer)?.isDisplayed?.setData(true);
2020-07-31 04:58:58 +02:00
const favs = State.state.favourteLayers.data;
const ind = favs.indexOf(layer);
if (ind >= 0) {
return;
}
console.log("Adding fav layer", layer);
favs.push(layer);
const osmConnection = State.state.osmConnection;
const count = osmConnection.GetPreference("mapcomplete-custom-layer-count");
if (count.data === undefined || isNaN(Number(count.data))) {
count.data = "0";
}
const lastId = Number(count.data);
for (let i = 0; i < lastId; i++) {
const layerIDescr = osmConnection.GetPreference("mapcomplete-custom-layer-" + i);
2020-07-31 16:17:16 +02:00
if (layerIDescr.data === undefined || layerIDescr.data === "") {
2020-07-31 04:58:58 +02:00
// An earlier item was removed -> overwrite it
layerIDescr.setData(layer);
count.ping();
return;
}
}
// No empty slot found -> create a new one
const layerIDescr = osmConnection.GetPreference("mapcomplete-custom-layer-" + lastId);
layerIDescr.setData(layer);
count.setData((lastId + 1) + "");
}
2020-07-31 16:17:16 +02:00
static InitFavouriteLayers(state: State) {
const osmConnection = state.osmConnection;
2020-07-31 04:58:58 +02:00
const count = osmConnection.GetPreference("mapcomplete-custom-layer-count");
2020-07-31 16:17:16 +02:00
const favs = state.favourteLayers.data;
2020-07-31 04:58:58 +02:00
let changed = false;
count.addCallback((countStr) => {
2020-07-31 16:17:16 +02:00
console.log("UPdating favourites")
2020-07-31 04:58:58 +02:00
if (countStr === undefined) {
return;
}
let countI = Number(countStr);
if (isNaN(countI)) {
countI = 999;
}
for (let i = 0; i < countI; i++) {
const layerId = osmConnection.GetPreference("mapcomplete-custom-layer-" + i).data;
2020-07-31 16:17:16 +02:00
if (layerId !== undefined && layerId !== "" && favs.indexOf(layerId) < 0) {
state.favourteLayers.data.push(layerId);
2020-07-31 04:58:58 +02:00
changed = true;
}
}
if (changed) {
2020-07-31 16:17:16 +02:00
state.favourteLayers.ping();
2020-07-31 04:58:58 +02:00
}
})
}
}