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