annotate core/misc/active-link.es6.js @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
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@17 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@17 26 const querySelector = path.currentQuery
Chris@17 27 ? `[data-drupal-link-query='${queryString}']`
Chris@17 28 : ':not([data-drupal-link-query])';
Chris@17 29 const originalSelectors = [
Chris@17 30 `[data-drupal-link-system-path="${path.currentPath}"]`,
Chris@17 31 ];
Chris@0 32 let selectors;
Chris@0 33
Chris@0 34 // If this is the front page, we have to check for the <front> path as
Chris@0 35 // well.
Chris@0 36 if (path.isFront) {
Chris@0 37 originalSelectors.push('[data-drupal-link-system-path="<front>"]');
Chris@0 38 }
Chris@0 39
Chris@0 40 // Add language filtering.
Chris@0 41 selectors = [].concat(
Chris@0 42 // Links without any hreflang attributes (most of them).
Chris@0 43 originalSelectors.map(selector => `${selector}:not([hreflang])`),
Chris@0 44 // Links with hreflang equals to the current language.
Chris@17 45 originalSelectors.map(
Chris@17 46 selector => `${selector}[hreflang="${path.currentLanguage}"]`,
Chris@17 47 ),
Chris@0 48 );
Chris@0 49
Chris@0 50 // Add query string selector for pagers, exposed filters.
Chris@0 51 selectors = selectors.map(current => current + querySelector);
Chris@0 52
Chris@0 53 // Query the DOM.
Chris@0 54 const activeLinks = context.querySelectorAll(selectors.join(','));
Chris@0 55 const il = activeLinks.length;
Chris@0 56 for (let i = 0; i < il; i++) {
Chris@0 57 activeLinks[i].classList.add('is-active');
Chris@0 58 }
Chris@0 59 },
Chris@0 60 detach(context, settings, trigger) {
Chris@0 61 if (trigger === 'unload') {
Chris@17 62 const activeLinks = context.querySelectorAll(
Chris@17 63 '[data-drupal-link-system-path].is-active',
Chris@17 64 );
Chris@0 65 const il = activeLinks.length;
Chris@0 66 for (let i = 0; i < il; i++) {
Chris@0 67 activeLinks[i].classList.remove('is-active');
Chris@0 68 }
Chris@0 69 }
Chris@0 70 },
Chris@0 71 };
Chris@17 72 })(Drupal, drupalSettings);