1 import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'
2 import { ServerService } from '@app/core'
3 import { I18n } from '@ngx-translate/i18n-polyfill'
4 import { AdvancedSearch } from '@app/search/advanced-search.model'
5 import { VideoConstant } from '../../../../shared'
8 selector: 'my-search-filters',
9 styleUrls: [ './search-filters.component.scss' ],
10 templateUrl: './search-filters.component.html'
12 export class SearchFiltersComponent implements OnInit {
13 @Input() advancedSearch: AdvancedSearch = new AdvancedSearch()
15 @Output() filtered = new EventEmitter<AdvancedSearch>()
17 videoCategories: VideoConstant<number>[] = []
18 videoLicences: VideoConstant<number>[] = []
19 videoLanguages: VideoConstant<string>[] = []
21 publishedDateRanges: { id: string, label: string }[] = []
22 sorts: { id: string, label: string }[] = []
23 durationRanges: { id: string, label: string }[] = []
25 publishedDateRange: string
30 private serverService: ServerService
32 this.publishedDateRanges = [
35 label: this.i18n('Today')
39 label: this.i18n('Last 7 days')
43 label: this.i18n('Last 30 days')
47 label: this.i18n('Last 365 days')
51 this.durationRanges = [
54 label: this.i18n('Short (< 4 min)')
58 label: this.i18n('Long (> 10 min)')
62 label: this.i18n('Medium (4-10 min)')
69 label: this.i18n('Relevance')
73 label: this.i18n('Publish date')
77 label: this.i18n('Views')
83 this.videoCategories = this.serverService.getVideoCategories()
84 this.videoLicences = this.serverService.getVideoLicences()
85 this.videoLanguages = this.serverService.getVideoLanguages()
87 this.loadFromDurationRange()
88 this.loadFromPublishedRange()
92 this.updateModelFromDurationRange()
93 this.updateModelFromPublishedRange()
95 this.filtered.emit(this.advancedSearch)
98 private loadFromDurationRange () {
99 if (this.advancedSearch.durationMin || this.advancedSearch.durationMax) {
100 const fourMinutes = 60 * 4
101 const tenMinutes = 60 * 10
103 if (this.advancedSearch.durationMin === fourMinutes && this.advancedSearch.durationMax === tenMinutes) {
104 this.durationRange = 'medium'
105 } else if (this.advancedSearch.durationMax === fourMinutes) {
106 this.durationRange = 'short'
107 } else if (this.advancedSearch.durationMin === tenMinutes) {
108 this.durationRange = 'long'
113 private loadFromPublishedRange () {
114 if (this.advancedSearch.startDate) {
115 const date = new Date(this.advancedSearch.startDate)
116 const now = new Date()
118 const diff = Math.abs(date.getTime() - now.getTime())
120 const dayMS = 1000 * 3600 * 24
121 const numberOfDays = diff / dayMS
123 if (numberOfDays >= 365) this.publishedDateRange = 'last_365days'
124 else if (numberOfDays >= 30) this.publishedDateRange = 'last_30days'
125 else if (numberOfDays >= 7) this.publishedDateRange = 'last_7days'
126 else if (numberOfDays >= 0) this.publishedDateRange = 'today'
130 private updateModelFromDurationRange () {
131 if (!this.durationRange) return
133 const fourMinutes = 60 * 4
134 const tenMinutes = 60 * 10
136 switch (this.durationRange) {
138 this.advancedSearch.durationMin = undefined
139 this.advancedSearch.durationMax = fourMinutes
143 this.advancedSearch.durationMin = fourMinutes
144 this.advancedSearch.durationMax = tenMinutes
148 this.advancedSearch.durationMin = tenMinutes
149 this.advancedSearch.durationMax = undefined
154 private updateModelFromPublishedRange () {
155 if (!this.publishedDateRange) return
158 const date = new Date()
159 date.setHours(0, 0, 0, 0)
161 switch (this.publishedDateRange) {
163 date.setDate(date.getDate() - 7)
167 date.setDate(date.getDate() - 30)
171 date.setDate(date.getDate() - 365)
175 this.advancedSearch.startDate = date.toISOString()