-
Notifications
You must be signed in to change notification settings - Fork 3
/
s3uploaddirective.ts
71 lines (58 loc) · 1.7 KB
/
s3uploaddirective.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { Directive, EventEmitter, ElementRef, HostListener, Input, Output } from 'angular2/core';
import {s3upload} from './s3upload';
@Directive({ selector: '[s3upload]' })
export class UploadDirective {
@Input() public s3upload: s3upload;
@Output() public fileOver: EventEmitter<any> = new EventEmitter();
private element: ElementRef;
public constructor(element: ElementRef) {
this.element = element;
}
@HostListener('drop', ['$event'])
public onDrop(event: any): void {
let transfer = this._getTransfer(event);
if (!transfer) {
return;
}
this._preventAndStop(event);
this.s3upload.add(transfer.files);
this.fileOver.emit(false);
}
@HostListener('dragover', ['$event'])
public onDragOver(event: any): void {
let transfer = this._getTransfer(event);
if (!this._haveFiles(transfer.types)) {
return;
}
transfer.dropEffect = 'copy';
this._preventAndStop(event);
this.fileOver.emit(true);
}
@HostListener('dragleave', ['$event'])
public onDragLeave(event: any): any {
if (event.currentTarget === (this as any).element[0]) {
return;
}
this._preventAndStop(event);
this.fileOver.emit(false);
}
private _getTransfer(event: any): any {
return event.dataTransfer ? event.dataTransfer : event.originalEvent.dataTransfer; // jQuery fix;
}
private _preventAndStop(event: any): any {
event.preventDefault();
event.stopPropagation();
}
private _haveFiles(types: any): any {
if (!types) {
return false;
}
if (types.indexOf) {
return types.indexOf('Files') !== -1;
} else if (types.contains) {
return types.contains('Files');
} else {
return false;
}
}
}