File

packages/data/src/lib/files/dropzone/dropzone.directive.ts

Description

Directive that turns any element into a zone to drop files. TODO: demo

Metadata

Selector [ecDropzone]

Index

Inputs
Outputs
HostBindings
HostListeners

Constructor

constructor(el: ElementRef)
Parameters :
Name Type Optional
el ElementRef No

Inputs

disabled
Default value : false

If true the element acts normal

Outputs

ecDropzone
Type : EventEmitter<any>

Emits when files are dropped

HostBindings

class.is-active
Type : boolean

The is-active class is bound to the host when dragover is active

HostListeners

dragleave
Arguments : '$event'
dragleave(e)

flips active to false on dragleave

dragover
Arguments : '$event'
dragover(e)

flips active to true on dragover

drop
Arguments : '$event'
drop(e)

emits dropped files and flips active to false on drop

import { Directive, ElementRef, EventEmitter, HostBinding, HostListener, Input, Output } from '@angular/core';

/** Directive that turns any element into a zone to drop files. TODO: demo */
@Directive({ selector: '[ecDropzone]' })
export class DropzoneDirective {
  /** The is-active class is bound to the host when dragover is active */
  @HostBinding('class.is-active') public active: boolean;
  /** Emits when files are dropped */
  @Output() ecDropzone: EventEmitter<any> = new EventEmitter();
  /** If true the element acts normal */
  @Input() disabled = false;
  /** flips active to true on dragover */
  @HostListener('dragover', ['$event']) onDragOver(e) {
    if (this.disabled) {
      return;
    }
    e.preventDefault();
    e.stopPropagation();
    if (!this.active) {
      this.active = true;
    }
  }
  /** emits dropped files and flips active to false on drop */
  @HostListener('drop', ['$event']) onDrop(e) {
    if (this.disabled) {
      return;
    }
    e.preventDefault();
    e.stopPropagation();
    if (!e.dataTransfer.files || !e.dataTransfer.files.length) {
      return;
    }
    this.ecDropzone.emit(e);
    this.active = false;
  }
  /** flips active to false on dragleave */
  @HostListener('dragleave', ['$event']) onDragLeave(e) {
    if (this.disabled) {
      return;
    }
    e.preventDefault();
    e.stopPropagation();
    if (this.active) {
      this.active = false;
    }
  }
  constructor(private el: ElementRef) {}
}

result-matching ""

    No results matching ""