2019-07-26 11:46:59 +02:00
|
|
|
import { Component, EventEmitter, Input, OnInit, Output, TemplateRef } from '@angular/core';
|
|
|
|
import { ControlValueAccessor, FormControl } from '@angular/forms';
|
2019-05-14 16:59:47 +02:00
|
|
|
import { MatDialog } from '@angular/material';
|
2019-07-26 11:46:59 +02:00
|
|
|
|
2019-08-21 09:03:07 +02:00
|
|
|
import { Observable } from 'rxjs';
|
|
|
|
import { map } from 'rxjs/operators';
|
2019-05-14 16:59:47 +02:00
|
|
|
|
|
|
|
import { MediafileRepositoryService } from 'app/core/repositories/mediafiles/mediafile-repository.service';
|
2019-07-30 10:30:19 +02:00
|
|
|
import { mediumDialogSettings } from 'app/shared/utils/dialog-settings';
|
2019-05-14 16:59:47 +02:00
|
|
|
import { ViewMediafile } from 'app/site/mediafiles/models/view-mediafile';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'os-attachment-control',
|
|
|
|
templateUrl: './attachment-control.component.html',
|
|
|
|
styleUrls: ['./attachment-control.component.scss']
|
|
|
|
})
|
|
|
|
export class AttachmentControlComponent implements OnInit, ControlValueAccessor {
|
|
|
|
/**
|
|
|
|
* Output for an error handler
|
|
|
|
*/
|
|
|
|
@Output()
|
|
|
|
public errorHandler: EventEmitter<string> = new EventEmitter();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The form-control name to access the value for the form-control
|
|
|
|
*/
|
|
|
|
@Input()
|
|
|
|
public controlName: FormControl;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The file list that is necessary for the `SearchValueSelector`
|
|
|
|
*/
|
2019-08-21 09:03:07 +02:00
|
|
|
public mediaFileList: Observable<ViewMediafile[]>;
|
2019-05-14 16:59:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default constructor
|
|
|
|
*
|
|
|
|
* @param dialogService Reference to the `MatDialog`
|
|
|
|
* @param mediaService Reference for the `MediaFileRepositoryService`
|
|
|
|
*/
|
|
|
|
public constructor(private dialogService: MatDialog, private mediaService: MediafileRepositoryService) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On init method
|
|
|
|
*/
|
|
|
|
public ngOnInit(): void {
|
2019-08-21 09:03:07 +02:00
|
|
|
this.mediaFileList = this.mediaService
|
|
|
|
.getViewModelListObservable()
|
|
|
|
.pipe(map(files => files.filter(file => !file.is_directory)));
|
2019-05-14 16:59:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to open a given dialog
|
|
|
|
*
|
|
|
|
* @param dialog the dialog to open
|
|
|
|
*/
|
|
|
|
public openUploadDialog(dialog: TemplateRef<string>): void {
|
2019-08-21 09:03:07 +02:00
|
|
|
this.dialogService.open(dialog, { ...mediumDialogSettings, disableClose: false });
|
2019-05-14 16:59:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to set the value for the `SearchValueSelector` after successful upload
|
|
|
|
*
|
|
|
|
* @param fileIDs a list with the ids of the uploaded files
|
|
|
|
*/
|
|
|
|
public uploadSuccess(fileIDs: number[]): void {
|
|
|
|
if (this.controlName) {
|
|
|
|
const newValues = [...this.controlName.value, ...fileIDs];
|
|
|
|
this.controlName.setValue(newValues);
|
|
|
|
this.dialogService.closeAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to emit an occurring error.
|
|
|
|
*
|
|
|
|
* @param error The occurring error
|
|
|
|
*/
|
|
|
|
public uploadError(error: string): void {
|
|
|
|
this.errorHandler.emit(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to write a new value to the form.
|
|
|
|
* Satisfy the interface.
|
|
|
|
*
|
|
|
|
* @param value The new value for this form.
|
|
|
|
*/
|
|
|
|
public writeValue(value: any): void {
|
|
|
|
if (value && this.controlName) {
|
|
|
|
this.controlName.setValue(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function executed when the control's value changed.
|
|
|
|
*
|
|
|
|
* @param fn the function that is executed.
|
|
|
|
*/
|
|
|
|
public registerOnChange(fn: any): void {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To satisfy the interface
|
|
|
|
*
|
|
|
|
* @param fn the registered callback function for onBlur-events.
|
|
|
|
*/
|
|
|
|
public registerOnTouched(fn: any): void {}
|
|
|
|
}
|