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

fuji.js « js « assets - github.com/amzrk2/hugo-theme-fuji.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ed10d174ed22a41d9c667ac1f4bb733b6b3b04c2 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
'use strict';

// get current theme
function getNowTheme() {
  let nowTheme = document.body.getAttribute('data-theme');
  if (nowTheme === 'auto') {
    return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
  } else {
    return nowTheme === 'dark' ? 'dark' : 'light';
  }
}

// load comment button only when comment area exist
if (document.querySelector('span.post-comment-notloaded')) {
  document.querySelector('span.post-comment-notloaded').addEventListener('click', loadComment);
}

// to-top button
document.querySelector('.btn .btn-scroll-top').addEventListener('click', () => {
  document.documentElement.scrollTop = 0;
});

document.addEventListener('DOMContentLoaded', () => {
  if (typeof mediumZoom === 'function') {
    mediumZoom('.img-zoomable', {
      margin: 32,
      background: '#00000054',
      scrollOffset: 128,
    });
  }
});

// update utterances theme
function updateUtterancesTheme(utterancesFrame) {
  let targetTheme = getNowTheme();
  if (utterancesFrame) {
    if (targetTheme === 'dark') {
      utterancesFrame.contentWindow.postMessage(
        {
          type: 'set-theme',
          theme: 'photon-dark',
        },
        'https://utteranc.es'
      );
    } else {
      utterancesFrame.contentWindow.postMessage(
        {
          type: 'set-theme',
          theme: 'github-light',
        },
        'https://utteranc.es'
      );
    }
  }
}

// theme switch button
document.querySelector('.btn .btn-toggle-mode').addEventListener('click', () => {
  let nowTheme = getNowTheme();
  let domTheme = document.body.getAttribute('data-theme');
  const needAuto = document.body.getAttribute('data-theme-auto') === 'true';
  let systemTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';

  if (domTheme === 'auto') {
    // if now in auto mode, switch to user mode
    document.body.setAttribute('data-theme', nowTheme === 'light' ? 'dark' : 'light');
    localStorage.setItem('fuji_data-theme', nowTheme === 'light' ? 'dark' : 'light');
  } else if (domTheme === 'light') {
    const tar = systemTheme === 'dark' ? (needAuto ? 'auto' : 'dark') : 'dark';
    document.body.setAttribute('data-theme', tar);
    localStorage.setItem('fuji_data-theme', tar);
  } else {
    const tar = systemTheme === 'light' ? (needAuto ? 'auto' : 'light') : 'light';
    document.body.setAttribute('data-theme', tar);
    localStorage.setItem('fuji_data-theme', tar);
  }

  // switch comment area theme
  // if this page has comment area
  let commentArea = document.querySelector('.post-comment');
  if (commentArea) {
    // if comment area loaded
    if (document.querySelector('span.post-comment-notloaded').getAttribute('style')) {
      if (commentArea.getAttribute('data-comment') === 'utterances') {
        updateUtterancesTheme(document.querySelector('.post-comment iframe'));
      }
      if (commentArea.getAttribute('data-comment') === 'disqus') {
        DISQUS.reset({
          reload: true,
        });
      }
    }
  }
});

// search by fuse.js
function searchAll(key, index, counter) {
  let fuse = new Fuse(index, {
    shouldSort: true,
    distance: 10000,
    keys: [
      {
        name: 'title',
        weight: 2.0,
      },
      {
        name: 'tags',
        weight: 1.5,
      },
      {
        name: 'content',
        weight: 1.0,
      },
    ],
  });
  let result = fuse.search(key);
  // console.log(result);
  if (result.length > 0) {
    document.getElementById('search-result').innerHTML = template('search-result-template', result);
    return [new Date().getTime() - counter, result.length];
  } else {
    return 'notFound';
  }
}

let urlParams = new URLSearchParams(window.location.search); // get params from URL
if (urlParams.has('s')) {
  let counter = new Date().getTime();
  let infoElements = document.querySelectorAll('.search-result-info');
  let key = urlParams.get('s'); // get search keyword, divided by space
  document.querySelector('.search-input input').setAttribute('value', key);
  // get search index from json
  let xhr = new XMLHttpRequest();
  xhr.open('GET', 'index.json', true);
  xhr.responseType = 'json';
  xhr.onerror = () => {
    infoElements[2].removeAttribute('style');
  };
  xhr.ontimeout = () => {
    infoElements[2].removeAttribute('style');
  };
  xhr.onreadystatechange = () => {
    if (xhr.readyState === 4) {
      if (xhr.status === 200) {
        // use index json to search
        // console.log(xhr.response);
        counter = searchAll(key, xhr.response, counter);
        // console.log(counter);
        if (counter === 'notFound') {
          infoElements[1].removeAttribute('style');
        } else {
          infoElements[0].innerHTML = infoElements[0].innerHTML.replace('[TIME]', counter[0]);
          infoElements[0].innerHTML = infoElements[0].innerHTML.replace('[NUM]', counter[1]);
          infoElements[0].removeAttribute('style');
        }
      } else {
        infoElements[2].removeAttribute('style');
      }
    }
  };
  xhr.send(null);
}

/* mobile menu  */
const openMenu = document.getElementById('btn-menu');
if (openMenu) {
  openMenu.addEventListener('click', () => {
    const menu = document.querySelector('.sidebar-mobile');
    if (menu) {
      if (menu.style.display === 'none') {
        menu.setAttribute('style', 'display: flex;');
      } else {
        menu.setAttribute('style', 'display: none;');
      }
    }
  });
}