Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / themes / seven / js / responsive-details.es6.js
1 /**
2  * @file
3  * Provides responsive behaviors to HTML details elements.
4  */
5
6 (function ($, Drupal) {
7   /**
8    * Initializes the responsive behaviors for details elements.
9    *
10    * @type {Drupal~behavior}
11    *
12    * @prop {Drupal~behaviorAttach} attach
13    *   Attaches the responsive behavior to status report specific details elements.
14    */
15   Drupal.behaviors.responsiveDetails = {
16     attach(context) {
17       const $details = $(context).find('details').once('responsive-details');
18
19       if (!$details.length) {
20         return;
21       }
22
23       function detailsToggle(matches) {
24         if (matches) {
25           $details.attr('open', true);
26           $summaries.attr('aria-expanded', true);
27           $summaries.on('click.details-open', false);
28         }
29         else {
30           // If user explicitly opened one, leave it alone.
31           const $notPressed = $details
32             .find('> summary[aria-pressed!=true]')
33             .attr('aria-expanded', false);
34           $notPressed
35             .parent('details')
36             .attr('open', false);
37           // After resize, allow user to close previously opened details.
38           $summaries.off('.details-open');
39         }
40       }
41
42       function handleDetailsMQ(event) {
43         detailsToggle(event.matches);
44       }
45
46       const $summaries = $details.find('> summary');
47       const mql = window.matchMedia('(min-width:48em)');
48       mql.addListener(handleDetailsMQ);
49       detailsToggle(mql.matches);
50     },
51   };
52 }(jQuery, Drupal));