3 * Attaches behaviors for Drupal's active link marking.
6 (function (Drupal, drupalSettings) {
8 * Append is-active class.
10 * The link is only active if its path corresponds to the current path, the
11 * language of the linked path is equal to the current language, and if the
12 * query parameters of the link equal those of the current request, since the
13 * same request with different query parameters may yield a different page
14 * (e.g. pagers, exposed View filters).
16 * Does not discriminate based on element type, so allows you to set the
17 * is-active class on any element: a, li…
19 * @type {Drupal~behavior}
21 Drupal.behaviors.activeLinks = {
23 // Start by finding all potentially active links.
24 const path = drupalSettings.path;
25 const queryString = JSON.stringify(path.currentQuery);
26 const querySelector = path.currentQuery ? `[data-drupal-link-query='${queryString}']` : ':not([data-drupal-link-query])';
27 const originalSelectors = [`[data-drupal-link-system-path="${path.currentPath}"]`];
30 // If this is the front page, we have to check for the <front> path as
33 originalSelectors.push('[data-drupal-link-system-path="<front>"]');
36 // Add language filtering.
37 selectors = [].concat(
38 // Links without any hreflang attributes (most of them).
39 originalSelectors.map(selector => `${selector}:not([hreflang])`),
40 // Links with hreflang equals to the current language.
41 originalSelectors.map(selector => `${selector}[hreflang="${path.currentLanguage}"]`),
44 // Add query string selector for pagers, exposed filters.
45 selectors = selectors.map(current => current + querySelector);
48 const activeLinks = context.querySelectorAll(selectors.join(','));
49 const il = activeLinks.length;
50 for (let i = 0; i < il; i++) {
51 activeLinks[i].classList.add('is-active');
54 detach(context, settings, trigger) {
55 if (trigger === 'unload') {
56 const activeLinks = context.querySelectorAll('[data-drupal-link-system-path].is-active');
57 const il = activeLinks.length;
58 for (let i = 0; i < il; i++) {
59 activeLinks[i].classList.remove('is-active');
64 }(Drupal, drupalSettings));