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