search-filters.component.ts 7.02 KB
Newer Older
Chocobozzz's avatar
Chocobozzz committed
1
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'
Chocobozzz's avatar
Chocobozzz committed
2
import { ServerService } from '@app/core'
Chocobozzz's avatar
Chocobozzz committed
3
import { AdvancedSearch } from '@app/shared/shared-search'
Chocobozzz's avatar
Chocobozzz committed
4
import { HTMLServerConfig, VideoConstant } from '@shared/models'
Chocobozzz's avatar
Chocobozzz committed
5

Chocobozzz's avatar
Chocobozzz committed
6
7
type FormOption = { id: string, label: string }

Chocobozzz's avatar
Chocobozzz committed
8
9
10
11
12
13
14
15
16
17
@Component({
  selector: 'my-search-filters',
  styleUrls: [ './search-filters.component.scss' ],
  templateUrl: './search-filters.component.html'
})
export class SearchFiltersComponent implements OnInit {
  @Input() advancedSearch: AdvancedSearch = new AdvancedSearch()

  @Output() filtered = new EventEmitter<AdvancedSearch>()

Chocobozzz's avatar
Chocobozzz committed
18
19
  videoCategories: VideoConstant<number>[] = []
  videoLicences: VideoConstant<number>[] = []
Chocobozzz's avatar
Chocobozzz committed
20
21
  videoLanguages: VideoConstant<string>[] = []

Chocobozzz's avatar
Chocobozzz committed
22
23
24
25
  publishedDateRanges: FormOption[] = []
  sorts: FormOption[] = []
  durationRanges: FormOption[] = []
  videoType: FormOption[] = []
Chocobozzz's avatar
Chocobozzz committed
26
27
28
29

  publishedDateRange: string
  durationRange: string

30
31
32
  originallyPublishedStartYear: string
  originallyPublishedEndYear: string

Chocobozzz's avatar
Chocobozzz committed
33
  private serverConfig: HTMLServerConfig
Chocobozzz's avatar
Chocobozzz committed
34

Chocobozzz's avatar
Chocobozzz committed
35
36
37
38
39
40
  constructor (
    private serverService: ServerService
  ) {
    this.publishedDateRanges = [
      {
        id: 'today',
Chocobozzz's avatar
Chocobozzz committed
41
        label: $localize`Today`
Chocobozzz's avatar
Chocobozzz committed
42
43
44
      },
      {
        id: 'last_7days',
Chocobozzz's avatar
Chocobozzz committed
45
        label: $localize`Last 7 days`
Chocobozzz's avatar
Chocobozzz committed
46
47
48
      },
      {
        id: 'last_30days',
Chocobozzz's avatar
Chocobozzz committed
49
        label: $localize`Last 30 days`
Chocobozzz's avatar
Chocobozzz committed
50
51
52
      },
      {
        id: 'last_365days',
Chocobozzz's avatar
Chocobozzz committed
53
        label: $localize`Last 365 days`
Chocobozzz's avatar
Chocobozzz committed
54
55
56
      }
    ]

Chocobozzz's avatar
Chocobozzz committed
57
    this.videoType = [
58
      {
Chocobozzz's avatar
Chocobozzz committed
59
60
        id: 'vod',
        label: $localize`VOD videos`
61
      },
Chocobozzz's avatar
Chocobozzz committed
62
63
64
65
66
67
68
      {
        id: 'live',
        label: $localize`Live videos`
      }
    ]

    this.durationRanges = [
Chocobozzz's avatar
Chocobozzz committed
69
70
      {
        id: 'short',
Chocobozzz's avatar
Chocobozzz committed
71
        label: $localize`Short (< 4 min)`
Chocobozzz's avatar
Chocobozzz committed
72
73
74
      },
      {
        id: 'medium',
Chocobozzz's avatar
Chocobozzz committed
75
        label: $localize`Medium (4-10 min)`
76
77
78
      },
      {
        id: 'long',
Chocobozzz's avatar
Chocobozzz committed
79
        label: $localize`Long (> 10 min)`
Chocobozzz's avatar
Chocobozzz committed
80
81
82
83
84
85
      }
    ]

    this.sorts = [
      {
        id: '-match',
Chocobozzz's avatar
Chocobozzz committed
86
        label: $localize`Relevance`
Chocobozzz's avatar
Chocobozzz committed
87
88
89
      },
      {
        id: '-publishedAt',
Chocobozzz's avatar
Chocobozzz committed
90
        label: $localize`Publish date`
Chocobozzz's avatar
Chocobozzz committed
91
92
93
      },
      {
        id: '-views',
Chocobozzz's avatar
Chocobozzz committed
94
        label: $localize`Views`
Chocobozzz's avatar
Chocobozzz committed
95
96
97
98
99
      }
    ]
  }

