digitransit-ui/app/store/MapLayerStore.js
2023-05-15 15:58:30 +03:00

125 lines
3 KiB
JavaScript

import Store from 'fluxible/addons/BaseStore';
import PropTypes from 'prop-types';
import { setMapLayerSettings, getMapLayerSettings } from './localStorage';
import { showCityBikes } from '../util/modeUtils';
class MapLayerStore extends Store {
static defaultLayers = {
parkAndRide: false,
parkAndRideForBikes: false,
stop: {
bus: true,
ferry: true,
rail: true,
subway: true,
tram: true,
funicular: true,
},
terminal: {
bus: true,
ferry: true,
rail: true,
subway: true,
tram: true,
},
vehicles: false,
geoJson: {},
};
static handlers = {
UpdateMapLayers: 'updateMapLayers',
};
static storeName = 'MapLayerStore';
mapLayers = { ...MapLayerStore.defaultLayers };
constructor(dispatcher) {
super(dispatcher);
const { config } = dispatcher.getContext();
this.mapLayers.citybike = showCityBikes(config.cityBike?.networks, config);
if (config.hideMapLayersByDefault) {
this.mapLayers.stop = Object.keys(this.mapLayers.stop).map(() => false);
this.mapLayers.citybike = false;
}
const storedMapLayers = getMapLayerSettings();
if (Object.keys(storedMapLayers).length > 0) {
this.mapLayers = {
...this.mapLayers,
...storedMapLayers,
terminal: { ...this.mapLayers.terminal, ...storedMapLayers.terminal },
};
}
}
getMapLayers = skip => {
if (!skip?.notThese && !skip?.force) {
return this.mapLayers;
}
const layers = { ...this.mapLayers };
if (skip.notThese) {
skip.notThese.forEach(key => {
if (typeof layers[key] === 'object') {
layers[key] = {};
Object.keys(this.mapLayers[key]).forEach(subKey => {
layers[key][subKey] = false;
});
} else {
layers[key] = false;
}
});
}
if (skip.force) {
skip.force.forEach(key => {
if (typeof layers[key] === 'object') {
layers[key] = {};
Object.keys(this.mapLayers[key]).forEach(subKey => {
layers[key][subKey] = true;
});
} else {
layers[key] = true;
}
});
}
return layers;
};
updateMapLayers = mapLayers => {
this.mapLayers = {
...this.mapLayers,
...mapLayers,
stop: {
...this.mapLayers.stop,
...mapLayers.stop,
},
};
setMapLayerSettings({ ...this.mapLayers });
this.emitChange();
};
}
export const mapLayerShape = PropTypes.shape({
citybike: PropTypes.bool,
parkAndRide: PropTypes.bool,
parkAndRideForBikes: PropTypes.bool,
stop: PropTypes.shape({
bus: PropTypes.bool,
ferry: PropTypes.bool,
rail: PropTypes.bool,
subway: PropTypes.bool,
tram: PropTypes.bool,
funicular: PropTypes.bool,
}).isRequired,
terminal: PropTypes.shape({
bus: PropTypes.bool,
rail: PropTypes.bool,
subway: PropTypes.bool,
}).isRequired,
vehicles: PropTypes.bool,
geoJson: PropTypes.object,
});
export default MapLayerStore;