2024-02-06 22:58:49 +01:00
|
|
|
import omit from 'lodash/omit.js';
|
|
|
|
import {createFindInTree} from '../lib/find-in-tree.js';
|
2020-04-08 09:55:15 -07:00
|
|
|
|
|
|
|
const findInTree = createFindInTree([
|
2024-02-06 22:58:49 +01:00
|
|
|
'**.oprX',
|
|
|
|
'**.icoX',
|
|
|
|
'**.prodX',
|
|
|
|
'**.pRefL',
|
|
|
|
'**.locX',
|
|
|
|
'**.ani.fLocX',
|
|
|
|
'**.ani.tLocX',
|
|
|
|
'**.fLocX',
|
|
|
|
'**.tLocX',
|
|
|
|
'**.remX',
|
|
|
|
'**.himX',
|
|
|
|
'**.polyG.polyXL',
|
|
|
|
'**.rRefL',
|
|
|
|
'**.msgL',
|
|
|
|
'**.remL',
|
|
|
|
]);
|
2019-08-23 15:59:38 +02:00
|
|
|
|
2021-01-26 22:12:09 +01:00
|
|
|
// there are circular references (e.g. warning -> loc -> warning)
|
|
|
|
// todo: parse either on-the-fly or in a recursive/iterative process
|
2019-10-20 00:19:11 +02:00
|
|
|
const parseCommonData = (_ctx) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
const {profile, opt, res} = _ctx;
|
|
|
|
const c = res.common || {};
|
|
|
|
const matches = findInTree(res);
|
2018-10-27 14:47:19 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
const common = {};
|
|
|
|
const ctx = {..._ctx, common};
|
2019-10-20 00:19:11 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
common.operators = [];
|
2018-10-27 14:47:19 +02:00
|
|
|
if (Array.isArray(c.opL)) {
|
2024-02-06 22:58:49 +01:00
|
|
|
common.operators = c.opL.map(op => profile.parseOperator(ctx, op));
|
2020-02-22 11:13:56 -08:00
|
|
|
matches['**.oprX'].forEach(([idx, parents]) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
if ('number' === typeof idx) {
|
|
|
|
parents[0].operator = common.operators[idx];
|
|
|
|
}
|
|
|
|
});
|
2018-10-27 14:47:19 +02:00
|
|
|
}
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
common.icons = [];
|
2019-08-23 16:02:34 +02:00
|
|
|
if (Array.isArray(c.icoL)) {
|
2024-02-06 22:58:49 +01:00
|
|
|
common.icons = c.icoL.map(icon => profile.parseIcon(ctx, icon));
|
2020-02-22 11:13:56 -08:00
|
|
|
matches['**.icoX'].forEach(([idx, parents]) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
if ('number' === typeof idx) {
|
|
|
|
parents[0].icon = common.icons[idx];
|
|
|
|
}
|
|
|
|
});
|
2019-08-23 16:02:34 +02:00
|
|
|
}
|
2018-10-27 14:47:19 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
common.lines = [];
|
2018-10-27 14:47:19 +02:00
|
|
|
if (Array.isArray(c.prodL)) {
|
2024-02-06 22:58:49 +01:00
|
|
|
common.lines = c.prodL.map(l => profile.parseLine(ctx, l));
|
2019-08-23 16:06:21 +02:00
|
|
|
|
2020-02-22 11:13:56 -08:00
|
|
|
matches['**.prodX'].forEach(([idx, parents]) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
if ('number' === typeof idx) {
|
|
|
|
parents[0].line = common.lines[idx];
|
|
|
|
}
|
|
|
|
});
|
2020-02-22 11:13:56 -08:00
|
|
|
matches['**.pRefL'].forEach(([idxs, parents]) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
parents[0].lines = idxs.filter(idx => Boolean(common.lines[idx]))
|
|
|
|
.map(idx => common.lines[idx]);
|
|
|
|
});
|
2019-08-23 16:06:21 +02:00
|
|
|
// todo
|
2019-10-20 00:19:11 +02:00
|
|
|
// **.dep.dProdX: departureLine -> common.lines[idx]
|
|
|
|
// **.arr.aProdX: arrivalLine -> common.lines[idx]
|
2018-10-27 14:47:19 +02:00
|
|
|
}
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
common.hints = [];
|
2020-02-15 19:09:05 +00:00
|
|
|
if (Array.isArray(c.remL)) {
|
2024-02-06 22:58:49 +01:00
|
|
|
common.hints = c.remL.map(hint => profile.parseHint(ctx, hint));
|
2020-02-15 19:09:05 +00:00
|
|
|
matches['**.remX'].forEach(([idx, parents]) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
if ('number' === typeof idx) {
|
|
|
|
parents[0].hint = common.hints[idx];
|
|
|
|
}
|
|
|
|
});
|
2021-01-26 21:04:10 +01:00
|
|
|
matches['**.remL'].forEach(([idxs, parents]) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
if (!Array.isArray(idxs)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-01-26 21:04:10 +01:00
|
|
|
parents[0].hints = idxs
|
2024-02-06 22:58:49 +01:00
|
|
|
.filter(idx => Boolean(common.hints[idx]))
|
|
|
|
.map(idx => common.hints[idx]);
|
|
|
|
});
|
2020-02-15 19:09:05 +00:00
|
|
|
matches['**.rRefL'].forEach(([idxs, parents]) => {
|
|
|
|
parents[0].hints = idxs
|
2024-02-06 22:58:49 +01:00
|
|
|
.filter(idx => Boolean(common.hints[idx]))
|
|
|
|
.map(idx => common.hints[idx]);
|
|
|
|
});
|
2020-02-15 19:09:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 21:02:50 +01:00
|
|
|
// resolve .msgL[] references
|
|
|
|
// todo: `himMsgEdgeL[].msgRefL[]` look different, it seems they only reference
|
|
|
|
// `common.himL[]` items?
|
|
|
|
const parseRemarkRef = (ref) => {
|
|
|
|
if (ref.type === 'REM' && ref.hint) {
|
2024-02-06 22:58:49 +01:00
|
|
|
return omit(ref, ['type', 'remX']);
|
2021-01-26 21:02:50 +01:00
|
|
|
}
|
|
|
|
if (ref.type === 'HIM' && ref.warning) {
|
2024-02-06 22:58:49 +01:00
|
|
|
return omit(ref, ['type', 'himX']);
|
2021-01-26 21:02:50 +01:00
|
|
|
}
|
2024-02-06 22:58:49 +01:00
|
|
|
return null;
|
|
|
|
};
|
2021-01-26 21:02:50 +01:00
|
|
|
matches['**.msgL'].forEach(([refs, parents]) => {
|
|
|
|
// todo: store as parents[0].(hints|warnings)
|
|
|
|
parents[0].remarkRefs = refs
|
2024-02-06 22:58:49 +01:00
|
|
|
.map(parseRemarkRef)
|
|
|
|
.filter(ref => ref !== null);
|
|
|
|
});
|
2021-01-26 21:02:50 +01:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
common.locations = [];
|
2018-10-27 14:47:19 +02:00
|
|
|
if (Array.isArray(c.locL)) {
|
2024-02-06 22:58:49 +01:00
|
|
|
common.locations = c.locL.map(loc => profile.parseLocation(ctx, loc));
|
2018-10-27 14:47:19 +02:00
|
|
|
|
2019-10-20 00:19:11 +02:00
|
|
|
for (let i = 0; i < common.locations.length; i++) {
|
2024-02-06 22:58:49 +01:00
|
|
|
const raw = c.locL[i];
|
|
|
|
const loc = common.locations[i];
|
2018-10-27 14:47:19 +02:00
|
|
|
if ('number' === typeof raw.mMastLocX) {
|
2024-02-06 22:58:49 +01:00
|
|
|
loc.station = Object.assign({}, common.locations[raw.mMastLocX]);
|
|
|
|
loc.station.type = 'station';
|
|
|
|
} else if (raw.isMainMast) {
|
|
|
|
loc.type = 'station';
|
|
|
|
}
|
2018-10-27 14:47:19 +02:00
|
|
|
}
|
2019-08-23 16:17:20 +02:00
|
|
|
|
|
|
|
// todo: correct props?
|
2020-02-22 11:13:56 -08:00
|
|
|
matches['**.locX'].forEach(([idx, parents]) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
if ('number' === typeof idx) {
|
|
|
|
parents[0].location = common.locations[idx];
|
|
|
|
}
|
|
|
|
});
|
2020-02-22 11:13:56 -08:00
|
|
|
matches['**.ani.fLocX'].forEach(([idxs, parents]) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
parents[0].fromLocations = idxs.map(idx => common.locations[idx]);
|
|
|
|
});
|
2020-02-22 11:13:56 -08:00
|
|
|
matches['**.ani.tLocX'].forEach(([idxs, parents]) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
parents[0].toLocations = idxs.map(idx => common.locations[idx]);
|
|
|
|
});
|
2020-02-22 11:13:56 -08:00
|
|
|
matches['**.fLocX'].forEach(([idx, parents]) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
if ('number' === typeof idx) {
|
|
|
|
parents[0].fromLocation = common.locations[idx];
|
|
|
|
}
|
|
|
|
});
|
2020-02-22 11:13:56 -08:00
|
|
|
matches['**.tLocX'].forEach(([idx, parents]) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
if ('number' === typeof idx) {
|
|
|
|
parents[0].toLocation = common.locations[idx];
|
|
|
|
}
|
|
|
|
});
|
2018-10-27 14:47:19 +02:00
|
|
|
}
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
common.warnings = [];
|
2020-03-07 00:31:25 +01:00
|
|
|
if (Array.isArray(c.himL)) {
|
2024-02-06 22:58:49 +01:00
|
|
|
common.warnings = c.himL.map(w => profile.parseWarning(ctx, w));
|
2020-02-22 11:13:56 -08:00
|
|
|
matches['**.himX'].forEach(([idx, parents]) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
if ('number' === typeof idx) {
|
|
|
|
parents[0].warning = common.warnings[idx];
|
|
|
|
}
|
|
|
|
});
|
2019-02-25 15:03:33 +01:00
|
|
|
}
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
common.polylines = [];
|
2020-06-11 16:12:33 +02:00
|
|
|
if ((opt.polylines || opt.polyline) && Array.isArray(c.polyL)) {
|
2024-02-06 22:58:49 +01:00
|
|
|
common.polylines = c.polyL.map(p => profile.parsePolyline(ctx, p));
|
2019-08-23 15:39:42 +02:00
|
|
|
// todo: **.ani.poly -> parsePolyline()
|
|
|
|
|
2020-02-22 11:13:56 -08:00
|
|
|
matches['**.polyG.polyXL'].forEach(([idxs, parents]) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
const idx = idxs.find(idx => Boolean(common.polylines[idx])); // find any given polyline
|
|
|
|
parents[1].polyline = common.polylines[idx];
|
|
|
|
});
|
2019-08-23 15:39:42 +02:00
|
|
|
}
|
2018-10-27 14:47:19 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
return common;
|
|
|
|
};
|
2018-10-27 14:47:19 +02:00
|
|
|
|
2022-05-07 16:17:37 +02:00
|
|
|
export {
|
|
|
|
parseCommonData,
|
2024-02-06 22:58:49 +01:00
|
|
|
};
|