mobility-toolbox-js
Version:
Toolbox for JavaScript applications in the domains of mobility and logistics.
195 lines (194 loc) • 9.69 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _MocoLayer_abortController, _MocoLayer_graphMapping;
import { getMocoIconRefFeatures, getMocoNotificationsAsFeatureCollection, isMocoNotificationActive, isMocoNotificationPublished, MocoAPI, } from '..';
import MaplibreStyleLayer from './MaplibreStyleLayer';
export const MOCO_SOURCE_ID = 'moco';
export const MOCO_MD_LAYER_FILTER = 'moco';
export const DEFAULT_GRAPH_MAPPING = { 1: 'osm' };
/**
* An OpenLayers layer able to display data from the [geOps MOCO API](https://geops.com/de/solution/disruption-information).
*
* @example
* import { MaplibreLayer, MaplibreStyleLayer } from 'mobility-toolbox-js/ol';
*
* const maplibreLayer = new MaplibreLayer({
* apiKey: 'yourApiKey',
* });
*
* const layer = new MocoLayer({
* apiKey: 'yourApiKey',
* maplibreLayer: maplibreLayer,
* // date: new Date(),
* // loadAll: true,
* // notifications: undefined,
* // tenant: "geopstest",
* // url: 'https://moco.geops.io'
* });
*
* @see <a href="/example/ol-maplibre-style-layer">OpenLayers MaplibreStyle layer example</a>
* @extends {MaplibreStyleLayer}
* @private
*/
class MocoLayer extends MaplibreStyleLayer {
get apiKey() {
return this.get('apiKey');
}
set apiKey(value) {
this.set('apiKey', value);
void this.updateData();
}
get date() {
return this.get('date') || new Date();
}
set date(value) {
this.set('date', value);
void this.updateData();
}
get loadAll() {
var _a;
return (_a = this.get('loadAll')) !== null && _a !== void 0 ? _a : true;
}
set loadAll(value) {
this.set('loadAll', value);
void this.updateData();
}
get notifications() {
return this.get('notifications');
}
set notifications(value) {
this.set('notifications', value);
void this.updateData();
}
get tenant() {
return this.get('tenant');
}
set tenant(value) {
this.set('tenant', value);
void this.updateData();
}
get url() {
return this.get('url');
}
set url(value) {
this.set('url', value);
void this.updateData();
}
/**
* Constructor.
*
* @param {Object} options
* @param {string} options.apiKey Access key for [geOps APIs](https://developer.geops.io/). *
* @param {string} [options.date] The date to filter notifications. If not set, the current date is used.
* @param {boolean} [options.loadAll=true] If true, all active and published notifications will be loaded, otherwise only the notifications set in 'notifications' will be displayed.
* @param {MocoNotification[]} [options.notifications] The notifications to display. If not set and loadAll is true, all active and published notifications will be loaded.
* @param {string} [options.tenant] The SSO config to use to get notifications from.
* @param {string} [options.url] The URL of the [geOps MOCO API](https://geops.com/de/solution/disruption-information).
* @public
*/
constructor(options) {
super(Object.assign(Object.assign({}, options), { layersFilter: ({ metadata }) => {
return ((metadata === null || metadata === void 0 ? void 0 : metadata['general.filter']) ===
MOCO_MD_LAYER_FILTER);
}, maplibreLayer: options.maplibreLayer }));
_MocoLayer_abortController.set(this, null);
_MocoLayer_graphMapping.set(this, DEFAULT_GRAPH_MAPPING);
}
attachToMap(map) {
var _a, _b;
super.attachToMap(map);
// If the source is already there (no load event triggered) update data
const source = (_b = (_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.mapLibreMap) === null || _b === void 0 ? void 0 : _b.getSource(MOCO_SOURCE_ID);
if (source) {
void this.updateData();
}
}
detachFromMap() {
var _a, _b;
super.detachFromMap();
const source = (_b = (_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.mapLibreMap) === null || _b === void 0 ? void 0 : _b.getSource(MOCO_SOURCE_ID);
if (source) {
// Remove the data from the map
source.setData({
features: [],
type: 'FeatureCollection',
});
}
if (__classPrivateFieldGet(this, _MocoLayer_abortController, "f")) {
__classPrivateFieldGet(this, _MocoLayer_abortController, "f").abort();
__classPrivateFieldSet(this, _MocoLayer_abortController, null, "f");
}
}
onLoad() {
super.onLoad();
void this.updateData();
}
updateData() {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e;
if (__classPrivateFieldGet(this, _MocoLayer_abortController, "f")) {
__classPrivateFieldGet(this, _MocoLayer_abortController, "f").abort();
}
__classPrivateFieldSet(this, _MocoLayer_abortController, new AbortController(), "f");
// Get graphs mapping
const styleMetadata = (_c = (_b = (_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.mapLibreMap) === null || _b === void 0 ? void 0 : _b.getStyle()) === null || _c === void 0 ? void 0 : _c.metadata;
__classPrivateFieldSet(this, _MocoLayer_graphMapping, (styleMetadata === null || styleMetadata === void 0 ? void 0 : styleMetadata.graphs) || DEFAULT_GRAPH_MAPPING, "f");
const graphsString = [...new Set(Object.values(__classPrivateFieldGet(this, _MocoLayer_graphMapping, "f")))].join(',');
// We get the data from the MocoAPI
const api = new MocoAPI({
apiKey: this.apiKey,
graph: graphsString,
ssoConfig: this.tenant,
url: this.url,
});
const date = this.date;
let notifications = this.notifications;
if (!notifications && this.loadAll) {
notifications = yield api.getNotifications({ date: date }, { signal: __classPrivateFieldGet(this, _MocoLayer_abortController, "f").signal });
}
const source = (_e = (_d = this.maplibreLayer) === null || _d === void 0 ? void 0 : _d.mapLibreMap) === null || _e === void 0 ? void 0 : _e.getSource(MOCO_SOURCE_ID);
if (!source) {
console.warn('MocoLayer: No source found for id : ', MOCO_SOURCE_ID);
return;
}
const notifsToRender = (notifications !== null && notifications !== void 0 ? notifications : [])
.map((notification) => {
// Add status properties to the features, these properties are only there for rendering purposes
return Object.assign(Object.assign({}, notification), { properties: Object.assign(Object.assign({}, notification.properties), { isActive: isMocoNotificationActive(notification.properties, date), isPublished: isMocoNotificationPublished(notification.properties, date) }) });
})
.filter((n) => {
return n.properties.isPublished || n.properties.isActive;
});
notifsToRender.forEach((notification) => {
var _a;
// Add icon ref features, these features are only there for rendering purposes
const iconRefFeatures = getMocoIconRefFeatures(notification);
(_a = notification.features) === null || _a === void 0 ? void 0 : _a.push(...iconRefFeatures);
});
const data = getMocoNotificationsAsFeatureCollection(notifsToRender);
// Apply new data to the source
source.setData(data);
return true;
});
}
}
_MocoLayer_abortController = new WeakMap(), _MocoLayer_graphMapping = new WeakMap();
export default MocoLayer;