From 592034ee28d11b6723e7b1c4413eb04d0f5b76f7 Mon Sep 17 00:00:00 2001 From: Cristian Deluxe Date: Tue, 13 Dec 2016 05:45:51 +0100 Subject: [PATCH] Use localStorage first, location.hash later. --- templates/munstrap/static/js/munstrap.js | 40 ++++++++++++++------ templates/munstrap/static/js/munstrap.min.js | 3 +- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/templates/munstrap/static/js/munstrap.js b/templates/munstrap/static/js/munstrap.js index 66c18b7a..21f63c61 100644 --- a/templates/munstrap/static/js/munstrap.js +++ b/templates/munstrap/static/js/munstrap.js @@ -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'); -}); \ No newline at end of file + + $('[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'); + } + }); +})(); \ No newline at end of file diff --git a/templates/munstrap/static/js/munstrap.min.js b/templates/munstrap/static/js/munstrap.min.js index 142b24ff..eff493b2 100644 --- a/templates/munstrap/static/js/munstrap.min.js +++ b/templates/munstrap/static/js/munstrap.min.js @@ -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")}})})(); \ No newline at end of file