summaryrefslogtreecommitdiffstats
path: root/static/assets/js/fuji.dev.js
blob: d6917a3b668a8c03e95e4f14b7a3dde7669dfbce (plain) (blame)
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
'use strict';

// toggle theme
function toggleTheme() {
    $('body').attr('data-theme', (index, attr) => {
        if (attr === 'light') {
            localStorage.setItem('fuji_theme', 'dark');
            return 'dark';
        } else {
            localStorage.setItem('fuji_theme', 'light');
            return 'light';
        }
    });
}

// update medium-zoom theme
// @params targetTheme, mediumInst
function updateMeidumTheme(targetTheme, mediumInst) {
    if (targetTheme && mediumInst) {
        mediumInst.detach();
        if (targetTheme === 'dark') {
            mediumInst = mediumZoom('.img-zoomable', {
                background: '#2f3136',
            });
        } else {
            mediumInst = mediumZoom('.img-zoomable', {
                background: '#fffffd',
            });
        }
    }
}

// update utterances theme
// @params targetTheme, utterancesFrame
function updateUtterancesTheme(targetTheme, utterancesFrame) {
    if (targetTheme && 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'
            );
        }
    }
}

// to-top button
$('.btn .btn-scroll-top').on('click', () => {
    $('html, body').animate({
        scrollTop: 0,
    });
});

// toc anchors
$('.sidebar-toc a').on('click', (event) => {
    $('html, body').animate({
        scrollTop: $($(event.currentTarget).attr('href')).offset().top,
    });
});

// remove empty ul in toc if article only have ## and ###
if ($('.sidebar-toc ul ul').length > 0 && $('.sidebar-toc ul ul li').text() === '') {
    $('.sidebar-toc ul ul').hide();
}

// init medium-zoom
var mediumInst; // medium-zoom instance
if ($('body').attr('data-theme') === 'dark') {
    mediumInst = mediumZoom('.img-zoomable', {
        background: '#2f3136',
    });
} else {
    mediumInst = mediumZoom('.img-zoomable', {
        background: '#fffffd',
    });
}

// init highlight.js
hljs.initHighlighting();

// if in post page and using utterances
// add utterances comment loading indicator
if ($('.post-loading').length >= 1) {
    var commentStatus; // utterence status
    var commentLoadingTime = 0; // loading time passed
    var commentCheckInterval = self.setInterval(checkUtterances, 500);

    function checkUtterances() {
        commentStatus = $('.post-comment .utterances').attr('style');
        if (commentStatus) {
            clearInterval(commentCheckInterval);
            updateUtterancesTheme($('body').attr('data-theme'), $('.post-comment iframe')[0]);
            $('.post-loading').hide();
        } else {
            if (++commentLoadingTime > 20) {
                clearInterval(commentCheckInterval);
                $('.post-comment').hide();
                $('.post-loading i').attr('class', 'far fa-times-circle');
            }
        }
    }
}

// init theme switch button
$('.btn .btn-toggle-mode').on('click', () => {
    // toggle theme
    toggleTheme();
    // update medium background
    updateMeidumTheme($('body').attr('data-theme'), mediumInst);
    // switch comment area theme
    // only works after comment area are initialized
    if ($('.post-loading').length >= 1 && commentStatus) {
        updateUtterancesTheme($('body').attr('data-theme'), $('.post-comment iframe')[0]);
    }
    if ($('#disqus_thread').length >= 1 && typeof DISQUS !== 'undefined') {
        DISQUS.reset({
            reload: true,
        });
    }
});