theme: make storage of multiple Hugo sites on same server distinct #214

This commit is contained in:
Sören Weber 2022-03-07 18:26:20 +01:00
parent 73dde63ccd
commit 710713da25
No known key found for this signature in database
GPG key ID: 07D17FF580AE7589
2 changed files with 31 additions and 25 deletions

View file

@ -31,21 +31,21 @@ function switchTab(tabGroup, tabId) {
// Store the selection to make it persistent // Store the selection to make it persistent
if(window.localStorage){ if(window.localStorage){
var selectionsJSON = window.localStorage.getItem("tabSelections"); var selectionsJSON = window.localStorage.getItem(baseUriFull+"tab-selections");
if(selectionsJSON){ if(selectionsJSON){
var tabSelections = JSON.parse(selectionsJSON); var tabSelections = JSON.parse(selectionsJSON);
}else{ }else{
var tabSelections = {}; var tabSelections = {};
} }
tabSelections[tabGroup] = tabId; tabSelections[tabGroup] = tabId;
window.localStorage.setItem("tabSelections", JSON.stringify(tabSelections)); window.localStorage.setItem(baseUriFull+"tab-selections", JSON.stringify(tabSelections));
} }
} }
} }
function restoreTabSelections() { function restoreTabSelections() {
if(window.localStorage){ if(window.localStorage){
var selectionsJSON = window.localStorage.getItem("tabSelections"); var selectionsJSON = window.localStorage.getItem(baseUriFull+"tab-selections");
if(selectionsJSON){ if(selectionsJSON){
var tabSelections = JSON.parse(selectionsJSON); var tabSelections = JSON.parse(selectionsJSON);
}else{ }else{
@ -439,7 +439,11 @@ jQuery(function() {
initSwipeHandler(); initSwipeHandler();
jQuery('[data-clear-history-toggle]').on('click', function() { jQuery('[data-clear-history-toggle]').on('click', function() {
sessionStorage.clear(); for( var item in sessionStorage ){
if( item.substring( 0, baseUriFull.length ) === baseUriFull ){
sessionStorage.removeItem( item );
}
}
location.reload(); location.reload();
return false; return false;
}); });
@ -453,13 +457,13 @@ jQuery(function() {
if (!value.length) { if (!value.length) {
$('ul.topics').removeClass('searched'); $('ul.topics').removeClass('searched');
items.css('display', 'block'); items.css('display', 'block');
sessionStorage.removeItem('search-value'); sessionStorage.removeItem(baseUriFull+'search-value');
$("mark").parents(".expand-marked").removeClass("expand-marked"); $("mark").parents(".expand-marked").removeClass("expand-marked");
$(".highlightable").unhighlight({ element: 'mark' }) $(".highlightable").unhighlight({ element: 'mark' })
return; return;
} }
sessionStorage.setItem('search-value', value); sessionStorage.setItem(baseUriFull+'search-value', value);
$("mark").parents(".expand-marked").removeClass("expand-marked"); $("mark").parents(".expand-marked").removeClass("expand-marked");
$(".highlightable").unhighlight({ element: 'mark' }).highlight(value, { element: 'mark' }); $(".highlightable").unhighlight({ element: 'mark' }).highlight(value, { element: 'mark' });
$("mark").parents(".expand").addClass("expand-marked"); $("mark").parents(".expand").addClass("expand-marked");
@ -468,7 +472,7 @@ jQuery(function() {
jQuery('[data-search-clear]').on('click', function() { jQuery('[data-search-clear]').on('click', function() {
jQuery('[data-search-input]').val('').trigger('input'); jQuery('[data-search-input]').val('').trigger('input');
sessionStorage.removeItem('search-input'); sessionStorage.removeItem(baseUriFull+'search-input');
$("mark").parents(".expand-marked").removeClass("expand-marked"); $("mark").parents(".expand-marked").removeClass("expand-marked");
$(".highlightable").unhighlight({ element: 'mark' }) $(".highlightable").unhighlight({ element: 'mark' })
}); });
@ -480,8 +484,8 @@ jQuery(function() {
}; };
}); });
if (sessionStorage.getItem('search-value')) { if (sessionStorage.getItem(baseUriFull+'search-value')) {
var searchValue = sessionStorage.getItem('search-value') var searchValue = sessionStorage.getItem(baseUriFull+'search-value')
$('[data-search-input]').val(searchValue); $('[data-search-input]').val(searchValue);
$('[data-search-input]').trigger('input'); $('[data-search-input]').trigger('input');
var searchedElem = $('#body-inner').find(':contains(' + searchValue + ')').get(0); var searchedElem = $('#body-inner').find(':contains(' + searchValue + ')').get(0);
@ -494,17 +498,19 @@ jQuery(function() {
} }
} }
$(".highlightable").highlight(sessionStorage.getItem('search-value'), { element: 'mark' }); $(".highlightable").highlight(sessionStorage.getItem(baseUriFull+'search-value'), { element: 'mark' });
$("mark").parents(".expand").addClass("expand-marked"); $("mark").parents(".expand").addClass("expand-marked");
$('#topbar a:not(:has(img)):not(.btn)').addClass('highlight'); $('#topbar a:not(:has(img)):not(.btn)').addClass('highlight');
$('#body-inner a:not(:has(img)):not(.btn):not(a[rel="footnote"])').addClass('highlight'); $('#body-inner a:not(:has(img)):not(.btn):not(a[rel="footnote"])').addClass('highlight');
sessionStorage.setItem(jQuery('body').data('url'), 1); var visitedItem = baseUriFull + 'visited-url/'
sessionStorage.setItem(visitedItem+jQuery('body').data('url'), 1);
// loop through the sessionStorage and see if something should be marked as visited // loop through the sessionStorage and see if something should be marked as visited
for (var url in sessionStorage) { for( var item in sessionStorage ){
if (sessionStorage.getItem(url) == 1){ if( item.substring( 0, visitedItem.length ) === visitedItem && sessionStorage.getItem( item ) == 1 ){
var url = item.substring( visitedItem.length );
// in case we have `relativeURLs=true` we have to strip the // in case we have `relativeURLs=true` we have to strip the
// relative path to root // relative path to root
url = url.replace( /\.\.\//g, '/' ).replace( /^\/+\//, '/' ); url = url.replace( /\.\.\//g, '/' ).replace( /^\/+\//, '/' );

View file

@ -7,7 +7,7 @@ var variants = {
init: function( variants ){ init: function( variants ){
this.variants = variants; this.variants = variants;
var variant = window.localStorage.getItem( 'variant' ) || ( this.variants.length ? this.variants[0] : '' ); var variant = window.localStorage.getItem( baseUriFull+'variant' ) || ( this.variants.length ? this.variants[0] : '' );
this.changeVariant( variant ); this.changeVariant( variant );
document.addEventListener( 'readystatechange', function(){ document.addEventListener( 'readystatechange', function(){
if( document.readyState == 'interactive' ){ if( document.readyState == 'interactive' ){
@ -22,7 +22,7 @@ var variants = {
setVariant: function( variant ){ setVariant: function( variant ){
this.variant = variant; this.variant = variant;
window.localStorage.setItem( 'variant', variant ); window.localStorage.setItem( baseUriFull+'variant', variant );
}, },
markSelectedVariant: function(){ markSelectedVariant: function(){
@ -49,11 +49,11 @@ var variants = {
}, },
addCustomVariantOption: function(){ addCustomVariantOption: function(){
var variantbase = window.localStorage.getItem( 'customvariantbase' ); var variantbase = window.localStorage.getItem( baseUriFull+'customvariantbase' );
if( this.variants.indexOf( variantbase ) < 0 ){ if( this.variants.indexOf( variantbase ) < 0 ){
variantbase = ''; variantbase = '';
} }
if( !window.localStorage.getItem( 'customvariant' ) ){ if( !window.localStorage.getItem( baseUriFull+'customvariant' ) ){
variantbase = ''; variantbase = '';
} }
if( !variantbase ){ if( !variantbase ){
@ -90,15 +90,15 @@ var variants = {
saveCustomVariant: function(){ saveCustomVariant: function(){
if( this.getVariant() != this.customvariantname ){ if( this.getVariant() != this.customvariantname ){
window.localStorage.setItem( 'customvariantbase', this.getVariant() ); window.localStorage.setItem( baseUriFull+'customvariantbase', this.getVariant() );
} }
window.localStorage.setItem( 'customvariant', this.generateStylesheet() ); window.localStorage.setItem( baseUriFull+'customvariant', this.generateStylesheet() );
this.setVariant( this.customvariantname ); this.setVariant( this.customvariantname );
this.markSelectedVariant(); this.markSelectedVariant();
}, },
loadCustomVariant: function(){ loadCustomVariant: function(){
var stylesheet = window.localStorage.getItem( 'customvariant' ); var stylesheet = window.localStorage.getItem( baseUriFull+'customvariant' );
// temp styles to document // temp styles to document
var head = document.querySelector( 'head' ); var head = document.querySelector( 'head' );
@ -125,10 +125,10 @@ var variants = {
}, },
resetVariant: function(){ resetVariant: function(){
var variantbase = window.localStorage.getItem( 'customvariantbase' ); var variantbase = window.localStorage.getItem( baseUriFull+'customvariantbase' );
if( variantbase && confirm( 'You have made changes to your custom variant. Are you sure you want to reset all changes?' ) ){ if( variantbase && confirm( 'You have made changes to your custom variant. Are you sure you want to reset all changes?' ) ){
window.localStorage.removeItem( 'customvariantbase' ); window.localStorage.removeItem( baseUriFull+'customvariantbase' );
window.localStorage.removeItem( 'customvariant' ); window.localStorage.removeItem( baseUriFull+'customvariant' );
this.removeCustomVariantOption(); this.removeCustomVariantOption();
if( this.getVariant() == this.customvariantname ){ if( this.getVariant() == this.customvariantname ){
this.changeVariant( variantbase ); this.changeVariant( variantbase );
@ -148,11 +148,11 @@ var variants = {
changeVariant: function( variant ){ changeVariant: function( variant ){
if( variant == this.customvariantname ){ if( variant == this.customvariantname ){
var variantbase = window.localStorage.getItem( 'customvariantbase' ); var variantbase = window.localStorage.getItem( baseUriFull+'customvariantbase' );
if( this.variants.indexOf( variantbase ) < 0 ){ if( this.variants.indexOf( variantbase ) < 0 ){
variant = ''; variant = '';
} }
if( !window.localStorage.getItem( 'customvariant' ) ){ if( !window.localStorage.getItem( baseUriFull+'customvariant' ) ){
variant = ''; variant = '';
} }
this.setVariant( variant ); this.setVariant( variant );