Skip to content

Commit

Permalink
theme: make storage of multiple Hugo sites on same server distinct ma…
Browse files Browse the repository at this point in the history
  • Loading branch information
McShelby committed Mar 7, 2022
1 parent 73dde63 commit 710713d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
32 changes: 19 additions & 13 deletions static/js/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ function switchTab(tabGroup, tabId) {

// Store the selection to make it persistent
if(window.localStorage){
var selectionsJSON = window.localStorage.getItem("tabSelections");
var selectionsJSON = window.localStorage.getItem(baseUriFull+"tab-selections");
if(selectionsJSON){
var tabSelections = JSON.parse(selectionsJSON);
}else{
var tabSelections = {};
}
tabSelections[tabGroup] = tabId;
window.localStorage.setItem("tabSelections", JSON.stringify(tabSelections));
window.localStorage.setItem(baseUriFull+"tab-selections", JSON.stringify(tabSelections));
}
}
}

function restoreTabSelections() {
if(window.localStorage){
var selectionsJSON = window.localStorage.getItem("tabSelections");
var selectionsJSON = window.localStorage.getItem(baseUriFull+"tab-selections");
if(selectionsJSON){
var tabSelections = JSON.parse(selectionsJSON);
}else{
Expand Down Expand Up @@ -439,7 +439,11 @@ jQuery(function() {
initSwipeHandler();

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();
return false;
});
Expand All @@ -453,13 +457,13 @@ jQuery(function() {
if (!value.length) {
$('ul.topics').removeClass('searched');
items.css('display', 'block');
sessionStorage.removeItem('search-value');
sessionStorage.removeItem(baseUriFull+'search-value');
$("mark").parents(".expand-marked").removeClass("expand-marked");
$(".highlightable").unhighlight({ element: 'mark' })
return;
}

sessionStorage.setItem('search-value', value);
sessionStorage.setItem(baseUriFull+'search-value', value);
$("mark").parents(".expand-marked").removeClass("expand-marked");
$(".highlightable").unhighlight({ element: 'mark' }).highlight(value, { element: 'mark' });
$("mark").parents(".expand").addClass("expand-marked");
Expand All @@ -468,7 +472,7 @@ jQuery(function() {

jQuery('[data-search-clear]').on('click', function() {
jQuery('[data-search-input]').val('').trigger('input');
sessionStorage.removeItem('search-input');
sessionStorage.removeItem(baseUriFull+'search-input');
$("mark").parents(".expand-marked").removeClass("expand-marked");
$(".highlightable").unhighlight({ element: 'mark' })
});
Expand All @@ -480,8 +484,8 @@ jQuery(function() {
};
});

if (sessionStorage.getItem('search-value')) {
var searchValue = sessionStorage.getItem('search-value')
if (sessionStorage.getItem(baseUriFull+'search-value')) {
var searchValue = sessionStorage.getItem(baseUriFull+'search-value')
$('[data-search-input]').val(searchValue);
$('[data-search-input]').trigger('input');
var searchedElem = $('#body-inner').find(':contains(' + searchValue + ')').get(0);
Expand All @@ -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");

$('#topbar a:not(:has(img)):not(.btn)').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
for (var url in sessionStorage) {
if (sessionStorage.getItem(url) == 1){
for( var item in sessionStorage ){
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
// relative path to root
url = url.replace( /\.\.\//g, '/' ).replace( /^\/+\//, '/' );
Expand Down
24 changes: 12 additions & 12 deletions static/js/variant.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var variants = {

init: function( 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 );
document.addEventListener( 'readystatechange', function(){
if( document.readyState == 'interactive' ){
Expand All @@ -22,7 +22,7 @@ var variants = {

setVariant: function( variant ){
this.variant = variant;
window.localStorage.setItem( 'variant', variant );
window.localStorage.setItem( baseUriFull+'variant', variant );
},

markSelectedVariant: function(){
Expand All @@ -49,11 +49,11 @@ var variants = {
},

addCustomVariantOption: function(){
var variantbase = window.localStorage.getItem( 'customvariantbase' );
var variantbase = window.localStorage.getItem( baseUriFull+'customvariantbase' );
if( this.variants.indexOf( variantbase ) < 0 ){
variantbase = '';
}
if( !window.localStorage.getItem( 'customvariant' ) ){
if( !window.localStorage.getItem( baseUriFull+'customvariant' ) ){
variantbase = '';
}
if( !variantbase ){
Expand Down Expand Up @@ -90,15 +90,15 @@ var variants = {

saveCustomVariant: function(){
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.markSelectedVariant();
},

loadCustomVariant: function(){
var stylesheet = window.localStorage.getItem( 'customvariant' );
var stylesheet = window.localStorage.getItem( baseUriFull+'customvariant' );

// temp styles to document
var head = document.querySelector( 'head' );
Expand All @@ -125,10 +125,10 @@ var variants = {
},

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?' ) ){
window.localStorage.removeItem( 'customvariantbase' );
window.localStorage.removeItem( 'customvariant' );
window.localStorage.removeItem( baseUriFull+'customvariantbase' );
window.localStorage.removeItem( baseUriFull+'customvariant' );
this.removeCustomVariantOption();
if( this.getVariant() == this.customvariantname ){
this.changeVariant( variantbase );
Expand All @@ -148,11 +148,11 @@ var variants = {

changeVariant: function( variant ){
if( variant == this.customvariantname ){
var variantbase = window.localStorage.getItem( 'customvariantbase' );
var variantbase = window.localStorage.getItem( baseUriFull+'customvariantbase' );
if( this.variants.indexOf( variantbase ) < 0 ){
variant = '';
}
if( !window.localStorage.getItem( 'customvariant' ) ){
if( !window.localStorage.getItem( baseUriFull+'customvariant' ) ){
variant = '';
}
this.setVariant( variant );
Expand Down

0 comments on commit 710713d

Please sign in to comment.