Spaces:
Sleeping
Sleeping
Update application/static/js/components/navbar.js
Browse files
application/static/js/components/navbar.js
CHANGED
@@ -1,36 +1,73 @@
|
|
1 |
-
//
|
2 |
class Navbar {
|
3 |
constructor(UIManager) {
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
}
|
17 |
|
18 |
run() {
|
19 |
-
console.log("Navbar run called");
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
}
|
34 |
}
|
35 |
|
36 |
-
export default Navbar
|
|
|
1 |
+
// navbar.js
|
2 |
class Navbar {
|
3 |
constructor(UIManager) {
|
4 |
+
this.uiManager = UIManager;
|
5 |
+
this.menu = this.uiManager.menu;
|
6 |
+
this.hamburger = this.uiManager.hamburger;
|
7 |
+
this.container = this.uiManager.container;
|
8 |
+
this.state = true; // true = open, false = closed
|
9 |
+
this.menuWidth = 0;
|
10 |
+
this.containerWidth = this.container.clientWidth;
|
11 |
+
this.NAV_AREA_LARGE = 20; // % of window width
|
12 |
+
this.NAV_AREA_MEDIUM = 25; // % of window width
|
13 |
+
this.NAV_AREA_SMALL = 60; // % of window width
|
14 |
+
this.ANIMATION_STEP = 5; // pixels per frame
|
15 |
+
|
16 |
+
console.log("Navbar constructor: uiManager =", this.uiManager);
|
17 |
+
console.log("Navbar constructor: menu =", this.menu);
|
18 |
+
}
|
19 |
+
|
20 |
+
calculateNavArea() {
|
21 |
+
if (window.innerWidth > 785) {
|
22 |
+
return window.innerWidth < 1100
|
23 |
+
? window.innerWidth * (this.NAV_AREA_MEDIUM / 100)
|
24 |
+
: window.innerWidth * (this.NAV_AREA_LARGE / 100);
|
25 |
+
}
|
26 |
+
return window.innerWidth * (this.NAV_AREA_SMALL / 100);
|
27 |
+
}
|
28 |
+
|
29 |
+
animateNav(action) {
|
30 |
+
if (action === 'open' && this.menuWidth < this.navArea) {
|
31 |
+
this.menuWidth += this.ANIMATION_STEP;
|
32 |
+
} else if (action === 'close' && this.menuWidth > 0) {
|
33 |
+
this.menuWidth -= this.ANIMATION_STEP;
|
34 |
+
} else {
|
35 |
+
return; // Stop animation
|
36 |
+
}
|
37 |
+
|
38 |
+
this.menu.style.width = `${this.menuWidth}px`;
|
39 |
+
if(window.innerWidth>775){
|
40 |
+
this.container.style.width = `${this.containerWidth - this.menuWidth}px`;
|
41 |
+
this.container.style.left = `${this.menuWidth}px`;
|
42 |
+
}
|
43 |
+
requestAnimationFrame(() => this.animateNav(action));
|
44 |
}
|
45 |
|
46 |
run() {
|
47 |
+
console.log("Navbar run called");
|
48 |
+
this.navArea = this.calculateNavArea();
|
49 |
+
this.nav = {
|
50 |
+
open: () => this.animateNav('open'),
|
51 |
+
close: () => this.animateNav('close'),
|
52 |
+
};
|
53 |
+
|
54 |
+
if (window.innerWidth <= 785) {
|
55 |
+
this.state = false;
|
56 |
+
} else {
|
57 |
+
this.nav.open();
|
58 |
+
}
|
59 |
+
|
60 |
+
this.hamburger.addEventListener('click', () => this.toggleState());
|
61 |
+
}
|
62 |
+
|
63 |
+
toggleState() {
|
64 |
+
if (this.state) {
|
65 |
+
this.nav.close();
|
66 |
+
} else {
|
67 |
+
this.nav.open();
|
68 |
+
}
|
69 |
+
this.state = !this.state;
|
70 |
}
|
71 |
}
|
72 |
|
73 |
+
export default Navbar;
|