  ngOnInit () {
Chocobozzz's avatar
Chocobozzz committed
100
101
    this.serverConfig = this.serverService.getHTMLConfig()

Chocobozzz's avatar
Chocobozzz committed
102
103
104
105

    this.serverService.getVideoCategories().subscribe(categories => this.videoCategories = categories)
    this.serverService.getVideoLicences().subscribe(licences => this.videoLicences = licences)
    this.serverService.getVideoLanguages().subscribe(languages => this.videoLanguages = languages)
Chocobozzz's avatar
Chocobozzz committed
106
107
108

    this.loadFromDurationRange()
    this.loadFromPublishedRange()
109
    this.loadOriginallyPublishedAtYears()
Chocobozzz's avatar
Chocobozzz committed
110
111
  }

Chocobozzz's avatar
Chocobozzz committed
112
  onInputUpdated () {
Chocobozzz's avatar
Chocobozzz committed
113
114
    this.updateModelFromDurationRange()
    this.updateModelFromPublishedRange()
115
    this.updateModelFromOriginallyPublishedAtYears()
116
  }
Chocobozzz's avatar
Chocobozzz committed
117

118
  formUpdated () {
Chocobozzz's avatar
Chocobozzz committed
119
    this.onInputUpdated()
Chocobozzz's avatar
Chocobozzz committed
120
121
122
    this.filtered.emit(this.advancedSearch)
  }

Chocobozzz's avatar
Chocobozzz committed
123
124
  reset () {
    this.advancedSearch.reset()
Chocobozzz's avatar
Chocobozzz committed
125
126
127

    this.resetOriginalPublicationYears()

Chocobozzz's avatar
Chocobozzz committed
128
129
    this.durationRange = undefined
    this.publishedDateRange = undefined
Chocobozzz's avatar
Chocobozzz committed
130
131

    this.onInputUpdated()
Chocobozzz's avatar
Chocobozzz committed
132
133
134
135
136
137
138
139
  }

  resetField (fieldName: string, value?: any) {
    this.advancedSearch[fieldName] = value
  }

  resetLocalField (fieldName: string, value?: any) {
    this[fieldName] = value
Chocobozzz's avatar
Chocobozzz committed
140
    this.onInputUpdated()
Chocobozzz's avatar
Chocobozzz committed
141
142
143
144
145
146
  }

  resetOriginalPublicationYears () {
    this.originallyPublishedStartYear = this.originallyPublishedEndYear = undefined
  }

Chocobozzz's avatar
Chocobozzz committed
147
148
149
150
  isSearchTargetEnabled () {
    return this.serverConfig.search.searchIndex.enabled && this.serverConfig.search.searchIndex.disableLocalSearch !== true
  }

151
152
153
154
155
156
157
158
159
160
  private loadOriginallyPublishedAtYears () {
    this.originallyPublishedStartYear = this.advancedSearch.originallyPublishedStartDate
      ? new Date(this.advancedSearch.originallyPublishedStartDate).getFullYear().toString()
      : null

    this.originallyPublishedEndYear = this.advancedSearch.originallyPublishedEndDate
      ? new Date(this.advancedSearch.originallyPublishedEndDate).getFullYear().toString()
      : null
  }

Chocobozzz's avatar
Chocobozzz committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  private loadFromDurationRange () {
    if (this.advancedSearch.durationMin || this.advancedSearch.durationMax) {
      const fourMinutes = 60 * 4
      const tenMinutes = 60 * 10

      if (this.advancedSearch.durationMin === fourMinutes && this.advancedSearch.durationMax === tenMinutes) {
        this.durationRange = 'medium'
      } else if (this.advancedSearch.durationMax === fourMinutes) {
        this.durationRange = 'short'
      } else if (this.advancedSearch.durationMin === tenMinutes) {
        this.durationRange = 'long'
      }
    }
  }

  private loadFromPublishedRange () {
    if (this.advancedSearch.startDate) {
      const date = new Date(this.advancedSearch.startDate)
      const now = new Date()

      const diff = Math.abs(date.getTime() - now.getTime())

      const dayMS = 1000 * 3600 * 24
      const numberOfDays = diff / dayMS

      if (numberOfDays >= 365) this.publishedDateRange = 'last_365days'
      else if (numberOfDays >= 30) this.publishedDateRange = 'last_30days'
      else if (numberOfDays >= 7) this.publishedDateRange = 'last_7days'
      else if (numberOfDays >= 0) this.publishedDateRange = 'today'
    }
  }

193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
  private updateModelFromOriginallyPublishedAtYears () {
    const baseDate = new Date()
    baseDate.setHours(0, 0, 0, 0)
    baseDate.setMonth(0, 1)

    if (this.originallyPublishedStartYear) {
      const year = parseInt(this.originallyPublishedStartYear, 10)
      const start = new Date(baseDate)
      start.setFullYear(year)

      this.advancedSearch.originallyPublishedStartDate = start.toISOString()
    } else {
      this.advancedSearch.originallyPublishedStartDate = null
    }

    if (this.originallyPublishedEndYear) {
      const year = parseInt(this.originallyPublishedEndYear, 10)
      const end = new Date(baseDate)
      end.setFullYear(year)

      this.advancedSearch.originallyPublishedEndDate = end.toISOString()
    } else {
      this.advancedSearch.originallyPublishedEndDate = null
    }
  }

Chocobozzz's avatar
Chocobozzz committed
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
  private updateModelFromDurationRange () {
    if (!this.durationRange) return

    const fourMinutes = 60 * 4
    const tenMinutes = 60 * 10

    switch (this.durationRange) {
      case 'short':
        this.advancedSearch.durationMin = undefined
        this.advancedSearch.durationMax = fourMinutes
        break

      case 'medium':
        this.advancedSearch.durationMin = fourMinutes
        this.advancedSearch.durationMax = tenMinutes
        break

      case 'long':
        this.advancedSearch.durationMin = tenMinutes
        this.advancedSearch.durationMax = undefined
        break
    }
  }

  private updateModelFromPublishedRange () {
    if (!this.publishedDateRange) return

    // today
    const date = new Date()
    date.setHours(0, 0, 0, 0)

    switch (this.publishedDateRange) {
      case 'last_7days':
        date.setDate(date.getDate() - 7)
        break

      case 'last_30days':
        date.setDate(date.getDate() - 30)
        break

      case 'last_365days':
        date.setDate(date.getDate() - 365)
        break
    }

    this.advancedSearch.startDate = date.toISOString()
  }
}