UNPKG

2.3 kBJavaScriptView Raw
1var Pjax = require('pjax')
2var Nprogress = require('nprogress')
3var onmount = require('onmount')
4var toggleClass = require('dom101/toggle-class')
5var ready = require('dom101/ready')
6var Scrolltrack = require('./scrolltrack')
7var Scrollclass = require('./scrollclass')
8
9/*
10 * pjax/nprogress
11 */
12
13void (function () {
14 ready(function () {
15 new Pjax({ // eslint-disable-line
16 selectors: ['.body', '.toc-menu', 'title'],
17 analytics: sendPageview
18 })
19 })
20
21 ready(sendPageview)
22
23 function sendPageview () {
24 var title = document.querySelector('title')
25 if (window.ga) {
26 window.ga('send', 'pageview', {
27 page: window.location.href,
28 title: title && title.text
29 })
30 }
31 }
32
33 document.addEventListener('pjax:send', function () {
34 Nprogress.start()
35 })
36
37 document.addEventListener('pjax:error', function () {
38 Nprogress.done()
39 })
40
41 document.addEventListener('pjax:complete', function () {
42 Nprogress.done()
43 })
44}())
45
46/*
47 * menu toggle
48 */
49
50onmount('.js-menu-toggle', function () {
51 this.addEventListener('click', function () {
52 toggleClass(document.body, '-menu-visible')
53 })
54})
55
56/*
57 * onmount
58 */
59
60void (function () {
61 ready(function () {
62 onmount()
63 })
64
65 document.addEventListener('pjax:complete', function () {
66 onmount()
67 })
68}())
69
70/*
71 * scrolltrack
72 */
73
74void (function () {
75 var st = new Scrolltrack({
76 menu: '.toc-menu',
77 selector: 'h2, h3',
78 onupdate: function (active, last) {
79 var menu = document.querySelector('.toc-menu')
80 var link = menu.querySelector('.link.-active, .link.-notactive')
81
82 toggleClass(link, '-active', !active)
83 toggleClass(link, '-notactive', active)
84 }
85 })
86
87 document.addEventListener('pjax:complete', function () {
88 st.update()
89 })
90
91 ready(function () {
92 st.update()
93 })
94}())
95
96void (function () {
97 onmount('.footer-nav', function (b) {
98 b.sc = Scrollclass(this, {
99 className: '-expanded',
100 onscroll: function (y) {
101 return this.maxScroll - y < 88
102 }
103 })
104 }, function (b) {
105 b.sc.destroy()
106 })
107}())
108
109void (function () {
110 onmount('.header-nav', function (b) {
111 b.sc = Scrollclass(this, {
112 className: '-expanded',
113 onscroll: function (y) { return y < 40 }
114 })
115 }, function (b) {
116 b.sc.destroy()
117 })
118}())