]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/menu/menu.service.ts
Fix process delete promise return
[github/Chocobozzz/PeerTube.git] / client / src / app / core / menu / menu.service.ts
CommitLineData
3b20bdd6
RK
1import { fromEvent } from 'rxjs'
2import { debounceTime } from 'rxjs/operators'
67ed6552 3import { Injectable } from '@angular/core'
2539932e
C
4import { GlobalIconName } from '@app/shared/shared-icons'
5import { sortObjectComparator } from '@shared/core-utils/miscs/miscs'
2989628b 6import { HTMLServerConfig } from '@shared/models/server'
67ed6552 7import { ScreenService } from '../wrappers'
3b20bdd6 8
2539932e
C
9export type MenuLink = {
10 icon: GlobalIconName
11 label: string
12 menuLabel: string
13 path: string
14 priority: number
15}
16
3b20bdd6
RK
17@Injectable()
18export class MenuService {
19 isMenuDisplayed = true
20 isMenuChangedByUser = false
8544d8f5 21 menuWidth = 240 // should be kept equal to $menu-width
3b20bdd6 22
ac940348 23 constructor (
3b20bdd6
RK
24 private screenService: ScreenService
25 ) {
245b9d27
K
26 // Do not display menu on small or touch screens
27 if (this.screenService.isInSmallView() || this.screenService.isInTouchScreen()) {
28 this.setMenuDisplay(false)
3b20bdd6
RK
29 }
30
245b9d27 31 this.handleWindowResize()
3b20bdd6
RK
32 }
33
34 toggleMenu () {
245b9d27 35 this.setMenuDisplay(!this.isMenuDisplayed)
3b20bdd6
RK
36 this.isMenuChangedByUser = true
37 }
38
d95bc702
C
39 isDisplayed () {
40 return this.isMenuDisplayed
41 }
42
245b9d27
K
43 setMenuDisplay (display: boolean) {
44 this.isMenuDisplayed = display
45
1bfc7b73
C
46 if (!this.screenService.isInTouchScreen()) return
47
245b9d27 48 // On touch screens, lock body scroll and display content overlay when memu is opened
1bfc7b73
C
49 if (this.isMenuDisplayed) {
50 document.body.classList.add('menu-open')
51 this.screenService.onFingerSwipe('left', () => { this.setMenuDisplay(false) })
52 return
245b9d27 53 }
1bfc7b73
C
54
55 document.body.classList.remove('menu-open')
245b9d27
K
56 }
57
3b20bdd6
RK
58 onResize () {
59 this.isMenuDisplayed = window.innerWidth >= 800 && !this.isMenuChangedByUser
60 }
245b9d27 61
2989628b 62 buildCommonLinks (config: HTMLServerConfig) {
2539932e
C
63 let entries: MenuLink[] = [
64 {
65 icon: 'globe' as 'globe',
66 label: $localize`Discover videos`,
67 menuLabel: $localize`Discover`,
68 path: '/videos/overview',
69 priority: 150
70 },
71 {
72 icon: 'trending' as 'trending',
73 label: $localize`Trending videos`,
74 menuLabel: $localize`Trending`,
75 path: '/videos/trending',
76 priority: 140
77 },
78 {
79 icon: 'recently-added' as 'recently-added',
80 label: $localize`Recently added videos`,
81 menuLabel: $localize`Recently added`,
82 path: '/videos/recently-added',
83 priority: 130
84 },
85 {
5351a058 86 icon: 'local' as 'local',
2539932e
C
87 label: $localize`Local videos`,
88 menuLabel: $localize`Local videos`,
89 path: '/videos/local',
90 priority: 120
91 }
92 ]
93
94 if (config.homepage.enabled) {
95 entries.push({
96 icon: 'home' as 'home',
97 label: $localize`Home`,
98 menuLabel: $localize`Home`,
99 path: '/home',
100 priority: 160
101 })
102 }
103
104 entries = entries.sort(sortObjectComparator('priority', 'desc'))
105
106 return entries
107 }
108
245b9d27
K
109 private handleWindowResize () {
110 // On touch screens, do not handle window resize event since opened menu is handled with a content overlay
1bfc7b73 111 if (this.screenService.isInTouchScreen()) return
245b9d27
K
112
113 fromEvent(window, 'resize')
114 .pipe(debounceTime(200))
115 .subscribe(() => this.onResize())
116 }
3b20bdd6 117}