blob: 90749187cc5728f094cff38c97fde7754245b15d [file] [log] [blame]
/********************************************************************************
* 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 {IAPIAttachmentModel} from "../../../../core";
import {IAttachmentWithTags} from "../../../../store";
import {arrayJoin} from "../../../../util";
import {AbstractControlValueAccessorComponent} from "../../common";
@Component({
selector: "app-file-select",
templateUrl: "./file-select.component.html",
styleUrls: ["./file-select.component.scss"],
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => FileSelectComponent),
multi: true
}
]
})
export class FileSelectComponent extends AbstractControlValueAccessorComponent<IAttachmentWithTags<number>[]> {
private static id = 0;
@Input()
public appId = `FileSelectComponent-${FileSelectComponent.id}`;
@Input()
public appAttachments: IAPIAttachmentModel[];
@Output()
public appOpenAttachment = new EventEmitter<number>();
public constructor(public readonly changeDetectorRef: ChangeDetectorRef) {
super();
}
public isSelected(attachmentId: number): boolean {
return !arrayJoin(this.appValue).some((_) => _?.attachment === attachmentId);
}
public select(attachmentId: number) {
const value = arrayJoin(this.appValue).filter((_) => _?.attachment !== attachmentId);
this.writeValue(value, true);
}
public deselect(attachmentId: number) {
this.writeValue(arrayJoin(this.appValue, [{attachment: attachmentId, tags: [], remove: true}]), true);
}
public writeValue(obj: IAttachmentWithTags<number>[], emit?: boolean) {
super.writeValue(obj, emit);
this.changeDetectorRef.markForCheck();
}
public setDisabledState(isDisabled: boolean) {
super.setDisabledState(isDisabled);
this.changeDetectorRef.markForCheck();
}
}