aboutsummaryrefslogtreecommitdiff
path: root/src/js/even.js
blob: 6aef5404e3cfe916f4c89e08b3a0f1599d936702 (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
'use strict'

var Even = {}

Even.backToTop = function () {
  var $backToTop = $('#back-to-top')

  $(window).scroll(function () {
    if ($(window).scrollTop() > 100) {
      $backToTop.fadeIn(1000)
    } else {
      $backToTop.fadeOut(1000)
    }
  })

  $backToTop.click(function () {
    $('body,html').animate({ scrollTop: 0 })
  })
}

Even.mobileNavbar = function () {
  var $mobileNav = $('#mobile-navbar')
  var $mobileNavIcon = $('.mobile-navbar-icon')
  var slideout = new Slideout({
    'panel': document.getElementById('mobile-panel'),
    'menu': document.getElementById('mobile-menu'),
    'padding': 180,
    'tolerance': 70
  })
  slideout.disableTouch()

  $mobileNavIcon.click(function () {
    slideout.toggle()
  })

  slideout.on('beforeopen', function () {
    $mobileNav.addClass('fixed-open')
    $mobileNavIcon.addClass('icon-click').removeClass('icon-out')
  })

  slideout.on('beforeclose', function () {
    $mobileNav.removeClass('fixed-open')
    $mobileNavIcon.addClass('icon-out').removeClass('icon-click')
  })

  $('#mobile-panel').on('touchend', function () {
    slideout.isOpen() && $mobileNavIcon.click()
  })
}

Even.toc = function () {
  var SPACING = 20
  var $toc = $('.post-toc')
  var $footer = $('.post-footer')

  if ($toc.length) {
    var minScrollTop = $toc.offset().top - SPACING
    var maxScrollTop = $footer.offset().top - $toc.height() - SPACING

    var tocState = {
      start: {
        'position': 'absolute',
        'top': minScrollTop
      },
      process: {
        'position': 'fixed',
        'top': SPACING
      },
      end: {
        'position': 'absolute',
        'top': maxScrollTop
      }
    }

    $(window).scroll(function () {
      var scrollTop = $(window).scrollTop()

      if (scrollTop < minScrollTop) {
        $toc.css(tocState.start)
      } else if (scrollTop > maxScrollTop) {
        $toc.css(tocState.end)
      } else {
        $toc.css(tocState.process)
      }
    })
  }

  var HEADERFIX = 30
  var $toclink = $('.toc-link')
  var $headerlink = $('.headerlink')

  var headerlinkTop = $.map($headerlink, function (link) {
    return $(link).offset().top
  })

  $(window).scroll(function () {
    var scrollTop = $(window).scrollTop()

    for (var i = 0; i < $toclink.length; i++) {
      var isLastOne = i + 1 === $toclink.length
      var currentTop = headerlinkTop[i] - HEADERFIX
      var nextTop = isLastOne ? Infinity : headerlinkTop[i + 1] - HEADERFIX

      if (currentTop < scrollTop && scrollTop <= nextTop) {
        $($toclink[i]).addClass('active')
      } else {
        $($toclink[i]).removeClass('active')
      }
    }
  })
}

Even.fancybox = function () {
  if ($.fancybox) {
    $('.post').each(function () {
      $(this).find('img').each(function () {
        $(this).wrap(`<a class="fancybox" href="${this.src}" data-fancybox="gallery" data-caption="${this.title}"></a>`)
      })
    })

    $('.fancybox').fancybox({
      selector: '.fancybox',
      protect: true
    })
  }
}

Even.highlight = function () {
  const blocks = document.querySelectorAll('pre code')
  for (const block of blocks) {
    const classList = block.classList
    const rootElement = block.parentElement
    const lineCodes = block.innerHTML.split(/\n/).slice(0, -1)
    const lineLength = lineCodes.length

    let codeLineHtml = ''
    for (let i = 0; i < lineLength; i++) {
      codeLineHtml += `<div class="line">${i + 1}</div>`
    }

    let codeHtml = ''
    for (const lineCode of lineCodes) {
      codeHtml += `<div class="line">${lineCode}</div>`
    }

    classList.add('highlight')
    const figure = document.createElement('figure')
    figure.classList = classList
    figure.innerHTML = `<table><tbody><tr><td class="gutter"><pre>${codeLineHtml}</pre></td><td class="code"><pre>${codeHtml}</pre></td></tr></tbody></table>`

    rootElement.parentElement.replaceChild(figure, rootElement)
  }
}

Even.beforeToc = function () {
  const links = document.querySelectorAll('#TableOfContents a')
  for (const link of links) link.classList.add('toc-link')

  for (const num of [1, 2, 3, 4, 5, 6]) {
    const headers = document.querySelectorAll('.post-content>h' + num)
    for (const header of headers) {
      header.innerHTML = `<a href="#${header.id}" class="headerlink" title="${header.innerHTML}"></a>${header.innerHTML}`
    }
  }
}

export {Even}