Skip to content

Commit

Permalink
Fixed DragEvent on Safari and other browsers that don't support this …
Browse files Browse the repository at this point in the history
…feature.
  • Loading branch information
BenevidesLecontes committed Apr 8, 2017
1 parent eaf3241 commit 9c1216e
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/directives/ng-file-drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
OnInit,
SimpleChange
} from '@angular/core';
import { NgUploaderService } from '../services/ngx-uploader';
import { NgUploaderOptions, UploadedFile, UploadRejected } from '../classes/index';
import {NgUploaderService} from '../services/ngx-uploader';
import {NgUploaderOptions, UploadedFile, UploadRejected} from '../classes/index';

@Directive({
selector: '[ngFileDrop]',
Expand All @@ -30,9 +30,9 @@ export class NgFileDropDirective implements OnChanges, OnInit {

files: File[] = [];

constructor(
@Inject(ElementRef) public el: ElementRef,
@Inject(NgUploaderService) public uploader: NgUploaderService) { }
constructor(@Inject(ElementRef) public el: ElementRef,
@Inject(NgUploaderService) public uploader: NgUploaderService) {
}

ngOnInit() {
this.uploader._emitter.subscribe((data: any) => {
Expand Down Expand Up @@ -63,12 +63,12 @@ export class NgFileDropDirective implements OnChanges, OnInit {
this.initEvents();
}

ngOnChanges(changes: {[propName: string]: SimpleChange}) {
ngOnChanges(changes: { [propName: string]: SimpleChange }) {
if (!this.options || !changes) {
return;
}

if(this.options.allowedExtensions) {
if (this.options.allowedExtensions) {
this.options.allowedExtensions = this.options.allowedExtensions.map(ext => ext.toLowerCase());
}
this.options = new NgUploaderOptions(this.options);
Expand All @@ -85,7 +85,7 @@ export class NgFileDropDirective implements OnChanges, OnInit {
this.el.nativeElement.addEventListener('dragover', this.stopEvent, false);
}

@HostListener('drop', ['$event']) onDrop(e: any): void {
@HostListener('drop', ['$event']) onDrop(e: Event | any): void {
this.onFileOver.emit(false);
this.files = Array.from<File>(e.dataTransfer.files);
if (!this.files || !this.files.length) {
Expand Down Expand Up @@ -121,7 +121,7 @@ export class NgFileDropDirective implements OnChanges, OnInit {
});
}

if(this.options.maxUploads > 0 && this.files.length > this.options.maxUploads) {
if (this.options.maxUploads > 0 && this.files.length > this.options.maxUploads) {
this.onUploadRejected.emit({file: this.files.pop(), reason: UploadRejected.MAX_UPLOADS_EXCEEDED});
this.files = [];
}
Expand All @@ -133,17 +133,21 @@ export class NgFileDropDirective implements OnChanges, OnInit {

@HostListener('dragover', ['$event'])
public onDragOver(e: any) {
if (!e) { return; }
if (!e) {
return;
}
this.onFileOver.emit(true);
}

@HostListener('dragleave', ['$event'])
public onDragLeave(e: any) {
if (!e) { return; }
if (!e) {
return;
}
this.onFileOver.emit(false);
}

private stopEvent(e: any): void {
private stopEvent(e: Event): void {
e.stopPropagation();
e.preventDefault();
}
Expand Down

0 comments on commit 9c1216e

Please sign in to comment.