1/** 2 * @file element-link.js 3 * 4 * JS File to make a link on wrappers. 5 */ 6(function (Drupal, once) { 7 const elementUrlSelector = "[data-element-url]"; 8 9 /** 10 * @Behavior 11 * 12 * Redirect users to data-element-url on click of elements using data-element-url. 13 */ 14 Drupal.behaviors.rddsElementLink = { 15 onceName: "element-link", 16 attach(context) { 17 18 // Define if externalLinks library is available. 19 let externalLinksEnabled = false; 20 if ( 21 Drupal.behaviors.rddsExternalLinks && 22 typeof Drupal.behaviors.rddsExternalLinks.addExternalAttributes === 'function' 23 ) { 24 Drupal.behaviors.rddsExternalLinks.addExternalAttributes(context, '*', 'data-element-url'); 25 externalLinksEnabled = true; 26 } 27 28 once( 29 this.onceName, 30 context.querySelectorAll(elementUrlSelector), 31 context, 32 ).forEach(function (element) { 33 element.classList.add("cursor-pointer"); 34 35 let url = element.getAttribute("data-element-url"); 36 const linkDetail = { url, element }; 37 // Expose an event to allow url modifications. 38 const linkEvent = new CustomEvent('rdds:element-link-url', { 39 bubbles: true, 40 cancelable: true, 41 detail: linkDetail, 42 }); 43 element.dispatchEvent(linkEvent); 44 if (linkEvent.defaultPrevented) { 45 return; 46 } 47 url = linkDetail.url; 48 element.setAttribute("data-element-url", url); 49 50 ["click", "touch"].forEach(function (e) { 51 element.addEventListener(e, function (event) { 52 const excludeTags = ['input']; 53 // If one of the parent item match this selector, no redirection. 54 const excludeSelector = ['[data-element-link-exclude]', '.contextual']; 55 if ( 56 (event.target.getAttribute('data-element-link-exclude')) || 57 (excludeTags.includes(event.target.tagName.toLowerCase())) 58 ) { 59 return; 60 } 61 // If the target is the child of an excluded element. 62 if (event.target.closest(excludeSelector)) { 63 return; 64 } 65 66 if (externalLinksEnabled) { 67 if (element.getAttribute('data-target') === '_blank') { 68 window.open(url, '_blank').focus(); 69 } else { 70 window.location.href = url; 71 } 72 } else { 73 // Fallback if externalLinks library is disabled. 74 if (url.includes(location.host) || url.startsWith('/')) { 75 window.location.href = url; 76 } else { 77 window.open(url, '_blank').focus(); 78 } 79 } 80 }); 81 }); 82 }); 83 }, 84 detach: function detach(context, settings, trigger) { 85 if (trigger === "unload") { 86 once.remove( 87 this.onceName, 88 context.querySelectorAll(elementUrlSelector), 89 ); 90 } 91 }, 92 }; 93})(Drupal, once);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.