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'
9 selector: 'my-search-filters',
10 styleUrls: [ './search-filters.component.scss' ],
11 templateUrl: './search-filters.component.html'
13 export class SearchFiltersComponent implements OnInit {
14 @Input() advancedSearch: AdvancedSearch = new AdvancedSearch()
16 @Output() filtered = new EventEmitter<AdvancedSearch>()
18 videoCategories: VideoConstant<number>[] = []
19 videoLicences: VideoConstant<number>[] = []
20 videoLanguages: VideoConstant<string>[] = []
22 publishedDateRanges: { id: string, label: string }[] = []
23 sorts: { id: string, label: string }[] = []
24 durationRanges: { id: string, label: string }[] = []
26 publishedDateRange: string
29 originallyPublishedStartYear: string
30 originallyPublishedEndYear: string
32 private serverConfig: ServerConfig
36 private videoValidatorsService: VideoValidatorsService,
37 private serverService: ServerService
39 this.publishedDateRanges = [
41 id: 'any_published_date',
42 label: this.i18n('Any')
46 label: this.i18n('Today')
50 label: this.i18n('Last 7 days')
54 label: this.i18n('Last 30 days')
58 label: this.i18n('Last 365 days')
62 this.durationRanges = [
65 label: this.i18n('Any')
69 label: this.i18n('Short (< 4 min)')
73 label: this.i18n('Medium (4-10 min)')
77 label: this.i18n('Long (> 10 min)')
84 label: this.i18n('Relevance')
88 label: this.i18n('Publish date')
92 label: this.i18n('Views')
98 this.serverConfig = this.serverService.getTmpConfig()
99 this.serverService.getConfig()
100 .subscribe(config => this.serverConfig = config)
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)
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 isSearchTargetEnabled () {
145 return this.serverConfig.search.searchIndex.enabled && this.serverConfig.search.searchIndex.disableLocalSearch !== true
148 private loadOriginallyPublishedAtYears () {
149 this.originallyPublishedStartYear = this.advancedSearch.originallyPublishedStartDate
150 ? new Date(this.advancedSearch.originallyPublishedStartDate).getFullYear().toString()
153 this.originallyPublishedEndYear = this.advancedSearch.originallyPublishedEndDate
154 ? new Date(this.advancedSearch.originallyPublishedEndDate).getFullYear().toString()
158 private loadFromDurationRange () {
159 if (this.advancedSearch.durationMin || this.advancedSearch.durationMax) {
160 const fourMinutes = 60 * 4
161 const tenMinutes = 60 * 10
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'
173 private loadFromPublishedRange () {
174 if (this.advancedSearch.startDate) {
175 const date = new Date(this.advancedSearch.startDate)
176 const now = new Date()
178 const diff = Math.abs(date.getTime() - now.getTime())
180 const dayMS = 1000 * 3600 * 24
181 const numberOfDays = diff / dayMS
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'
190 private updateModelFromOriginallyPublishedAtYears () {
191 const baseDate = new Date()
192 baseDate.setHours(0, 0, 0, 0)
193 baseDate.setMonth(0, 1)
195 if (this.originallyPublishedStartYear) {
196 const year = parseInt(this.originallyPublishedStartYear, 10)
197 const start = new Date(baseDate)
198 start.setFullYear(year)
200 this.advancedSearch.originallyPublishedStartDate = start.toISOString()
202 this.advancedSearch.originallyPublishedStartDate = null
205 if (this.originallyPublishedEndYear) {
206 const year = parseInt(this.originallyPublishedEndYear, 10)
207 const end = new Date(baseDate)
208 end.setFullYear(year)
210 this.advancedSearch.originallyPublishedEndDate = end.toISOString()
212 this.advancedSearch.originallyPublishedEndDate = null
216 private updateModelFromDurationRange () {
217 if (!this.durationRange) return
219 const fourMinutes = 60 * 4
220 const tenMinutes = 60 * 10
222 switch (this.durationRange) {
224 this.advancedSearch.durationMin = undefined
225 this.advancedSearch.durationMax = fourMinutes
229 this.advancedSearch.durationMin = fourMinutes
230 this.advancedSearch.durationMax = tenMinutes
234 this.advancedSearch.durationMin = tenMinutes
235 this.advancedSearch.durationMax = undefined
240 private updateModelFromPublishedRange () {
241 if (!this.publishedDateRange) return
244 const date = new Date()
245 date.setHours(0, 0, 0, 0)
247 switch (this.publishedDateRange) {
249 date.setDate(date.getDate() - 7)
253 date.setDate(date.getDate() - 30)
257 date.setDate(date.getDate() - 365)
261 this.advancedSearch.startDate = date.toISOString()