forked from Alfresco/alfresco-ng2-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file-draggable.directive.ts
159 lines (137 loc) · 5.22 KB
/
file-draggable.directive.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* tslint:disable:no-input-rename */
import { FileUtils } from '@alfresco/adf-core';
import { Directive, ElementRef, EventEmitter, Input, NgZone, OnDestroy, OnInit, Output } from '@angular/core';
@Directive({
selector: '[file-draggable]'
})
export class FileDraggableDirective implements OnInit, OnDestroy {
files: File [];
/** Enables/disables drag-and-drop functionality. */
@Input('file-draggable')
enabled: boolean = true;
/** Emitted when one or more files are dragged and dropped onto the draggable element. */
@Output()
filesDropped: EventEmitter<File[]> = new EventEmitter<File[]>();
/** @deprecated in 2.4.0: use `filesDropped` instead.
* Emitted when one or more files are dragged and dropped onto the draggable element.
*/
@Output()
filesEntityDropped: EventEmitter<any> = new EventEmitter();
/** Emitted when a directory is dragged and dropped onto the draggable element. */
@Output()
folderEntityDropped: EventEmitter<any> = new EventEmitter();
private cssClassName: string = 'file-draggable__input-focus';
private element: HTMLElement;
constructor(el: ElementRef, private ngZone: NgZone) {
this.element = el.nativeElement;
}
ngOnInit() {
this.ngZone.runOutsideAngular(() => {
this.element.addEventListener('dragenter', this.onDragEnter.bind(this));
this.element.addEventListener('dragover', this.onDragOver.bind(this));
this.element.addEventListener('dragleave', this.onDragLeave.bind(this));
this.element.addEventListener('drop', this.onDropFiles.bind(this));
});
}
ngOnDestroy() {
this.element.removeEventListener('dragenter', this.onDragEnter);
this.element.removeEventListener('dragover', this.onDragOver);
this.element.removeEventListener('dragleave', this.onDragLeave);
this.element.removeEventListener('drop', this.onDropFiles);
}
/**
* Method called when files is dropped in the drag and drop area.
* @param event DOM event.
*/
onDropFiles(event: any): void {
if (this.enabled && !event.defaultPrevented) {
this.preventDefault(event);
// Chrome, Edge, Firefox, Opera (Files + Folders)
const items = event.dataTransfer.items;
if (items) {
const files: File[] = [];
for (let i = 0; i < items.length; i++) {
if (items[i].webkitGetAsEntry) {
const item = items[i].webkitGetAsEntry();
if (item) {
if (item.isFile) {
const file = items[i].getAsFile();
if (file) {
files.push(file);
}
} else if (item.isDirectory) {
this.folderEntityDropped.emit(item);
}
}
}
}
if (files.length > 0) {
this.filesDropped.emit(files);
}
} else {
// IE, Safari, Chrome, Edge, Firefox, Opera (Files only)
const files = FileUtils.toFileArray(event.dataTransfer.files);
this.filesDropped.emit(files);
}
this.element.classList.remove(this.cssClassName);
}
}
/**
* Change the style of the drag area when a file drag in.
*
* @param event - DOM event.
*/
onDragEnter(event: Event): void {
if (this.enabled && !event.defaultPrevented) {
this.preventDefault(event);
this.element.classList.add(this.cssClassName);
}
}
/**
* Change the style of the drag area when a file drag out.
*
* @param event - DOM event.
*/
onDragLeave(event: Event): void {
if (this.enabled && !event.defaultPrevented) {
this.preventDefault(event);
this.element.classList.remove(this.cssClassName);
}
}
/**
* Change the style of the drag area when a file is over the drag area.
*
* @param event
*/
onDragOver(event: Event): void {
if (this.enabled && !event.defaultPrevented) {
this.preventDefault(event);
this.element.classList.add(this.cssClassName);
}
}
/**
* Prevent default and stop propagation of the DOM event.
*
* @param $event - DOM event.
*/
preventDefault(event: Event): void {
event.stopPropagation();
event.preventDefault();
}
}