1 import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'
2 import { ServerService } from '@app/core'
3 import { AdvancedSearch } from '@app/shared/shared-search'
4 import { HTMLServerConfig, VideoConstant } from '@shared/models'
6 type FormOption = { id: string, label: string }
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: FormOption[] = []
23 sorts: FormOption[] = []
24 durationRanges: FormOption[] = []
25 videoType: FormOption[] = []
27 publishedDateRange: string
30 originallyPublishedStartYear: string
31 originallyPublishedEndYear: string
33 private serverConfig: HTMLServerConfig
36 private serverService: ServerService
38 this.publishedDateRanges = [
41 label: $localize`Today`
45 label: $localize`Last 7 days`
49 label: $localize`Last 30 days`
53 label: $localize`Last 365 days`
60 label: $localize`VOD videos`
64 label: $localize`Live videos`
68 this.durationRanges = [
71 label: $localize`Short (< 4 min)`
75 label: $localize`Medium (4-10 min)`
79 label: $localize`Long (> 10 min)`
86 label: $localize`Relevance`
90 label: $localize`Publish date`
94 label: $localize`Views`
100 this.serverConfig = this.serverService.getHTMLConfig()
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()
111 onDurationOrPublishedUpdated () {
112 this.updateModelFromDurationRange()
113 this.updateModelFromPublishedRange()
114 this.updateModelFromOriginallyPublishedAtYears()
118 this.onDurationOrPublishedUpdated()
119 this.filtered.emit(this.advancedSearch)
123 this.advancedSearch.reset()
125 this.resetOriginalPublicationYears()
127 this.durationRange = undefined
128 this.publishedDateRange = undefined
130 this.onDurationOrPublishedUpdated()
133 resetField (fieldName: string, value?: any) {
134 this.advancedSearch[fieldName] = value
137 resetLocalField (fieldName: string, value?: any) {
138 this[fieldName] = value
139 this.onDurationOrPublishedUpdated()
142 resetOriginalPublicationYears () {
143 this.originallyPublishedStartYear = this.originallyPublishedEndYear = undefined
146 isSearchTargetEnabled () {
147 return this.serverConfig.search.searchIndex.enabled && this.serverConfig.search.searchIndex.disableLocalSearch !== true
150 private loadOriginallyPublishedAtYears () {
151 this.originallyPublishedStartYear = this.advancedSearch.originallyPublishedStartDate
152 ? new Date(this.advancedSearch.originallyPublishedStartDate).getFullYear().toString()
155 this.originallyPublishedEndYear = this.advancedSearch.originallyPublishedEndDate
156 ? new Date(this.advancedSearch.originallyPublishedEndDate).getFullYear().toString()
160 private loadFromDurationRange () {
161 if (this.advancedSearch.durationMin || this.advancedSearch.durationMax) {
162 const fourMinutes = 60 * 4
163 const tenMinutes = 60 * 10
165 if (this.advancedSearch.durationMin === fourMinutes && this.advancedSearch.durationMax === tenMinutes) {
166 this.durationRange = 'medium'
167 } else if (this.advancedSearch.durationMax === fourMinutes) {
168 this.durationRange = 'short'
169 } else if (this.advancedSearch.durationMin === tenMinutes) {
170 this.durationRange = 'long'
175 private loadFromPublishedRange () {
176 if (this.advancedSearch.startDate) {
177 const date = new Date(this.advancedSearch.startDate)
178 const now = new Date()
180 const diff = Math.abs(date.getTime() - now.getTime())
182 const dayMS = 1000 * 3600 * 24
183 const numberOfDays = diff / dayMS
185 if (numberOfDays >= 365) this.publishedDateRange = 'last_365days'
186 else if (numberOfDays >= 30) this.publishedDateRange = 'last_30days'
187 else if (numberOfDays >= 7) this.publishedDateRange = 'last_7days'
188 else if (numberOfDays >= 0) this.publishedDateRange = 'today'
192 private updateModelFromOriginallyPublishedAtYears () {
193 const baseDate = new Date()
194 baseDate.setHours(0, 0, 0, 0)
195 baseDate.setMonth(0, 1)
197 if (this.originallyPublishedStartYear) {
198 const year = parseInt(this.originallyPublishedStartYear, 10)
199 const start = new Date(baseDate)
200 start.setFullYear(year)
202 this.advancedSearch.originallyPublishedStartDate = start.toISOString()
204 this.advancedSearch.originallyPublishedStartDate = null
207 if (this.originallyPublishedEndYear) {
208 const year = parseInt(this.originallyPublishedEndYear, 10)
209 const end = new Date(baseDate)
210 end.setFullYear(year)
212 this.advancedSearch.originallyPublishedEndDate = end.toISOString()
214 this.advancedSearch.originallyPublishedEndDate = null
218 private updateModelFromDurationRange () {
219 if (!this.durationRange) return
221 const fourMinutes = 60 * 4
222 const tenMinutes = 60 * 10
224 switch (this.durationRange) {
226 this.advancedSearch.durationMin = undefined
227 this.advancedSearch.durationMax = fourMinutes
231 this.advancedSearch.durationMin = fourMinutes
232 this.advancedSearch.durationMax = tenMinutes
236 this.advancedSearch.durationMin = tenMinutes
237 this.advancedSearch.durationMax = undefined
242 private updateModelFromPublishedRange () {
243 if (!this.publishedDateRange) return
246 const date = new Date()
247 date.setHours(0, 0, 0, 0)
249 switch (this.publishedDateRange) {
251 date.setDate(date.getDate() - 7)
255 date.setDate(date.getDate() - 30)
259 date.setDate(date.getDate() - 365)
263 this.advancedSearch.startDate = date.toISOString()