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
28 originallyPublishedStartYear: string
29 originallyPublishedEndYear: string
33 private serverService: ServerService
35 this.publishedDateRanges = [
38 label: this.i18n('Today')
42 label: this.i18n('Last 7 days')
46 label: this.i18n('Last 30 days')
50 label: this.i18n('Last 365 days')
54 this.durationRanges = [
57 label: this.i18n('Short (< 4 min)')
61 label: this.i18n('Medium (4-10 min)')
65 label: this.i18n('Long (> 10 min)')
72 label: this.i18n('Relevance')
76 label: this.i18n('Publish date')
80 label: this.i18n('Views')
86 this.serverService.videoCategoriesLoaded.subscribe(() => this.videoCategories = this.serverService.getVideoCategories())
87 this.serverService.videoLicencesLoaded.subscribe(() => this.videoLicences = this.serverService.getVideoLicences())
88 this.serverService.videoLanguagesLoaded.subscribe(() => this.videoLanguages = this.serverService.getVideoLanguages())
90 this.loadFromDurationRange()
91 this.loadFromPublishedRange()
92 this.loadOriginallyPublishedAtYears()
96 this.updateModelFromDurationRange()
97 this.updateModelFromPublishedRange()
98 this.updateModelFromOriginallyPublishedAtYears()
100 this.filtered.emit(this.advancedSearch)
103 private loadOriginallyPublishedAtYears () {
104 this.originallyPublishedStartYear = this.advancedSearch.originallyPublishedStartDate
105 ? new Date(this.advancedSearch.originallyPublishedStartDate).getFullYear().toString()
108 this.originallyPublishedEndYear = this.advancedSearch.originallyPublishedEndDate
109 ? new Date(this.advancedSearch.originallyPublishedEndDate).getFullYear().toString()
113 private loadFromDurationRange () {
114 if (this.advancedSearch.durationMin || this.advancedSearch.durationMax) {
115 const fourMinutes = 60 * 4
116 const tenMinutes = 60 * 10
118 if (this.advancedSearch.durationMin === fourMinutes && this.advancedSearch.durationMax === tenMinutes) {
119 this.durationRange = 'medium'
120 } else if (this.advancedSearch.durationMax === fourMinutes) {
121 this.durationRange = 'short'
122 } else if (this.advancedSearch.durationMin === tenMinutes) {
123 this.durationRange = 'long'
128 private loadFromPublishedRange () {
129 if (this.advancedSearch.startDate) {
130 const date = new Date(this.advancedSearch.startDate)
131 const now = new Date()
133 const diff = Math.abs(date.getTime() - now.getTime())
135 const dayMS = 1000 * 3600 * 24
136 const numberOfDays = diff / dayMS
138 if (numberOfDays >= 365) this.publishedDateRange = 'last_365days'
139 else if (numberOfDays >= 30) this.publishedDateRange = 'last_30days'
140 else if (numberOfDays >= 7) this.publishedDateRange = 'last_7days'
141 else if (numberOfDays >= 0) this.publishedDateRange = 'today'
145 private updateModelFromOriginallyPublishedAtYears () {
146 const baseDate = new Date()
147 baseDate.setHours(0, 0, 0, 0)
148 baseDate.setMonth(0, 1)
150 if (this.originallyPublishedStartYear) {
151 const year = parseInt(this.originallyPublishedStartYear, 10)
152 const start = new Date(baseDate)
153 start.setFullYear(year)
155 this.advancedSearch.originallyPublishedStartDate = start.toISOString()
157 this.advancedSearch.originallyPublishedStartDate = null
160 if (this.originallyPublishedEndYear) {
161 const year = parseInt(this.originallyPublishedEndYear, 10)
162 const end = new Date(baseDate)
163 end.setFullYear(year)
165 this.advancedSearch.originallyPublishedEndDate = end.toISOString()
167 this.advancedSearch.originallyPublishedEndDate = null
171 private updateModelFromDurationRange () {
172 if (!this.durationRange) return
174 const fourMinutes = 60 * 4
175 const tenMinutes = 60 * 10
177 switch (this.durationRange) {
179 this.advancedSearch.durationMin = undefined
180 this.advancedSearch.durationMax = fourMinutes
184 this.advancedSearch.durationMin = fourMinutes
185 this.advancedSearch.durationMax = tenMinutes
189 this.advancedSearch.durationMin = tenMinutes
190 this.advancedSearch.durationMax = undefined
195 private updateModelFromPublishedRange () {
196 if (!this.publishedDateRange) return
199 const date = new Date()
200 date.setHours(0, 0, 0, 0)
202 switch (this.publishedDateRange) {
204 date.setDate(date.getDate() - 7)
208 date.setDate(date.getDate() - 30)
212 date.setDate(date.getDate() - 365)
216 this.advancedSearch.startDate = date.toISOString()