Skip to content
Snippets Groups Projects
files.component.ts 6.58 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { LoginData } from './../../app.interfaces';
    import { MainDataService } from './../../maindata.service';
    
    Martin Mechtel's avatar
    Martin Mechtel committed
    import { ServerError } from './../../backend.service';
    import { WorkspaceDataService } from './../workspacedata.service';
    import { GetFileResponseData, CheckWorkspaceResponseData } from './../workspace.interfaces';
    
    import { ConfirmDialogComponent, ConfirmDialogData, MessageDialogComponent,
    
      MessageDialogData, MessageType } from '../../iqb-common';
    
    import { DataSource } from '@angular/cdk/collections';
    
    import { Observable, BehaviorSubject, Subscription, merge } from 'rxjs';
    
    import { MatTableDataSource } from '@angular/material/table';
    import { MatSnackBar } from '@angular/material';
    
    Martin Mechtel's avatar
    Martin Mechtel committed
    import { BackendService } from '../backend.service';
    
    import { Input, Output, EventEmitter, Component, OnInit, Inject, ElementRef, OnDestroy } from '@angular/core';
    
    import { NgModule, ViewChild } from '@angular/core';
    import { MatSort, MatDialog } from '@angular/material';
    import { HttpEventType, HttpErrorResponse, HttpEvent } from '@angular/common/http';
    
    import { IqbFilesUploadQueueComponent, IqbFilesUploadInputForDirective } from '../../iqb-files';
    
    Martin Mechtel's avatar
    Martin Mechtel committed
      templateUrl: './files.component.html',
      styleUrls: ['./files.component.css']
    
    export class FilesComponent implements OnInit, OnDestroy {
    
      public serverfiles: MatTableDataSource<GetFileResponseData>;
      public displayedColumns = ['checked', 'filename', 'typelabel', 'filesize', 'filedatetime'];
    
      public uploadUrl = '';
    
      public fileNameAlias = 'fileforvo';
    
      public dataLoading = false;
    
      private logindataSubscription: Subscription = null;
    
    
      // for workspace-check
      public checkErrors = [];
      public checkWarnings = [];
      public checkInfos = [];
    
      @ViewChild(MatSort) sort: MatSort;
    
      constructor(
        @Inject('SERVER_URL') private serverUrl: string,
        private bs: BackendService,
    
        private mds: MainDataService,
    
    Martin Mechtel's avatar
    Martin Mechtel committed
        private wds: WorkspaceDataService,
    
        public confirmDialog: MatDialog,
        public messsageDialog: MatDialog,
        public snackBar: MatSnackBar
      ) {
    
    Martin Mechtel's avatar
    Martin Mechtel committed
        this.uploadUrl = this.serverUrl + 'php_admin/uploadFile.php';
    
        this.logindataSubscription = this.mds.loginData$.subscribe(ld => {
            const ws = this.wds.ws;
            let at = ld ? ld.admintoken : '';
            this.updateFileList((ws <= 0) || (at.length === 0));
        });
    
      }
    
      // ***********************************************************************************
      checkAll(isChecked: boolean) {
        this.serverfiles.data.forEach(element => {
          element.isChecked = isChecked;
        });
      }
    
      // ***********************************************************************************
      deleteFiles() {
        this.checkErrors = [];
        this.checkWarnings = [];
        this.checkInfos = [];
    
        const filesToDelete = [];
        this.serverfiles.data.forEach(element => {
          if (element.isChecked) {
            filesToDelete.push(element.type + '::' + element.filename);
          }
        });
    
        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',
              content: prompt + ' diese gelöscht werden?',
              confirmbuttonlabel: 'Löschen'
            }
          });
    
          dialogRef.afterClosed().subscribe(result => {
            if (result !== false) {
              // =========================================================
              this.dataLoading = true;
    
    Martin Mechtel's avatar
    Martin Mechtel committed
              this.bs.deleteFiles(filesToDelete).subscribe(deletefilesresponse => {
                if (deletefilesresponse instanceof ServerError) {
                  this.wds.setNewErrorMsg(deletefilesresponse as ServerError);
                } else {
                  const deletefilesresponseOk = deletefilesresponse as string;
                  if ((deletefilesresponseOk.length > 5) && (deletefilesresponseOk.substr(0, 2) === 'e:')) {
                    this.snackBar.open(deletefilesresponseOk.substr(2), 'Fehler', {duration: 1000});
    
    Martin Mechtel's avatar
    Martin Mechtel committed
                    this.snackBar.open(deletefilesresponseOk, '', {duration: 1000});
    
                    this.updateFileList();
                  }
    
    Martin Mechtel's avatar
    Martin Mechtel committed
                  this.wds.setNewErrorMsg();
                }
              });
    
              // =========================================================
            }
          });
        } else {
          this.messsageDialog.open(MessageDialogComponent, {
            width: '400px',
            data: <MessageDialogData>{
              title: 'Löschen von Dateien',
              content: 'Bitte markieren Sie erst Dateien!',
              type: MessageType.error
            }
          });
        }
    
      }
    
      // ***********************************************************************************
    
      updateFileList(empty = false) {
    
        this.checkErrors = [];
        this.checkWarnings = [];
        this.checkInfos = [];
    
    
        if (empty) {
          this.serverfiles = new MatTableDataSource([]);
        } else {
          this.dataLoading = true;
          this.bs.getFiles().subscribe(
            (filedataresponse: GetFileResponseData[]) => {
              this.serverfiles = new MatTableDataSource(filedataresponse);
              this.serverfiles.sort = this.sort;
              this.dataLoading = false;
              this.wds.setNewErrorMsg();
            }, (err: ServerError) => {
              this.wds.setNewErrorMsg(err);
              this.dataLoading = false;
            }
          );
        }
    
      }
    
      // ***********************************************************************************
      getDownloadRef(element: GetFileResponseData): string {
        return this.serverUrl
    
    Martin Mechtel's avatar
    Martin Mechtel committed
            + 'admin/php_admin/getFile.php?t=' + element.type
    
            + '&fn=' + element.filename;
      }
    
      checkWorkspace() {
        this.checkErrors = [];
        this.checkWarnings = [];
        this.checkInfos = [];
    
    
    Martin Mechtel's avatar
    Martin Mechtel committed
        this.dataLoading = true;
        this.bs.checkWorkspace().subscribe(
          (checkResponse: CheckWorkspaceResponseData) => {
            // this.serverfiles = new MatTableDataSource(filedataresponse);
            // this.serverfiles.sort = this.sort;
            this.checkErrors = checkResponse.errors;
            this.checkWarnings = checkResponse.warnings;
            this.checkInfos = checkResponse.infos;
            this.wds.setNewErrorMsg();
    
            this.dataLoading = false;
          }, (err: ServerError) => {
            this.wds.setNewErrorMsg(err);
    
            this.dataLoading = false;
          }
    
    Martin Mechtel's avatar
    Martin Mechtel committed
        );
    
    
      // % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
      ngOnDestroy() {
        if (this.logindataSubscription !== null) {
          this.logindataSubscription.unsubscribe();
        }
      }