File

projects/angular-material-fileupload/src/lib/mat-file-upload-queue/mat-file-upload-queue.component.ts

Implements

OnChanges OnDestroy

Metadata

changeDetection ChangeDetectionStrategy.OnPush
providers MatFileUploadQueueService
selector mat-file-upload-queue
styleUrls ./mat-file-upload-queue.component.scss
templateUrl ./mat-file-upload-queue.component.html

Index

Properties
Methods
Inputs
Accessors

Constructor

constructor(matFileUploadQueueService: MatFileUploadQueueService, changeDetectorRef: ChangeDetectorRef)
Parameters :
Name Type Optional
matFileUploadQueueService MatFileUploadQueueService No
changeDetectorRef ChangeDetectorRef No

Inputs

fileAlias
Type : string
Default value : "file"
httpRequestHeaders
Type : HttpHeaders | literal type
Default value : new HttpHeaders()
httpRequestParams
Type : HttpParams | literal type
Default value : new HttpParams()
httpUrl
Type : string
removeAllColor
Default value : "primary"
removeAllLabel
Default value : "Remove All"
uploadAllColor
Default value : "primary"
uploadAllLabel
Default value : "Upload All"

Methods

Private _listenTofileRemoved
_listenTofileRemoved()
Returns : void
add
add(file: any)
Parameters :
Name Type Optional
file any No
Returns : void
ngAfterViewInit
ngAfterViewInit()
Returns : void
ngOnChanges
ngOnChanges(changes: SimpleChanges)
Parameters :
Name Type Optional
changes SimpleChanges No
Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void
Public removeAll
removeAll()
Returns : void
Public uploadAll
uploadAll()
Returns : void

Properties

Private _changeSubscription
Type : Subscription

Subscription to changes in the files.

Private _fileRemoveSubscription
Type : Subscription | null

Subscription to remove changes in files.

Public files
Type : Array<any>
Default value : []
fileUploads
Type : QueryList<MatFileUploadComponent>
Decorators :
@ContentChildren(undefined)

Accessors

fileUploadRemoveEvents
getfileUploadRemoveEvents()

Combined stream of all of the file upload remove change events.

import {
  Component,
  ChangeDetectionStrategy,
  ContentChildren,
  forwardRef,
  OnDestroy,
  OnChanges,
  QueryList,
  SimpleChanges,
  Input,
  ChangeDetectorRef,
} from "@angular/core";
import { Subscription, Observable, merge } from "rxjs";
import { HttpHeaders, HttpParams } from "@angular/common/http";
import { startWith } from "rxjs/operators";
import { MatFileUploadQueueService } from "./mat-file-upload-queue.service";
import { MatFileUploadComponent } from "../mat-file-upload/mat-file-upload.component";

@Component({
  selector: "mat-file-upload-queue",
  templateUrl: "./mat-file-upload-queue.component.html",
  styleUrls: ["./mat-file-upload-queue.component.scss"],
  changeDetection: ChangeDetectionStrategy.OnPush,
  providers: [MatFileUploadQueueService],
})
export class MatFileUploadQueueComponent implements OnChanges, OnDestroy {
  @ContentChildren(forwardRef(() => MatFileUploadComponent))
  fileUploads: QueryList<MatFileUploadComponent>;

  /** Subscription to remove changes in files. */
  private _fileRemoveSubscription: Subscription | null;

  /** Subscription to changes in the files. */
  private _changeSubscription: Subscription;

  /** Combined stream of all of the file upload remove change events. */
  get fileUploadRemoveEvents(): Observable<MatFileUploadComponent> {
    return merge(
      ...this.fileUploads.map((fileUpload) => fileUpload.removeEvent)
    );
  }

  public files: Array<any> = [];

  ngOnChanges(changes: SimpleChanges): void {
    this.matFileUploadQueueService.initialize({
      httpUrl: changes["httpUrl"] ? changes["httpUrl"].currentValue : undefined,
      httpRequestHeaders: changes["httpRequestHeaders"]
        ? changes["httpRequestHeaders"].currentValue
        : undefined,
      httpRequestParams: changes["httpRequestParams"]
        ? changes["httpRequestParams"].currentValue
        : undefined,
      fileAlias: changes["fileAlias"]
        ? changes["fileAlias"].currentValue
        : undefined,
    });
  }

  constructor(
    private matFileUploadQueueService: MatFileUploadQueueService,
    private changeDetectorRef: ChangeDetectorRef
  ) {}

  /* Http request input bindings */
  @Input()
  httpUrl: string;

  @Input()
  httpRequestHeaders:
    | HttpHeaders
    | {
        [header: string]: string | string[];
      } = new HttpHeaders();

  @Input()
  httpRequestParams:
    | HttpParams
    | {
        [param: string]: string | string[];
      } = new HttpParams();

  @Input()
  fileAlias: string = "file";

  @Input()
  uploadAllColor = "primary";

  @Input()
  uploadAllLabel = "Upload All";

  @Input()
  removeAllColor = "primary";

  @Input()
  removeAllLabel = "Remove All";

  ngAfterViewInit() {
    // When the list changes, re-subscribe
    this._changeSubscription = this.fileUploads.changes
      .pipe(startWith(null))
      .subscribe(() => {
        if (this._fileRemoveSubscription) {
          this._fileRemoveSubscription.unsubscribe();
        }
        this._listenTofileRemoved();
      });
  }

  private _listenTofileRemoved(): void {
    this._fileRemoveSubscription = this.fileUploadRemoveEvents.subscribe(
      (event: MatFileUploadComponent) => {
        this.files.splice(event.id, 1);
        this.changeDetectorRef.markForCheck();
      }
    );
  }

  add(file: any) {
    this.files.push(file);
    this.changeDetectorRef.markForCheck();
  }

  public uploadAll() {
    this.fileUploads.forEach((fileUpload) => {
      fileUpload.upload();
    });
  }

  public removeAll() {
    this.files.splice(0, this.files.length);
    this.changeDetectorRef.markForCheck();
  }

  ngOnDestroy() {
    if (this._changeSubscription) this._changeSubscription.unsubscribe();
    if (this._fileRemoveSubscription)
      this._fileRemoveSubscription.unsubscribe();

    if (this.files) {
      this.removeAll();
    }
  }
}
<ng-content></ng-content>
<br />
<button
  mat-raised-button
  [color]="uploadAllColor"
  *ngIf="files.length > 0"
  (click)="uploadAll()"
>
  {{ uploadAllLabel }}
</button>
<button
  mat-raised-button
  [color]="removeAllColor"
  *ngIf="files.length > 0"
  (click)="removeAll()"
>
  {{ removeAllLabel }}
</button>

./mat-file-upload-queue.component.scss

Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""