theme: fix JS errors on hugo --minifiy #724

This commit is contained in:
Sören Weber 2023-11-16 23:53:06 +01:00
parent 682e6b6119
commit fcb255caf7
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D
3 changed files with 41 additions and 38 deletions

View file

@ -42,21 +42,22 @@
{{- end }} {{- end }}
var root_url="/"; var root_url="/";
var baseUri=root_url.replace(/\/$/, ''); var baseUri=root_url.replace(/\/$/, '');
{{ "// translations" | safeJS }} window.relearn = window.relearn || {};
window.T_Copy_to_clipboard = '{{ T "Copy-to-clipboard" | safeJS }}'; window.relearn.baseUriFull='{{ trim .Site.BaseURL "/" | safeJS }}/';
window.T_Copied_to_clipboard = '{{ T "Copied-to-clipboard" | safeJS }}'; {{ "// variant stuff" | safeJS }}
window.T_Copy_link_to_clipboard = '{{ T "Copy-link-to-clipboard" | safeJS }}';
window.T_Link_copied_to_clipboard = '{{ T "Link-copied-to-clipboard" | safeJS }}';
window.T_Reset_view = '{{ T "Reset-view" | safeJS }}';
window.T_View_reset = '{{ T "View-reset" | safeJS }}';
window.T_No_results_found = '{{ T "No-results-found" | safeJS }}';
window.T_N_results_found = '{{ T "N-results-found" | safeJS }}';
{{ "// some further base stuff" | safeJS }}
var baseUriFull='{{ trim .Site.BaseURL "/" | safeJS }}/';
{{- $quotedthemevariants := slice }} {{- $quotedthemevariants := slice }}
{{- range $themevariants }} {{- range $themevariants }}
{{- $quotedthemevariants = $quotedthemevariants | append (printf "'%s'" .) }} {{- $quotedthemevariants = $quotedthemevariants | append (printf "'%s'" .) }}
{{- end }} {{- end }}
window.variants && variants.init( [ {{ delimit $quotedthemevariants ", " | safeJS }} ] ); window.variants && variants.init( [ {{ delimit $quotedthemevariants ", " | safeJS }} ] );
{{ "// translations" | safeJS }}
{{ printf "window.T_Copy_to_clipboard = `%s`;" (T `Copy-to-clipboard`) | safeJS }}
{{ printf "window.T_Copied_to_clipboard = `%s`;" (T `Copied-to-clipboard`) | safeJS }}
{{ printf "window.T_Copy_link_to_clipboard = `%s`;" (T `Copy-link-to-clipboard`) | safeJS }}
{{ printf "window.T_Link_copied_to_clipboard = `%s`;" (T `Link-copied-to-clipboard`) | safeJS }}
{{ printf "window.T_Reset_view = `%s`;" (T `Reset-view`) | safeJS }}
{{ printf "window.T_View_reset = `%s`;" (T `View-reset`) | safeJS }}
{{ printf "window.T_No_results_found = `%s`;" (T "No-results-found") | safeJS }}
{{ printf "window.T_N_results_found = `%s`;" (T "N-results-found") | safeJS }}
</script> </script>
{{- end }} {{- end }}

View file

