2 import { Component, forwardRef, OnInit } from '@angular/core'
3 import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
4 import { ServerService } from '@app/core'
5 import { SelectOptionsItem } from '../../../../types/select-options-item.model'
6 import { ItemSelectCheckboxValue } from './select-checkbox.component'
9 selector: 'my-select-categories',
10 styleUrls: [ './select-shared.component.scss' ],
11 templateUrl: './select-categories.component.html',
14 provide: NG_VALUE_ACCESSOR,
15 useExisting: forwardRef(() => SelectCategoriesComponent),
20 export class SelectCategoriesComponent implements ControlValueAccessor, OnInit {
21 selectedCategories: ItemSelectCheckboxValue[] = []
22 availableCategories: SelectOptionsItem[] = []
24 allCategoriesGroup = $localize`All categories`
26 // Fix a bug on ng-select when we update items after we selected items
28 private loaded = false
31 private server: ServerService
37 this.server.getVideoCategories()
40 this.availableCategories = categories.map(c => ({ label: c.label, id: c.id + '', group: this.allCategoriesGroup }))
42 this.writeValue(this.toWrite)
47 propagateChange = (_: any) => { /* empty */ }
49 writeValue (categories: string[] | number[]) {
51 this.toWrite = categories
55 this.selectedCategories = categories
56 ? categories.map(c => c + '')
57 : categories as string[]
60 registerOnChange (fn: (_: any) => void) {
61 this.propagateChange = fn
64 registerOnTouched () {
69 this.propagateChange(this.selectedCategories)