annotate core/misc/active-link.es6.js @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
rev   line source
Chris@0 1 /**
Chris@0 2 * @file
Chris@0 3 * Attaches behaviors for Drupal's active link marking.
Chris@0 4 */
Chris@0 5
Chris@0 6 (function (Drupal, drupalSettings) {
Chris@0 7 /**
Chris@0 8 * Append is-active class.
Chris@0 9 *
Chris@0 10 * The link is only active if its path corresponds to the current path, the
Chris@0 11 * language of the linked path is equal to the current language, and if the
Chris@0 12 * query parameters of the link equal those of the current request, since the
Chris@0 13 * same request with different query parameters may yield a different page
Chris@0 14 * (e.g. pagers, exposed View filters).
Chris@0 15 *
Chris@0 16 * Does not discriminate based on element type, so allows you to set the
Chris@0 17 * is-active class on any element: a, li…
Chris@0 18 *
Chris@0 19 * @type {Drupal~behavior}
Chris@0 20 */
Chris@0 21 Drupal.behaviors.activeLinks = {
Chris@0 22 attach(context) {
Chris@0 23 // Start by finding all potentially active links.
Chris@0 24 const path = drupalSettings.path;
Chris@0 25 const queryString = JSON.stringify(path.currentQuery);
Chris@0 26 const querySelector = path.currentQuery ? `[data-drupal-link-query='${queryString}']` : ':not([data-drupal-link-query])';
Chris@0 27 const originalSelectors = [`[data-drupal-link-system-path="${path.currentPath}"]`];
Chris@0 28 let selectors;
Chris@0 29
Chris@0 30 // If this is the front page, we have to check for the <front> path as
Chris@0 31 // well.
Chris@0 32 if (path.isFront) {
Chris@0 33 originalSelectors.push('[data-drupal-link-system-path="<front>"]');
Chris@0 34 }
Chris@0 35
Chris@0 36 // Add language filtering.
Chris@0 37 selectors = [].concat(
Chris@0 38 // Links without any hreflang attributes (most of them).
Chris@0 39 originalSelectors.map(selector => `${selector}:not([hreflang])`),
Chris@0 40 // Links with hreflang equals to the current language.
Chris@0 41 originalSelectors.map(selector => `${selector}[hreflang="${path.currentLanguage}"]`),
Chris@0 42 );
Chris@0 43
Chris@0 44 // Add query string selector for pagers, exposed filters.
Chris@0 45 selectors = selectors.map(current => current + querySelector);
Chris@0 46
Chris@0 47 // Query the DOM.
Chris@0 48 const activeLinks = context.querySelectorAll(selectors.join(','));
Chris@0 49 const il = activeLinks.length;
Chris@0 50 for (let i = 0; i < il; i++) {
Chris@0 51 activeLinks[i].classList.add('is-active');
Chris@0 52 }
Chris@0 53 },
Chris@0 54 detach(context, settings, trigger) {
Chris@0 55 if (trigger === 'unload') {
Chris@0 56 const activeLinks = context.querySelectorAll('[data-drupal-link-system-path].is-active');
Chris@0 57 const il = activeLinks.length;
Chris@0 58 for (let i = 0; i < il; i++) {
Chris@0 59 activeLinks[i].classList.remove('is-active');
Chris@0 60 }
Chris@0 61 }
Chris@0 62 },
Chris@0 63 };
Chris@0 64 }(Drupal, drupalSettings));