| /******************************************************************************** |
| * Copyright (c) 2020 Contributors to the Eclipse Foundation |
| * |
| * See the NOTICE file(s) distributed with this work for additional |
| * information regarding copyright ownership. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0 |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| ********************************************************************************/ |
| |
| import {ChangeDetectionStrategy, ChangeDetectorRef, Component, EventEmitter, forwardRef, Input, Output} from "@angular/core"; |
| import {NG_VALUE_ACCESSOR} from "@angular/forms"; |
| import {IAPISearchOptions, IAPIStatementModel} from "../../../core"; |
| import {arrayJoin, filterDistinctValues} from "../../../util/store"; |
| import {IStatementTableEntry} from "../../layout/statement-table"; |
| import {AbstractControlValueAccessorComponent} from "../common"; |
| import {ISelectOption} from "../select/model"; |
| |
| @Component({ |
| selector: "app-statement-select", |
| templateUrl: "statement-select.component.html", |
| styleUrls: ["statement-select.component.scss"], |
| changeDetection: ChangeDetectionStrategy.OnPush, |
| providers: [ |
| { |
| provide: NG_VALUE_ACCESSOR, |
| useExisting: forwardRef(() => StatementSelectComponent), |
| multi: true |
| } |
| ] |
| }) |
| export class StatementSelectComponent extends AbstractControlValueAccessorComponent<number[]> { |
| |
| @Input() |
| public appIsLoading: boolean; |
| |
| @Input() |
| public appSearchText: string; |
| |
| @Input() |
| public appStatementTypeOptions: ISelectOption<number>[]; |
| |
| @Input() |
| public appSearchContent: IAPIStatementModel[]; |
| |
| @Output() |
| public appSearch: EventEmitter<IAPISearchOptions> = new EventEmitter(); |
| |
| public constructor(private readonly changeDetectorRef: ChangeDetectorRef) { |
| super(); |
| } |
| |
| public getEntries(onlySelected?: boolean): IStatementTableEntry[] { |
| const entries = arrayJoin(this.appSearchContent); |
| const value = arrayJoin(this.appValue); |
| return entries.map((statement) => { |
| return { |
| ...statement, |
| isSelected: value.indexOf(statement.id) !== -1 |
| }; |
| }).filter((entry) => !onlySelected || entry.isSelected); |
| } |
| |
| public toggle(id: number, isSelected: boolean) { |
| if (isSelected) { |
| const value = filterDistinctValues(arrayJoin(this.appValue, [id])) |
| .sort((a, b) => a - b); |
| this.writeValue(value, true); |
| } else { |
| const value = arrayJoin(this.appValue) |
| .filter((statementId) => statementId !== id); |
| this.writeValue(value, true); |
| } |
| } |
| |
| public writeValue(obj: number[], emit?: boolean) { |
| super.writeValue(obj, emit); |
| this.changeDetectorRef.markForCheck(); |
| } |
| |
| } |