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