]>
Commit | Line | Data |
---|---|---|
57c36b27 | 1 | import { Component, OnDestroy, OnInit } from '@angular/core' |
0b18f4aa | 2 | import { ActivatedRoute, Router } from '@angular/router' |
dffd5d12 | 3 | import { AuthService } from '@app/core' |
57c36b27 | 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' | |
57c36b27 | 9 | import { MetaService } from '@ngx-meta/core' |
0b18f4aa | 10 | import { AdvancedSearch } from '@app/search/advanced-search.model' |
f37dc0dd C |
11 | import { VideoChannel } from '@app/shared/video-channel/video-channel.model' |
12 | import { immutableAssign } from '@app/shared/misc/utils' | |
26fabbd6 | 13 | import { Video } from '@app/shared/video/video.model' |
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 { | |
26fabbd6 | 21 | results: (Video | VideoChannel)[] = [] |
f37dc0dd | 22 | |
57c36b27 C |
23 | pagination: ComponentPagination = { |
24 | currentPage: 1, | |
f37dc0dd | 25 | itemsPerPage: 10, // Only for videos, use another variable for channels |
57c36b27 C |
26 | totalItems: null |
27 | } | |
0b18f4aa C |
28 | advancedSearch: AdvancedSearch = new AdvancedSearch() |
29 | isSearchFilterCollapsed = true | |
f37dc0dd | 30 | currentSearch: string |
57c36b27 C |
31 | |
32 | private subActivatedRoute: Subscription | |
c5d04b4f | 33 | private isInitialLoad = false // set to false to show the search filters on first arrival |
b1ee8526 | 34 | private firstSearch = 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 | 42 | private metaService: MetaService, |
57c36b27 | 43 | private notificationsService: NotificationsService, |
26fabbd6 C |
44 | private searchService: SearchService, |
45 | private authService: AuthService | |
57c36b27 C |
46 | ) { } |
47 | ||
48 | ngOnInit () { | |
49 | this.subActivatedRoute = this.route.queryParams.subscribe( | |
50 | queryParams => { | |
51 | const querySearch = queryParams['search'] | |
52 | ||
0b18f4aa | 53 | // Search updated, reset filters |
7afea880 C |
54 | if (this.currentSearch !== querySearch) { |
55 | this.resetPagination() | |
56 | this.advancedSearch.reset() | |
57 | ||
47879669 | 58 | this.currentSearch = querySearch || undefined |
7afea880 C |
59 | this.updateTitle() |
60 | } | |
61 | ||
62 | this.advancedSearch = new AdvancedSearch(queryParams) | |
0b18f4aa | 63 | |
7afea880 C |
64 | // Don't hide filters if we have some of them AND the user just came on the webpage |
65 | this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues() | |
66 | this.isInitialLoad = false | |
57c36b27 | 67 | |
7afea880 | 68 | this.search() |
57c36b27 C |
69 | }, |
70 | ||
71 | err => this.notificationsService.error('Error', err.text) | |
72 | ) | |
73 | } | |
74 | ||
75 | ngOnDestroy () { | |
76 | if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe() | |
77 | } | |
78 | ||
26fabbd6 C |
79 | isVideoChannel (d: VideoChannel | Video): d is VideoChannel { |
80 | return d instanceof VideoChannel | |
81 | } | |
82 | ||
83 | isVideo (v: VideoChannel | Video): v is Video { | |
84 | return v instanceof Video | |
85 | } | |
86 | ||
87 | isUserLoggedIn () { | |
88 | return this.authService.isLoggedIn() | |
89 | } | |
90 | ||
57c36b27 | 91 | search () { |
f37dc0dd C |
92 | forkJoin([ |
93 | this.searchService.searchVideos(this.currentSearch, this.pagination, this.advancedSearch), | |
94 | this.searchService.searchVideoChannels(this.currentSearch, immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage })) | |
95 | ]) | |
57c36b27 | 96 | .subscribe( |
f37dc0dd | 97 | ([ videosResult, videoChannelsResult ]) => { |
26fabbd6 C |
98 | this.results = this.results |
99 | .concat(videoChannelsResult.data) | |
100 | .concat(videosResult.videos) | |
aa55a4da | 101 | this.pagination.totalItems = videosResult.totalVideos + videoChannelsResult.total |
f37dc0dd | 102 | |
b1ee8526 C |
103 | // Focus on channels if there are no enough videos |
104 | if (this.firstSearch === true && videosResult.videos.length < this.pagination.itemsPerPage) { | |
aa55a4da | 105 | this.resetPagination() |
b1ee8526 | 106 | this.firstSearch = false |
aa55a4da C |
107 | |
108 | this.channelsPerPage = 10 | |
109 | this.search() | |
110 | } | |
b1ee8526 C |
111 | |
112 | this.firstSearch = false | |
57c36b27 C |
113 | }, |
114 | ||
115 | error => { | |
116 | this.notificationsService.error(this.i18n('Error'), error.message) | |
117 | } | |
118 | ) | |
f37dc0dd | 119 | |
57c36b27 C |
120 | } |
121 | ||
122 | onNearOfBottom () { | |
123 | // Last page | |
124 | if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return | |
125 | ||
126 | this.pagination.currentPage += 1 | |
127 | this.search() | |
128 | } | |
129 | ||
0b18f4aa | 130 | onFiltered () { |
7afea880 | 131 | this.resetPagination() |
0b18f4aa | 132 | |
7afea880 | 133 | this.updateUrlFromAdvancedSearch() |
0b18f4aa C |
134 | } |
135 | ||
c5d04b4f RK |
136 | numberOfFilters () { |
137 | return this.advancedSearch.size() | |
138 | } | |
139 | ||
7afea880 | 140 | private resetPagination () { |
57c36b27 C |
141 | this.pagination.currentPage = 1 |
142 | this.pagination.totalItems = null | |
aa55a4da | 143 | this.channelsPerPage = 2 |
57c36b27 | 144 | |
26fabbd6 | 145 | this.results = [] |
57c36b27 C |
146 | } |
147 | ||
148 | private updateTitle () { | |
f107470e C |
149 | const suffix = this.currentSearch ? ' ' + this.currentSearch : '' |
150 | this.metaService.setTitle(this.i18n('Search') + suffix) | |
57c36b27 | 151 | } |
0b18f4aa C |
152 | |
153 | private updateUrlFromAdvancedSearch () { | |
47879669 | 154 | const search = this.currentSearch || undefined |
c5d04b4f | 155 | |
0b18f4aa C |
156 | this.router.navigate([], { |
157 | relativeTo: this.route, | |
c5d04b4f | 158 | queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search }) |
0b18f4aa C |
159 | }) |
160 | } | |
57c36b27 | 161 | } |