PageSourceSearch

https://www.enterespoo.fi/themes/custom/enter_espoo/js/utils.js?tltx8e

js enterespoo.fi collected 2026-09-24 19:19:56 UTC 2,872 bytes, 102 lines download raw bytes

1(function (window, Drupal, $) {
2  Drupal.behaviors.utils = {
3    scrollTrigger: function(selector, direction, scrollPosition) {
4      let button = $(selector);
5      scrollDetection(button, direction, scrollPosition);
6    },
7
8    isExpanded: function (button) {
9      return button.getAttribute('aria-expanded') === 'true' || false;
10    },
11
12    toggleMenu: function (button, expanded) {
13      button.setAttribute('aria-expanded', !expanded);
14    },
15
16    isEscPressed: function (e) {
17      // Edge and IE use a non-standard key identifier 'Esc'.
18      return e.key === 'Escape' || e.key === 'Esc';
19    },
20
21    loopWithTab: function (e) {
22      var firstElement = e.currentTarget.loopToElement.firstElement;
23      var lastElement = e.currentTarget.loopToElement.lastElement;
24      var currentElement = e.target;
25
26      if (e.key === 'Tab' && e.shiftKey === false) {
27        if (currentElement === lastElement) {
28          e.preventDefault();
29          firstElement.focus();
30        }
31      }
32
33      if (e.key === 'Tab' && e.shiftKey === true) {
34        if (currentElement === firstElement) {
35          e.preventDefault();
36          lastElement.focus();
37        }
38      }
39    },
40
41    setupMenuKeys: function (menu, callback) {
42      if(menu) {
43        var focusable = menu.querySelectorAll('[href], button, input, [tabindex="0"]');
44        var firstElement = focusable[0];
45        var lastElement = focusable[focusable.length - 1];
46
47        menu.addEventListener('keydown', callback);
48        menu.loopToElement = { firstElement: firstElement, lastElement: lastElement };
49      }
50    },
51
52    isMediaQueryMatch: function (mediaQuery) {
53      return window.matchMedia(mediaQuery).matches;
54    },
55
56    debounce: function (fn, wait) {
57      var timeout;
58      return function () {
59        var context = this;
60        var args = arguments;
61        clearTimeout(timeout);
62        timeout = setTimeout(function () {
63          fn.apply(context, args);
64        }, wait);
65      };
66    },
67
68    addListenerIfExists: function (targetElement, eventType, callback) {
69      var element = document.getElementById(targetElement);
70      if (element) {
71        element.addEventListener(eventType, callback);
72      }
73    },
74  };
75
76  function scrollDetection(button, direction, scrollPosition) {
77    button.on("click", function(e) {
78      e.preventDefault();
79      scrollAction(direction, scrollPosition);
80    });
81
82    button.on("keydown", function(e) {
83      if (e.keyCode === 32) {
84        e.preventDefault();
85        scrollAction(direction, scrollPosition);
86      }
87    });
88  }
89
90  function scrollAction(direction, scrollPosition) {
91    if (direction === "up") {
92      $("html, body").animate({
93        scrollTop: 0
94      }, "slow");
95    } else if (direction === "down") {
96      $("html, body").animate({
97        scrollTop: scrollPosition
98      }, "slow");
99    }
100  }
101
102})(window, Drupal, jQuery);

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.