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