]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/search/search.component.ts
Move send video components inside a dedicated directory
[github/Chocobozzz/PeerTube.git] / client / src / app / search / search.component.ts
CommitLineData
57c36b27 1import { Component, OnDestroy, OnInit } from '@angular/core'
0b18f4aa 2import { ActivatedRoute, Router } from '@angular/router'
57c36b27
C
3import { RedirectService } from '@app/core'
4import { NotificationsService } from 'angular2-notifications'
5import { Subscription } from 'rxjs'
6import { SearchService } from '@app/search/search.service'
7import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
8import { I18n } from '@ngx-translate/i18n-polyfill'
9import { Video } from '../../../../shared'
10import { MetaService } from '@ngx-meta/core'
0b18f4aa 11import { AdvancedSearch } from '@app/search/advanced-search.model'
57c36b27
C
12
13@Component({
14 selector: 'my-search',
15 styleUrls: [ './search.component.scss' ],
16 templateUrl: './search.component.html'
17})
18export class SearchComponent implements OnInit, OnDestroy {
19 videos: Video[] = []
20 pagination: ComponentPagination = {
21 currentPage: 1,
22 itemsPerPage: 10, // It's per object type (so 10 videos, 10 video channels etc)
23 totalItems: null
24 }
0b18f4aa
C
25 advancedSearch: AdvancedSearch = new AdvancedSearch()
26 isSearchFilterCollapsed = true
57c36b27
C
27
28 private subActivatedRoute: Subscription
29 private currentSearch: string
7afea880 30 private isInitialLoad = true
57c36b27
C
31
32 constructor (
33 private i18n: I18n,
34 private route: ActivatedRoute,
0b18f4aa 35 private router: Router,
57c36b27
C
36 private metaService: MetaService,
37 private redirectService: RedirectService,
38 private notificationsService: NotificationsService,
39 private searchService: SearchService
40 ) { }
41
42 ngOnInit () {
43 this.subActivatedRoute = this.route.queryParams.subscribe(
44 queryParams => {
45 const querySearch = queryParams['search']
46
4278710d
C
47 // New empty search
48 if (this.currentSearch && !querySearch) return this.redirectService.redirectToHomepage()
57c36b27 49
0b18f4aa 50 // Search updated, reset filters
7afea880
C
51 if (this.currentSearch !== querySearch) {
52 this.resetPagination()
53 this.advancedSearch.reset()
54
55 this.currentSearch = querySearch
56 this.updateTitle()
57 }
58
59 this.advancedSearch = new AdvancedSearch(queryParams)
0b18f4aa 60
7afea880
C
61 // Don't hide filters if we have some of them AND the user just came on the webpage
62 this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues()
63 this.isInitialLoad = false
57c36b27 64
7afea880 65 this.search()
57c36b27
C
66 },
67
68 err => this.notificationsService.error('Error', err.text)
69 )
70 }
71
72 ngOnDestroy () {
73 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
74 }
75
76 search () {
0b18f4aa 77 return this.searchService.searchVideos(this.currentSearch, this.pagination, this.advancedSearch)
57c36b27
C
78 .subscribe(
79 ({ videos, totalVideos }) => {
80 this.videos = this.videos.concat(videos)
81 this.pagination.totalItems = totalVideos
82 },
83
84 error => {
85 this.notificationsService.error(this.i18n('Error'), error.message)
86 }
87 )
88 }
89
90 onNearOfBottom () {
91 // Last page
92 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
93
94 this.pagination.currentPage += 1
95 this.search()
96 }
97
0b18f4aa 98 onFiltered () {
7afea880 99 this.resetPagination()
0b18f4aa 100
7afea880 101 this.updateUrlFromAdvancedSearch()
0b18f4aa
C
102 }
103
7afea880 104 private resetPagination () {
57c36b27
C
105 this.pagination.currentPage = 1
106 this.pagination.totalItems = null
107
108 this.videos = []
57c36b27
C
109 }
110
111 private updateTitle () {
112 this.metaService.setTitle(this.i18n('Search') + ' ' + this.currentSearch)
113 }
0b18f4aa
C
114
115 private updateUrlFromAdvancedSearch () {
116 this.router.navigate([], {
117 relativeTo: this.route,
118 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search: this.currentSearch })
119 })
120 }
57c36b27 121}