remember and recall last used (non-custom) profile

This commit is contained in:
Marcus Jaschen 2021-11-14 10:29:13 +01:00 committed by Gautier P
parent 05ff4bd430
commit f1557a1d5c

View file

@ -8,6 +8,9 @@ BR.RoutingOptions = L.Evented.extend({
initialize: function () { initialize: function () {
$('#profile-alternative').on('changed.bs.select', this._getChangeHandler()); $('#profile-alternative').on('changed.bs.select', this._getChangeHandler());
var remembered_profile = this.getRememberedProfile();
var remembered_profile_was_selected = false;
// build option list from config // build option list from config
var profiles = BR.conf.profiles; var profiles = BR.conf.profiles;
var profiles_list = L.DomUtil.get('profile'); var profiles_list = L.DomUtil.get('profile');
@ -15,12 +18,18 @@ BR.RoutingOptions = L.Evented.extend({
var option = document.createElement('option'); var option = document.createElement('option');
option.value = profiles[i]; option.value = profiles[i];
option.text = i18next.t('navbar.profile.' + profiles[i]); option.text = i18next.t('navbar.profile.' + profiles[i]);
if (remembered_profile !== null && remembered_profile === profiles[i]) {
option.selected = true;
remembered_profile_was_selected = true;
}
profiles_list.appendChild(option); profiles_list.appendChild(option);
} }
// set default value, used as indicator for empty custom profile // set default value, used as indicator for empty custom profile
profiles_list.children[0].value = 'Custom'; profiles_list.children[0].value = 'Custom';
if (!remembered_profile_was_selected) {
// <custom> profile is empty at start, select next one // <custom> profile is empty at start, select next one
profiles_list.children[1].selected = true; profiles_list.children[1].selected = true;
}
L.DomEvent.addListener(document, 'keydown', this._keydownListener, this); L.DomEvent.addListener(document, 'keydown', this._keydownListener, this);
}, },
@ -113,8 +122,29 @@ BR.RoutingOptions = L.Evented.extend({
return profile; return profile;
}, },
rememberProfile: function (profile) {
if (!BR.Util.localStorageAvailable()) {
return;
}
if (L.BRouter.isCustomProfile(profile)) {
return;
}
localStorage.setItem('routingprofile', profile);
},
getRememberedProfile: function () {
if (!BR.Util.localStorageAvailable()) {
return null;
}
return localStorage.getItem('routingprofile');
},
_getChangeHandler: function () { _getChangeHandler: function () {
return L.bind(function (evt) { return L.bind(function (evt) {
this.rememberProfile(evt.target.options[evt.target.options.selectedIndex].value);
this.fire('update', { options: this.getOptions() }); this.fire('update', { options: this.getOptions() });
}, this); }, this);
}, },