upload: new 'click' mode with file/folder dialog support (#1738)

* new 'click' mode with file/folder dialog support

* fix uploading files to folders via drop on Safari
This commit is contained in:
Denys Vuika
2017-03-21 11:26:07 +00:00
committed by Mario Romano
parent 88abad743f
commit 4df72af86d
3 changed files with 159 additions and 30 deletions

View File

@@ -27,7 +27,7 @@ describe('UploadDirective', () => {
nativeElement = {
dispatchEvent: () => {}
};
directive = new UploadDirective(new ElementRef(nativeElement));
directive = new UploadDirective(new ElementRef(nativeElement), null);
});
it('should be enabled by default', () => {

View File

@@ -15,12 +15,12 @@
* limitations under the License.
*/
import { Directive, Input, HostBinding, HostListener, ElementRef } from '@angular/core';
import { Directive, Input, HostBinding, HostListener, ElementRef, Renderer, OnInit } from '@angular/core';
@Directive({
selector: '[adf-upload]'
})
export class UploadDirective {
export class UploadDirective implements OnInit {
@Input('adf-upload')
enabled: boolean = true;
@@ -28,25 +28,68 @@ export class UploadDirective {
@Input('adf-upload-data')
data: any;
@Input()
mode: string[] = ['drop']; // click|drop
@Input()
multiple: boolean;
@Input()
accept: string;
@Input()
directory: boolean;
@Input()
debug: boolean = false;
@HostBinding('class.adf-upload__dragging')
isDragging: boolean;
constructor(private el: ElementRef) {
private upload: HTMLInputElement;
constructor(private el: ElementRef, private renderer: Renderer) {
}
ngOnInit() {
if (this.isClickMode() && this.renderer) {
this.upload = this.renderer.createElement(this.el.nativeElement.parentNode, 'input') as HTMLInputElement;
this.upload.type = 'file';
this.upload.style.display = 'none';
this.upload.addEventListener('change', e => this.onSelectFiles(e));
if (this.multiple) {
this.upload.setAttribute('multiple', '');
}
if (this.accept) {
this.upload.setAttribute('accept', this.accept);
}
if (this.directory) {
this.upload.setAttribute('webkitdirectory', '');
}
}
}
@HostListener('click', ['$event'])
onClick(event: Event) {
if (this.isClickMode() && this.upload) {
event.preventDefault();
this.upload.click();
}
}
@HostListener('dragenter')
onDragEnter() {
if (this.enabled) {
if (this.isDropMode()) {
this.isDragging = true;
}
}
@HostListener('dragover', ['$event'])
onDragOver(event: Event) {
if (this.enabled) {
if (this.isDropMode()) {
if (event) {
event.preventDefault();
}
@@ -56,58 +99,94 @@ export class UploadDirective {
@HostListener('dragleave')
onDragLeave() {
if (this.enabled) {
if (this.isDropMode()) {
this.isDragging = false;
}
}
@HostListener('drop', ['$event'])
onDrop(event: DragEvent) {
if (this.enabled) {
if (this.isDropMode()) {
event.preventDefault();
event.stopPropagation();
this.isDragging = false;
let files = this.getFilesDropped(event.dataTransfer);
if (files.length > 0) {
let e = new CustomEvent('upload-files', {
detail: {
sender: this,
data: this.data,
files: files
},
bubbles: true
});
this.el.nativeElement.dispatchEvent(e);
}
const files = this.getFilesDropped(event.dataTransfer);
this.onUploadFiles(files);
}
}
onUploadFiles(files: File[]) {
if (this.enabled && files.length > 0) {
let e = new CustomEvent('upload-files', {
detail: {
sender: this,
data: this.data,
files: files
},
bubbles: true
});
this.el.nativeElement.dispatchEvent(e);
}
}
protected hasMode(mode: string): boolean {
return this.enabled && mode && this.mode && this.mode.indexOf(mode) > -1;
}
protected isDropMode(): boolean {
return this.hasMode('drop');
}
protected isClickMode(): boolean {
return this.hasMode('click');
}
/**
* Extract files from the DataTransfer object used to hold the data that is being dragged during a drag and drop operation.
* @param dataTransfer DataTransfer object
*/
protected getFilesDropped(dataTransfer: DataTransfer): File[] {
let result: File[] = [];
const result: File[] = [];
if (dataTransfer) {
let items: DataTransferItemList = dataTransfer.items;
const items: FileList = dataTransfer.files;
if (items && items.length > 0) {
for (let i = 0; i < items.length; i++) {
let item: DataTransferItem = items[i];
if (item.type) {
let file = item.getAsFile();
if (file) {
result.push(file);
}
}
result.push(items[i]);
}
}
}
return result;
}
/**
* Extract files from the FileList object used to hold files that user selected by means of File Dialog.
* @param fileList List of selected files
*/
protected getFilesSelected(fileList: FileList) {
let result: File[] = [];
if (fileList && fileList.length > 0) {
for (let i = 0; i < fileList.length; i++) {
result.push(fileList[i]);
}
}
return result;
}
/**
* Invoked when user selects files or folders by means of File Dialog
* @param e DOM event
*/
protected onSelectFiles(e: Event) {
if (this.isClickMode()) {
const input = (<HTMLInputElement>e.currentTarget);
const files = this.getFilesSelected(input.files);
this.onUploadFiles(files);
}
}
}