-
Notifications
You must be signed in to change notification settings - Fork 1
/
podcast-menu.js
87 lines (77 loc) · 2.22 KB
/
podcast-menu.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
import ElementBase from "./lib/element-base.js";
class PodcastMenu extends ElementBase {
static boundMethods = [
"toggleMenu",
"onSelect",
"onBlur",
"onScroll",
"onToggle",
"showMenu",
"hideMenu",
];
constructor() {
super();
var { modal, menu, menuToggle } = this.elements;
menuToggle.addEventListener("click", this.onToggle);
menu
.querySelectorAll("button")
.forEach((b) => b.addEventListener("click", this.onSelect));
menu.addEventListener("blur", this.onBlur);
}
onToggle(e) {
e.preventDefault();
e.stopPropagation();
this.toggleMenu();
}
showMenu() {
var { menu, menuToggle } = this.elements;
menu.removeAttribute("hidden");
this.onScroll();
menuToggle.classList.add("open");
this.dispatch("menu-state", { open: true });
menu.focus({ preventScroll: true });
document
.querySelector("podcast-list")
.addEventListener("scroll", this.onScroll);
}
hideMenu() {
var { menu, menuToggle } = this.elements;
menu.toggleAttribute("hidden", true);
menuToggle.classList.remove("open");
this.dispatch("menu-state", { open: false });
document
.querySelector("podcast-list")
.removeEventListener("scroll", this.onScroll);
}
toggleMenu(hide) {
if (this.elements.menu.hasAttribute("hidden")) {
this.showMenu();
} else {
this.hideMenu();
}
}
onBlur(e) {
setTimeout(this.hideMenu, 100);
}
onSelect(e) {
var action = e.target.dataset.action;
this.hideMenu();
this.dispatch(`menu-action:${action}`);
}
onScroll() {
var { menu, menuToggle } = this.elements;
var menuBounds = menu.getBoundingClientRect();
var container = document.querySelector("podcast-list");
var origin = menuToggle.getBoundingClientRect().top;
// is this too high to be extended above?
if (origin + container.scrollTop < menuBounds.height - 20) {
menu.classList.remove("upward");
} else {
// adjust to scroll
var containerBounds = container.getBoundingClientRect();
var up = origin + menuBounds.height > containerBounds.bottom - 20;
menu.classList.toggle("upward", up);
}
}
}
PodcastMenu.define("podcast-menu", "podcast-menu.html");