From 55c9dabee4436b89324e68c8e3888cbc899cdd58 Mon Sep 17 00:00:00 2001 From: Jorran de Wit <jorrandewit@outlook.com> Date: Wed, 16 Aug 2017 16:31:48 +0200 Subject: [PATCH] Back and forth navigation with personal page tabs --- scipost/static/scipost/assets/js/scripts.js | 31 +++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/scipost/static/scipost/assets/js/scripts.js b/scipost/static/scipost/assets/js/scripts.js index ff2b6d631..1c7ac5005 100644 --- a/scipost/static/scipost/assets/js/scripts.js +++ b/scipost/static/scipost/assets/js/scripts.js @@ -2,12 +2,39 @@ function hide_all_alerts() { $(".alert").fadeOut(300); } +var getUrlParameter = function getUrlParameter(sParam) { + var sPageURL = decodeURIComponent(window.location.search.substring(1)), + sURLVariables = sPageURL.split('&'), + sParameterName, + i; + + for (i = 0; i < sURLVariables.length; i++) { + sParameterName = sURLVariables[i].split('='); + + if (sParameterName[0] === sParam) { + return sParameterName[1] === undefined ? true : sParameterName[1]; + } + } +}; + $(function(){ - // Remove all alerts in screen automatically after 10sec. - setTimeout(function() {hide_all_alerts()}, 10000); + // Remove all alerts in screen automatically after 15sec. + setTimeout(function() {hide_all_alerts()}, 15000); // Start general toggle $('[data-toggle="toggle"]').on('click', function() { $($(this).attr('data-target')).toggle(); }); + + // Show right tab if url contains `tab` GET request + var tab = getUrlParameter('tab') + if (tab) { + $('a[href="#' + tab + '"][data-toggle="tab"]').tab('show'); + } + + // Change `tab` GET parameter for page-reload + $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) { + var tab_name = e.target.hash.substring(1) + window.history.replaceState({}, null, '?tab=' + tab_name); + }); }); -- GitLab