diff --git a/js/control/Profile.js b/js/control/Profile.js index 8de56bc..4238577 100644 --- a/js/control/Profile.js +++ b/js/control/Profile.js @@ -38,11 +38,12 @@ BR.Profile = L.Evented.extend({ button.blur(); }, - update: function (options) { + update: function (options, cb) { var profileName = options.profile, profileUrl, empty = !this.editor.getValue(), - clean = this.editor.isClean(); + clean = this.editor.isClean(), + loading = false; if (profileName && BR.conf.profilesUrl) { // 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; if (!(profileName in this.cache)) { profileUrl = BR.conf.profilesUrl + profileName + '.brf'; + loading = true; BR.Util.get( profileUrl, L.bind(function (err, profileText) { if (err) { console.warn('Error getting profile from "' + profileUrl + '": ' + err); + if (cb) cb(); return; } @@ -65,6 +68,7 @@ BR.Profile = L.Evented.extend({ if (!this.profileName || this.profileName === profileName) { this._setValue(profileText); } + if (cb) cb(); }, this) ); } else { @@ -80,6 +84,8 @@ BR.Profile = L.Evented.extend({ } } } + + if (cb && !loading) cb(); }, show: function () { diff --git a/js/index.js b/js/index.js index 5299db6..ae235c9 100644 --- a/js/index.js +++ b/js/index.js @@ -226,9 +226,12 @@ } routingOptions = new BR.RoutingOptions(); - routingOptions.on('update', updateRoute); 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', { @@ -290,7 +293,7 @@ routingPathQuality = new BR.RoutingPathQuality(map, layersControl); - routing = new BR.Routing({ + routing = new BR.Routing(profile, { routing: { router: L.bind(router.getRouteSegment, router), }, @@ -399,11 +402,12 @@ // initial option settings (after controls are added and initialized with onAdd) router.setOptions(nogos.getOptions()); router.setOptions(routingOptions.getOptions()); - profile.update(routingOptions.getOptions()); - // restore active layers from local storage when called without hash // (check before hash plugin init) if (!location.hash) { + profile.update(routingOptions.getOptions()); + + // restore active layers from local storage when called without hash layersControl.loadActiveLayers(); } @@ -427,13 +431,16 @@ router.setOptions(opts); routingOptions.setOptions(opts); nogos.setOptions(opts); - profile.update(opts); - if (opts.lonlats) { - routing.draw(false); - routing.clear(); - routing.setWaypoints(opts.lonlats, opts.beelineFlags); - } + const optsOrDefault = Object.assign({}, routingOptions.getOptions(), opts); + profile.update(optsOrDefault, () => { + if (opts.lonlats) { + routing.draw(false); + routing.clear(); + routing.setWaypoints(opts.lonlats, opts.beelineFlags); + } + }); + if (opts.pois) { pois.setMarkers(opts.pois); } diff --git a/js/plugin/Routing.js b/js/plugin/Routing.js index 0acc4eb..648ecf9 100644 --- a/js/plugin/Routing.js +++ b/js/plugin/Routing.js @@ -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) { this.options.tooltips.waypoint = i18next.t('map.route-tooltip-waypoint'); this.options.tooltips.segment = i18next.t('map.route-tooltip-segment'); @@ -484,7 +490,7 @@ BR.Routing = L.Routing.extend({ }, _computeKinematic: function (distance, deltaHeight) { - const rc = new BR.RoutingContext(); + const rc = new BR.RoutingContext(this.profile); const stdPath = new BR.StdPath(); stdPath.computeKinematic(rc, distance, deltaHeight, true); diff --git a/js/util/StdPath.js b/js/util/StdPath.js index d24e487..3e66269 100644 --- a/js/util/StdPath.js +++ b/js/util/StdPath.js @@ -11,18 +11,30 @@ } class BExpressionContext { + constructor(profile) { + this.profile = profile; + } + 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 class RoutingContext { - constructor() { - this.expctxGlobal = new BExpressionContext(); + constructor(profile) { + this.expctxGlobal = new BExpressionContext(profile); 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.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); diff --git a/tests/util/StdPath.test.js b/tests/util/StdPath.test.js index 9673a5c..5561fe3 100644 --- a/tests/util/StdPath.test.js +++ b/tests/util/StdPath.test.js @@ -7,7 +7,13 @@ const geoJson = require('../format/data/track.json'); test('simple track', () => { const coordinates = geoJson.features[0].geometry.coordinates; 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(); for (let i = 0; i < coordinates.length; i++) {