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);
|
|
|
|
|
|
|
|
$container
|
2018-02-20 21:23:49 -05:00
|
|
|
.find('.js-toggle-button .fa-chevron-up, .js-toggle-button .fa-chevron-down')
|
2017-03-26 21:33:47 -04:00
|
|
|
.toggleClass('fa-chevron-up', toggleState)
|
|
|
|
.toggleClass('fa-chevron-down', toggleState !== undefined ? !toggleState : undefined);
|
|
|
|
|
|
|
|
$container
|
|
|
|
.find('.js-toggle-content')
|
|
|
|
.toggle(toggleState);
|
|
|
|
}
|
|
|
|
|
|
|
|
$('body').on('click', '.js-toggle-button', function toggleButton(e) {
|
2018-02-20 21:23:49 -05:00
|
|
|
e.currentTarget.classList.toggle(e.currentTarget.dataset.toggleOpenClass || 'open');
|
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();
|
|
|
|
}
|
|
|
|
});
|