]>
Commit | Line | Data |
---|---|---|
57c36b27 | 1 | import { Component, OnDestroy, OnInit } from '@angular/core' |
0b18f4aa | 2 | import { ActivatedRoute, Router } from '@angular/router' |
57c36b27 C |
3 | import { RedirectService } from '@app/core' |
4 | import { NotificationsService } from 'angular2-notifications' | |
f37dc0dd | 5 | import { forkJoin, Subscription } from 'rxjs' |
57c36b27 C |
6 | import { SearchService } from '@app/search/search.service' |
7 | import { ComponentPagination } from '@app/shared/rest/component-pagination.model' | |
8 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
9 | import { Video } from '../../../../shared' | |
10 | import { MetaService } from '@ngx-meta/core' | |
0b18f4aa | 11 | import { AdvancedSearch } from '@app/search/advanced-search.model' |
f37dc0dd C |
12 | import { VideoChannel } from '@app/shared/video-channel/video-channel.model' |
13 | import { immutableAssign } from '@app/shared/misc/utils' | |
57c36b27 C |
14 | |
15 | @Component({ | |
16 | selector: 'my-search', | |
17 | styleUrls: [ './search.component.scss' ], | |
18 | templateUrl: './search.component.html' | |
19 | }) | |
20 | export class SearchComponent implements OnInit, OnDestroy { | |
21 | videos: Video[] = [] | |
f37dc0dd C |
22 | videoChannels: VideoChannel[] = [] |
23 | ||
57c36b27 C |
24 | pagination: ComponentPagination = { |
25 | currentPage: 1, | |
f37dc0dd | 26 | itemsPerPage: 10, // Only for videos, use another variable for channels |
57c36b27 C |
27 | totalItems: null |
28 | } | |
0b18f4aa C |
29 | advancedSearch: AdvancedSearch = new AdvancedSearch() |
30 | isSearchFilterCollapsed = true | |
f37dc0dd | 31 | currentSearch: string |
57c36b27 C |
32 | |
33 | private subActivatedRoute: Subscription | |
7afea880 | 34 | private isInitialLoad = true |
57c36b27 | 35 | |
f37dc0dd C |
36 | private channelsPerPage = 2 |
37 | ||
57c36b27 C |
38 | constructor ( |
39 | private i18n: I18n, | |
40 | private route: ActivatedRoute, | |
0b18f4aa | 41 | private router: Router, |
57c36b27 C |
42 | private metaService: MetaService, |
43 | private redirectService: RedirectService, | |
44 | private notificationsService: NotificationsService, | |
45 | private searchService: SearchService | |
46 | ) { } | |
47 | ||
48 | ngOnInit () { | |
49 | this.subActivatedRoute = this.route.queryParams.subscribe( | |
50 | queryParams => { | |
51 | const querySearch = queryParams['search'] | |
52 | ||
4278710d C |
53 | // New empty search |
54 | if (this.currentSearch && !querySearch) return this.redirectService.redirectToHomepage() | |
57c36b27 | 55 | |
0b18f4aa | 56 | // Search updated, reset filters |
7afea880 C |
57 | if (this.currentSearch !== querySearch) { |
58 | this.resetPagination() | |
59 | this.advancedSearch.reset() | |
60 | ||
61 | this.currentSearch = querySearch | |
62 | this.updateTitle() | |
63 | } | |
64 | ||
65 | this.advancedSearch = new AdvancedSearch(queryParams) | |
0b18f4aa | 66 | |
7afea880 C |
67 | // Don't hide filters if we have some of them AND the user just came on the webpage |
68 | this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues() | |
69 | this.isInitialLoad = false | |
57c36b27 | 70 | |
7afea880 | 71 | this.search() |
57c36b27 C |
72 | }, |
73 | ||
74 | err => this.notificationsService.error('Error', err.text) | |
75 | ) | |
76 | } | |
77 | ||
78 | ngOnDestroy () { | |
79 | if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe() | |
80 | } | |
81 | ||
82 | search () { | |
f37dc0dd C |
83 | forkJoin([ |
84 | this.searchService.searchVideos(this.currentSearch, this.pagination, this.advancedSearch), | |
85 | this.searchService.searchVideoChannels(this.currentSearch, immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage })) | |
86 | ]) | |
57c36b27 | 87 | .subscribe( |
f37dc0dd C |
88 | ([ videosResult, videoChannelsResult ]) => { |
89 | this.videos = this.videos.concat(videosResult.videos) | |
aa55a4da | 90 | this.pagination.totalItems = videosResult.totalVideos + videoChannelsResult.total |
f37dc0dd | 91 | |
aa55a4da C |
92 | this.videoChannels = this.videoChannels.concat(videoChannelsResult.data) |
93 | ||
94 | // Focus on channels | |
95 | if (this.channelsPerPage !== 10 && this.videos.length < this.pagination.itemsPerPage) { | |
96 | this.resetPagination() | |
97 | ||
98 | this.channelsPerPage = 10 | |
99 | this.search() | |
100 | } | |
57c36b27 C |
101 | }, |
102 | ||
103 | error => { | |
104 | this.notificationsService.error(this.i18n('Error'), error.message) | |
105 | } | |
106 | ) | |
f37dc0dd | 107 | |
57c36b27 C |
108 | } |
109 | ||
110 | onNearOfBottom () { | |
111 | // Last page | |
112 | if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return | |
113 | ||
114 | this.pagination.currentPage += 1 | |
115 | this.search() | |
116 | } | |
117 | ||
0b18f4aa | 118 | onFiltered () { |
7afea880 | 119 | this.resetPagination() |
0b18f4aa | 120 | |
7afea880 | 121 | this.updateUrlFromAdvancedSearch() |
0b18f4aa C |
122 | } |
123 | ||
7afea880 | 124 | private resetPagination () { |
57c36b27 C |
125 | this.pagination.currentPage = 1 |
126 | this.pagination.totalItems = null | |
aa55a4da | 127 | this.channelsPerPage = 2 |
57c36b27 C |
128 | |
129 | this.videos = [] | |
aa55a4da | 130 | this.videoChannels = [] |
57c36b27 C |
131 | } |
132 | ||
133 | private updateTitle () { | |
134 | this.metaService.setTitle(this.i18n('Search') + ' ' + this.currentSearch) | |
135 | } | |
0b18f4aa C |
136 | |
137 | private updateUrlFromAdvancedSearch () { | |
138 | this.router.navigate([], { | |
139 | relativeTo: this.route, | |
140 | queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search: this.currentSearch }) | |
141 | }) | |
142 | } | |
57c36b27 | 143 | } |