2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
|
|
|
import { getLocationHash } from '../lib/utils/url_utility';
|
|
|
|
|
2017-03-26 21:33:47 -04:00
|
|
|
// Toggle button. Show/hide content inside parent container.
|
|
|
|
// Button does not change visibility. If button has icon - it changes chevron style.
|
|
|
|
//
|
|
|
|
// %div.js-toggle-container
|
|
|
|
// %button.js-toggle-button
|
|
|
|
// %div.js-toggle-content
|
|
|
|
//
|
|
|
|
|
|
|
|
$(() => {
|
|
|
|
function toggleContainer(container, toggleState) {
|
|
|
|
const $container = $(container);
|
2020-11-04 04:08:50 -05:00
|
|
|
const isExpanded = $container.data('is-expanded');
|
|
|
|
const $collapseIcon = $container.find('.js-sidebar-collapse');
|
|
|
|
const $expandIcon = $container.find('.js-sidebar-expand');
|
|
|
|
|
|
|
|
if (isExpanded && !toggleState) {
|
|
|
|
$container.data('is-expanded', false);
|
|
|
|
$collapseIcon.addClass('hidden');
|
|
|
|
$expandIcon.removeClass('hidden');
|
|
|
|
} else {
|
|
|
|
$container.data('is-expanded', true);
|
|
|
|
$expandIcon.addClass('hidden');
|
|
|
|
$collapseIcon.removeClass('hidden');
|
|
|
|
}
|
2017-03-26 21:33:47 -04:00
|
|
|
|
2018-10-10 02:09:55 -04:00
|
|
|
$container.find('.js-toggle-content').toggle(toggleState);
|
2017-03-26 21:33:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$('body').on('click', '.js-toggle-button', function toggleButton(e) {
|
2021-02-24 13:11:28 -05:00
|
|
|
e.currentTarget.classList.toggle(e.currentTarget.dataset.toggleOpenClass || 'selected');
|
2017-03-26 21:33:47 -04:00
|
|
|
toggleContainer($(this).closest('.js-toggle-container'));
|
|
|
|
|
|
|
|
const targetTag = e.currentTarget.tagName.toLowerCase();
|
|
|
|
if (targetTag === 'a' || targetTag === 'button') {
|
|
|
|
e.preventDefault();
|
2016-07-31 21:17:19 -04:00
|
|
|
}
|
|
|
|
});
|
2017-03-26 21:33:47 -04:00
|
|
|
|
|
|
|
// If we're accessing a permalink, ensure it is not inside a
|
|
|
|
// closed js-toggle-container!
|
2017-12-07 07:30:53 -05:00
|
|
|
const hash = getLocationHash();
|
2017-03-26 21:33:47 -04:00
|
|
|
const anchor = hash && document.getElementById(hash);
|
|
|
|
const container = anchor && $(anchor).closest('.js-toggle-container');
|
|
|
|
|
|
|
|
if (container) {
|
|
|
|
toggleContainer(container, true);
|
|
|
|
anchor.scrollIntoView();
|
|
|
|
}
|
|
|
|
});
|