]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/header/search-typeahead.component.ts
switch from softies to feathericons
[github/Chocobozzz/PeerTube.git] / client / src / app / header / search-typeahead.component.ts
CommitLineData
5fb2e288 1import { of } from 'rxjs'
67ed6552 2import { first, tap } from 'rxjs/operators'
5fb2e288 3import { ListKeyManager } from '@angular/cdk/a11y'
67ed6552 4import { AfterViewChecked, AfterViewInit, Component, OnDestroy, OnInit, QueryList, ViewChildren } from '@angular/core'
4c1c1709 5import { ActivatedRoute, Params, Router } from '@angular/router'
9677fca7 6import { AuthService, ServerService } from '@app/core'
67ed6552 7import { SearchTargetType, ServerConfig } from '@shared/models'
5fb2e288 8import { SuggestionComponent, SuggestionPayload, SuggestionPayloadType } from './suggestion.component'
f409f0c3
RK
9
10@Component({
11 selector: 'my-search-typeahead',
12 templateUrl: './search-typeahead.component.html',
13 styleUrls: [ './search-typeahead.component.scss' ]
14})
5fb2e288
C
15export class SearchTypeaheadComponent implements OnInit, AfterViewInit, AfterViewChecked, OnDestroy {
16 @ViewChildren(SuggestionComponent) suggestionItems: QueryList<SuggestionComponent>
f409f0c3
RK
17
18 hasChannel = false
19 inChannel = false
5fb2e288 20 areSuggestionsOpened = true
f409f0c3 21
9b8a7aa8 22 search = ''
9677fca7 23 serverConfig: ServerConfig
f409f0c3 24
f409f0c3
RK
25 inThisChannelText: string
26
6af662a5 27 keyboardEventsManager: ListKeyManager<SuggestionComponent>
5fb2e288
C
28 results: SuggestionPayload[] = []
29
30 activeSearch: SuggestionPayloadType
31
32 private scheduleKeyboardEventsInit = false
f409f0c3
RK
33
34 constructor (
35 private authService: AuthService,
36 private router: Router,
52cc0d54 37 private route: ActivatedRoute,
9b8a7aa8
RK
38 private serverService: ServerService
39 ) {}
f409f0c3
RK
40
41 ngOnInit () {
71e75ef2 42 this.route.queryParams
36004aa7 43 .pipe(first(params => this.isOnSearch() && params.search !== undefined && params.search !== null))
71e75ef2 44 .subscribe(params => this.search = params.search)
5fb2e288
C
45 }
46
47 ngAfterViewInit () {
9677fca7 48 this.serverService.getConfig()
5fb2e288
C
49 .subscribe(config => {
50 this.serverConfig = config
51
52 this.computeTypeahead()
53
54 this.serverService.configReloaded
55 .subscribe(config => {
56 this.serverConfig = config
57 this.computeTypeahead()
58 })
59 })
f409f0c3
RK
60 }
61
5fb2e288
C
62 ngAfterViewChecked () {
63 if (this.scheduleKeyboardEventsInit && !this.keyboardEventsManager) {
64 // Avoid ExpressionChangedAfterItHasBeenCheckedError errors
65 setTimeout(() => this.initKeyboardEventsManager(), 0)
66 }
6af662a5
RK
67 }
68
5fb2e288
C
69 ngOnDestroy () {
70 if (this.keyboardEventsManager) this.keyboardEventsManager.change.unsubscribe()
6af662a5
RK
71 }
72
5fb2e288 73 areInstructionsDisplayed () {
9b8a7aa8
RK
74 return !this.search
75 }
76
5fb2e288
C
77 showSearchGlobalHelp () {
78 return this.search && this.areSuggestionsOpened && this.keyboardEventsManager?.activeItem?.result?.type === 'search-index'
6af662a5
RK
79 }
80
5fb2e288 81 canSearchAnyURI () {
9b8a7aa8 82 if (!this.serverConfig) return false
5fb2e288 83
9b8a7aa8
RK
84 return this.authService.isLoggedIn()
85 ? this.serverConfig.search.remoteUri.users
86 : this.serverConfig.search.remoteUri.anonymous
87 }
88
89 onSearchChange () {
5fb2e288
C
90 this.computeTypeahead()
91 }
92
93 initKeyboardEventsManager () {
94 if (this.keyboardEventsManager) return
95
96 this.keyboardEventsManager = new ListKeyManager(this.suggestionItems)
97
98 const activeIndex = this.suggestionItems.toArray().findIndex(i => i.result.default === true)
99 if (activeIndex === -1) {
100 console.error('Cannot find active index.', { suggestionItems: this.suggestionItems })
f409f0c3
RK
101 }
102
5fb2e288
C
103 this.updateItemsState(activeIndex)
104
105 this.keyboardEventsManager.change.subscribe(
106 _ => this.updateItemsState()
f409f0c3
RK
107 )
108 }
109
5fb2e288
C
110 computeTypeahead () {
111 const searchIndexConfig = this.serverConfig.search.searchIndex
112
113 if (!this.activeSearch) {
3b0bd70a 114 if (searchIndexConfig.enabled && (searchIndexConfig.isDefaultSearch || searchIndexConfig.disableLocalSearch)) {
5fb2e288 115 this.activeSearch = 'search-index'
3b0bd70a
C
116 } else {
117 this.activeSearch = 'search-instance'
52cc0d54 118 }
5fb2e288
C
119 }
120
121 this.areSuggestionsOpened = true
122 this.results = []
123
124 if (!this.search) return
125
126 if (searchIndexConfig.enabled === false || searchIndexConfig.disableLocalSearch !== true) {
127 this.results.push({
128 text: this.search,
129 type: 'search-instance',
130 default: this.activeSearch === 'search-instance'
131 })
132 }
133
134 if (searchIndexConfig.enabled) {
135 this.results.push({
136 text: this.search,
137 type: 'search-index',
138 default: this.activeSearch === 'search-index'
139 })
140 }
141
142 this.scheduleKeyboardEventsInit = true
52cc0d54
RK
143 }
144
5fb2e288
C
145 updateItemsState (index?: number) {
146 if (index !== undefined) {
147 this.keyboardEventsManager.setActiveItem(index)
148 }
8a979d73 149
5fb2e288
C
150 for (const item of this.suggestionItems) {
151 if (this.keyboardEventsManager.activeItem && this.keyboardEventsManager.activeItem === item) {
152 item.active = true
153 this.activeSearch = item.result.type
154 continue
155 }
8a979d73 156
5fb2e288 157 item.active = false
6af662a5 158 }
5fb2e288 159 }
8a979d73 160
5fb2e288
C
161 onSuggestionlicked (payload: SuggestionPayload) {
162 this.doSearch(this.buildSearchTarget(payload))
163 }
164
165 onSuggestionHover (index: number) {
166 this.updateItemsState(index)
6af662a5
RK
167 }
168
be6343d2 169 handleKey (event: KeyboardEvent) {
8a979d73 170 if (!this.keyboardEventsManager) return
4c1c1709 171
8a979d73 172 switch (event.key) {
4c1c1709
C
173 case 'ArrowDown':
174 case 'ArrowUp':
5fb2e288
C
175 event.stopPropagation()
176
f409f0c3 177 this.keyboardEventsManager.onKeydown(event)
8a979d73 178 break
f409f0c3
RK
179 }
180 }
52cc0d54 181
36004aa7
RK
182 isOnSearch () {
183 return window.location.pathname === '/search'
184 }
185
5fb2e288
C
186 doSearch (searchTarget?: SearchTargetType) {
187 this.areSuggestionsOpened = false
52cc0d54
RK
188 const queryParams: Params = {}
189
36004aa7 190 if (this.isOnSearch() && this.route.snapshot.queryParams) {
52cc0d54
RK
191 Object.assign(queryParams, this.route.snapshot.queryParams)
192 }
193
5fb2e288
C
194 if (!searchTarget) {
195 searchTarget = this.buildSearchTarget(this.keyboardEventsManager.activeItem.result)
196 }
197
198 Object.assign(queryParams, { search: this.search, searchTarget })
52cc0d54
RK
199
200 const o = this.authService.isLoggedIn()
201 ? this.loadUserLanguagesIfNeeded(queryParams)
202 : of(true)
203
204 o.subscribe(() => this.router.navigate([ '/search' ], { queryParams }))
205 }
206
207 private loadUserLanguagesIfNeeded (queryParams: any) {
208 if (queryParams && queryParams.languageOneOf) return of(queryParams)
209
210 return this.authService.userInformationLoaded
211 .pipe(
212 first(),
213 tap(() => Object.assign(queryParams, { languageOneOf: this.authService.getUser().videoLanguages }))
214 )
215 }
5fb2e288
C
216
217 private buildSearchTarget (result: SuggestionPayload): SearchTargetType {
218 if (result.type === 'search-index') {
219 return 'search-index'
220 }
221
222 return 'local'
223 }
f409f0c3 224}