]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+search/search-filters.component.ts
aaa4ecc5a8534ece89e4381adf7f130acb86ad0d
[github/Chocobozzz/PeerTube.git] / client / src / app / +search / search-filters.component.ts
1 import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'
2 import { ServerService } from '@app/core'
3 import { AdvancedSearch } from '@app/shared/shared-search'
4 import { HTMLServerConfig, VideoConstant } from '@shared/models'
5
6 type FormOption = { id: string, label: string }
7
8 @Component({
9 selector: 'my-search-filters',
10 styleUrls: [ './search-filters.component.scss' ],
11 templateUrl: './search-filters.component.html'
12 })
13 export class SearchFiltersComponent implements OnInit {
14 @Input() advancedSearch: AdvancedSearch = new AdvancedSearch()
15
16 @Output() filtered = new EventEmitter<AdvancedSearch>()
17
18 videoCategories: VideoConstant<number>[] = []
19 videoLicences: VideoConstant<number>[] = []
20 videoLanguages: VideoConstant<string>[] = []
21
22 publishedDateRanges: FormOption[] = []
23 sorts: FormOption[] = []
24 durationRanges: FormOption[] = []
25
26 publishedDateRange: string
27 durationRange: string
28
29 originallyPublishedStartYear: string
30 originallyPublishedEndYear: string
31
32 private serverConfig: HTMLServerConfig
33
34 constructor (
35 private serverService: ServerService
36 ) {
37 this.publishedDateRanges = [
38 {
39 id: 'today',
40 label: $localize`Today`
41 },
42 {
43 id: 'last_7days',
44 label: $localize`Last 7 days`
45 },
46 {
47 id: 'last_30days',
48 label: $localize`Last 30 days`
49 },
50 {
51 id: 'last_365days',
52 label: $localize`Last 365 days`
53 }
54 ]
55
56 this.durationRanges = [
57 {
58 id: 'short',
59 label: $localize`Short (< 4 min)`
60 },
61 {
62 id: 'medium',
63 label: $localize`Medium (4-10 min)`
64 },
65 {
66 id: 'long',
67 label: $localize`Long (> 10 min)`
68 }
69 ]
70
71 this.sorts = [
72 {
73 id: '-match',
74 label: $localize`Relevance`
75 },
76 {
77 id: '-publishedAt',
78 label: $localize`Publish date`
79 },
80 {
81 id: '-views',
82 label: $localize`Views`
83 }
84 ]
85 }
86
87 ngOnInit () {
88 this.serverConfig = this.serverService.getHTMLConfig()
89
90 this.serverService.getVideoCategories().subscribe(categories => this.videoCategories = categories)
91 this.serverService.getVideoLicences().subscribe(licences => this.videoLicences = licences)
92 this.serverService.getVideoLanguages().subscribe(languages => this.videoLanguages = languages)
93
94 this.loadFromDurationRange()
95 this.loadFromPublishedRange()
96 this.loadOriginallyPublishedAtYears()
97 }
98
99 onDurationOrPublishedUpdated () {
100 this.updateModelFromDurationRange()
101 this.updateModelFromPublishedRange()
102 this.updateModelFromOriginallyPublishedAtYears()
103 }
104
105 formUpdated () {
106 this.onDurationOrPublishedUpdated()
107 this.filtered.emit(this.advancedSearch)
108 }
109
110 reset () {
111 this.advancedSearch.reset()
112
113 this.resetOriginalPublicationYears()
114
115 this.durationRange = undefined
116 this.publishedDateRange = undefined
117
118 this.onDurationOrPublishedUpdated()
119 }
120
121 resetField (fieldName: string, value?: any) {
122 this.advancedSearch[fieldName] = value
123 }
124
125 resetLocalField (fieldName: string, value?: any) {
126 this[fieldName] = value
127 this.onDurationOrPublishedUpdated()
128 }
129
130 resetOriginalPublicationYears () {
131 this.originallyPublishedStartYear = this.originallyPublishedEndYear = undefined
132 }
133
134 isSearchTargetEnabled () {
135 return this.serverConfig.search.searchIndex.enabled && this.serverConfig.search.searchIndex.disableLocalSearch !== true
136 }
137
138 private loadOriginallyPublishedAtYears () {
139 this.originallyPublishedStartYear = this.advancedSearch.originallyPublishedStartDate
140 ? new Date(this.advancedSearch.originallyPublishedStartDate).getFullYear().toString()
141 : null
142
143 this.originallyPublishedEndYear = this.advancedSearch.originallyPublishedEndDate
144 ? new Date(this.advancedSearch.originallyPublishedEndDate).getFullYear().toString()
145 : null
146 }
147
148 private loadFromDurationRange () {
149 if (this.advancedSearch.durationMin || this.advancedSearch.durationMax) {
150 const fourMinutes = 60 * 4
151 const tenMinutes = 60 * 10
152
153 if (this.advancedSearch.durationMin === fourMinutes && this.advancedSearch.durationMax === tenMinutes) {
154 this.durationRange = 'medium'
155 } else if (this.advancedSearch.durationMax === fourMinutes) {
156 this.durationRange = 'short'
157 } else if (this.advancedSearch.durationMin === tenMinutes) {
158 this.durationRange = 'long'
159 }
160 }
161 }
162
163 private loadFromPublishedRange () {
164 if (this.advancedSearch.startDate) {
165 const date = new Date(this.advancedSearch.startDate)
166 const now = new Date()
167
168 const diff = Math.abs(date.getTime() - now.getTime())
169
170 const dayMS = 1000 * 3600 * 24
171 const numberOfDays = diff / dayMS
172
173 if (numberOfDays >= 365) this.publishedDateRange = 'last_365days'
174 else if (numberOfDays >= 30) this.publishedDateRange = 'last_30days'
175 else if (numberOfDays >= 7) this.publishedDateRange = 'last_7days'
176 else if (numberOfDays >= 0) this.publishedDateRange = 'today'
177 }
178 }
179
180 private updateModelFromOriginallyPublishedAtYears () {
181 const baseDate = new Date()
182 baseDate.setHours(0, 0, 0, 0)
183 baseDate.setMonth(0, 1)
184
185 if (this.originallyPublishedStartYear) {
186 const year = parseInt(this.originallyPublishedStartYear, 10)
187 const start = new Date(baseDate)
188 start.setFullYear(year)
189
190 this.advancedSearch.originallyPublishedStartDate = start.toISOString()
191 } else {
192 this.advancedSearch.originallyPublishedStartDate = null
193 }
194
195 if (this.originallyPublishedEndYear) {
196 const year = parseInt(this.originallyPublishedEndYear, 10)
197 const end = new Date(baseDate)
198 end.setFullYear(year)
199
200 this.advancedSearch.originallyPublishedEndDate = end.toISOString()
201 } else {
202 this.advancedSearch.originallyPublishedEndDate = null
203 }
204 }
205
206 private updateModelFromDurationRange () {
207 if (!this.durationRange) return
208
209 const fourMinutes = 60 * 4
210 const tenMinutes = 60 * 10
211
212 switch (this.durationRange) {
213 case 'short':
214 this.advancedSearch.durationMin = undefined
215 this.advancedSearch.durationMax = fourMinutes
216 break
217
218 case 'medium':
219 this.advancedSearch.durationMin = fourMinutes
220 this.advancedSearch.durationMax = tenMinutes
221 break
222
223 case 'long':
224 this.advancedSearch.durationMin = tenMinutes
225 this.advancedSearch.durationMax = undefined
226 break
227 }
228 }
229
230 private updateModelFromPublishedRange () {
231 if (!this.publishedDateRange) return
232
233 // today
234 const date = new Date()
235 date.setHours(0, 0, 0, 0)
236
237 switch (this.publishedDateRange) {
238 case 'last_7days':
239 date.setDate(date.getDate() - 7)
240 break
241
242 case 'last_30days':
243 date.setDate(date.getDate() - 30)
244 break
245
246 case 'last_365days':
247 date.setDate(date.getDate() - 365)
248 break
249 }
250
251 this.advancedSearch.startDate = date.toISOString()
252 }
253 }