Mercurial > hg > cmmr2012-drupal-site
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 /** | |
2 * @file | |
3 * Attaches behaviors for Drupal's active link marking. | |
4 */ | |
5 | |
6 (function (Drupal, drupalSettings) { | |
7 /** | |
8 * Append is-active class. | |
9 * | |
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). | |
15 * | |
16 * Does not discriminate based on element type, so allows you to set the | |
17 * is-active class on any element: a, li… | |
18 * | |
19 * @type {Drupal~behavior} | |
20 */ | |
21 Drupal.behaviors.activeLinks = { | |
22 attach(context) { | |
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}"]`]; | |
28 let selectors; | |
29 | |
30 // If this is the front page, we have to check for the <front> path as | |
31 // well. | |
32 if (path.isFront) { | |
33 originalSelectors.push('[data-drupal-link-system-path="<front>"]'); | |
34 } | |
35 | |
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}"]`), | |
42 ); | |
43 | |
44 // Add query string selector for pagers, exposed filters. | |
45 selectors = selectors.map(current => current + querySelector); | |
46 | |
47 // Query the DOM. | |
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'); | |
52 } | |
53 }, | |
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'); | |
60 } | |
61 } | |
62 }, | |
63 }; | |
64 }(Drupal, drupalSettings)); |