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[] = []
26 publishedDateRange: string
29 originallyPublishedStartYear: string
30 originallyPublishedEndYear: string
32 private serverConfig: HTMLServerConfig
35 private serverService: ServerService
37 this.publishedDateRanges = [
40 label: $localize`Today`
44 label: $localize`Last 7 days`
48 label: $localize`Last 30 days`
52 label: $localize`Last 365 days`
56 this.durationRanges = [
59 label: $localize`Short (< 4 min)`
63 label: $localize`Medium (4-10 min)`
67 label: $localize`Long (> 10 min)`
74 label: $localize`Relevance`
78 label: $localize`Publish date`
82 label: $localize`Views`
88 this.serverConfig = this.serverService.getHTMLConfig()
90 this.serverService.getVideoCategories().subscribe(categories => this.videoCategories = categories)
91 this.serverService.getVideoLicences().subscribe(licences => this.videoLicences = licences)
92 this.serverService.getVideoLanguages().subscribe(languages => this.videoLanguages = languages)
94 this.loadFromDurationRange()
95 this.loadFromPublishedRange()
96 this.loadOriginallyPublishedAtYears()
99 onDurationOrPublishedUpdated () {
100 this.updateModelFromDurationRange()
101 this.updateModelFromPublishedRange()
102 this.updateModelFromOriginallyPublishedAtYears()
106 this.onDurationOrPublishedUpdated()
107 this.filtered.emit(this.advancedSearch)
111 this.advancedSearch.reset()
113 this.resetOriginalPublicationYears()
115 this.durationRange = undefined
116 this.publishedDateRange = undefined
118 this.onDurationOrPublishedUpdated()
121 resetField (fieldName: string, value?: any) {
122 this.advancedSearch[fieldName] = value
125 resetLocalField (fieldName: string, value?: any) {
126 this[fieldName] = value
127 this.onDurationOrPublishedUpdated()
130 resetOriginalPublicationYears () {
131 this.originallyPublishedStartYear = this.originallyPublishedEndYear = undefined
134 isSearchTargetEnabled () {
135 return this.serverConfig.search.searchIndex.enabled && this.serverConfig.search.searchIndex.disableLocalSearch !== true
138 private loadOriginallyPublishedAtYears () {
139 this.originallyPublishedStartYear = this.advancedSearch.originallyPublishedStartDate
140 ? new Date(this.advancedSearch.originallyPublishedStartDate).getFullYear().toString()
143 this.originallyPublishedEndYear = this.advancedSearch.originallyPublishedEndDate
144 ? new Date(this.advancedSearch.originallyPublishedEndDate).getFullYear().toString()
148 private loadFromDurationRange () {
149 if (this.advancedSearch.durationMin || this.advancedSearch.durationMax) {
150 const fourMinutes = 60 * 4
151 const tenMinutes = 60 * 10
153 if (this.advancedSearch.durationMin === fourMinutes && this.advancedSearch.durationMax === tenMinutes) {
154 this.durationRange = 'medium'
155 } else if (this.advancedSearch.durationMax === fourMinutes) {
156 this.durationRange = 'short'
157 } else if (this.advancedSearch.durationMin === tenMinutes) {
158 this.durationRange = 'long'
163 private loadFromPublishedRange () {
164 if (this.advancedSearch.startDate) {
165 const date = new Date(this.advancedSearch.startDate)
166 const now = new Date()
168 const diff = Math.abs(date.getTime() - now.getTime())
170 const dayMS = 1000 * 3600 * 24
171 const numberOfDays = diff / dayMS
173 if (numberOfDays >= 365) this.publishedDateRange = 'last_365days'
174 else if (numberOfDays >= 30) this.publishedDateRange = 'last_30days'
175 else if (numberOfDays >= 7) this.publishedDateRange = 'last_7days'
176 else if (numberOfDays >= 0) this.publishedDateRange = 'today'
180 private updateModelFromOriginallyPublishedAtYears () {
181 const baseDate = new Date()
182 baseDate.setHours(0, 0, 0, 0)
183 baseDate.setMonth(0, 1)
185 if (this.originallyPublishedStartYear) {
186 const year = parseInt(this.originallyPublishedStartYear, 10)
187 const start = new Date(baseDate)
188 start.setFullYear(year)
190 this.advancedSearch.originallyPublishedStartDate = start.toISOString()
192 this.advancedSearch.originallyPublishedStartDate = undefined
195 if (this.originallyPublishedEndYear) {
196 const year = parseInt(this.originallyPublishedEndYear, 10)
197 const end = new Date(baseDate)
198 end.setFullYear(year)
200 this.advancedSearch.originallyPublishedEndDate = end.toISOString()
202 this.advancedSearch.originallyPublishedEndDate = undefined
206 private updateModelFromDurationRange () {
207 if (!this.durationRange) return
209 const fourMinutes = 60 * 4
210 const tenMinutes = 60 * 10
212 switch (this.durationRange) {
214 this.advancedSearch.durationMin = undefined
215 this.advancedSearch.durationMax = fourMinutes
219 this.advancedSearch.durationMin = fourMinutes
220 this.advancedSearch.durationMax = tenMinutes
224 this.advancedSearch.durationMin = tenMinutes
225 this.advancedSearch.durationMax = undefined
230 private updateModelFromPublishedRange () {
231 if (!this.publishedDateRange) return
234 const date = new Date()
235 date.setHours(0, 0, 0, 0)
237 switch (this.publishedDateRange) {
239 date.setDate(date.getDate() - 7)
243 date.setDate(date.getDate() - 30)
247 date.setDate(date.getDate() - 365)
251 this.advancedSearch.startDate = date.toISOString()