123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- (function ($, Modernizr, Drupal) {
-
- function CollapsibleDetails(node) {
- this.$node = $(node);
- this.$node.data('details', this);
-
-
- const anchor = location.hash && location.hash !== '#' ? `, ${location.hash}` : '';
- if (this.$node.find(`.error${anchor}`).length) {
- this.$node.attr('open', true);
- }
-
- this.setupSummary();
-
- this.setupLegend();
- }
- $.extend(CollapsibleDetails, {
-
- instances: [],
- });
- $.extend(CollapsibleDetails.prototype, {
-
- setupSummary() {
- this.$summary = $('<span class="summary"></span>');
- this.$node
- .on('summaryUpdated', $.proxy(this.onSummaryUpdated, this))
- .trigger('summaryUpdated');
- },
-
- setupLegend() {
-
- const $legend = this.$node.find('> summary');
- $('<span class="details-summary-prefix visually-hidden"></span>')
- .append(this.$node.attr('open') ? Drupal.t('Hide') : Drupal.t('Show'))
- .prependTo($legend)
- .after(document.createTextNode(' '));
-
- $('<a class="details-title"></a>')
- .attr('href', `#${this.$node.attr('id')}`)
- .prepend($legend.contents())
- .appendTo($legend);
- $legend
- .append(this.$summary)
- .on('click', $.proxy(this.onLegendClick, this));
- },
-
- onLegendClick(e) {
- this.toggle();
- e.preventDefault();
- },
-
- onSummaryUpdated() {
- const text = $.trim(this.$node.drupalGetSummary());
- this.$summary.html(text ? ` (${text})` : '');
- },
-
- toggle() {
- const isOpen = !!this.$node.attr('open');
- const $summaryPrefix = this.$node.find('> summary span.details-summary-prefix');
- if (isOpen) {
- $summaryPrefix.html(Drupal.t('Show'));
- }
- else {
- $summaryPrefix.html(Drupal.t('Hide'));
- }
-
-
- setTimeout(() => {
- this.$node.attr('open', !isOpen);
- }, 0);
- },
- });
-
- Drupal.behaviors.collapse = {
- attach(context) {
- if (Modernizr.details) {
- return;
- }
- const $collapsibleDetails = $(context).find('details').once('collapse').addClass('collapse-processed');
- if ($collapsibleDetails.length) {
- for (let i = 0; i < $collapsibleDetails.length; i++) {
- CollapsibleDetails.instances.push(new CollapsibleDetails($collapsibleDetails[i]));
- }
- }
- },
- };
-
- const handleFragmentLinkClickOrHashChange = (e, $target) => {
- $target.parents('details').not('[open]').find('> summary').trigger('click');
- };
-
- $('body').on('formFragmentLinkClickOrHashChange.details', handleFragmentLinkClickOrHashChange);
-
- Drupal.CollapsibleDetails = CollapsibleDetails;
- }(jQuery, Modernizr, Drupal));
|