]>
Commit | Line | Data |
---|---|---|
ba430d75 | 1 | import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core' |
f8b2c1b4 | 2 | import { ServerService } from '@app/core' |
1942f11d | 3 | import { AdvancedSearch } from '@app/shared/shared-search' |
67ed6552 | 4 | import { ServerConfig, VideoConstant } from '@shared/models' |
0b18f4aa C |
5 | |
6 | @Component({ | |
7 | selector: 'my-search-filters', | |
8 | styleUrls: [ './search-filters.component.scss' ], | |
9 | templateUrl: './search-filters.component.html' | |
10 | }) | |
11 | export class SearchFiltersComponent implements OnInit { | |
12 | @Input() advancedSearch: AdvancedSearch = new AdvancedSearch() | |
13 | ||
14 | @Output() filtered = new EventEmitter<AdvancedSearch>() | |
15 | ||
8cd7faaa C |
16 | videoCategories: VideoConstant<number>[] = [] |
17 | videoLicences: VideoConstant<number>[] = [] | |
0b18f4aa C |
18 | videoLanguages: VideoConstant<string>[] = [] |
19 | ||
20 | publishedDateRanges: { id: string, label: string }[] = [] | |
cddf4503 | 21 | sorts: { id: string, label: string }[] = [] |
0b18f4aa C |
22 | durationRanges: { id: string, label: string }[] = [] |
23 | ||
24 | publishedDateRange: string | |
25 | durationRange: string | |
26 | ||
31d065cc AM |
27 | originallyPublishedStartYear: string |
28 | originallyPublishedEndYear: string | |
29 | ||
ba430d75 C |
30 | private serverConfig: ServerConfig |
31 | ||
0b18f4aa | 32 | constructor ( |
0b18f4aa C |
33 | private serverService: ServerService |
34 | ) { | |
35 | this.publishedDateRanges = [ | |
25266908 | 36 | { |
5fb2e288 | 37 | id: 'any_published_date', |
66357162 | 38 | label: $localize`Any` |
25266908 | 39 | }, |
0b18f4aa C |
40 | { |
41 | id: 'today', | |
66357162 | 42 | label: $localize`Today` |
0b18f4aa C |
43 | }, |
44 | { | |
45 | id: 'last_7days', | |
66357162 | 46 | label: $localize`Last 7 days` |
0b18f4aa C |
47 | }, |
48 | { | |
49 | id: 'last_30days', | |
66357162 | 50 | label: $localize`Last 30 days` |
0b18f4aa C |
51 | }, |
52 | { | |
53 | id: 'last_365days', | |
66357162 | 54 | label: $localize`Last 365 days` |
0b18f4aa C |
55 | } |
56 | ] | |
57 | ||
58 | this.durationRanges = [ | |
25266908 | 59 | { |
5fb2e288 | 60 | id: 'any_duration', |
66357162 | 61 | label: $localize`Any` |
25266908 | 62 | }, |
0b18f4aa C |
63 | { |
64 | id: 'short', | |
66357162 | 65 | label: $localize`Short (< 4 min)` |
0b18f4aa | 66 | }, |
0b18f4aa C |
67 | { |
68 | id: 'medium', | |
66357162 | 69 | label: $localize`Medium (4-10 min)` |
7cf75374 JF |
70 | }, |
71 | { | |
72 | id: 'long', | |
66357162 | 73 | label: $localize`Long (> 10 min)` |
cddf4503 C |
74 | } |
75 | ] | |
76 | ||
77 | this.sorts = [ | |
78 | { | |
79 | id: '-match', | |
66357162 | 80 | label: $localize`Relevance` |
cddf4503 C |
81 | }, |
82 | { | |
83 | id: '-publishedAt', | |
66357162 | 84 | label: $localize`Publish date` |
cddf4503 C |
85 | }, |
86 | { | |
87 | id: '-views', | |
66357162 | 88 | label: $localize`Views` |
0b18f4aa C |
89 | } |
90 | ] | |
91 | } | |
92 | ||
93 | ngOnInit () { | |
ba430d75 C |
94 | this.serverConfig = this.serverService.getTmpConfig() |
95 | this.serverService.getConfig() | |
96 | .subscribe(config => this.serverConfig = config) | |
97 | ||
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) | |
0b18f4aa C |
101 | |
102 | this.loadFromDurationRange() | |
103 | this.loadFromPublishedRange() | |
31d065cc | 104 | this.loadOriginallyPublishedAtYears() |
0b18f4aa C |
105 | } |
106 | ||
25266908 | 107 | inputUpdated () { |
0b18f4aa C |
108 | this.updateModelFromDurationRange() |
109 | this.updateModelFromPublishedRange() | |
31d065cc | 110 | this.updateModelFromOriginallyPublishedAtYears() |
25266908 | 111 | } |
0b18f4aa | 112 | |
25266908 RK |
113 | formUpdated () { |
114 | this.inputUpdated() | |
0b18f4aa C |
115 | this.filtered.emit(this.advancedSearch) |
116 | } | |
117 | ||
03aff3c6 C |
118 | reset () { |
119 | this.advancedSearch.reset() | |
120 | this.durationRange = undefined | |
121 | this.publishedDateRange = undefined | |
122 | this.originallyPublishedStartYear = undefined | |
123 | this.originallyPublishedEndYear = undefined | |
124 | this.inputUpdated() | |
125 | } | |
126 | ||
127 | resetField (fieldName: string, value?: any) { | |
128 | this.advancedSearch[fieldName] = value | |
129 | } | |
130 | ||
131 | resetLocalField (fieldName: string, value?: any) { | |
132 | this[fieldName] = value | |
133 | this.inputUpdated() | |
134 | } | |
135 | ||
136 | resetOriginalPublicationYears () { | |
137 | this.originallyPublishedStartYear = this.originallyPublishedEndYear = undefined | |
138 | } | |
139 | ||
5fb2e288 C |
140 | isSearchTargetEnabled () { |
141 | return this.serverConfig.search.searchIndex.enabled && this.serverConfig.search.searchIndex.disableLocalSearch !== true | |
142 | } | |
143 | ||
31d065cc AM |
144 | private loadOriginallyPublishedAtYears () { |
145 | this.originallyPublishedStartYear = this.advancedSearch.originallyPublishedStartDate | |
146 | ? new Date(this.advancedSearch.originallyPublishedStartDate).getFullYear().toString() | |
147 | : null | |
148 | ||
149 | this.originallyPublishedEndYear = this.advancedSearch.originallyPublishedEndDate | |
150 | ? new Date(this.advancedSearch.originallyPublishedEndDate).getFullYear().toString() | |
151 | : null | |
152 | } | |
153 | ||
0b18f4aa C |
154 | private loadFromDurationRange () { |
155 | if (this.advancedSearch.durationMin || this.advancedSearch.durationMax) { | |
156 | const fourMinutes = 60 * 4 | |
157 | const tenMinutes = 60 * 10 | |
158 | ||
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' | |
165 | } | |
166 | } | |
167 | } | |
168 | ||
169 | private loadFromPublishedRange () { | |
170 | if (this.advancedSearch.startDate) { | |
171 | const date = new Date(this.advancedSearch.startDate) | |
172 | const now = new Date() | |
173 | ||
174 | const diff = Math.abs(date.getTime() - now.getTime()) | |
175 | ||
176 | const dayMS = 1000 * 3600 * 24 | |
177 | const numberOfDays = diff / dayMS | |
178 | ||
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' | |
183 | } | |
184 | } | |
185 | ||
31d065cc AM |
186 | private updateModelFromOriginallyPublishedAtYears () { |
187 | const baseDate = new Date() | |
188 | baseDate.setHours(0, 0, 0, 0) | |
189 | baseDate.setMonth(0, 1) | |
190 | ||
191 | if (this.originallyPublishedStartYear) { | |
192 | const year = parseInt(this.originallyPublishedStartYear, 10) | |
193 | const start = new Date(baseDate) | |
194 | start.setFullYear(year) | |
195 | ||
196 | this.advancedSearch.originallyPublishedStartDate = start.toISOString() | |
197 | } else { | |
198 | this.advancedSearch.originallyPublishedStartDate = null | |
199 | } | |
200 | ||
201 | if (this.originallyPublishedEndYear) { | |
202 | const year = parseInt(this.originallyPublishedEndYear, 10) | |
203 | const end = new Date(baseDate) | |
204 | end.setFullYear(year) | |
205 | ||
206 | this.advancedSearch.originallyPublishedEndDate = end.toISOString() | |
207 | } else { | |
208 | this.advancedSearch.originallyPublishedEndDate = null | |
209 | } | |
210 | } | |
211 | ||
0b18f4aa C |
212 | private updateModelFromDurationRange () { |
213 | if (!this.durationRange) return | |
214 | ||
215 | const fourMinutes = 60 * 4 | |
216 | const tenMinutes = 60 * 10 | |
217 | ||
218 | switch (this.durationRange) { | |
219 | case 'short': | |
220 | this.advancedSearch.durationMin = undefined | |
221 | this.advancedSearch.durationMax = fourMinutes | |
222 | break | |
223 | ||
224 | case 'medium': | |
225 | this.advancedSearch.durationMin = fourMinutes | |
226 | this.advancedSearch.durationMax = tenMinutes | |
227 | break | |
228 | ||
229 | case 'long': | |
230 | this.advancedSearch.durationMin = tenMinutes | |
231 | this.advancedSearch.durationMax = undefined | |
232 | break | |
233 | } | |
234 | } | |
235 | ||
236 | private updateModelFromPublishedRange () { | |
237 | if (!this.publishedDateRange) return | |
238 | ||
239 | // today | |
240 | const date = new Date() | |
241 | date.setHours(0, 0, 0, 0) | |
242 | ||
243 | switch (this.publishedDateRange) { | |
244 | case 'last_7days': | |
245 | date.setDate(date.getDate() - 7) | |
246 | break | |
247 | ||
248 | case 'last_30days': | |
249 | date.setDate(date.getDate() - 30) | |
250 | break | |
251 | ||
252 | case 'last_365days': | |
253 | date.setDate(date.getDate() - 365) | |
254 | break | |
255 | } | |
256 | ||
257 | this.advancedSearch.startDate = date.toISOString() | |
258 | } | |
259 | } |