/
scripts.js
160 lines (131 loc) · 4.53 KB
/
scripts.js
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
/* global ga:false, Drawer: false, ImageLoader: false, NProgress: false, Prism: false */
(function ($, window, document, undefined) {
'use strict';
$(function () {
// Cache a couple of useful elements
// =================
var $window = $(window),
$document = $(document),
$html = $(document.documentElement),
$body = $(document.body),
$surface = $('.surface-container', $body);
// Drawer bindings
// =================
Drawer.init();
// PrismJS handler
// =================
Prism.languages.html = Prism.languages.markup;
var _prismHandler = function() {
$('code').not('[class*="language-"]').addClass(function() {
var _lang = $(this).attr('class') || 'markup';
_lang = _lang.replace(/(language|lang)+\-/gi, '');
return 'language-' + (Prism.languages.hasOwnProperty(_lang) ? _lang : 'markup');
});
Prism.highlightAll();
};
_prismHandler();
// PJax bindings
// =================
if ($.support.pjax) {
$document.on('pjax:start', function() {
NProgress.start();
$surface.animate({
scrollTop: 0
}, 500);
if(Drawer.isOpen())
Drawer.close();
});
$document.on('pjax:end', function() {
if(ga !== undefined) {
ga('set', 'location', window.location.href);
ga('send', 'pageview');
}
_prismHandler();
NProgress.done();
});
$document.pjax('a[data-pjax]', '[data-pjax-container]');
$document.on('submit', 'form[data-pjax]', function(e) {
$.pjax.submit(e, '[data-pjax-container]');
});
}
// Data API bindings
// =================
$document.on('click', '[data-action]', function(e) {
var _self = $(this),
action = _self.data('action');
var _openWindow = function(url, h, w) {
var dualScreenLeft = window.screenLeft !== undefined ? window.screenLeft : screen.left,
dualScreenTop = window.screenTop !== undefined ? window.screenTop : screen.top,
left = ((screen.width / 2) - (w / 2)) + dualScreenLeft,
top = ((screen.height / 2) - (h / 2)) + dualScreenTop;
var newWindow = window.open(url, '',
'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no' +
', width=' + w +
', height=' + h +
', top=' + top +
', left=' + left);
// Puts focus on the newWindow
if (window.focus) {
newWindow.focus();
}
return newWindow;
};
e.preventDefault();
switch(action) {
case 'open-drawer':
Drawer.open();
break;
case 'close-drawer':
Drawer.close();
break;
case 'share-gplus':
_openWindow(
'https://plus.google.com/share?url=' + encodeURIComponent(location.href),
600, 600);
break;
case 'share-facebook':
_openWindow(
'https://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(location.href),
436, 626);
break;
case 'share-twitter':
_openWindow(
'https://twitter.com/share?url=' + encodeURIComponent(location.href) + '&text=' + encodeURIComponent(document.title),
440, 550);
break;
}
return false;
});
// Async load images
$('[data-load-image]', $body).each(function() {
ImageLoader.load($(this));
});
// Hide drawer button on scroll for best readability
// =================
$surface.on('scroll', function() {
var offset = $surface.scrollTop(),
btn = $('#drawer-button');
if(offset === 0) {
btn.removeClass('drawer-button-hidden');
} else if(!btn.hasClass('drawer-button-hidden')) {
btn.addClass('drawer-button-hidden');
}
});
// Smooth scrolling for same page anchors
// =================
$document.on('click', 'a[href*=#]:not([href=#])', function(e) {
e.preventDefault();
if (location.pathname.replace(/^\//,'') === this.pathname.replace(/^\//,'') && location.hostname === this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$surface.animate({
scrollTop: target.offset().top
}, 500);
location.hash = this.hash;
return false;
}
}
});
});
})(jQuery, window, document);