pmd/docs/js/tabstate.js

44 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-09-19 21:11:44 +02:00
// needed for nav tabs on pages. See Formatting > Nav tabs for more details.
// script from http://stackoverflow.com/questions/10523433/how-do-i-keep-the-current-tab-active-with-twitter-bootstrap-after-a-page-reload
(function() {
var registerTabEvent = function() {
$('a[data-toggle="pill"], a[data-toggle="tab"]').on('shown.bs.tab', function(e) {
var tabId, json, parentId, tabsState;
2022-03-24 15:15:55 +01:00
tabsState = localStorage.getItem('tabs-state');
2020-09-19 21:11:44 +02:00
json = JSON.parse(tabsState || "{}");
2022-03-24 15:15:55 +01:00
parentId = $(e.target).parents('ul.nav.nav-pills, ul.nav.nav-tabs').attr('id');
2020-09-19 21:11:44 +02:00
tabId = $(e.target).attr('id');
json[parentId] = tabId;
2022-03-24 15:15:55 +01:00
return localStorage.setItem('tabs-state', JSON.stringify(json));
2020-09-19 21:11:44 +02:00
});
};
document.addEventListener('DOMContentLoaded', function () {
var json, tabsState;
2022-03-24 15:15:55 +01:00
tabsState = localStorage.getItem('tabs-state');
json = JSON.parse(tabsState || '{}');
2020-09-19 21:11:44 +02:00
$.each(json, function(containerId, tabId) {
2022-03-24 15:15:55 +01:00
if (containerId && containerId !== 'undefined' && tabId && tabId !== 'undefined'
2020-09-19 21:11:44 +02:00
&& tabId.indexOf('#') !== 0) {
2022-03-24 15:15:55 +01:00
$('#' + tabId).tab('show');
2020-09-19 21:11:44 +02:00
}
});
2022-03-24 15:15:55 +01:00
$('ul.nav.nav-pills, ul.nav.nav-tabs').each(function() {
2020-09-19 21:11:44 +02:00
var $this = $(this);
2022-03-24 15:15:55 +01:00
if (!json[$this.attr('id')]) {
return $this.find('a[data-toggle=tab]:first, a[data-toggle=pill]:first').tab('show');
2020-09-19 21:11:44 +02:00
}
});
// cleanup "undefined" entries
delete json['undefined'];
2022-03-24 15:15:55 +01:00
localStorage.setItem('tabs-state', JSON.stringify(json));
2020-09-19 21:11:44 +02:00
registerTabEvent();
});
})();