-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathbottomfloat.js
191 lines (108 loc) · 3.71 KB
/
bottomfloat.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
// function runs in loadFile() in gitsidebar.js
// if isMobile
function updateFloat() {
// close sidebar
toggleSidebar(false);
saveSidebarStateLS();
// show bottom floater
bottomWrapper.classList.remove('hidden');
// if selected file is modified, show flag
if (modifiedFiles[selectedFile.sha] &&
!modifiedFiles[selectedFile.sha].eclipsed) {
bottomFloat.classList.add('modified');
} else {
bottomFloat.classList.remove('modified');
}
if (pushWrapper.classList.contains('checked')) {
pushWrapper.classList.remove('checked');
}
// show selected file name
floatLogo.innerText = selectedFile.name;
}
// open sidebar when clicked on button
sidebarOpen.addEventListener('click', () => {
// if bottom float isn't expanded
if (!liveView.querySelector('.live-frame')) {
toggleSidebar(true);
saveSidebarStateLS();
let selectedEl = fileWrapper.querySelector('.item.selected');
if (selectedEl) {
// scroll to selected file
selectedEl.scrollIntoViewIfNeeded();
}
}
})
const pushAnimDuration = 0.85; // s
const endAnimDuration = 0.18; // s
const checkDelay = (2 - endAnimDuration) * 1000; // ms
function playPushAnimation(element) {
element.classList.add('checked');
window.setTimeout(() => {
element.classList.remove('checked');
}, checkDelay);
}
// show push icon in button
pushWrapper.innerHTML = pushIcon;
// push when clicked on button
pushWrapper.addEventListener('click', async () => {
// get selected file element
let selectedEl = fileWrapper.querySelector('.file.modified[sha="'+ selectedFile.sha +'"]');
// if selected file element is modified
if (selectedEl) {
const dialogResp = await checkPushDialogs();
if (dialogResp === 'return') return;
// play push animation
playPushAnimation(pushWrapper);
// push file
pushFileFromHTML(selectedEl, ('Update ' + selectedEl.innerText));
}
})
// download selected file on click of button
floatDownload.addEventListener('click', downloadSelFile);
// if on mobile
if (isMobile) {
cd.on('scroll', checkBottomFloat, false);
cd.on('blur', () => {
bottomWrapper.classList.remove('hidden');
});
// update on screen resize
bottomWrapper.style.setProperty('--window-height', window.innerHeight + 'px');
window.addEventListener('resize', () => {
// if the window's height changed
if (window.innerHeight != bottomWrapper.prevWindowHeight) {
bottomWrapper.prevWindowHeight = window.innerHeight;
} else {
return;
}
// update bottom float
bottomWrapper.style.setProperty('--window-height', window.innerHeight + 'px');
window.setTimeout(() => {
bottomWrapper.style.setProperty('--window-height', window.innerHeight + 'px');
}, 50);
});
}
// show bottom float when scrolled up
let lastScrollTop = 0;
function checkBottomFloat() {
let st = cd.scrollTop;
// if scrolled down
if (st > lastScrollTop) {
// hide bottom float
bottomWrapper.classList.add('hidden');
// if scrolled to bottom of codeit
if (cd.scrollTop >= (cd.scrollHeight - cd.offsetHeight - 1)) {
// set timeout
window.setTimeout(() => {
// if still on bottom of codeit
if (cd.scrollTop >= (cd.scrollHeight - cd.offsetHeight - 1)) {
// show bottom float
bottomWrapper.classList.remove('hidden');
}
}, 400);
}
} else if (document.activeElement !== cd) { // if scrolled up
// show bottom float
bottomWrapper.classList.remove('hidden');
}
lastScrollTop = st <= 0 ? 0 : st; // for mobile or negative scrolling
}