Skip to content

Commit

Permalink
Remove some low hanging JQuery fruits
Browse files Browse the repository at this point in the history
Related #1402
  • Loading branch information
Konafets committed Aug 2, 2023
1 parent 1c09422 commit 6fda882
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 20 deletions.
2 changes: 1 addition & 1 deletion modules/wowchemy/assets/js/algolia-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function getSearchQuery(name) {
document.addEventListener('DOMContentLoaded', () => {
let queryURL = getSearchQuery('q');
if (queryURL) {
$('body').addClass('searching');
document.querySelector('body').classList.add('searching');
$('.search-results').css({opacity: 0, visibility: 'visible'}).animate({opacity: 1}, 200);
let commonQueries = document.querySelector('#search-common-queries');

Expand Down
4 changes: 2 additions & 2 deletions modules/wowchemy/assets/js/wowchemy-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ function scrollToAnchor(target, duration = 0) {
target = '#' + $.escapeSelector(target.substring(1)); // Previously, `target = target.replace(/:/g, '\\:');`

let elementOffset = Math.ceil($(target).offset().top - getNavBarHeight()); // Round up to highlight right ID!
$('body').addClass('scrolling');
document.querySelector('body').classList.add('scrolling');
$('html, body').animate(
{
scrollTop: elementOffset,
},
duration,
function () {
$('body').removeClass('scrolling');
document.querySelector('body').classList.remove('scrolling');
},
);
} else {
Expand Down
2 changes: 1 addition & 1 deletion modules/wowchemy/assets/js/wowchemy-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ if (typeof Fuse === 'function') {
// On page load, check for search query in URL.
let query = getSearchQuery('q');
if (query) {
$('body').addClass('searching');
document.querySelector('body').classList.add('searching');
$('.search-results').css({opacity: 0, visibility: 'visible'}).animate({opacity: 1}, 200);
$('#search-query').val(query);
$('#search-query').focus();
Expand Down
48 changes: 32 additions & 16 deletions modules/wowchemy/assets/js/wowchemy.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ function removeQueryParamsFromUrl() {
* --------------------------------------------------------------------------- */

function toggleSearchDialog() {
if ($('body').hasClass('searching')) {
const body = document.querySelector('body');
if (body.classList.contains('searching')) {
// Clear search query and hide search modal.
$('[id=search-query]').blur();
$('body').removeClass('searching compensate-for-scrollbar');
document.querySelector('[id=search-query]').blur();
body.classList.remove('searching', 'compensate-for-scrollbar');

// Remove search query params from URL as user has finished searching.
removeQueryParamsFromUrl();
Expand All @@ -50,17 +51,20 @@ function toggleSearchDialog() {
(window.innerWidth - document.documentElement.clientWidth) +
'px;}</style>',
);
$('body').addClass('compensate-for-scrollbar');
body.classList.add('compensate-for-scrollbar');
}

// Show search modal.
$('body').addClass('searching');
body.classList.add('searching');

// I assume that is broken
$('.search-results').css({opacity: 0, visibility: 'visible'}).animate({opacity: 1}, 200);

let algoliaSearchBox = document.querySelector('.ais-SearchBox-input');
if (algoliaSearchBox) {
algoliaSearchBox.focus();
} else {
$('#search-query').focus();
document.getElementById('search-query').focus();
}
}
}
Expand All @@ -75,15 +79,25 @@ function toggleSearchDialog() {
function fixHugoOutput() {
// Fix Goldmark table of contents.
// - Must be performed prior to initializing ScrollSpy.
$('#TableOfContents').addClass('nav flex-column');
$('#TableOfContents li').addClass('nav-item');
$('#TableOfContents li a').addClass('nav-link');
if (document.querySelector('#TableOfContents')) {
document.querySelector('#TableOfContents').classList.add('nav flex-column');
}
if (document.querySelector('#TableOfContents li')) {
document.querySelector('#TableOfContents li').classList.add('nav-item');
}
if (document.querySelector('#TableOfContents li a')) {
document.querySelector('#TableOfContents li a').classList.add('nav-link');
}

// Fix Goldmark task lists (remove bullet points).
$("input[type='checkbox'][disabled]").parents('ul').addClass('task-list');
if (document.querySelector('input[type=\'checkbox\'][disabled]')) {
document.querySelector('input[type=\'checkbox\'][disabled]').closest('ul').classList.add('task-list');
}

// Bootstrap table style is opt-in and Goldmark doesn't add it.
$('table').addClass('table');
if (document.querySelector('table')) {
document.querySelector('table').classList.add('table');
}
}

// Get an element's siblings.
Expand Down Expand Up @@ -123,7 +137,7 @@ document.addEventListener('DOMContentLoaded', function () {
* On window loaded.
* --------------------------------------------------------------------------- */

$(window).on('load', function () {
window.addEventListener('load', function () {
// Re-initialize Scrollspy with dynamic navbar height offset.
fixScrollspy();

Expand Down Expand Up @@ -263,10 +277,12 @@ $(window).on('load', function () {
// Check that built-in search or Algolia enabled.
if (searchEnabled) {
// On search icon click toggle search dialog.
$('.js-search').click(function (e) {
e.preventDefault();
toggleSearchDialog();
});
document.querySelectorAll('.js-search').forEach((element) => {
element.addEventListener('click', (e) => {
e.preventDefault();
toggleSearchDialog();
});
})
}

// Init. author notes (tooltips).
Expand Down

0 comments on commit 6fda882

Please sign in to comment.