@ -127,21 +127,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(baseUriFull+"tab-selections"); var selectionsJSON = window.localStorage.getItem(window.relearn.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(baseUriFull+"tab-selections", JSON.stringify(tabSelections)); window.localStorage.setItem(window.relearn.baseUriFull+"tab-selections", JSON.stringify(tabSelections));
} }
} }
} }
function restoreTabSelections() { function restoreTabSelections() {
if(window.localStorage){ if(window.localStorage){
var selectionsJSON = window.localStorage.getItem(baseUriFull+"tab-selections"); var selectionsJSON = window.localStorage.getItem(window.relearn.baseUriFull+"tab-selections");
if(selectionsJSON){ if(selectionsJSON){
var tabSelections = JSON.parse(selectionsJSON); var tabSelections = JSON.parse(selectionsJSON);
}else{ }else{
@ -304,7 +304,7 @@ function initMermaid( update, attrs ) {
var search; var search;
if( update ){ if( update ){
search = sessionStorage.getItem( baseUriFull+'search-value' ); search = sessionStorage.getItem( window.relearn.baseUriFull+'search-value' );
unmark(); unmark();
} }
var is_initialized = ( update ? update_func( attrs ) : init_func( attrs ) ); var is_initialized = ( update ? update_func( attrs ) : init_func( attrs ) );
@ -352,7 +352,7 @@ function initMermaid( update, attrs ) {
}); });
} }
if( update && search && search.length ){ if( update && search && search.length ){
sessionStorage.setItem( baseUriFull+'search-value', search ); sessionStorage.setItem( window.relearn.baseUriFull+'search-value', search );
mark(); mark();
} }
} }
@ -1121,7 +1121,7 @@ function initExpand(){
} }
function clearHistory() { function clearHistory() {
var visitedItem = baseUriFull + 'visited-url/' var visitedItem = window.relearn.baseUriFull + 'visited-url/'
for( var item in sessionStorage ){ for( var item in sessionStorage ){
if( item.substring( 0, visitedItem.length ) === visitedItem ){ if( item.substring( 0, visitedItem.length ) === visitedItem ){
sessionStorage.removeItem( item ); sessionStorage.removeItem( item );
@ -1137,7 +1137,7 @@ function clearHistory() {
} }
function initHistory() { function initHistory() {
var visitedItem = baseUriFull + 'visited-url/' var visitedItem = window.relearn.baseUriFull + 'visited-url/'
sessionStorage.setItem( visitedItem+document.querySelector( 'body' ).dataset.url, 1); sessionStorage.setItem( visitedItem+document.querySelector( 'body' ).dataset.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
@ -1192,7 +1192,7 @@ function scrollToPositions() {
return; return;
} }
var search = sessionStorage.getItem( baseUriFull+'search-value' ); var search = sessionStorage.getItem( window.relearn.baseUriFull+'search-value' );
if( search && search.length ){ if( search && search.length ){
search = regexEscape( search ); search = regexEscape( search );
var found = elementContains( search, elc ); var found = elementContains( search, elc );
@ -1229,7 +1229,7 @@ function mark() {
bodyInnerLinks[i].classList.add( 'highlight' ); bodyInnerLinks[i].classList.add( 'highlight' );
} }
var value = sessionStorage.getItem( baseUriFull + 'search-value' ); var value = sessionStorage.getItem( window.relearn.baseUriFull + 'search-value' );
var highlightableElements = document.querySelectorAll( '.highlightable' ); var highlightableElements = document.querySelectorAll( '.highlightable' );
highlight( highlightableElements, value, { element: 'mark' } ); highlight( highlightableElements, value, { element: 'mark' } );
@ -1317,7 +1317,7 @@ function highlightNode( node, re, nodeName, className ){
}; };
function unmark() { function unmark() {
sessionStorage.removeItem( baseUriFull + 'search-value' ); sessionStorage.removeItem( window.relearn.baseUriFull + 'search-value' );
var markedElements = document.querySelectorAll( 'mark' ); var markedElements = document.querySelectorAll( 'mark' );
for( var i = 0; i < markedElements.length; i++ ){ for( var i = 0; i < markedElements.length; i++ ){
var parent = markedElements[i].parentNode; var parent = markedElements[i].parentNode;
@ -1383,7 +1383,7 @@ function elementContains( txt, e ){
function searchInputHandler( value ){ function searchInputHandler( value ){
unmark(); unmark();
if( value.length ){ if( value.length ){
sessionStorage.setItem( baseUriFull+'search-value', value ); sessionStorage.setItem( window.relearn.baseUriFull+'search-value', value );
mark(); mark();
} }
} }
@ -1395,7 +1395,7 @@ function initSearch() {
e.addEventListener( 'keydown', function( event ){ e.addEventListener( 'keydown', function( event ){
if( event.key == 'Escape' ){ if( event.key == 'Escape' ){
var input = event.target; var input = event.target;
var search = sessionStorage.getItem( baseUriFull+'search-value' ); var search = sessionStorage.getItem( window.relearn.baseUriFull+'search-value' );
if( !search || !search.length ){ if( !search || !search.length ){
input.blur(); input.blur();
} }
@ -1435,13 +1435,13 @@ function initSearch() {
var urlParams = new URLSearchParams( window.location.search ); var urlParams = new URLSearchParams( window.location.search );
var value = urlParams.get( 'search-by' ); var value = urlParams.get( 'search-by' );
if( value ){ if( value ){
sessionStorage.setItem( baseUriFull+'search-value', value ); sessionStorage.setItem( window.relearn.baseUriFull+'search-value', value );
} }
mark(); mark();
// set initial search value for inputs on page load // set initial search value for inputs on page load
if( sessionStorage.getItem( baseUriFull+'search-value' ) ){ if( sessionStorage.getItem( window.relearn.baseUriFull+'search-value' ) ){
var search = sessionStorage.getItem( baseUriFull+'search-value' ); var search = sessionStorage.getItem( window.relearn.baseUriFull+'search-value' );
inputs.forEach( function( e ){ inputs.forEach( function( e ){
e.value = search; e.value = search;
var event = document.createEvent( 'Event' ); var event = document.createEvent( 'Event' );

View file

@ -1,3 +1,5 @@
window.relearn = window.relearn || {};
// we need to load this script in the html head to avoid flickering // we need to load this script in the html head to avoid flickering
// on page load if the user has selected a non default variant // on page load if the user has selected a non default variant
@ -40,7 +42,7 @@ var variants = {
init: function( variants ){ init: function( variants ){
this.variants = variants; this.variants = variants;
var variant = window.localStorage.getItem( baseUriFull+'variant' ) || ( this.variants.length ? this.variants[0] : '' ); var variant = window.localStorage.getItem( window.relearn.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' ){
@ -55,7 +57,7 @@ var variants = {
setVariant: function( variant ){ setVariant: function( variant ){
this.variant = variant; this.variant = variant;
window.localStorage.setItem( baseUriFull+'variant', variant ); window.localStorage.setItem( window.relearn.baseUriFull+'variant', variant );
}, },
isVariantLoaded: function(){ isVariantLoaded: function(){
@ -92,11 +94,11 @@ var variants = {
}, },
addCustomVariantOption: function(){ addCustomVariantOption: function(){
var variantbase = window.localStorage.getItem( baseUriFull+'customvariantbase' ); var variantbase = window.localStorage.getItem( window.relearn.baseUriFull+'customvariantbase' );
if( this.variants.indexOf( variantbase ) < 0 ){ if( this.variants.indexOf( variantbase ) < 0 ){
variantbase = ''; variantbase = '';
} }
if( !window.localStorage.getItem( baseUriFull+'customvariant' ) ){ if( !window.localStorage.getItem( window.relearn.baseUriFull+'customvariant' ) ){
variantbase = ''; variantbase = '';
} }
if( !variantbase ){ if( !variantbase ){
@ -133,15 +135,15 @@ var variants = {
saveCustomVariant: function(){ saveCustomVariant: function(){
if( this.getVariant() != this.customvariantname ){ if( this.getVariant() != this.customvariantname ){
window.localStorage.setItem( baseUriFull+'customvariantbase', this.getVariant() ); window.localStorage.setItem( window.relearn.baseUriFull+'customvariantbase', this.getVariant() );
} }
window.localStorage.setItem( baseUriFull+'customvariant', this.generateStylesheet() ); window.localStorage.setItem( window.relearn.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( baseUriFull+'customvariant' ); var stylesheet = window.localStorage.getItem( window.relearn.baseUriFull+'customvariant' );
// temp styles to document // temp styles to document
var head = document.querySelector( 'head' ); var head = document.querySelector( 'head' );
@ -167,10 +169,10 @@ var variants = {
}, },
resetVariant: function(){ resetVariant: function(){
var variantbase = window.localStorage.getItem( baseUriFull+'customvariantbase' ); var variantbase = window.localStorage.getItem( window.relearn.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( baseUriFull+'customvariantbase' ); window.localStorage.removeItem( window.relearn.baseUriFull+'customvariantbase' );
window.localStorage.removeItem( baseUriFull+'customvariant' ); window.localStorage.removeItem( window.relearn.baseUriFull+'customvariant' );
this.removeCustomVariantOption(); this.removeCustomVariantOption();
if( this.getVariant() == this.customvariantname ){ if( this.getVariant() == this.customvariantname ){
this.changeVariant( variantbase ); this.changeVariant( variantbase );
@ -202,11 +204,11 @@ var variants = {
changeVariant: function( variant ){ changeVariant: function( variant ){
if( variant == this.customvariantname ){ if( variant == this.customvariantname ){
var variantbase = window.localStorage.getItem( baseUriFull+'customvariantbase' ); var variantbase = window.localStorage.getItem( window.relearn.baseUriFull+'customvariantbase' );
if( this.variants.indexOf( variantbase ) < 0 ){ if( this.variants.indexOf( variantbase ) < 0 ){
variant = ''; variant = '';
} }
if( !window.localStorage.getItem( baseUriFull+'customvariant' ) ){ if( !window.localStorage.getItem( window.relearn.baseUriFull+'customvariant' ) ){
variant = ''; variant = '';
} }
this.setVariant( variant ); this.setVariant( variant );