Use localStorage first, location.hash later.

This commit is contained in:
Cristian Deluxe 2016-12-13 05:45:51 +01:00
parent 5f38fb4015
commit 592034ee28
2 changed files with 30 additions and 13 deletions

View File

@ -17,15 +17,33 @@ $("div#munin_nodeview_tab>div").each(function (index) {
/*
* Update the URL with selected tab and active selected tab on page refresh
*/
$(document).ready(function () {
if (location.hash) {
$('a[href="' + location.hash + '"]').tab('show');
}
$(document.body).on("click", "a[data-toggle=tab]", function (event) {
location.hash = this.getAttribute("href");
(function () {
'use strict';
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
var id = $(this).parents('[role="tablist"]').attr('id');
var key = 'lastTag';
if (id) {
key += ':' + id;
}
localStorage.setItem(key, $(e.target).attr('href'));
location.hash = $(e.target).attr('href');
});
});
$(window).on('popstate', function () {
var anchor = location.hash || $("a[data-toggle=tab]").first().attr("href");
$('a[href="' + anchor + '"]').tab('show');
});
$('[role="tablist"]').each(function (idx, elem) {
var id = $(elem).attr('id');
var key = 'lastTag';
if (id) {
key += ':' + id;
}
var lastTab = localStorage.getItem(key);
if (!lastTab) {
lastTab = location.hash;
}
if (lastTab) {
$('[href="' + lastTab + '"]').tab('show');
}
});
})();

View File

@ -1,2 +1 @@
$("ul#tabs>li>a").each(function(a){a=$(this).attr("href").replace(/[^#\w]/gi,"_");$(this).attr("href",a)});$("div#munin_nodeview_tab>div").each(function(a){a=$(this).attr("id").replace(/[^\w]/gi,"_");$(this).attr("id",a)});$(document).ready(function(){location.hash&&$('a[href="'+location.hash+'"]').tab("show");$(document.body).on("click","a[data-toggle=tab]",function(a){location.hash=this.getAttribute("href")})});
$(window).on("popstate",function(){var a=location.hash||$("a[data-toggle=tab]").first().attr("href");$('a[href="'+a+'"]').tab("show")});
$("ul#tabs>li>a").each(function(index){var eid=$(this).attr("href").replace(/[^#\w]/gi,"_");$(this).attr("href",eid)});$("div#munin_nodeview_tab>div").each(function(index){var eid=$(this).attr("id").replace(/[^\w]/gi,"_");$(this).attr("id",eid)});(function(){"use strict";$('a[data-toggle="tab"]').on("shown.bs.tab",function(e){var id=$(this).parents('[role="tablist"]').attr("id");var key="lastTag";if(id){key+=":"+id}localStorage.setItem(key,$(e.target).attr("href"));location.hash=$(e.target).attr("href")});$('[role="tablist"]').each(function(idx,elem){var id=$(elem).attr("id");var key="lastTag";if(id){key+=":"+id}var lastTab=localStorage.getItem(key);if(!lastTab){lastTab=location.hash}if(lastTab){$('[href="'+lastTab+'"]').tab("show")}})})();