aboutsummaryrefslogtreecommitdiff
path: root/template/script.js
blob: 3c584039269bc1aa0d71120d03ebeffdb6e82c32 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
window.addEventListener('load', async () => {
  // dither image on mouse over replace
  // document.querySelectorAll('article img').forEach(img => {
  //   const ditheredImage = img.src;
  //   const originalImage = img.src.replace('.dith.gif', '');

  //   img.addEventListener('mouseover', evt => {
  //     evt.target.src = originalImage;
  //   });

  //   img.addEventListener('mouseout', evt => {
  //     evt.target.src = ditheredImage;
  //   });
  // });

  // flip CV image on mouse over
  const cvImage = document.querySelector('.cv-picture img');
  if (cvImage) {
    cvImage.addEventListener('mouseover', evt => {
      evt.target.style.transform = 'scaleX(-1)';
    });

    cvImage.addEventListener('mouseout', evt => {
      evt.target.style.transform = 'scaleX(1)';
    });
  }

  // Search functionality

  window.index = null;

  const response = await fetch('/feed.json');
  const feed = await response.json();

  window.index = elasticlunr(function () {
    this.addField('title');
    this.addField('body');
    this.setRef('id');
  });

  for (const item of feed.items) {
    item.id = item.url;
    window.index.addDoc({
      id: item.url,
      title: item.title,
      body: item.content_html,
      url: item.url,
    });
  }

  const blur = document.querySelector('.blur');
  const searchForm = document.querySelector('.search-form');
  const searchResultsList = document.querySelector('.search-form ul');

  function showSearchModal() {
    blur.classList.remove('hidden');
    searchForm.classList.remove('hidden');

    // Clear search input.
    searchForm.querySelector('input').value = '';

    // We need to clear the list before opening modal.
    searchResultsList.innerHTML = '';

    // Focus on search input.
    searchForm.querySelector('input').focus();
  }

  document.querySelector('.search-trigger').addEventListener('click', async (evt) => {
    showSearchModal();
  });

  document.onkeydown = function (e) {
    // Show search modal on F key.
    if (blur.classList.contains('hidden')) {
      if (e.key === 'f') {
        setTimeout(() => {
          showSearchModal();
        }, 100);
      }
    }

    // Hide search modal on escape key.
    if (!blur.classList.contains('hidden')) {
      if (e.key === 'Escape') {
        blur.classList.add('hidden');
        searchForm.classList.add('hidden');
      }
    }
  };

  blur.addEventListener('click', async (evt) => {
    evt.target.classList.add('hidden');
    searchForm.classList.add('hidden');
  });

  document.querySelector('.search-form input').addEventListener('keyup', async (evt) => {
    // Perform search.
    const searchResults = window.index.search(evt.target.value);

    // We need to clear the list before adding new results.
    searchResultsList.innerHTML = '';

    // Loop through the results and add them to the list.
    for (const result of searchResults.slice(0, 9)) {
      const listItem = document.createElement('li');
      listItem.innerHTML = `<a href="${result.doc.url}">${result.doc.title}</a>`;
      searchResultsList.appendChild(listItem);
    }
  });

});