From dd24f1bb0a4b252e5342b251ba36853364da7b8e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 19 Aug 2021 09:24:29 +0200 Subject: Add video filters to common video pages --- .../select/select-categories.component.ts | 71 ++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 client/src/app/shared/shared-forms/select/select-categories.component.ts (limited to 'client/src/app/shared/shared-forms/select/select-categories.component.ts') diff --git a/client/src/app/shared/shared-forms/select/select-categories.component.ts b/client/src/app/shared/shared-forms/select/select-categories.component.ts new file mode 100644 index 000000000..b921714ff --- /dev/null +++ b/client/src/app/shared/shared-forms/select/select-categories.component.ts @@ -0,0 +1,71 @@ + +import { Component, forwardRef, OnInit } from '@angular/core' +import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms' +import { ServerService } from '@app/core' +import { SelectOptionsItem } from '../../../../types/select-options-item.model' +import { ItemSelectCheckboxValue } from './select-checkbox.component' + +@Component({ + selector: 'my-select-categories', + styleUrls: [ './select-shared.component.scss' ], + templateUrl: './select-categories.component.html', + providers: [ + { + provide: NG_VALUE_ACCESSOR, + useExisting: forwardRef(() => SelectCategoriesComponent), + multi: true + } + ] +}) +export class SelectCategoriesComponent implements ControlValueAccessor, OnInit { + selectedCategories: ItemSelectCheckboxValue[] = [] + availableCategories: SelectOptionsItem[] = [] + + allCategoriesGroup = $localize`All categories` + + // Fix a bug on ng-select when we update items after we selected items + private toWrite: any + private loaded = false + + constructor ( + private server: ServerService + ) { + + } + + ngOnInit () { + this.server.getVideoCategories() + .subscribe( + categories => { + this.availableCategories = categories.map(c => ({ label: c.label, id: c.id + '', group: this.allCategoriesGroup })) + this.loaded = true + this.writeValue(this.toWrite) + } + ) + } + + propagateChange = (_: any) => { /* empty */ } + + writeValue (categories: string[] | number[]) { + if (!this.loaded) { + this.toWrite = categories + return + } + + this.selectedCategories = categories + ? categories.map(c => c + '') + : categories as string[] + } + + registerOnChange (fn: (_: any) => void) { + this.propagateChange = fn + } + + registerOnTouched () { + // Unused + } + + onModelChange () { + this.propagateChange(this.selectedCategories) + } +} -- cgit v1.2.3