]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/menu/menu.service.ts
Fix confirm modal containing 2 inputs
[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 4import { GlobalIconName } from '@app/shared/shared-icons'
2989628b 5import { HTMLServerConfig } from '@shared/models/server'
67ed6552 6import { ScreenService } from '../wrappers'
3b20bdd6 7
2539932e
C
8export type MenuLink = {
9 icon: GlobalIconName
77662dae 10 iconClass?: string
8beea2d3 11
2539932e 12 label: string
8beea2d3
C
13 // Used by the left menu for example
14 shortLabel: string
15
2539932e 16 path: string
8beea2d3
C
17}
18
19export type MenuSection = {
20 key: string
21 title: string
22 links: MenuLink[]
2539932e
C
23}
24
3b20bdd6
RK
25@Injectable()
26export class MenuService {
27 isMenuDisplayed = true
28 isMenuChangedByUser = false
9df52d66 29 menuWidth = 240 // should be kept equal to $menu-width
3b20bdd6 30
ac940348 31 constructor (
3b20bdd6
RK
32 private screenService: ScreenService
33 ) {
245b9d27
K
34 // Do not display menu on small or touch screens
35 if (this.screenService.isInSmallView() || this.screenService.isInTouchScreen()) {
36 this.setMenuDisplay(false)
3b20bdd6
RK
37 }
38
245b9d27 39 this.handleWindowResize()
3b20bdd6
RK
40 }
41
42 toggleMenu () {
245b9d27 43 this.setMenuDisplay(!this.isMenuDisplayed)
3b20bdd6
RK
44 this.isMenuChangedByUser = true
45 }
46
d95bc702
C
47 isDisplayed () {
48 return this.isMenuDisplayed
49 }
50
245b9d27
K
51 setMenuDisplay (display: boolean) {
52 this.isMenuDisplayed = display
53
1bfc7b73
C
54 if (!this.screenService.isInTouchScreen()) return
55
245b9d27 56 // On touch screens, lock body scroll and display content overlay when memu is opened
1bfc7b73
C
57 if (this.isMenuDisplayed) {
58 document.body.classList.add('menu-open')
9df52d66 59 this.screenService.onFingerSwipe('left', () => this.setMenuDisplay(false))
1bfc7b73 60 return
245b9d27 61 }
1bfc7b73
C
62
63 document.body.classList.remove('menu-open')
245b9d27
K
64 }
65
3b20bdd6
RK
66 onResize () {
67 this.isMenuDisplayed = window.innerWidth >= 800 && !this.isMenuChangedByUser
68 }
245b9d27 69
8beea2d3
C
70 buildLibraryLinks (userCanSeeVideosLink: boolean): MenuSection {
71 let links: MenuLink[] = []
72
73 if (userCanSeeVideosLink) {
77662dae
C
74 links.push({
75 path: '/my-library/video-channels',
76 icon: 'channel' as GlobalIconName,
77 iconClass: 'channel-icon',
78 shortLabel: $localize`Channels`,
79 label: $localize`My channels`
80 })
81
8beea2d3
C
82 links.push({
83 path: '/my-library/videos',
84 icon: 'videos' as GlobalIconName,
85 shortLabel: $localize`Videos`,
86 label: $localize`My videos`
87 })
88 }
89
90 links = links.concat([
91 {
92 path: '/my-library/video-playlists',
93 icon: 'playlists' as GlobalIconName,
94 shortLabel: $localize`Playlists`,
95 label: $localize`My playlists`
96 },
97 {
98 path: '/videos/subscriptions',
99 icon: 'subscriptions' as GlobalIconName,
100 shortLabel: $localize`Subscriptions`,
101 label: $localize`My subscriptions`
102 },
103 {
104 path: '/my-library/history/videos',
105 icon: 'history' as GlobalIconName,
106 shortLabel: $localize`History`,
107 label: $localize`My history`
108 }
109 ])
110
111 return {
112 key: 'in-my-library',
c3185413 113 title: $localize`In my library`,
8beea2d3
C
114 links
115 }
116 }
117
118 buildCommonLinks (config: HTMLServerConfig): MenuSection {
119 let links: MenuLink[] = []
120
121 if (config.homepage.enabled) {
122 links.push({
123 icon: 'home' as 'home',
124 label: $localize`Home`,
125 shortLabel: $localize`Home`,
126 path: '/home'
127 })
128 }
129
130 links = links.concat([
2539932e
C
131 {
132 icon: 'globe' as 'globe',
133 label: $localize`Discover videos`,
8beea2d3
C
134 shortLabel: $localize`Discover`,
135 path: '/videos/overview'
2539932e
C
136 },
137 {
138 icon: 'trending' as 'trending',
139 label: $localize`Trending videos`,
8beea2d3
C
140 shortLabel: $localize`Trending`,
141 path: '/videos/trending'
2539932e
C
142 },
143 {
dc1296a9 144 icon: 'add' as 'add',
2539932e 145 label: $localize`Recently added videos`,
8beea2d3
C
146 shortLabel: $localize`Recently added`,
147 path: '/videos/recently-added'
2539932e
C
148 },
149 {
5351a058 150 icon: 'local' as 'local',
2539932e 151 label: $localize`Local videos`,
8beea2d3
C
152 shortLabel: $localize`Local videos`,
153 path: '/videos/local'
2539932e 154 }
8beea2d3 155 ])
2539932e 156
8beea2d3
C
157 return {
158 key: 'on-instance',
159 title: $localize`ON ${config.instance.name}`,
160 links
2539932e 161 }
2539932e
C
162 }
163
245b9d27
K
164 private handleWindowResize () {
165 // On touch screens, do not handle window resize event since opened menu is handled with a content overlay
1bfc7b73 166 if (this.screenService.isInTouchScreen()) return
245b9d27
K
167
168 fromEvent(window, 'resize')
169 .pipe(debounceTime(200))
170 .subscribe(() => this.onResize())
171 }
3b20bdd6 172}