-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasybook.js
198 lines (175 loc) · 5.96 KB
/
easybook.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*!
* EasyBook Jekyll Theme Javascript
*
* http://laobubu.github.io/jekyll-theme-EasyBook
* https://github.com/laobubu/jekyll-theme-EasyBook
*
* This is just a extension for my theme.
*/
function TOCize(toc, content, matchHeightTo) {
if (!(toc && content && matchHeightTo)) return false
var cnt = 0;
var make = function(tag) {
return document.createElement(tag)
}
var aniscroll = {
to: function(top) {
aniscroll.target = top;
if (aniscroll.running) return;
aniscroll.running = setInterval(aniscroll.tick, 20);
},
target: 0,
running: 0,
getTop: function() {
return window.scrollY || window.pageYOffset || document.documentElement.scrollTop;
},
setTop: function(value) {
(window['scrollTo'] && window.scrollTo(window.scrollX, value))
},
tick: function() {
var oldST = aniscroll.getTop(), newST = ~~((oldST + aniscroll.target) / 2);
aniscroll.setTop(newST);
if (aniscroll.getTop() < newST || Math.abs(newST - aniscroll.target) < 10) {
aniscroll.setTop(aniscroll.target);
clearInterval(aniscroll.running)
aniscroll.running = 0
}
}
}
function scrollToHeader(header, hash, ev) {
var y = header.getBoundingClientRect().top + aniscroll.getTop();
if (window.history['pushState']) {
window.history.pushState({}, header.textContent, "#" + hash);
aniscroll.to(y);
ev.preventDefault();
} else {
var y2 = aniscroll.getTop();
setTimeout(function() {
aniscroll.setTop(y2);
aniscroll.to(y);
}, 0);
}
}
var generateLink = function(h) {
var q = make('a');
cnt++;
var hash = h.getAttribute('id');
if (!hash) {
hash = ('generated-hash-' + cnt);
h.setAttribute('id', hash);
}
q.textContent = h.textContent;
q.setAttribute('href', '#' + hash );
q.addEventListener('click', scrollToHeader.bind(this, h, hash), false);
return q;
};
var hs = content.querySelectorAll('h1, h2, h3, h4, h5, h6');
var cul = null, plevel = 1;
var uls = [make('ul')];
for (var i=0;i<hs.length;i++) {
var level = +hs[i].tagName.substr(1);
var hi = hs[i];
var ti = make('li');
ti.appendChild(generateLink(hi));
if (plevel < level) {
do {
uls.push(make('ul'));
uls[uls.length-2].appendChild(uls[uls.length-1]);
} while (++plevel < level);
} else if (plevel > level) {
do {
cul = uls.pop();
} while (--plevel > level);
}
cul = uls[uls.length-1];
cul.appendChild(ti);
}
while(true) {
var chs = uls[0].children;
if (chs.length == 1 && chs[0].tagName == 'UL')
uls.shift();
else
break;
}
if (!cnt) return false;
var scrolldummy=make('div');
toc.appendChild(scrolldummy);
toc.appendChild(uls[0]);
toc.style.display = 'block';
var maxHeightTOC = '';
var ppc = document.querySelector('.col-main');
var s1 = function(){
var scrollTop=aniscroll.getTop(), dummyClientTop=scrolldummy.getBoundingClientRect().top,
margin = 10,c,d; // c = dummyHeight, d = TOC.maxHeight (+'px')
if ((c = -dummyClientTop + margin) < 0) c = 0;
if (c) {
var wh = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight,
cbox = matchHeightTo.getBoundingClientRect(),
vq = cbox.bottom - dummyClientTop - uls[0].offsetHeight;
if (c>vq) c=vq;
d = (wh - (margin<<1)) + 'px';
} else {
d = "";
}
if (d != maxHeightTOC) { //status lock.
maxHeightTOC = d;
if (d) {
uls[0].setAttribute('style', 'max-height:' + d + '; width:' + (toc.offsetWidth-20) + "px" );
} else {
uls[0].setAttribute("style","");
}
}
scrolldummy.style.height = (c+'px');
};
window.addEventListener('scroll', s1, false);
window.addEventListener('resize', s1, false);
}
function SelectAllize(selector,tips) {
if (!window.getSelection) return null;
var obj = document.querySelectorAll(selector);
var selection=window.getSelection();
var z = document.createElement("div");
z.className = "util-notify1";
z.textContent = tips;
document.body.appendChild(z)
function hide() {
z.classList.add('hidden')
z.style.top = '-200px'
}
hide();
z.addEventListener('mouseover', hide, false);
function clickHandler(e){
if (!selection.isCollapsed) return;
var tt = e.pageY-z.offsetHeight - 15;
z.setAttribute('style', 'left:' + (e.pageX-z.offsetWidth/2) + 'px;top:' + (tt+10) + 'px');
z.classList.remove('hidden');
setTimeout(hide, 1000);
}
function dblClickHandler(e){
selection.selectAllChildren(this);
hide();
}
for(var i = obj.length; i--;) {
var oi = obj[i];
oi.addEventListener('click', clickHandler, false);
oi.addEventListener('dblclick', dblClickHandler, false);
}
return true;
}
function RealLoad(){
TOCize(
document.querySelector('.post-toc'),
document.querySelector('.post-content'),
document.querySelector('.col-main')
);
SelectAllize("pre.highlight", "Dblclick to select all");
var imgs = document.querySelectorAll('.post-content > p > img');
for(var i=imgs.length; i--;){
if (imgs[i].parentElement.childNodes.length === 1) {
imgs[i].classList.add('middle-image');
}
}
}
RealLoad();