forked from maksrom/javascript-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout.js
More file actions
executable file
·95 lines (70 loc) · 2.7 KB
/
layout.js
File metadata and controls
executable file
·95 lines (70 loc) · 2.7 KB
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
var requestAnimationFrameId;
var DEBUG = false;
function log() {
if (DEBUG) {
console.log.apply(console, arguments);
}
}
var TABLET_WIDTH = 840;
(function() {
// don't handle onscroll more often than animation
function onWindowScrollAndResizeThrottled() {
log("onWindowScrollAndResizeThrottled", requestAnimationFrameId);
if (requestAnimationFrameId) return;
requestAnimationFrameId = window.requestAnimationFrame(function() {
onWindowScrollAndResize();
requestAnimationFrameId = null;
});
}
window.addEventListener('scroll', onWindowScrollAndResizeThrottled);
window.addEventListener('resize', onWindowScrollAndResizeThrottled);
document.addEventListener('DOMContentLoaded', onWindowScrollAndResizeThrottled);
})();
function compactifySidebar() {
log("compactifySidebar");
var sidebar = document.querySelector('.sidebar');
var sidebarContent = sidebar.querySelector('.sidebar__content');
var sidebarInner = sidebar.querySelector('.sidebar__inner');
var hasStickyFooter = sidebar.classList.contains('sidebar_sticky-footer');
var isCompact = sidebar.classList.contains('sidebar_compact');
if (isCompact) {
var emptySpaceSize;
if (hasStickyFooter) {
emptySpaceSize = sidebarContent.lastElementChild.getBoundingClientRect().top -
sidebarContent.lastElementChild.previousElementSibling.getBoundingClientRect().bottom;
} else {
emptySpaceSize = sidebarContent.getBoundingClientRect().bottom -
sidebarContent.lastElementChild.getBoundingClientRect().bottom;
}
log("decompact?", emptySpaceSize);
// enough space to occupy the full height in decompacted form without scrollbar
if (emptySpaceSize > 150) {
sidebar.classList.remove('sidebar_compact');
}
} else {
log(sidebarInner.scrollHeight, sidebarInner.clientHeight);
if (sidebarInner.scrollHeight > sidebarInner.clientHeight) {
log("compact!");
sidebar.classList.add('sidebar_compact');
}
}
}
function onWindowScrollAndResize() {
var sitetoolbar = document.querySelector('.sitetoolbar');
if (!sitetoolbar) {
log("no sitetoolbar");
return; // page in a no-top-nav layout
}
var sidebar = document.querySelector('.sidebar');
if (sidebar) {
sidebar.style.top = Math.max(sitetoolbar.getBoundingClientRect().bottom, 0) + 'px';
compactifySidebar();
}
setUserScaleIfTablet();
}
function setUserScaleIfTablet() {
var isTablet = document.documentElement.clientWidth <= TABLET_WIDTH;
var content = document.querySelector('meta[name="viewport"]').content;
content = content.replace(/user-scalable=\w+/, 'user-scalable=' + (isTablet ? 'yes' : 'no'));
document.querySelector('meta[name="viewport"]').content = content;
}