| 123456789101112131415161718192021222324252627282930313233343536 |
- /* global jQuery */
- /**
- * Tiny site-wide script. One purpose today: turn any element carrying a
- * `data-href` attribute into a clickable row / button without needing an
- * inline onclick (which would force `'unsafe-inline'` in the CSP).
- *
- * Middle-click / Ctrl+click / Cmd+click open in a new tab, matching
- * native anchor-link behaviour.
- */
- (function ($) {
- 'use strict';
- $(document).on('click', '[data-href]', function (ev) {
- // Ignore clicks that originated on an interactive descendant.
- if ($(ev.target).closest('a, button, input, select, textarea, label').length > 0) {
- return;
- }
- const href = String($(this).attr('data-href') || '');
- if (href === '') { return; }
- if (ev.metaKey || ev.ctrlKey || ev.button === 1) {
- window.open(href, '_blank');
- } else {
- window.location.href = href;
- }
- });
- // Make the row visibly clickable via keyboard + screen-readers.
- $('[data-href]').attr('role', 'link').attr('tabindex', '0');
- $(document).on('keydown', '[data-href]', function (ev) {
- if (ev.key === 'Enter') {
- ev.preventDefault();
- window.location.href = String($(this).attr('data-href') || '');
- }
- });
- })(jQuery);
|