Skip to content

Commit

Permalink
Allow unfolding of toggle/accordeon item by changing window.location.…
Browse files Browse the repository at this point in the history
…hash
  • Loading branch information
joostdekeijzer committed Feb 13, 2023
1 parent aaeb87d commit 6c47c1c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/frontend/blocks/accordion/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,30 @@
(function ($) {
$(document).ready(function (e) {

var getwid_window_hook = false;

//Init block loaded via AJAX
$(document.body).on('post-load', function (e) {
getwid_init_accordions();
});

var getwid_init_accordions = () => {
if( getwid_window_hook === false && $('.wp-block-getwid-accordion').length > 0 ) {
getwid_window_hook = true;

$(window).on('hashchange', function (e) {
if (window.location.hash) {
$('.wp-block-getwid-accordion.getwid-init .wp-block-getwid-accordion__header-wrapper' + window.location.hash).each(function (index, row) {
var $row = $(row);

if ( ! $row.hasClass('ui-accordion-header-active') ) {
$row.trigger('click');
}
});
}
});
}

var getwid_accordions = $('.wp-block-getwid-accordion:not(.getwid-init)'),
getwid_accordion_active = 0;

Expand Down
22 changes: 22 additions & 0 deletions src/frontend/blocks/toggle/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,30 @@
(function ($) {
$(document).ready(function (e) {

var getwid_window_hook = false;

//Init block loaded via AJAX
$(document.body).on('post-load', function (e) {
getwid_init_toggles();
});

var getwid_init_toggles = () => {
if( getwid_window_hook === false && $('.wp-block-getwid-toggle').length > 0 ) {
getwid_window_hook = true;

$(window).on('hashchange', function (e) {
if (window.location.hash) {
$('.wp-block-getwid-toggle.getwid-init .wp-block-getwid-toggle__row' + window.location.hash).each(function (index, row) {
var $row = $(row);

if ( ! $row.hasClass('is-active') ) {
$row.find('.wp-block-getwid-toggle__header-wrapper').trigger('click');
}
});
}
});
}

var getwid_toggles = $('.wp-block-getwid-toggle:not(.getwid-init)');

getwid_toggles.each(function (index, toggle) {
Expand Down Expand Up @@ -55,6 +73,10 @@
row.addClass('is-active');
}
});

if (window.location.hash) {
$(window).trigger('hashchange');
}
});
};

Expand Down

0 comments on commit 6c47c1c

Please sign in to comment.