/*! * @license * Copyright 2019 Alfresco Software, Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { Component, Input, OnChanges, OnDestroy } from '@angular/core'; import { CardViewSelectItemModel } from '../../models/card-view-selectitem.model'; import { CardViewUpdateService } from '../../services/card-view-update.service'; import { BehaviorSubject, combineLatest, Observable, Subject } from 'rxjs'; import { CardViewSelectItemOption } from '../../interfaces/card-view.interfaces'; import { MatSelectChange } from '@angular/material/select'; import { BaseCardView } from '../base-card-view'; import { AppConfigService } from '../../../app-config/app-config.service'; import { takeUntil, map } from 'rxjs/operators'; @Component({ selector: 'adf-card-view-selectitem', templateUrl: './card-view-selectitem.component.html', styleUrls: ['./card-view-selectitem.component.scss'] }) export class CardViewSelectItemComponent extends BaseCardView> implements OnChanges, OnDestroy { static HIDE_FILTER_LIMIT = 5; @Input() editable: boolean = false; @Input() options$: Observable[]>; @Input() displayNoneOption: boolean = true; @Input() displayEmpty: boolean = true; value: string | number; filter$: BehaviorSubject = new BehaviorSubject(''); showInputFilter: boolean = false; private onDestroy$ = new Subject(); list$: Observable[]> = null; constructor(cardViewUpdateService: CardViewUpdateService, private appConfig: AppConfigService) { super(cardViewUpdateService); } ngOnChanges(): void { this.value = this.property.value; } ngOnInit() { this.getOptions() .pipe(takeUntil(this.onDestroy$)) .subscribe((options: CardViewSelectItemOption[]) => { this.showInputFilter = options.length > this.optionsLimit; }); this.list$ = this.getList(); } onFilterInputChange(value: string) { this.filter$.next(value.toString()); } isEditable(): boolean { return this.editable && this.property.editable; } getOptions(): Observable[]> { return this.options$ || this.property.options$; } getList(): Observable[]> { return combineLatest([this.getOptions(), this.filter$]) .pipe( map(([items, filter]) => items.filter((item: CardViewSelectItemOption) => filter ? item.label.toLowerCase().includes(filter.toLowerCase()) : true)), takeUntil(this.onDestroy$) ); } onChange(event: MatSelectChange): void { const selectedOption = event.value !== undefined ? event.value : null; this.cardViewUpdateService.update(> { ...this.property }, selectedOption); this.property.value = selectedOption; } showNoneOption() { return this.displayNoneOption; } get showProperty(): boolean { return this.displayEmpty || !this.property.isEmpty(); } ngOnDestroy() { this.onDestroy$.next(); this.onDestroy$.complete(); } private get optionsLimit(): number { return this.appConfig.get('content-metadata.selectFilterLimit', CardViewSelectItemComponent.HIDE_FILTER_LIMIT); } }