This repository has been archived on 2025-01-19. You can view files and clone it, but cannot push or open issues or pull requests.
drupalcampbristol/web/modules/contrib/webform/js/webform.element.more.js

60 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-11-23 12:29:20 +00:00
/**
* @file
* JavaScript behaviors for element (read) more.
*/
(function ($, Drupal) {
'use strict';
/**
* Element (read) more.
*
* @type {Drupal~behavior}
*/
Drupal.behaviors.webformElementMore = {
attach: function (context) {
$(context).find('.js-webform-element-more').once('webform-element-more').each(function (event) {
var $more = $(this);
var $a = $more.find('a');
var $content = $more.find('.webform-element-more--content');
// Add aria-* attributes.
$a.attr({
'aria-expanded': false,
'aria-controls': $content.attr('id')
});
// Add event handlers.
$a.on('click', toggle)
.on('keydown', function (event) {
// Space or Return.
if (event.which === 32 || event.which === 13) {
toggle(event);
}
});
function toggle(event) {
var expanded = ($a.attr('aria-expanded') === 'true');
// Toggle `aria-expanded` attributes on link.
$a.attr('aria-expanded', !expanded);
// Toggle content and more .is-open state.
if (expanded) {
$more.removeClass('is-open');
$content.slideUp();
}
else {
$more.addClass('is-open');
$content.slideDown();
}
event.preventDefault();
}
});
}
};
})(jQuery, Drupal);