File

projects/web-mev/src/app/features/file-manager/components/dialogs/add-file-dialog/add-file-dialog.component.ts

Description

Add File Dialog Component

Modal dialog component which is used to add files to the file list

Metadata

selector mev-add-file-dialog
styleUrls ./add-file-dialog.component.scss
templateUrl ./add-file-dialog.component.html

Index

Properties
Methods

Constructor

constructor(dialogRef: MatDialogRef, data: any, fileService: FileService)
Parameters :
Name Type Optional
dialogRef MatDialogRef<AddFileDialogComponent> No
data any No
fileService FileService No

Methods

Public confirmAdd
confirmAdd()

Function is triggered when user clicks the Upload button

Returns : void
getErrorMessage
getErrorMessage()
Returns : "" | "Required field"
onNoClick
onNoClick()

Function is triggered when user clicks the Cancel button

Returns : void
onUploadBtnClick
onUploadBtnClick()

Function is triggered when user clicks the Select files button. Simulates clicking the hidden input element

Returns : void
setFile
setFile(event)

Function is triggered when user selects 1 or more files in the input element

For large files a warning message is displayed

Parameters :
Name Optional
event No
Returns : void
submit
submit()
Returns : void

Properties

Public data
Type : any
Decorators :
@Inject(MAT_DIALOG_DATA)
Public dialogRef
Type : MatDialogRef<AddFileDialogComponent>
Public fileNames
Type : string[]
Public fileSelected
Type : boolean
Public fileService
Type : FileService
Private filesToUpload
Type : any[]
Default value : []
fileUpload
Type : ElementRef
Decorators :
@ViewChild('fileUpload', {static: false})
formControl
Default value : new FormControl('', [Validators.required])
Public isLargeFile
Type : boolean
Public resourceTypes
Default value : Object.keys(FileType)
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { Component, ElementRef, Inject, ViewChild } from '@angular/core';
import { FileService } from '@file-manager/services/file-manager.service';
import { FormControl, Validators } from '@angular/forms';
import { FileType } from '@app/shared/models/file-type';

/**
 * Add File Dialog Component
 *
 * Modal dialog component which is used to add files to the file list
 */
@Component({
  selector: 'mev-add-file-dialog',
  templateUrl: './add-file-dialog.component.html',
  styleUrls: ['./add-file-dialog.component.scss']
})
export class AddFileDialogComponent {
  private filesToUpload: any[] = [];
  public resourceTypes = Object.keys(FileType);
  @ViewChild('fileUpload', { static: false }) fileUpload: ElementRef;
  public fileNames: string[];
  public isLargeFile: boolean;
  public fileSelected: boolean;

  constructor(
    public dialogRef: MatDialogRef<AddFileDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: any,
    public fileService: FileService
  ) {}

  formControl = new FormControl('', [Validators.required]);

  /**
   * Function is triggered when user clicks the Select files button.
   * Simulates clicking the hidden input element
   *
   */
  onUploadBtnClick() {
    const fileUpload = this.fileUpload.nativeElement;
    fileUpload.click();
  }

  /**
   * Function is triggered when user selects 1 or more files in the input element
   *
   * For large files a warning message is displayed
   */
  setFile(event) {
    const fileSizeTreshold = 524288000;

    this.filesToUpload = event.target.files;
    if (!this.filesToUpload) {
      return;
    }
    this.fileSelected = this.filesToUpload.length > 0;

    // clean previous selection if exists
    this.fileNames = [];
    this.isLargeFile = false;

    for (let i = 0; i < this.filesToUpload.length; i++) {
      if (this.filesToUpload[i].size >= fileSizeTreshold) {
        this.isLargeFile = true;
      }
      this.fileNames.push(this.filesToUpload[i].name);
    }
  }

  /**
   * Function is triggered when user clicks the Upload button
   *
   */
  public confirmAdd(): void {
    this.fileService.addFile(this.filesToUpload);
  }

  getErrorMessage() {
    return this.formControl.hasError('required') ? 'Required field' : '';
  }

  submit() {
    // empty
  }

  /**
   * Function is triggered when user clicks the Cancel button
   *
   */
  onNoClick(): void {
    this.dialogRef.close();
  }
}
<div class="container">
  <h3 mat-dialog-title>Locate and select one or more files on your computer.
    <br> Then click the Upload button.</h3>

  <form class="mat-dialog-content" (ngSubmit)="submit" #formControl="ngForm">

     <div class="form">
      <button mat-raised-button color="accent" (click)="onUploadBtnClick()" class="upload-button">
        <mat-icon>file_upload</mat-icon>
        Select files
      </button>
      <!--span *ngIf="file_name" id="file-label"> {{ file_name }} </span-->
      <div *ngIf="fileNames" class="upload-info">
        You selected the following file(s):
        <ul>
          <li *ngFor="let fileName of fileNames">
            <span id="file-label"> {{ fileName }}
            </span>
          </li>
        </ul>
        <span *ngIf="isLargeFile" class="upload-warning"> 
          The file specified is larger than what MeV is configured to support. 
          Please use Dropbox to upload large files (>500Mb). 
        </span> 
      </div>
      
      <input type="file" #fileUpload (change)="setFile($event)" id="fileUpload" name="upload_file" multiple="multiple" required style="display:none;"/>
      <mat-error *ngIf="!fileSelected">
        <span class="help-block error" [hidden]="!fileSelected">
          File is Required
        </span>
      </mat-error>

    </div>

    <!--div class="form">

      <mat-form-field color="accent">
        <mat-label>Select a resource type</mat-label>
        <mat-select [(ngModel)]=data.resource_type name="resource_type" required>
          <mat-option *ngFor="let type of resourceTypes" [value]="type">
            {{ type }}
          </mat-option>
        </mat-select>
        <mat-error *ngIf="formControl.invalid">{{getErrorMessage()}}</mat-error>
      </mat-form-field>

    </div-->

    <div mat-dialog-actions>
      <button mat-button mat-raised-button color="accent" [type]="submit" [disabled]="!(formControl.valid && fileSelected)" [mat-dialog-close]="1" (click)="confirmAdd()">Upload</button>
      <button mat-button (click)="onNoClick()" tabindex="-1">Cancel</button>
    </div>
    <p></p>
  </form>
</div>

./add-file-dialog.component.scss

.container {
  display: flex;
  flex-direction: column;
  min-width: 450px;
}

.container > * {
  width: 100%;
}

.mat-dialog-title {
  font-size: 16px;
}

.mat-dialog-content {
  max-height: none;
  overflow: visible;
}

.form {
  display: flex;
  flex-direction: column;
  padding-top: 6px;
}

.mat-form-field {
  font-size: 16px;
  flex-grow: 1;
}

.upload-button {
  max-height: 36px;
}

.upload-button {
  max-width: 200px;
}

.upload-info {
  padding-top: 10px;
  padding-bottom: 10px;
}

.upload-warning {
  color: red;
  font-weight: bold;
}
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""