Newer
Older
import {
Component, OnInit, Inject, ViewChild
} from '@angular/core';
import { MatTableDataSource } from '@angular/material/table';
import { MatSnackBar } from '@angular/material/snack-bar';
import { MatDialog } from '@angular/material/dialog';
import { MatSort } from '@angular/material/sort';
import { saveAs } from 'file-saver';
import {
ConfirmDialogComponent, ConfirmDialogData, MessageDialogComponent,
MessageDialogData, MessageType
} from 'iqb-components';
import { WorkspaceDataService } from '../workspacedata.service';
import { GetFileResponseData, CheckWorkspaceResponseData } from '../workspace.interfaces';
import { BackendService, FileDeletionReport } from '../backend.service';
import { MainDataService } from '../../maindata.service';
templateUrl: './files.component.html',
styleUrls: ['./files.component.css']
export class FilesComponent implements OnInit {
public serverfiles: MatTableDataSource<GetFileResponseData>;
public displayedColumns = ['checked', 'filename', 'status', 'typelabel', 'filesize', 'filedatetime'];
// for workspace-check
public checkErrors = [];
public checkWarnings = [];
public checkInfos = [];
constructor(
@Inject('SERVER_URL') private serverUrl: string,
private bs: BackendService,
public confirmDialog: MatDialog,
public snackBar: MatSnackBar
this.uploadUrl = `${this.serverUrl}workspace/${this.wds.wsId}/file`;
}
checkAll(isChecked: boolean) {
element.isChecked = isChecked;
});
}
deleteFiles() {
if (this.wds.wsRole === 'RW') {
this.checkErrors = [];
this.checkWarnings = [];
this.checkInfos = [];
const filesToDelete = [];
if (filesToDelete.length > 0) {
let prompt = 'Sie haben ';
if (filesToDelete.length > 1) {
prompt = prompt + filesToDelete.length + ' Dateien ausgewählt. Sollen';
} else {
prompt = prompt + ' eine Datei ausgewählt. Soll';
}
const dialogRef = this.confirmDialog.open(ConfirmDialogComponent, {
width: '400px',
data: <ConfirmDialogData>{
title: 'Löschen von Dateien',
confirmbuttonlabel: 'Löschen',
showcancel: true
}
});
dialogRef.afterClosed().subscribe(result => {
if (result !== false) {
this.bs.deleteFiles(filesToDelete).subscribe((fileDeletionReport: FileDeletionReport) => {
const message = [];
if (fileDeletionReport.deleted.length > 0) {
message.push(fileDeletionReport.deleted.length + ' Dateien erfolgreich gelöscht.');
if (fileDeletionReport.not_allowed.length > 0) {
message.push(fileDeletionReport.not_allowed.length + ' Dateien konnten nicht gelöscht werden.');
}
this.snackBar.open(message.join('<br>'), message.length > 1 ? 'Achtung' : '', {duration: 1000});
this.updateFileList();
this.messageDialog.open(MessageDialogComponent, {
width: '400px',
data: <MessageDialogData>{
title: 'Löschen von Dateien',
content: 'Bitte markieren Sie erst Dateien!',
type: MessageType.error
}
});
}
this.checkErrors = [];
this.checkWarnings = [];
this.checkInfos = [];
this.serverfiles = new MatTableDataSource([]);
this.bs.getFiles().subscribe(
(fileList: GetFileResponseData[]) => {
this.serverfiles = new MatTableDataSource(fileList);
private static getStats(fileList: GetFileResponseData[]): {[type: string]: number} {
const stats: {[type: string]: number} = {};
// TODO filter validity
return fileList.reduce((carry, file) => {
if (typeof carry[file.type] === 'undefined') {
// eslint-disable-next-line no-param-reassign
carry[file.type] = 0;
}
// eslint-disable-next-line no-param-reassign
carry[file.type] += 1;
return carry;
}, stats);
}
download(element: GetFileResponseData): void {
this.bs.downloadFile(element.type, element.filename)
(fileData: Blob|boolean) => {
if (fileData !== false) {
saveAs(fileData as Blob, element.filename);
this.checkErrors = [];
this.checkWarnings = [];
this.checkInfos = [];
this.bs.checkWorkspace().subscribe(
this.checkErrors = checkResponse.errors;
this.checkWarnings = checkResponse.warnings;
this.checkInfos = checkResponse.infos;