annotate core/misc/collapse.es6.js @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
rev   line source
Chris@0 1 /**
Chris@0 2 * @file
Chris@0 3 * Polyfill for HTML5 details elements.
Chris@0 4 */
Chris@0 5
Chris@4 6 (function($, Modernizr, Drupal) {
Chris@0 7 /**
Chris@0 8 * The collapsible details object represents a single details element.
Chris@0 9 *
Chris@0 10 * @constructor Drupal.CollapsibleDetails
Chris@0 11 *
Chris@0 12 * @param {HTMLElement} node
Chris@0 13 * The details element.
Chris@0 14 */
Chris@0 15 function CollapsibleDetails(node) {
Chris@0 16 this.$node = $(node);
Chris@0 17 this.$node.data('details', this);
Chris@0 18 // Expand details if there are errors inside, or if it contains an
Chris@0 19 // element that is targeted by the URI fragment identifier.
Chris@4 20 const anchor =
Chris@4 21 window.location.hash && window.location.hash !== '#'
Chris@4 22 ? `, ${window.location.hash}`
Chris@4 23 : '';
Chris@0 24 if (this.$node.find(`.error${anchor}`).length) {
Chris@0 25 this.$node.attr('open', true);
Chris@0 26 }
Chris@0 27 // Initialize and setup the summary,
Chris@0 28 this.setupSummary();
Chris@0 29 // Initialize and setup the legend.
Chris@0 30 this.setupLegend();
Chris@0 31 }
Chris@0 32
Chris@4 33 $.extend(
Chris@4 34 CollapsibleDetails,
Chris@4 35 /** @lends Drupal.CollapsibleDetails */ {
Chris@4 36 /**
Chris@4 37 * Holds references to instantiated CollapsibleDetails objects.
Chris@4 38 *
Chris@4 39 * @type {Array.<Drupal.CollapsibleDetails>}
Chris@4 40 */
Chris@4 41 instances: [],
Chris@4 42 },
Chris@4 43 );
Chris@0 44
Chris@4 45 $.extend(
Chris@4 46 CollapsibleDetails.prototype,
Chris@4 47 /** @lends Drupal.CollapsibleDetails# */ {
Chris@4 48 /**
Chris@4 49 * Initialize and setup summary events and markup.
Chris@4 50 *
Chris@4 51 * @fires event:summaryUpdated
Chris@4 52 *
Chris@4 53 * @listens event:summaryUpdated
Chris@4 54 */
Chris@4 55 setupSummary() {
Chris@4 56 this.$summary = $('<span class="summary"></span>');
Chris@4 57 this.$node
Chris@4 58 .on('summaryUpdated', $.proxy(this.onSummaryUpdated, this))
Chris@4 59 .trigger('summaryUpdated');
Chris@4 60 },
Chris@0 61
Chris@4 62 /**
Chris@4 63 * Initialize and setup legend markup.
Chris@4 64 */
Chris@4 65 setupLegend() {
Chris@4 66 // Turn the summary into a clickable link.
Chris@4 67 const $legend = this.$node.find('> summary');
Chris@0 68
Chris@4 69 $('<span class="details-summary-prefix visually-hidden"></span>')
Chris@4 70 .append(this.$node.attr('open') ? Drupal.t('Hide') : Drupal.t('Show'))
Chris@4 71 .prependTo($legend)
Chris@4 72 .after(document.createTextNode(' '));
Chris@4 73
Chris@4 74 // .wrapInner() does not retain bound events.
Chris@4 75 $('<a class="details-title"></a>')
Chris@4 76 .attr('href', `#${this.$node.attr('id')}`)
Chris@4 77 .prepend($legend.contents())
Chris@4 78 .appendTo($legend);
Chris@4 79
Chris@4 80 $legend
Chris@4 81 .append(this.$summary)
Chris@4 82 .on('click', $.proxy(this.onLegendClick, this));
Chris@4 83 },
Chris@4 84
Chris@4 85 /**
Chris@4 86 * Handle legend clicks.
Chris@4 87 *
Chris@4 88 * @param {jQuery.Event} e
Chris@4 89 * The event triggered.
Chris@4 90 */
Chris@4 91 onLegendClick(e) {
Chris@4 92 this.toggle();
Chris@4 93 e.preventDefault();
Chris@4 94 },
Chris@4 95
Chris@4 96 /**
Chris@4 97 * Update summary.
Chris@4 98 */
Chris@4 99 onSummaryUpdated() {
Chris@4 100 const text = $.trim(this.$node.drupalGetSummary());
Chris@4 101 this.$summary.html(text ? ` (${text})` : '');
Chris@4 102 },
Chris@4 103
Chris@4 104 /**
Chris@4 105 * Toggle the visibility of a details element using smooth animations.
Chris@4 106 */
Chris@4 107 toggle() {
Chris@4 108 const isOpen = !!this.$node.attr('open');
Chris@4 109 const $summaryPrefix = this.$node.find(
Chris@4 110 '> summary span.details-summary-prefix',
Chris@4 111 );
Chris@4 112 if (isOpen) {
Chris@4 113 $summaryPrefix.html(Drupal.t('Show'));
Chris@4 114 } else {
Chris@4 115 $summaryPrefix.html(Drupal.t('Hide'));
Chris@4 116 }
Chris@4 117 // Delay setting the attribute to emulate chrome behavior and make
Chris@4 118 // details-aria.js work as expected with this polyfill.
Chris@4 119 setTimeout(() => {
Chris@4 120 this.$node.attr('open', !isOpen);
Chris@4 121 }, 0);
Chris@4 122 },
Chris@0 123 },
Chris@4 124 );
Chris@0 125
Chris@0 126 /**
Chris@0 127 * Polyfill HTML5 details element.
Chris@0 128 *
Chris@0 129 * @type {Drupal~behavior}
Chris@0 130 *
Chris@0 131 * @prop {Drupal~behaviorAttach} attach
Chris@0 132 * Attaches behavior for the details element.
Chris@0 133 */
Chris@0 134 Drupal.behaviors.collapse = {
Chris@0 135 attach(context) {
Chris@0 136 if (Modernizr.details) {
Chris@0 137 return;
Chris@0 138 }
Chris@4 139 const $collapsibleDetails = $(context)
Chris@4 140 .find('details')
Chris@4 141 .once('collapse')
Chris@4 142 .addClass('collapse-processed');
Chris@0 143 if ($collapsibleDetails.length) {
Chris@0 144 for (let i = 0; i < $collapsibleDetails.length; i++) {
Chris@4 145 CollapsibleDetails.instances.push(
Chris@4 146 new CollapsibleDetails($collapsibleDetails[i]),
Chris@4 147 );
Chris@0 148 }
Chris@0 149 }
Chris@0 150 },
Chris@0 151 };
Chris@0 152
Chris@0 153 /**
Chris@0 154 * Open parent details elements of a targeted page fragment.
Chris@0 155 *
Chris@0 156 * Opens all (nested) details element on a hash change or fragment link click
Chris@0 157 * when the target is a child element, in order to make sure the targeted
Chris@0 158 * element is visible. Aria attributes on the summary
Chris@0 159 * are set by triggering the click event listener in details-aria.js.
Chris@0 160 *
Chris@0 161 * @param {jQuery.Event} e
Chris@0 162 * The event triggered.
Chris@0 163 * @param {jQuery} $target
Chris@0 164 * The targeted node as a jQuery object.
Chris@0 165 */
Chris@0 166 const handleFragmentLinkClickOrHashChange = (e, $target) => {
Chris@4 167 $target
Chris@4 168 .parents('details')
Chris@4 169 .not('[open]')
Chris@4 170 .find('> summary')
Chris@4 171 .trigger('click');
Chris@0 172 };
Chris@0 173
Chris@0 174 /**
Chris@0 175 * Binds a listener to handle fragment link clicks and URL hash changes.
Chris@0 176 */
Chris@4 177 $('body').on(
Chris@4 178 'formFragmentLinkClickOrHashChange.details',
Chris@4 179 handleFragmentLinkClickOrHashChange,
Chris@4 180 );
Chris@0 181
Chris@0 182 // Expose constructor in the public space.
Chris@0 183 Drupal.CollapsibleDetails = CollapsibleDetails;
Chris@4 184 })(jQuery, Modernizr, Drupal);