-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresizable-page.js
103 lines (91 loc) · 3.16 KB
/
resizable-page.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
document.addEventListener('DOMContentLoaded', onDocumentReady, false);
function onDocumentReady() {
var header = document.querySelector('header');
var footer = document.querySelector('footer');
var aside = document.querySelector('aside');
var section = document.querySelector('section');
var resizer = document.querySelector('.vertical-resizer');
if (resizer != null) {
resizer.addEventListener('mousedown', startDrag, false);
}
var startX, startY, startWidth, startHeight;
// drag and drop to manage the resize feature
function startDrag(e) {
startX = e.clientX;
startY = e.clientY;
startWidth = getWidthOf(aside);
document.documentElement.addEventListener('mousemove', drag, false);
document.documentElement.addEventListener('mouseup', stopDrag, false);
}
function drag(e) {
aside.style.width = (startWidth + e.clientX - startX) + 'px';
section.style.width = (window.innerWidth - getWidthOf(aside)) + 'px';
}
function stopDrag(e) {
document.documentElement.removeEventListener('mousemove', drag, false);
document.documentElement.removeEventListener('mouseup', stopDrag, false);
var asidePercentage = Math.round(getWidthOf(aside) * 100 / window.innerWidth);
setCookie(asidePercentage)
setWidths();
}
// on window resize event
window.addEventListener('resize', windowResized, false);
function windowResized() {
if (window.width > 700) {
var asidePercentage = Math.round(getWidthOf(aside)) * 100 / window.innerWidth;
aside.style.width = asidePercentage + '%';
section.style.width = (100 - asidePercentage) + '%';
setHeights();
}
}
function setHeights() {
var contentHeight = window.innerHeight - getHeightOf(header) - getHeightOf(footer) + 'px';
aside.style.height = section.style.height = contentHeight;
aside.style.top = section.style.top = getHeightOf(header) + 'px';
}
function setWidths() {
var cookieAsideWidth = readFromCookie('asideWidthPercentage');
if (cookieAsideWidth != null && cookieAsideWidth != '') {
aside.style.width = cookieAsideWidth + '%';
section.style.width = (100 - cookieAsideWidth) + '%';
}
else {
aside.style.width = '20%';
section.style.width = '80%';
}
}
// initialize dimensions
if (window.width > 700) {
setHeights();
setWidths();
}
/*** Utils ***/
function getWidthOf(element) {
return parseInt(document.defaultView.getComputedStyle(element).width, 10);
}
function getHeightOf(element) {
if (element == null) {
return 0
}
return parseInt(document.defaultView.getComputedStyle(element).height, 10);
}
/*** handle cookie ***/
function setCookie(asideWidth) {
document.cookie = "asideWidthPercentage=" + asideWidth;
}
function readFromCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
}