]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/search/search-filters.component.ts
Fix build
[github/Chocobozzz/PeerTube.git] / client / src / app / search / search-filters.component.ts
CommitLineData
25266908
RK
1import { Component, EventEmitter, Input, OnInit, Output, SimpleChanges } from '@angular/core'
2import { ValidatorFn } from '@angular/forms'
3import { VideoValidatorsService } from '@app/shared'
f8b2c1b4 4import { ServerService } from '@app/core'
0b18f4aa 5import { I18n } from '@ngx-translate/i18n-polyfill'
0b18f4aa
C
6import { AdvancedSearch } from '@app/search/advanced-search.model'
7import { VideoConstant } from '../../../../shared'
8
9@Component({
10 selector: 'my-search-filters',
11 styleUrls: [ './search-filters.component.scss' ],
12 templateUrl: './search-filters.component.html'
13})
14export class SearchFiltersComponent implements OnInit {
15 @Input() advancedSearch: AdvancedSearch = new AdvancedSearch()
16
17 @Output() filtered = new EventEmitter<AdvancedSearch>()
18
8cd7faaa
C
19 videoCategories: VideoConstant<number>[] = []
20 videoLicences: VideoConstant<number>[] = []
0b18f4aa
C
21 videoLanguages: VideoConstant<string>[] = []
22
25266908
RK
23 tagValidators: ValidatorFn[]
24 tagValidatorsMessages: { [ name: string ]: string }
25
0b18f4aa 26 publishedDateRanges: { id: string, label: string }[] = []
cddf4503 27 sorts: { id: string, label: string }[] = []
0b18f4aa
C
28 durationRanges: { id: string, label: string }[] = []
29
30 publishedDateRange: string
31 durationRange: string
32
31d065cc
AM
33 originallyPublishedStartYear: string
34 originallyPublishedEndYear: string
35
0b18f4aa
C
36 constructor (
37 private i18n: I18n,
25266908 38 private videoValidatorsService: VideoValidatorsService,
0b18f4aa
C
39 private serverService: ServerService
40 ) {
25266908
RK
41 this.tagValidators = this.videoValidatorsService.VIDEO_TAGS.VALIDATORS
42 this.tagValidatorsMessages = this.videoValidatorsService.VIDEO_TAGS.MESSAGES
0b18f4aa 43 this.publishedDateRanges = [
25266908
RK
44 {
45 id: undefined,
46 label: this.i18n('Any')
47 },
0b18f4aa
C
48 {
49 id: 'today',
50 label: this.i18n('Today')
51 },
52 {
53 id: 'last_7days',
54 label: this.i18n('Last 7 days')
55 },
56 {
57 id: 'last_30days',
58 label: this.i18n('Last 30 days')
59 },
60 {
61 id: 'last_365days',
62 label: this.i18n('Last 365 days')
63 }
64 ]
65
66 this.durationRanges = [
25266908
RK
67 {
68 id: undefined,
69 label: this.i18n('Any')
70 },
0b18f4aa
C
71 {
72 id: 'short',
cddf4503 73 label: this.i18n('Short (< 4 min)')
0b18f4aa 74 },
0b18f4aa
C
75 {
76 id: 'medium',
cddf4503 77 label: this.i18n('Medium (4-10 min)')
7cf75374
JF
78 },
79 {
80 id: 'long',
81 label: this.i18n('Long (> 10 min)')
cddf4503
C
82 }
83 ]
84
85 this.sorts = [
86 {
87 id: '-match',
88 label: this.i18n('Relevance')
89 },
90 {
91 id: '-publishedAt',
92 label: this.i18n('Publish date')
93 },
94 {
95 id: '-views',
96 label: this.i18n('Views')
0b18f4aa
C
97 }
98 ]
99 }
100
101 ngOnInit () {
baeb429d
C
102 this.serverService.videoCategoriesLoaded.subscribe(() => this.videoCategories = this.serverService.getVideoCategories())
103 this.serverService.videoLicencesLoaded.subscribe(() => this.videoLicences = this.serverService.getVideoLicences())
104 this.serverService.videoLanguagesLoaded.subscribe(() => this.videoLanguages = this.serverService.getVideoLanguages())
0b18f4aa
C
105
106 this.loadFromDurationRange()
107 this.loadFromPublishedRange()
31d065cc 108 this.loadOriginallyPublishedAtYears()
0b18f4aa
C
109 }
110
25266908 111 inputUpdated () {
0b18f4aa
C
112 this.updateModelFromDurationRange()
113 this.updateModelFromPublishedRange()
31d065cc 114 this.updateModelFromOriginallyPublishedAtYears()
25266908 115 }
0b18f4aa 116
25266908
RK
117 formUpdated () {
118 this.inputUpdated()
0b18f4aa
C
119 this.filtered.emit(this.advancedSearch)
120 }
121
03aff3c6
C
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
31d065cc
AM
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
0b18f4aa
C
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
31d065cc
AM
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
0b18f4aa
C
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}