2018-03-09 20:18:59 +00:00
|
|
|
import $ from 'jquery';
|
|
|
|
import { getLocationHash } from '../lib/utils/url_utility';
|
|
|
|
|
2017-03-27 01:33:47 +00: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 09:08:50 +00: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-27 01:33:47 +00:00
|
|
|
|
2018-10-10 06:09:55 +00:00
|
|
|
$container.find('.js-toggle-content').toggle(toggleState);
|
2017-03-27 01:33:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$('body').on('click', '.js-toggle-button', function toggleButton(e) {
|
2018-02-21 02:23:49 +00:00
|
|
|
e.currentTarget.classList.toggle(e.currentTarget.dataset.toggleOpenClass || 'open');
|
2017-03-27 01:33:47 +00:00
|
|
|
toggleContainer($(this).closest('.js-toggle-container'));
|
|
|
|
|
|
|
|
const targetTag = e.currentTarget.tagName.toLowerCase();
|
|
|
|
if (targetTag === 'a' || targetTag === 'button') {
|
|
|
|
e.preventDefault();
|
2016-08-01 01:17:19 +00:00
|
|
|
}
|
|
|
|
});
|
2017-03-27 01:33:47 +00:00
|
|
|
|
|
|
|
// If we're accessing a permalink, ensure it is not inside a
|
|
|
|
// closed js-toggle-container!
|
2017-12-07 12:30:53 +00:00
|
|
|
const hash = getLocationHash();
|
2017-03-27 01:33:47 +00:00
|
|
|
const anchor = hash && document.getElementById(hash);
|
|
|
|
const container = anchor && $(anchor).closest('.js-toggle-container');
|
|
|
|
|
|
|
|
if (container) {
|
|
|
|
toggleContainer(container, true);
|
|
|
|
anchor.scrollIntoView();
|
|
|
|
}
|
|
|
|
});
|