1 import { Component, EventEmitter, Input, OnInit, Output, SimpleChanges } from '@angular/core'
2 import { ValidatorFn } from '@angular/forms'
3 import { VideoValidatorsService } from '@app/shared'
4 import { ServerService } from '@app/core'
5 import { I18n } from '@ngx-translate/i18n-polyfill'
6 import { AdvancedSearch } from '@app/search/advanced-search.model'
7 import { VideoConstant } from '../../../../shared'
10 selector: 'my-search-filters',
11 styleUrls: [ './search-filters.component.scss' ],
12 templateUrl: './search-filters.component.html'
14 export class SearchFiltersComponent implements OnInit {
15 @Input() advancedSearch: AdvancedSearch = new AdvancedSearch()
17 @Output() filtered = new EventEmitter<AdvancedSearch>()
19 videoCategories: VideoConstant<number>[] = []
20 videoLicences: VideoConstant<number>[] = []
21 videoLanguages: VideoConstant<string>[] = []
23 tagValidators: ValidatorFn[]
24 tagValidatorsMessages: { [ name: string ]: string }
26 publishedDateRanges: { id: string, label: string }[] = []
27 sorts: { id: string, label: string }[] = []
28 durationRanges: { id: string, label: string }[] = []
30 publishedDateRange: string
33 originallyPublishedStartYear: string
34 originallyPublishedEndYear: string
38 private videoValidatorsService: VideoValidatorsService,
39 private serverService: ServerService
41 this.tagValidators = this.videoValidatorsService.VIDEO_TAGS.VALIDATORS
42 this.tagValidatorsMessages = this.videoValidatorsService.VIDEO_TAGS.MESSAGES
43 this.publishedDateRanges = [
46 label: this.i18n('Any')
50 label: this.i18n('Today')
54 label: this.i18n('Last 7 days')
58 label: this.i18n('Last 30 days')
62 label: this.i18n('Last 365 days')
66 this.durationRanges = [
69 label: this.i18n('Any')
73 label: this.i18n('Short (< 4 min)')
77 label: this.i18n('Medium (4-10 min)')
81 label: this.i18n('Long (> 10 min)')
88 label: this.i18n('Relevance')
92 label: this.i18n('Publish date')
96 label: this.i18n('Views')
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())
106 this.loadFromDurationRange()
107 this.loadFromPublishedRange()
108 this.loadOriginallyPublishedAtYears()
112 this.updateModelFromDurationRange()
113 this.updateModelFromPublishedRange()
114 this.updateModelFromOriginallyPublishedAtYears()
119 this.filtered.emit(this.advancedSearch)
123 this.advancedSearch.reset()
124 this.durationRange = undefined
125 this.publishedDateRange = undefined
126 this.originallyPublishedStartYear = undefined
127 this.originallyPublishedEndYear = undefined
131 resetField (fieldName: string, value?: any) {
132 this.advancedSearch[fieldName] = value
135 resetLocalField (fieldName: string, value?: any) {
136 this[fieldName] = value
140 resetOriginalPublicationYears () {
141 this.originallyPublishedStartYear = this.originallyPublishedEndYear = undefined
144 private loadOriginallyPublishedAtYears () {
145 this.originallyPublishedStartYear = this.advancedSearch.originallyPublishedStartDate
146 ? new Date(this.advancedSearch.originallyPublishedStartDate).getFullYear().toString()
149 this.originallyPublishedEndYear = this.advancedSearch.originallyPublishedEndDate
150 ? new Date(this.advancedSearch.originallyPublishedEndDate).getFullYear().toString()
154 private loadFromDurationRange () {
155 if (this.advancedSearch.durationMin || this.advancedSearch.durationMax) {
156 const fourMinutes = 60 * 4
157 const tenMinutes = 60 * 10
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'
169 private loadFromPublishedRange () {
170 if (this.advancedSearch.startDate) {
171 const date = new Date(this.advancedSearch.startDate)
172 const now = new Date()
174 const diff = Math.abs(date.getTime() - now.getTime())
176 const dayMS = 1000 * 3600 * 24
177 const numberOfDays = diff / dayMS
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'
186 private updateModelFromOriginallyPublishedAtYears () {
187 const baseDate = new Date()
188 baseDate.setHours(0, 0, 0, 0)
189 baseDate.setMonth(0, 1)
191 if (this.originallyPublishedStartYear) {
192 const year = parseInt(this.originallyPublishedStartYear, 10)
193 const start = new Date(baseDate)
194 start.setFullYear(year)
196 this.advancedSearch.originallyPublishedStartDate = start.toISOString()
198 this.advancedSearch.originallyPublishedStartDate = null
201 if (this.originallyPublishedEndYear) {
202 const year = parseInt(this.originallyPublishedEndYear, 10)
203 const end = new Date(baseDate)
204 end.setFullYear(year)
206 this.advancedSearch.originallyPublishedEndDate = end.toISOString()
208 this.advancedSearch.originallyPublishedEndDate = null
212 private updateModelFromDurationRange () {
213 if (!this.durationRange) return
215 const fourMinutes = 60 * 4
216 const tenMinutes = 60 * 10
218 switch (this.durationRange) {
220 this.advancedSearch.durationMin = undefined
221 this.advancedSearch.durationMax = fourMinutes
225 this.advancedSearch.durationMin = fourMinutes
226 this.advancedSearch.durationMax = tenMinutes
230 this.advancedSearch.durationMin = tenMinutes
231 this.advancedSearch.durationMax = undefined
236 private updateModelFromPublishedRange () {
237 if (!this.publishedDateRange) return
240 const date = new Date()
241 date.setHours(0, 0, 0, 0)
243 switch (this.publishedDateRange) {
245 date.setDate(date.getDate() - 7)
249 date.setDate(date.getDate() - 30)
253 date.setDate(date.getDate() - 365)
257 this.advancedSearch.startDate = date.toISOString()