1 import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'
2 import { ActivatedRoute } from '@angular/router'
3 import { RedirectService, ServerService } from '@app/core'
4 import { NotificationsService } from 'angular2-notifications'
5 import { SearchService } from '@app/search/search.service'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { MetaService } from '@ngx-meta/core'
8 import { AdvancedSearch } from '@app/search/advanced-search.model'
9 import { VideoConstant } from '../../../../shared'
12 selector: 'my-search-filters',
13 styleUrls: [ './search-filters.component.scss' ],
14 templateUrl: './search-filters.component.html'
16 export class SearchFiltersComponent implements OnInit {
17 @Input() advancedSearch: AdvancedSearch = new AdvancedSearch()
19 @Output() filtered = new EventEmitter<AdvancedSearch>()
21 videoCategories: VideoConstant<string>[] = []
22 videoLicences: VideoConstant<string>[] = []
23 videoLanguages: VideoConstant<string>[] = []
25 publishedDateRanges: { id: string, label: string }[] = []
26 durationRanges: { id: string, label: string }[] = []
28 publishedDateRange: string
33 private route: ActivatedRoute,
34 private metaService: MetaService,
35 private redirectService: RedirectService,
36 private notificationsService: NotificationsService,
37 private searchService: SearchService,
38 private serverService: ServerService
40 this.publishedDateRanges = [
43 label: this.i18n('Today')
47 label: this.i18n('Last 7 days')
51 label: this.i18n('Last 30 days')
55 label: this.i18n('Last 365 days')
59 this.durationRanges = [
62 label: this.i18n('Short (< 4 minutes)')
66 label: this.i18n('Long (> 10 minutes)')
70 label: this.i18n('Medium (4-10 minutes)')
76 this.videoCategories = this.serverService.getVideoCategories()
77 this.videoLicences = this.serverService.getVideoLicences()
78 this.videoLanguages = this.serverService.getVideoLanguages()
80 this.loadFromDurationRange()
81 this.loadFromPublishedRange()
85 this.updateModelFromDurationRange()
86 this.updateModelFromPublishedRange()
88 this.filtered.emit(this.advancedSearch)
91 private loadFromDurationRange () {
92 if (this.advancedSearch.durationMin || this.advancedSearch.durationMax) {
93 const fourMinutes = 60 * 4
94 const tenMinutes = 60 * 10
96 if (this.advancedSearch.durationMin === fourMinutes && this.advancedSearch.durationMax === tenMinutes) {
97 this.durationRange = 'medium'
98 } else if (this.advancedSearch.durationMax === fourMinutes) {
99 this.durationRange = 'short'
100 } else if (this.advancedSearch.durationMin === tenMinutes) {
101 this.durationRange = 'long'
106 private loadFromPublishedRange () {
107 if (this.advancedSearch.startDate) {
108 const date = new Date(this.advancedSearch.startDate)
109 const now = new Date()
111 const diff = Math.abs(date.getTime() - now.getTime())
113 const dayMS = 1000 * 3600 * 24
114 const numberOfDays = diff / dayMS
116 if (numberOfDays >= 365) this.publishedDateRange = 'last_365days'
117 else if (numberOfDays >= 30) this.publishedDateRange = 'last_30days'
118 else if (numberOfDays >= 7) this.publishedDateRange = 'last_7days'
119 else if (numberOfDays >= 0) this.publishedDateRange = 'today'
123 private updateModelFromDurationRange () {
124 if (!this.durationRange) return
126 const fourMinutes = 60 * 4
127 const tenMinutes = 60 * 10
129 switch (this.durationRange) {
131 this.advancedSearch.durationMin = undefined
132 this.advancedSearch.durationMax = fourMinutes
136 this.advancedSearch.durationMin = fourMinutes
137 this.advancedSearch.durationMax = tenMinutes
141 this.advancedSearch.durationMin = tenMinutes
142 this.advancedSearch.durationMax = undefined
147 private updateModelFromPublishedRange () {
148 if (!this.publishedDateRange) return
151 const date = new Date()
152 date.setHours(0, 0, 0, 0)
154 switch (this.publishedDateRange) {
156 date.setDate(date.getDate() - 7)
160 date.setDate(date.getDate() - 30)
164 date.setDate(date.getDate() - 365)
168 this.advancedSearch.startDate = date.toISOString()