]>
Commit | Line | Data |
---|---|---|
dd24f1bb C |
1 | import * as debug from 'debug' |
2 | import { Subscription } from 'rxjs' | |
3 | import { Component, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core' | |
4 | import { FormBuilder, FormGroup } from '@angular/forms' | |
5 | import { AuthService } from '@app/core' | |
6 | import { ServerService } from '@app/core/server/server.service' | |
7 | import { UserRight } from '@shared/models' | |
dd24f1bb C |
8 | import { PeertubeModalService } from '../shared-main' |
9 | import { VideoFilters } from './video-filters.model' | |
10 | ||
11 | const logger = debug('peertube:videos:VideoFiltersHeaderComponent') | |
12 | ||
13 | @Component({ | |
14 | selector: 'my-video-filters-header', | |
15 | styleUrls: [ './video-filters-header.component.scss' ], | |
16 | templateUrl: './video-filters-header.component.html' | |
17 | }) | |
18 | export class VideoFiltersHeaderComponent implements OnInit, OnDestroy { | |
19 | @Input() filters: VideoFilters | |
dd24f1bb | 20 | @Input() displayModerationBlock = false |
1b206245 C |
21 | @Input() hideScope = false |
22 | ||
dd24f1bb C |
23 | @Output() filtersChanged = new EventEmitter() |
24 | ||
25 | areFiltersCollapsed = true | |
26 | ||
27 | form: FormGroup | |
28 | ||
29 | private routeSub: Subscription | |
30 | ||
31 | constructor ( | |
32 | private auth: AuthService, | |
33 | private serverService: ServerService, | |
34 | private fb: FormBuilder, | |
35 | private modalService: PeertubeModalService | |
36 | ) { | |
37 | } | |
38 | ||
39 | ngOnInit () { | |
40 | this.form = this.fb.group({ | |
41 | sort: [ '' ], | |
42 | nsfw: [ '' ], | |
43 | languageOneOf: [ '' ], | |
44 | categoryOneOf: [ '' ], | |
45 | scope: [ '' ], | |
46 | allVideos: [ '' ], | |
47 | live: [ '' ] | |
48 | }) | |
49 | ||
50 | this.patchForm(false) | |
51 | ||
52 | this.filters.onChange(() => { | |
53 | this.patchForm(false) | |
54 | }) | |
55 | ||
56 | this.form.valueChanges.subscribe(values => { | |
57 | logger('Loading values from form: %O', values) | |
58 | ||
59 | this.filters.load(values) | |
60 | this.filtersChanged.emit() | |
61 | }) | |
62 | } | |
63 | ||
64 | ngOnDestroy () { | |
65 | if (this.routeSub) this.routeSub.unsubscribe() | |
66 | } | |
67 | ||
68 | canSeeAllVideos () { | |
69 | if (!this.auth.isLoggedIn()) return false | |
70 | if (!this.displayModerationBlock) return false | |
71 | ||
72 | return this.auth.getUser().hasRight(UserRight.SEE_ALL_VIDEOS) | |
73 | } | |
74 | ||
75 | isTrendingSortEnabled (sort: 'most-viewed' | 'hot' | 'best' | 'most-liked') { | |
76 | const serverConfig = this.serverService.getHTMLConfig() | |
77 | ||
78 | const enabled = serverConfig.trending.videos.algorithms.enabled.includes(sort) | |
79 | ||
80 | // Best is adapted from the user | |
81 | if (sort === 'best') return enabled && this.auth.isLoggedIn() | |
82 | ||
83 | return enabled | |
84 | } | |
85 | ||
86 | resetFilter (key: string, canRemove: boolean) { | |
87 | if (!canRemove) return | |
88 | ||
89 | this.filters.reset(key) | |
90 | this.patchForm(false) | |
91 | this.filtersChanged.emit() | |
92 | } | |
93 | ||
94 | getFilterTitle (canRemove: boolean) { | |
95 | if (canRemove) return $localize`Remove this filter` | |
96 | ||
97 | return '' | |
98 | } | |
99 | ||
100 | onAccountSettingsClick (event: Event) { | |
101 | if (this.auth.isLoggedIn()) return | |
102 | ||
103 | event.preventDefault() | |
104 | event.stopPropagation() | |
105 | ||
106 | this.modalService.openQuickSettingsSubject.next() | |
107 | } | |
108 | ||
109 | private patchForm (emitEvent: boolean) { | |
110 | const defaultValues = this.filters.toFormObject() | |
111 | this.form.patchValue(defaultValues, { emitEvent }) | |
112 | ||
113 | logger('Patched form: %O', defaultValues) | |
114 | } | |
115 | } |