]>
Commit | Line | Data |
---|---|---|
1 | import { Component, ComponentFactoryResolver, Injector, OnDestroy, OnInit } from '@angular/core' | |
2 | import { ActivatedRoute, Router } from '@angular/router' | |
3 | import { AuthService, LocalStorageService, Notifier, RedirectService, ScreenService, ServerService, UserService } from '@app/core' | |
4 | import { HooksService } from '@app/core/plugins/hooks.service' | |
5 | import { immutableAssign } from '@app/helpers' | |
6 | import { VideoService } from '@app/shared/shared-main' | |
7 | import { AbstractVideoList } from '@app/shared/shared-video-miniature' | |
8 | import { VideoSortField } from '@shared/models' | |
9 | import { Subscription } from 'rxjs' | |
10 | import { VideoTrendingHeaderComponent } from './video-trending-header.component' | |
11 | ||
12 | @Component({ | |
13 | selector: 'my-videos-hot', | |
14 | styleUrls: [ '../../../shared/shared-video-miniature/abstract-video-list.scss' ], | |
15 | templateUrl: '../../../shared/shared-video-miniature/abstract-video-list.html' | |
16 | }) | |
17 | export class VideoTrendingComponent extends AbstractVideoList implements OnInit, OnDestroy { | |
18 | HeaderComponent = VideoTrendingHeaderComponent | |
19 | titlePage: string | |
20 | defaultSort: VideoSortField = '-trending' | |
21 | ||
22 | useUserVideoPreferences = true | |
23 | ||
24 | private algorithmChangeSub: Subscription | |
25 | ||
26 | constructor ( | |
27 | protected router: Router, | |
28 | protected serverService: ServerService, | |
29 | protected route: ActivatedRoute, | |
30 | protected notifier: Notifier, | |
31 | protected authService: AuthService, | |
32 | protected userService: UserService, | |
33 | protected screenService: ScreenService, | |
34 | protected storageService: LocalStorageService, | |
35 | protected cfr: ComponentFactoryResolver, | |
36 | private videoService: VideoService, | |
37 | private hooks: HooksService | |
38 | ) { | |
39 | super() | |
40 | ||
41 | this.defaultSort = this.parseAlgorithm(RedirectService.DEFAULT_TRENDING_ALGORITHM) | |
42 | ||
43 | this.headerComponentInjector = this.getInjector() | |
44 | } | |
45 | ||
46 | ngOnInit () { | |
47 | super.ngOnInit() | |
48 | ||
49 | this.generateSyndicationList() | |
50 | ||
51 | this.algorithmChangeSub = this.route.queryParams.subscribe( | |
52 | queryParams => { | |
53 | const algorithm = queryParams['alg'] || RedirectService.DEFAULT_TRENDING_ALGORITHM | |
54 | ||
55 | this.sort = this.parseAlgorithm(algorithm) | |
56 | this.reloadVideos() | |
57 | } | |
58 | ) | |
59 | } | |
60 | ||
61 | ngOnDestroy () { | |
62 | super.ngOnDestroy() | |
63 | if (this.algorithmChangeSub) this.algorithmChangeSub.unsubscribe() | |
64 | } | |
65 | ||
66 | getVideosObservable (page: number) { | |
67 | const newPagination = immutableAssign(this.pagination, { currentPage: page }) | |
68 | const params = { | |
69 | videoPagination: newPagination, | |
70 | sort: this.sort, | |
71 | categoryOneOf: this.categoryOneOf, | |
72 | languageOneOf: this.languageOneOf, | |
73 | nsfwPolicy: this.nsfwPolicy, | |
74 | skipCount: true | |
75 | } | |
76 | ||
77 | return this.hooks.wrapObsFun( | |
78 | this.videoService.getVideos.bind(this.videoService), | |
79 | params, | |
80 | 'common', | |
81 | 'filter:api.trending-videos.videos.list.params', | |
82 | 'filter:api.trending-videos.videos.list.result' | |
83 | ) | |
84 | } | |
85 | ||
86 | generateSyndicationList () { | |
87 | this.syndicationItems = this.videoService.getVideoFeedUrls(this.sort, undefined, this.categoryOneOf) | |
88 | } | |
89 | ||
90 | getInjector () { | |
91 | return Injector.create({ | |
92 | providers: [{ | |
93 | provide: 'data', | |
94 | useValue: { | |
95 | model: this.defaultSort | |
96 | } | |
97 | }] | |
98 | }) | |
99 | } | |
100 | ||
101 | private parseAlgorithm (algorithm: string): VideoSortField { | |
102 | switch (algorithm) { | |
103 | case 'most-viewed': | |
104 | return '-trending' | |
105 | case 'most-liked': | |
106 | return '-likes' | |
107 | default: | |
108 | return '-' + algorithm as VideoSortField | |
109 | } | |
110 | } | |
111 | } |