Read time/energy calc variables from profile

and ensure profile text is loaded before updating route and straight line stats
This commit is contained in:
Norbert Renner 2022-02-15 19:30:49 +01:00
parent 9abf4b94c4
commit 4d44153316
5 changed files with 57 additions and 20 deletions

View file

@ -38,11 +38,12 @@ BR.Profile = L.Evented.extend({
button.blur(); button.blur();
}, },
update: function (options) { update: function (options, cb) {
var profileName = options.profile, var profileName = options.profile,
profileUrl, profileUrl,
empty = !this.editor.getValue(), empty = !this.editor.getValue(),
clean = this.editor.isClean(); clean = this.editor.isClean(),
loading = false;
if (profileName && BR.conf.profilesUrl) { if (profileName && BR.conf.profilesUrl) {
// only synchronize profile editor/parameters with selection if no manual changes in full editor, // only synchronize profile editor/parameters with selection if no manual changes in full editor,
@ -51,11 +52,13 @@ BR.Profile = L.Evented.extend({
this.profileName = profileName; this.profileName = profileName;
if (!(profileName in this.cache)) { if (!(profileName in this.cache)) {
profileUrl = BR.conf.profilesUrl + profileName + '.brf'; profileUrl = BR.conf.profilesUrl + profileName + '.brf';
loading = true;
BR.Util.get( BR.Util.get(
profileUrl, profileUrl,
L.bind(function (err, profileText) { L.bind(function (err, profileText) {
if (err) { if (err) {
console.warn('Error getting profile from "' + profileUrl + '": ' + err); console.warn('Error getting profile from "' + profileUrl + '": ' + err);
if (cb) cb();
return; return;
} }
@ -65,6 +68,7 @@ BR.Profile = L.Evented.extend({
if (!this.profileName || this.profileName === profileName) { if (!this.profileName || this.profileName === profileName) {
this._setValue(profileText); this._setValue(profileText);
} }
if (cb) cb();
}, this) }, this)
); );
} else { } else {
@ -80,6 +84,8 @@ BR.Profile = L.Evented.extend({
} }
} }
} }
if (cb && !loading) cb();
}, },
show: function () { show: function () {

View file

@ -226,9 +226,12 @@
} }
routingOptions = new BR.RoutingOptions(); routingOptions = new BR.RoutingOptions();
routingOptions.on('update', updateRoute);
routingOptions.on('update', function (evt) { routingOptions.on('update', function (evt) {
profile.update(evt.options); if (urlHash.movingMap) return;
profile.update(evt.options, () => {
updateRoute(evt);
});
}); });
BR.NogoAreas.MSG_BUTTON = i18next.t('keyboard.generic-shortcut', { BR.NogoAreas.MSG_BUTTON = i18next.t('keyboard.generic-shortcut', {
@ -290,7 +293,7 @@
routingPathQuality = new BR.RoutingPathQuality(map, layersControl); routingPathQuality = new BR.RoutingPathQuality(map, layersControl);
routing = new BR.Routing({ routing = new BR.Routing(profile, {
routing: { routing: {
router: L.bind(router.getRouteSegment, router), router: L.bind(router.getRouteSegment, router),
}, },
@ -399,11 +402,12 @@
// initial option settings (after controls are added and initialized with onAdd) // initial option settings (after controls are added and initialized with onAdd)
router.setOptions(nogos.getOptions()); router.setOptions(nogos.getOptions());
router.setOptions(routingOptions.getOptions()); router.setOptions(routingOptions.getOptions());
profile.update(routingOptions.getOptions());
// restore active layers from local storage when called without hash
// (check before hash plugin init) // (check before hash plugin init)
if (!location.hash) { if (!location.hash) {
profile.update(routingOptions.getOptions());
// restore active layers from local storage when called without hash
layersControl.loadActiveLayers(); layersControl.loadActiveLayers();
} }
@ -427,13 +431,16 @@
router.setOptions(opts); router.setOptions(opts);
routingOptions.setOptions(opts); routingOptions.setOptions(opts);
nogos.setOptions(opts); nogos.setOptions(opts);
profile.update(opts);
if (opts.lonlats) { const optsOrDefault = Object.assign({}, routingOptions.getOptions(), opts);
routing.draw(false); profile.update(optsOrDefault, () => {
routing.clear(); if (opts.lonlats) {
routing.setWaypoints(opts.lonlats, opts.beelineFlags); routing.draw(false);
} routing.clear();
routing.setWaypoints(opts.lonlats, opts.beelineFlags);
}
});
if (opts.pois) { if (opts.pois) {
pois.setMarkers(opts.pois); pois.setMarkers(opts.pois);
} }

View file

@ -39,6 +39,12 @@ BR.Routing = L.Routing.extend({
}, },
}, },
initialize: function (profile, options) {
L.Routing.prototype.initialize.call(this, options);
this.profile = profile;
},
onAdd: function (map) { onAdd: function (map) {
this.options.tooltips.waypoint = i18next.t('map.route-tooltip-waypoint'); this.options.tooltips.waypoint = i18next.t('map.route-tooltip-waypoint');
this.options.tooltips.segment = i18next.t('map.route-tooltip-segment'); this.options.tooltips.segment = i18next.t('map.route-tooltip-segment');
@ -484,7 +490,7 @@ BR.Routing = L.Routing.extend({
}, },
_computeKinematic: function (distance, deltaHeight) { _computeKinematic: function (distance, deltaHeight) {
const rc = new BR.RoutingContext(); const rc = new BR.RoutingContext(this.profile);
const stdPath = new BR.StdPath(); const stdPath = new BR.StdPath();
stdPath.computeKinematic(rc, distance, deltaHeight, true); stdPath.computeKinematic(rc, distance, deltaHeight, true);

View file

@ -11,18 +11,30 @@
} }
class BExpressionContext { class BExpressionContext {
constructor(profile) {
this.profile = profile;
}
getVariableValue(name, defaultValue) { getVariableValue(name, defaultValue) {
return defaultValue; let value = this.profile?.getProfileVar(name) ?? defaultValue;
if (value === 'true') {
value = 1;
} else if (value === 'false') {
value = 0;
}
return +value;
} }
} }
// from BRouter btools.router.RoutingContext // from BRouter btools.router.RoutingContext
class RoutingContext { class RoutingContext {
constructor() { constructor(profile) {
this.expctxGlobal = new BExpressionContext(); this.expctxGlobal = new BExpressionContext(profile);
this.expctxWay = new BExpressionContextWay(); this.expctxWay = new BExpressionContextWay();
this.bikeMode = true;
this.footMode = false; this.bikeMode = 0 !== this.expctxGlobal.getVariableValue('validForBikes', 0);
this.footMode = 0 !== this.expctxGlobal.getVariableValue('validForFoot', 0);
this.totalMass = this.expctxGlobal.getVariableValue('totalMass', 90.0); this.totalMass = this.expctxGlobal.getVariableValue('totalMass', 90.0);
this.maxSpeed = this.expctxGlobal.getVariableValue('maxSpeed', this.footMode ? 6.0 : 45.0) / 3.6; this.maxSpeed = this.expctxGlobal.getVariableValue('maxSpeed', this.footMode ? 6.0 : 45.0) / 3.6;
this.S_C_x = this.expctxGlobal.getVariableValue('S_C_x', 0.5 * 0.45); this.S_C_x = this.expctxGlobal.getVariableValue('S_C_x', 0.5 * 0.45);

View file

@ -7,7 +7,13 @@ const geoJson = require('../format/data/track.json');
test('simple track', () => { test('simple track', () => {
const coordinates = geoJson.features[0].geometry.coordinates; const coordinates = geoJson.features[0].geometry.coordinates;
const properties = geoJson.features[0].properties; const properties = geoJson.features[0].properties;
const rc = new BR.RoutingContext(); const dummyProfileVars = {
getProfileVar(name) {
const vars = { validForBikes: 1 };
return vars[name];
},
};
const rc = new BR.RoutingContext(dummyProfileVars);
const stdPath = new BR.StdPath(); const stdPath = new BR.StdPath();
for (let i = 0; i < coordinates.length; i++) { for (let i = 0; i < coordinates.length; i++) {