-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.js
223 lines (184 loc) · 7.11 KB
/
helper.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
function makeBottomheet(title, height) {
let bottomSheet = addElement("div", document.body, "bottomSheet");
bottomSheet.innerHTML = "<md-elevation></md-elevation>"
let sheetContents = addElement("div", bottomSheet, "sheetContents");
let draggableArea = addElement("div", bottomSheet, "handleHolder");
var btTitle;
if (title !== "") {
btTitle = addElement("div", draggableArea, "bottomSheetTitle");
btTitle.innerHTML = '<p style="margin:0;">' + title + "</p>";
addElement("md-elevation", btTitle);
btTitle.style.margin = "-" + (btTitle.offsetHeight + 34) + "px";
}
let handle = addElement("div", draggableArea, "bottomSheetHandle");
let scrim = addElement("div", document.body, "bottomSheetScrim");
if(height){
scrim.style.display = "none"
scrim.style.visibillity = "hidden"
}
if(title == null){
btTitle.style.display = "none"
btTitle.style.visibillity = "hidden"
}
setTimeout(() => {
scrim.style.opacity = ".32";
}, 10);
scrim.addEventListener("click", function () {
sheetHeight = 0;
onDragEnd();
});
let toolbarColor = document
.querySelector('meta[name="theme-color"]')
.getAttribute("content");
let sheetHeight;
const setSheetHeight = (value) => {
sheetHeight = Math.max(0, Math.min(100, value));
sheetContents.style.height = `${sheetHeight}dvh`;
if (sheetHeight === 100) {
bottomSheet.classList.add("fullscreenSheet");
document
.querySelector('meta[name="theme-color"]')
.setAttribute(
"content",
getComputedStyle(document.body).getPropertyValue(
"--md-sys-color-surface-container"
)
);
} else {
bottomSheet.classList.remove("fullscreenSheet");
document
.querySelector('meta[name="theme-color"]')
.setAttribute("content", toolbarColor);
}
};
const touchPosition = (event) => (event.touches ? event.touches[0] : event);
let dragPosition;
const onDragStart = (event) => {
dragPosition = touchPosition(event).pageY;
sheetContents.classList.add("not-selectable");
vh = Math.max(
document.documentElement.clientHeight || 0,
window.innerHeight || 0
);
};
var vh = Math.max(
document.documentElement.clientHeight || 0,
window.innerHeight || 0
);
var mouseDown = 0;
window.onmousedown = function () {
++mouseDown;
};
window.onmouseup = function () {
--mouseDown;
};
const onDragMove = (event) => {
if ((mouseDown || event.type == "touchmove" ) && event.target.closest('.bottomSheet')) {
const y = touchPosition(event).pageY;
var deltaY = dragPosition - y;
if (
mainContent.innerHTML.includes("md-list") &&
sheetContents.scrollHeight > sheetContents.clientHeight && deltaY>0
){
if(sheetHeight<100)sheetContents.style.overflow = "hidden"; else sheetContents.style.overflow = "scroll"
}
if (
mainContent.innerHTML.includes("md-list") &&
sheetContents.scrollHeight > sheetContents.clientHeight && deltaY<0
){
if(sheetContents.scrollTop>1) deltaY = 0
}
const mainContentHeight = Math.min(
mainContent.clientHeight,
mainContent.scrollHeight
);
sheetHeight3 = (mainContentHeight / vh) * 100;
if (
mainContent.innerHTML.includes("md-list") && sheetHeight < 30 &&deltaY<0
) {
deltaY = 0;
}
const deltaHeight = (deltaY / window.innerHeight) * 100;
setSheetHeight(sheetHeight + deltaHeight);
dragPosition = y;
}
};
const onDragEnd = () => {
setTimeout(() => {
dragPosition = undefined;
sheetContents.classList.remove("not-selectable");
var sheetHeight3;
const mainContentHeight = Math.min(
mainContent.clientHeight,
mainContent.scrollHeight
);
sheetHeight3 = (mainContentHeight / vh) * 100;
if (mainContent.innerHTML.includes("md-list")) {
if (sheetHeight > 65) {
setSheetHeight(100);
} else {
setSheetHeight(30);
}
} else if (sheetHeight > sheetHeight3 + (100 - sheetHeight3) / 2) {
setSheetHeight(100);
}
}, 6);
};
const setIsSheetShown = (isShown) => {
bottomSheet.setAttribute("aria-hidden", String(!isShown));
scrim.style.opacity = "0";
if (title !== "") {
btTitle.style.transform = "scale(1,0)";
}
bottomSheet.addEventListener(
"transitionend",
function (event) {
bottomSheet.remove();
scrim.remove();
},
false
);
};
window.addEventListener("mousedown", onDragStart);
window.addEventListener("touchstart", onDragStart);
window.addEventListener("mousemove", onDragMove);
window.addEventListener("touchmove", onDragMove);
window.addEventListener("mouseup", onDragEnd);
window.addEventListener("touchend", onDragEnd);
let mainContent = addElement("main", sheetContents, "mainSheet");
if(height)setSheetHeight(height); else{
setSheetHeight(
Math.min(sheetContents.offsetHeight, 50, (720 / window.innerHeight) * 100)
);
}
setTimeout(() => {
const observer = new MutationObserver((mutations) =>
mutations.forEach((mutation) => {
if (mutation.type === "childList") {
setTimeout(() => {
const mainContentHeight = Math.min(
mainContent.clientHeight,
mainContent.scrollHeight
); // Adding 60px for padding or margin
// Calculate the percentage height of mainContent relative to the viewport height
const sheetHeight2 = (mainContentHeight / vh) * 100;
// Set the height of .mainSheet using the calculated percentage height
if (mainContent.innerHTML.includes("arrivalsScroll")&& !mainContent.innerHTML.includes('ajokyxw')) {
console.log("kkek")
setSheetHeight(100);
} else {
console.log("kkk")
if(mainContent.innerHTML.includes('ajokyxw')){
setSheetHeight(30);
}else{
setSheetHeight(30);
}
}
}, 10);
}
})
);
observer.observe(mainContent, { childList: true });
}, 1000);
return mainContent;
}