-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
173 lines (153 loc) · 4.84 KB
/
script.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
var trad = document.getElementById('trad');
var source = document.getElementById('source');
var slider = document.getElementById('slider');
var rmllMention = document.getElementById('rmll');
var readOnly = document.getElementById('read');
var h, w, portrait;
window.onload = init;
window.addEventListener('resize', update);
document.getElementById('button').onclick = displayMenu;
slider.addEventListener('mousedown', sliderListener);
slider.addEventListener('touchstart', sliderTouchListener);
window.addEventListener('scroll', adaptScroll, { passive: true });
readOnly.addEventListener('click', readOnlyView);
function init() {
duplicateNodes();
// setup the needed css to display the source here in case of disabled js
// or for very old browsers
trad.style.overflow = 'hidden';
trad.style.position = 'fixed';
trad.style.top = 0;
source.style.display = 'block';
if (window.scrollY != 0) {
trad.scrollTop = window.scrollY;
}
update();
}
function update() {
h = window.innerHeight;
w = window.innerWidth;
portrait = h > w;
if (portrait) {
changeHeight(h / 1.15);
} else {
changeHeight(h / 1.4);
}
setSourceBlockHeight();
}
function displayMenu() {
function hideMenu() {
menu.style.display = 'none';
if (portrait) {
setTimeout(function () {
window.scrollBy(0, -80)
}, 10);
}
menu.removeEventListener('click', hideMenu);
}
var menu = document.getElementById('menu');
if (menu.style.display === 'block') {
hideMenu();
}
else {
menu.style.display = 'block';
rmllMention.style.display = 'block';
menu.addEventListener('click', hideMenu);
}
}
function sliderListener(e) {
function getY(event) {
changeHeight(event.clientY)
}
function removeListeners(event) {
getY(event);
window.removeEventListener("mousemove", getY);
window.removeEventListener("mouseup", removeListeners);
}
e.preventDefault();
getY(e);
window.addEventListener("mousemove", getY);
window.addEventListener("mouseup", removeListeners);
}
function sliderTouchListener(e) {
function getY(event) {
changeHeight(event.changedTouches[0].clientY)
}
function removeListeners(event) {
getY(event);
window.removeEventListener("touchmove", getY);
window.removeEventListener("touchend", removeListeners);
}
e.preventDefault();
getY(e);
window.addEventListener("touchmove", getY);
window.addEventListener("touchend", removeListeners);
}
function adaptScroll(e) {
var scroll = e.pageY || document.body.scrollTop || window.scrollY;
// Adapt scroll value changes to the fixed 'trad' div
trad.scrollTop = scroll;
// Hide the rmll mention for small screens after scrolling the splash screen
var rmllDisplay = rmllMention.style.display;
var smallScreen = portrait && w < 768 && scroll > h;
if (smallScreen && (rmllDisplay === 'block' || rmllDisplay === '')) {
rmllMention.style.display = 'none';
}
else if (rmllDisplay === 'none' && scroll <= h) {
rmllMention.style.display = 'block';
}
}
function readOnlyView(e) {
var color = readOnly.style.backgroundColor;
if (color === 'black' || color === '') {
readOnly.style.backgroundColor = '#8400ff';
changeHeight(h);
}
else {
readOnly.style.backgroundColor = 'black';
if (portrait) {
changeHeight(h / 1.15);
} else {
changeHeight(h / 1.4);
}
}
}
function changeHeight(y) {
if (y >= h) {
slider.style.bottom = '-5px';
trad.style.height = '100%';
}
else if (y > 0) {
slider.style.bottom = h - y - 5 + 'px';
trad.style.height = y + 'px';
}
else {
slider.style.bottom = h - 5 + 'px';
trad.style.height = 0;
}
}
function setSourceBlockHeight() {
function uniformHeigth(base, adapt, orientation) {
// Adapt each given elements height to its base clone
for (var i = 0, b, a; b = base[i], a = adapt[i]; i++) {
if (orientation) {
a.style.height = b.getBoundingClientRect().width + 'px';
}
else {
a.style.height = b.getBoundingClientRect().height + 'px';
}
}
}
var titles = document.getElementsByTagName('HEADER');
uniformHeigth(titles[1].children, titles[0].children, portrait)
var tags = ['P', 'H2', 'H3', 'H4', 'H5', 'UL']
tags.forEach(function (tag) {
var tradTags = trad.getElementsByTagName(tag);
var sourceTags = source.getElementsByTagName(tag);
uniformHeigth(tradTags, sourceTags)
});
}
function duplicateNodes() {
var main = document.getElementsByTagName('SECTION')[2].cloneNode(true);
source.appendChild(main);
}