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 { ServerConfig, VideoConstant } from '@shared/models'
7 selector: 'my-search-filters',
8 styleUrls: [ './search-filters.component.scss' ],
9 templateUrl: './search-filters.component.html'
11 export class SearchFiltersComponent implements OnInit {
12 @Input() advancedSearch: AdvancedSearch = new AdvancedSearch()
14 @Output() filtered = new EventEmitter<AdvancedSearch>()
16 videoCategories: VideoConstant<number>[] = []
17 videoLicences: VideoConstant<number>[] = []
18 videoLanguages: VideoConstant<string>[] = []
20 publishedDateRanges: { id: string, label: string }[] = []
21 sorts: { id: string, label: string }[] = []
22 durationRanges: { id: string, label: string }[] = []
24 publishedDateRange: string
27 originallyPublishedStartYear: string
28 originallyPublishedEndYear: string
30 private serverConfig: ServerConfig
33 private serverService: ServerService
35 this.publishedDateRanges = [
37 id: 'any_published_date',
42 label: $localize`Today`
46 label: $localize`Last 7 days`
50 label: $localize`Last 30 days`
54 label: $localize`Last 365 days`
58 this.durationRanges = [
65 label: $localize`Short (< 4 min)`
69 label: $localize`Medium (4-10 min)`
73 label: $localize`Long (> 10 min)`
80 label: $localize`Relevance`
84 label: $localize`Publish date`
88 label: $localize`Views`
94 this.serverConfig = this.serverService.getTmpConfig()
95 this.serverService.getConfig()
96 .subscribe(config => this.serverConfig = config)
98 this.serverService.getVideoCategories().subscribe(categories => this.videoCategories = categories)
99 this.serverService.getVideoLicences().subscribe(licences => this.videoLicences = licences)
100 this.serverService.getVideoLanguages().subscribe(languages => this.videoLanguages = languages)
102 this.loadFromDurationRange()
103 this.loadFromPublishedRange()
104 this.loadOriginallyPublishedAtYears()
108 this.updateModelFromDurationRange()
109 this.updateModelFromPublishedRange()
110 this.updateModelFromOriginallyPublishedAtYears()
115 this.filtered.emit(this.advancedSearch)
119 this.advancedSearch.reset()
120 this.durationRange = undefined
121 this.publishedDateRange = undefined
122 this.originallyPublishedStartYear = undefined
123 this.originallyPublishedEndYear = undefined
127 resetField (fieldName: string, value?: any) {
128 this.advancedSearch[fieldName] = value
131 resetLocalField (fieldName: string, value?: any) {
132 this[fieldName] = value
136 resetOriginalPublicationYears () {
137 this.originallyPublishedStartYear = this.originallyPublishedEndYear = undefined
140 isSearchTargetEnabled () {
141 return this.serverConfig.search.searchIndex.enabled && this.serverConfig.search.searchIndex.disableLocalSearch !== true
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()