-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
209 lines (167 loc) · 5.9 KB
/
main.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
199
200
201
202
203
204
205
206
207
208
209
'use strict';
// This file is for main actions on the page
// GLOBAL VARIABLES (DON'T REMOVE ITS!)
var snippetIndex = 0;
//==============================================================================
// ADD SNIPPET ON THE PAGE |
//==============================================================================
function addSnippetOnPage(funcName) {
let snippet = {
main: {
tag: 'div',
class: 'snippet page__snippet',
data: ['index', ++snippetIndex]
},
header: {
tag: 'div',
class: 'snippet__header'
},
title: {
tag: 'h3',
class: 'snippet__title',
},
button: {
tag: 'button',
class: 'button button_collapse snippet__button',
data: ['expandButton', snippetIndex],
type: 'button'
},
body: {
tag: 'div',
class: 'snippet__body',
data: ['expandContent', snippetIndex]
},
code: {
tag: 'pre',
class: 'snippet__code',
},
};
for (let element in snippet) {
let node = snippet[element];
let newElem = document.createElement(node.tag);
newElem.className = node.class;
if (node.data) {
newElem.dataset[node.data[0]] = node.data[1];
};
if (node.type) {
newElem.setAttribute('type', node.type);
};
snippet[element] = newElem;
};
snippet.title.innerHTML = funcName.name;
snippet.code.innerHTML = funcName;
snippet.header.appendChild(snippet.title);
snippet.header.appendChild(snippet.button);
snippet.body.appendChild(snippet.code);
snippet.main.appendChild(snippet.header);
snippet.main.appendChild(snippet.body);
document.querySelector('.page').appendChild(snippet.main);
};
//==============================================================================
// SHOW / HIDE FULL SNIPPET |
//==============================================================================
document.addEventListener('click', function(e) {
if (e.target.dataset.expandButton) {
let index = e.target.dataset.expandButton;
let snippet = this.querySelector(`[data-expand-content="${index}"]`);
toggleVisibility(snippet, 500);
}
});
function toggleVisibility (elem, timing) {
let show = function (elem, timing) {
let getHeight = function () {
elem.style.display = 'block';
let height = elem.scrollHeight + 'px';
elem.style.display = '';
return height;
};
let height = getHeight();
elem.classList.add('is-visible');
elem.style.height = height;
window.setTimeout( function () {
elem.style.height = '';
}, timing);
};
let hide = function (elem, timing) {
elem.style.height = elem.scrollHeight + 'px';
window.setTimeout( function () {
elem.style.height = '0';
}, 1);
window.setTimeout( function () {
elem.classList.remove('is-visible');
}, timing);
};
let toggle = function (elem, timing) {
if (elem.classList.contains('is-visible')) {
hide(elem, timing);
return;
};
show(elem, timing);
};
toggle(elem, timing);
};
//==============================================================================
// STICKY HEADER |
//==============================================================================
var header = document.querySelector('.header');
var headerTitle = document.querySelector('.header__title');
var sticky = header.offsetTop;
window.addEventListener('scroll', function (e) {
if (window.pageYOffset > sticky) {
headerTitle.classList.add('header__title_narrow');
header.classList.add('sticky');
} else {
headerTitle.classList.remove('header__title_narrow');
header.classList.remove('sticky');
}
})
//==============================================================================
// SHOW MORE TEXT |
//==============================================================================
var hiddenText = document.querySelector('[data-show-more]');
textToHide(hiddenText, 300);
function textToHide (node, letters) {
let data = node.innerHTML;
if (data.length <= letters) {
return;
}
let dataCut = data.slice(0, letters) + '...';
let newButton = {
tag: 'button',
attributes: {
class: 'button button_more page__button',
type: 'button',
'data-event': 'show-more'
}
};
let button = createNode(newButton, 'Show More');
node.innerHTML = dataCut;
node.parentNode.appendChild(button);
let isHidden = true;
button.addEventListener('click', function () {
if (isHidden) {
node.innerHTML = data;
button.textContent = 'Hide Text';
} else {
node.innerHTML = dataCut;
button.textContent = 'Show More';
}
isHidden = !isHidden;
})
};
//==============================================================================
// NODE CREATOR |
//==============================================================================
// INFO:
// node = {tag: name[, attributes: {atributeName: value, ...}]}
// inner = some text (not a DOM element)
function createNode (node, inner) {
let newNode = document.createElement(node.tag);
for (name in node.attributes) {
newNode.setAttribute(name, node.attributes[name]);
}
if (inner) {
newNode.innerHTML = inner;
}
return newNode;
};