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