Welcome to mirror list, hosted at ThFree Co, Russian Federation.

navigation_utility.js « utils « lib « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 029e9f5fd9f47a98153407a389cb687daa5870a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { visitUrl } from './url_utility';

/**
 * Helper function that finds the href of the fiven selector and updates the location.
 *
 * @param  {String} selector
 */
export default function findAndFollowLink(selector) {
  const element = document.querySelector(selector);
  const link = element && element.getAttribute('href');

  if (link) {
    visitUrl(link);
  }
}

export function prefetchDocument(url) {
  const newPrefetchLink = document.createElement('link');
  newPrefetchLink.rel = 'prefetch';
  newPrefetchLink.href = url;
  newPrefetchLink.setAttribute('as', 'document');
  document.head.appendChild(newPrefetchLink);
}

export function initPrefetchLinks(selector) {
  document.querySelectorAll(selector).forEach((el) => {
    let mouseOverTimer;

    const mouseOutHandler = () => {
      if (mouseOverTimer) {
        clearTimeout(mouseOverTimer);
        mouseOverTimer = undefined;
      }
    };

    const mouseOverHandler = () => {
      el.addEventListener('mouseout', mouseOutHandler, { once: true, passive: true });

      mouseOverTimer = setTimeout(() => {
        if (el.href) prefetchDocument(el.href);

        // Only execute once
        el.removeEventListener('mouseover', mouseOverHandler, true);

        mouseOverTimer = undefined;
      }, 100);
    };

    el.addEventListener('mouseover', mouseOverHandler, {
      capture: true,
      passive: true,
    });
  });
}