From 595ac34d58fce4cbeec210156aae9e2ab3ab2e3f Mon Sep 17 00:00:00 2001 From: Gautier Pelloux-Prayer Date: Wed, 3 May 2017 21:34:01 +0200 Subject: [PATCH] Fix profile/alternative loading --- js/control/RoutingOptions.js | 30 +++++++++++++++++------------- js/index.js | 4 ++-- js/router/BRouter.js | 14 +++++++++----- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/js/control/RoutingOptions.js b/js/control/RoutingOptions.js index 4d6c205..42441ca 100644 --- a/js/control/RoutingOptions.js +++ b/js/control/RoutingOptions.js @@ -17,9 +17,10 @@ BR.RoutingOptions = BR.Control.extend({ return BR.Control.prototype.onAdd.call(this, map); }, - getOptions: function() { + refreshUI: function() { var profile = $('#profile option:selected'), alternative = $('#alternative option:selected'); + $('#stat-profile').html(profile.text() + ' (' + alternative.text() +')'); // we do not allow to select more than one profile and/or alternative at a time @@ -36,8 +37,13 @@ BR.RoutingOptions = BR.Control.extend({ if (custom.value === "Custom") { custom.disabled = true; } - $('.selectpicker').selectpicker('refresh') + }, + + getOptions: function() { + var profile = $('#profile option:selected'), + alternative = $('#alternative option:selected'); + this.refreshUI(); return { profile: profile.val(), @@ -46,21 +52,19 @@ BR.RoutingOptions = BR.Control.extend({ }, setOptions: function(options) { - var profiles_grp, - profile = options.profile; - - if (profile) { - profiles_grp = L.DomUtil.get('profile'); - profiles_grp.value = profile; + var values = [ + options.profile ? options.profile : $('#profile option:selected').val(), + options.alternative ? options.alternative : $('#alternative option:selected').val() + ]; + $('.selectpicker').selectpicker('val', values); + this.refreshUI(); + if (options.profile) { // profile got not selected = not in option values -> custom profile passed with permalink - if (profiles_grp.value != profile) { - this.setCustomProfile(profile, true); + if (L.DomUtil.get('profile').value != options.profile) { + this.setCustomProfile(options.profile, true); } } - if (options.alternative) { - L.DomUtil.get('alternative').value = options.alternative; - } }, setCustomProfile: function(profile, noUpdate) { diff --git a/js/index.js b/js/index.js index 4813fe4..8c6c2a7 100644 --- a/js/index.js +++ b/js/index.js @@ -274,8 +274,8 @@ // do not initialize immediately urlHash = new L.Hash(null, null); urlHash.additionalCb = function() { - var url = router.getUrl(routing.getWaypoints(), null); - return '&' + url.substr('brouter?'.length+1); + var url = router.getUrl(routing.getWaypoints(), null).substr('brouter?'.length+1); + return url.length > 0 ? '&' + url : null; }; urlHash.onHashChangeCb = onHashChangeCb; urlHash.onInvalidHashChangeCb = onInvalidHashChangeCb; diff --git a/js/router/BRouter.js b/js/router/BRouter.js index 78af931..5e55cd5 100644 --- a/js/router/BRouter.js +++ b/js/router/BRouter.js @@ -47,13 +47,17 @@ L.BRouter = L.Class.extend({ if (this.options.profile != null) params.profile = this.options.profile; - // do not put alternative in URL if it has its default value, - // but always set it if we want to generate route because Brouter API requires it. - if (this.options.alternative != 0 || format != null) - params.alternativeidx = this.options.alternative; + params.alternativeidx = this.options.alternative; - if (format != null) + if (format != null) { params.format = format; + } else { + // do not put values in URL if this is the default value (format===null) + if (params.profile === BR.conf.profiles[0]) + delete params.profile; + if (params.alternativeidx == 0) + delete params.alternativeidx; + } return params; },