]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/search/search.component.ts
Add advanced search in client
[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
30
31 constructor (
32 private i18n: I18n,
33 private route: ActivatedRoute,
0b18f4aa 34 private router: Router,
57c36b27
C
35 private metaService: MetaService,
36 private redirectService: RedirectService,
37 private notificationsService: NotificationsService,
38 private searchService: SearchService
39 ) { }
40
41 ngOnInit () {
0b18f4aa
C
42 this.advancedSearch = new AdvancedSearch(this.route.snapshot.queryParams)
43 if (this.advancedSearch.containsValues()) this.isSearchFilterCollapsed = false
44
57c36b27
C
45 this.subActivatedRoute = this.route.queryParams.subscribe(
46 queryParams => {
47 const querySearch = queryParams['search']
48
49 if (!querySearch) return this.redirectService.redirectToHomepage()
50 if (querySearch === this.currentSearch) return
51
0b18f4aa
C
52 // Search updated, reset filters
53 if (this.currentSearch) this.advancedSearch.reset()
54
57c36b27
C
55 this.currentSearch = querySearch
56 this.updateTitle()
57
58 this.reload()
59 },
60
61 err => this.notificationsService.error('Error', err.text)
62 )
63 }
64
65 ngOnDestroy () {
66 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
67 }
68
69 search () {
0b18f4aa 70 return this.searchService.searchVideos(this.currentSearch, this.pagination, this.advancedSearch)
57c36b27
C
71 .subscribe(
72 ({ videos, totalVideos }) => {
73 this.videos = this.videos.concat(videos)
74 this.pagination.totalItems = totalVideos
75 },
76
77 error => {
78 this.notificationsService.error(this.i18n('Error'), error.message)
79 }
80 )
81 }
82
83 onNearOfBottom () {
84 // Last page
85 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
86
87 this.pagination.currentPage += 1
88 this.search()
89 }
90
0b18f4aa
C
91 onFiltered () {
92 this.updateUrlFromAdvancedSearch()
93 // Hide the filters
94 this.isSearchFilterCollapsed = true
95
96 this.reload()
97 }
98
57c36b27
C
99 private reload () {
100 this.pagination.currentPage = 1
101 this.pagination.totalItems = null
102
103 this.videos = []
104
105 this.search()
106 }
107
108 private updateTitle () {
109 this.metaService.setTitle(this.i18n('Search') + ' ' + this.currentSearch)
110 }
0b18f4aa
C
111
112 private updateUrlFromAdvancedSearch () {
113 this.router.navigate([], {
114 relativeTo: this.route,
115 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search: this.currentSearch })
116 })
117 }
57c36b27 118}