]>
Commit | Line | Data |
---|---|---|
0b18f4aa | 1 | import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core' |
f8b2c1b4 | 2 | import { ServerService } from '@app/core' |
0b18f4aa | 3 | import { I18n } from '@ngx-translate/i18n-polyfill' |
0b18f4aa C |
4 | import { AdvancedSearch } from '@app/search/advanced-search.model' |
5 | import { VideoConstant } from '../../../../shared' | |
6 | ||
7 | @Component({ | |
8 | selector: 'my-search-filters', | |
9 | styleUrls: [ './search-filters.component.scss' ], | |
10 | templateUrl: './search-filters.component.html' | |
11 | }) | |
12 | export class SearchFiltersComponent implements OnInit { | |
13 | @Input() advancedSearch: AdvancedSearch = new AdvancedSearch() | |
14 | ||
15 | @Output() filtered = new EventEmitter<AdvancedSearch>() | |
16 | ||
8cd7faaa C |
17 | videoCategories: VideoConstant<number>[] = [] |
18 | videoLicences: VideoConstant<number>[] = [] | |
0b18f4aa C |
19 | videoLanguages: VideoConstant<string>[] = [] |
20 | ||
21 | publishedDateRanges: { id: string, label: string }[] = [] | |
cddf4503 | 22 | sorts: { id: string, label: string }[] = [] |
0b18f4aa C |
23 | durationRanges: { id: string, label: string }[] = [] |
24 | ||
25 | publishedDateRange: string | |
26 | durationRange: string | |
27 | ||
28 | constructor ( | |
29 | private i18n: I18n, | |
0b18f4aa C |
30 | private serverService: ServerService |
31 | ) { | |
32 | this.publishedDateRanges = [ | |
33 | { | |
34 | id: 'today', | |
35 | label: this.i18n('Today') | |
36 | }, | |
37 | { | |
38 | id: 'last_7days', | |
39 | label: this.i18n('Last 7 days') | |
40 | }, | |
41 | { | |
42 | id: 'last_30days', | |
43 | label: this.i18n('Last 30 days') | |
44 | }, | |
45 | { | |
46 | id: 'last_365days', | |
47 | label: this.i18n('Last 365 days') | |
48 | } | |
49 | ] | |
50 | ||
51 | this.durationRanges = [ | |
52 | { | |
53 | id: 'short', | |
cddf4503 | 54 | label: this.i18n('Short (< 4 min)') |
0b18f4aa C |
55 | }, |
56 | { | |
57 | id: 'long', | |
cddf4503 | 58 | label: this.i18n('Long (> 10 min)') |
0b18f4aa C |
59 | }, |
60 | { | |
61 | id: 'medium', | |
cddf4503 C |
62 | label: this.i18n('Medium (4-10 min)') |
63 | } | |
64 | ] | |
65 | ||
66 | this.sorts = [ | |
67 | { | |
68 | id: '-match', | |
69 | label: this.i18n('Relevance') | |
70 | }, | |
71 | { | |
72 | id: '-publishedAt', | |
73 | label: this.i18n('Publish date') | |
74 | }, | |
75 | { | |
76 | id: '-views', | |
77 | label: this.i18n('Views') | |
0b18f4aa C |
78 | } |
79 | ] | |
80 | } | |
81 | ||
82 | ngOnInit () { | |
83 | this.videoCategories = this.serverService.getVideoCategories() | |
84 | this.videoLicences = this.serverService.getVideoLicences() | |
85 | this.videoLanguages = this.serverService.getVideoLanguages() | |
86 | ||
87 | this.loadFromDurationRange() | |
88 | this.loadFromPublishedRange() | |
89 | } | |
90 | ||
91 | formUpdated () { | |
92 | this.updateModelFromDurationRange() | |
93 | this.updateModelFromPublishedRange() | |
94 | ||
95 | this.filtered.emit(this.advancedSearch) | |
96 | } | |
97 | ||
98 | private loadFromDurationRange () { | |
99 | if (this.advancedSearch.durationMin || this.advancedSearch.durationMax) { | |
100 | const fourMinutes = 60 * 4 | |
101 | const tenMinutes = 60 * 10 | |
102 | ||
103 | if (this.advancedSearch.durationMin === fourMinutes && this.advancedSearch.durationMax === tenMinutes) { | |
104 | this.durationRange = 'medium' | |
105 | } else if (this.advancedSearch.durationMax === fourMinutes) { | |
106 | this.durationRange = 'short' | |
107 | } else if (this.advancedSearch.durationMin === tenMinutes) { | |
108 | this.durationRange = 'long' | |
109 | } | |
110 | } | |
111 | } | |
112 | ||
113 | private loadFromPublishedRange () { | |
114 | if (this.advancedSearch.startDate) { | |
115 | const date = new Date(this.advancedSearch.startDate) | |
116 | const now = new Date() | |
117 | ||
118 | const diff = Math.abs(date.getTime() - now.getTime()) | |
119 | ||
120 | const dayMS = 1000 * 3600 * 24 | |
121 | const numberOfDays = diff / dayMS | |
122 | ||
123 | if (numberOfDays >= 365) this.publishedDateRange = 'last_365days' | |
124 | else if (numberOfDays >= 30) this.publishedDateRange = 'last_30days' | |
125 | else if (numberOfDays >= 7) this.publishedDateRange = 'last_7days' | |
126 | else if (numberOfDays >= 0) this.publishedDateRange = 'today' | |
127 | } | |
128 | } | |
129 | ||
130 | private updateModelFromDurationRange () { | |
131 | if (!this.durationRange) return | |
132 | ||
133 | const fourMinutes = 60 * 4 | |
134 | const tenMinutes = 60 * 10 | |
135 | ||
136 | switch (this.durationRange) { | |
137 | case 'short': | |
138 | this.advancedSearch.durationMin = undefined | |
139 | this.advancedSearch.durationMax = fourMinutes | |
140 | break | |
141 | ||
142 | case 'medium': | |
143 | this.advancedSearch.durationMin = fourMinutes | |
144 | this.advancedSearch.durationMax = tenMinutes | |
145 | break | |
146 | ||
147 | case 'long': | |
148 | this.advancedSearch.durationMin = tenMinutes | |
149 | this.advancedSearch.durationMax = undefined | |
150 | break | |
151 | } | |
152 | } | |
153 | ||
154 | private updateModelFromPublishedRange () { | |
155 | if (!this.publishedDateRange) return | |
156 | ||
157 | // today | |
158 | const date = new Date() | |
159 | date.setHours(0, 0, 0, 0) | |
160 | ||
161 | switch (this.publishedDateRange) { | |
162 | case 'last_7days': | |
163 | date.setDate(date.getDate() - 7) | |
164 | break | |
165 | ||
166 | case 'last_30days': | |
167 | date.setDate(date.getDate() - 30) | |
168 | break | |
169 | ||
170 | case 'last_365days': | |
171 | date.setDate(date.getDate() - 365) | |
172 | break | |
173 | } | |
174 | ||
175 | this.advancedSearch.startDate = date.toISOString() | |
176 | } | |
177 | } |