Merge branch 'development' into feature/emit-event-when-file-is-selected
This commit is contained in:
@@ -1,29 +1,29 @@
|
||||
import { Directive, EventEmitter, ElementRef, HostListener, Input, Output } from '@angular/core';
|
||||
|
||||
import { FileUploader } from './file-uploader.class';
|
||||
import { FileUploader, FileUploaderOptions } from './file-uploader.class';
|
||||
|
||||
@Directive({selector: '[ng2FileDrop]'})
|
||||
@Directive({ selector: '[ng2FileDrop]' })
|
||||
export class FileDropDirective {
|
||||
@Input() public uploader:FileUploader;
|
||||
@Output() public fileOver:EventEmitter<any> = new EventEmitter();
|
||||
@Output() public onFileDrop:EventEmitter<File[]> = new EventEmitter<File[]>();
|
||||
@Input() public uploader: FileUploader;
|
||||
@Output() public fileOver: EventEmitter<any> = new EventEmitter();
|
||||
@Output() public onFileDrop: EventEmitter<File[]> = new EventEmitter<File[]>();
|
||||
|
||||
protected element:ElementRef;
|
||||
protected element: ElementRef;
|
||||
|
||||
public constructor(element:ElementRef) {
|
||||
public constructor(element: ElementRef) {
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
public getOptions():any {
|
||||
public getOptions(): FileUploaderOptions {
|
||||
return this.uploader.options;
|
||||
}
|
||||
|
||||
public getFilters():any {
|
||||
public getFilters(): any {
|
||||
return {};
|
||||
}
|
||||
|
||||
@HostListener('drop', ['$event'])
|
||||
public onDrop(event:any):void {
|
||||
@HostListener('drop', [ '$event' ])
|
||||
public onDrop(event: any): void {
|
||||
let transfer = this._getTransfer(event);
|
||||
if (!transfer) {
|
||||
return;
|
||||
@@ -37,8 +37,8 @@ export class FileDropDirective {
|
||||
this.onFileDrop.emit(transfer.files);
|
||||
}
|
||||
|
||||
@HostListener('dragover', ['$event'])
|
||||
public onDragOver(event:any):void {
|
||||
@HostListener('dragover', [ '$event' ])
|
||||
public onDragOver(event: any): void {
|
||||
let transfer = this._getTransfer(event);
|
||||
if (!this._haveFiles(transfer.types)) {
|
||||
return;
|
||||
@@ -49,10 +49,10 @@ export class FileDropDirective {
|
||||
this.fileOver.emit(true);
|
||||
}
|
||||
|
||||
@HostListener('dragleave', ['$event'])
|
||||
public onDragLeave(event:any):any {
|
||||
@HostListener('dragleave', [ '$event' ])
|
||||
public onDragLeave(event: any): any {
|
||||
if ((this as any).element) {
|
||||
if (event.currentTarget === (this as any).element[0]) {
|
||||
if (event.currentTarget === (this as any).element[ 0 ]) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -61,16 +61,16 @@ export class FileDropDirective {
|
||||
this.fileOver.emit(false);
|
||||
}
|
||||
|
||||
protected _getTransfer(event:any):any {
|
||||
protected _getTransfer(event: any): any {
|
||||
return event.dataTransfer ? event.dataTransfer : event.originalEvent.dataTransfer; // jQuery fix;
|
||||
}
|
||||
|
||||
protected _preventAndStop(event:any):any {
|
||||
protected _preventAndStop(event: any): any {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
}
|
||||
|
||||
protected _haveFiles(types:any):any {
|
||||
protected _haveFiles(types: any): any {
|
||||
if (!types) {
|
||||
return false;
|
||||
}
|
||||
@@ -83,13 +83,4 @@ export class FileDropDirective {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
_addOverClass(item:any):any {
|
||||
item.addOverClass();
|
||||
}
|
||||
|
||||
_removeOverClass(item:any):any {
|
||||
item.removeOverClass();
|
||||
}*/
|
||||
}
|
||||
|
||||
@@ -4,31 +4,31 @@ import { FileUploader } from './file-uploader.class';
|
||||
|
||||
// todo: filters
|
||||
|
||||
@Directive({selector: '[ng2FileSelect]'})
|
||||
@Directive({ selector: '[ng2FileSelect]' })
|
||||
export class FileSelectDirective {
|
||||
@Input() public uploader:FileUploader;
|
||||
@Input() public uploader: FileUploader;
|
||||
@Output() public onFileSelected:EventEmitter<File[]> = new EventEmitter<File[]>();
|
||||
|
||||
protected element:ElementRef;
|
||||
protected element: ElementRef;
|
||||
|
||||
public constructor(element:ElementRef) {
|
||||
public constructor(element: ElementRef) {
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
public getOptions():any {
|
||||
public getOptions(): any {
|
||||
return this.uploader.options;
|
||||
}
|
||||
|
||||
public getFilters():any {
|
||||
return void 0;
|
||||
public getFilters(): any {
|
||||
return {};
|
||||
}
|
||||
|
||||
public isEmptyAfterSelection():boolean {
|
||||
public isEmptyAfterSelection(): boolean {
|
||||
return !!this.element.nativeElement.attributes.multiple;
|
||||
}
|
||||
|
||||
@HostListener('change', ['$event'])
|
||||
public onChange():any {
|
||||
public onChange(): any {
|
||||
// let files = this.uploader.isHTML5 ? this.element.nativeElement[0].files : this.element.nativeElement[0];
|
||||
let files = this.element.nativeElement.files;
|
||||
let options = this.getOptions();
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { EventEmitter } from '@angular/core';
|
||||
import { FileLikeObject } from './file-like-object.class';
|
||||
import { FileItem } from './file-item.class';
|
||||
import { FileType } from './file-type.class';
|
||||
@@ -32,6 +33,8 @@ export interface FileUploaderOptions {
|
||||
itemAlias?: string;
|
||||
authTokenHeader?: string;
|
||||
additionalParameter?:{[key: string]: any};
|
||||
formatDataFunction?:Function;
|
||||
formatDataFunctionIsAsync?:boolean;
|
||||
}
|
||||
|
||||
export class FileUploader {
|
||||
@@ -43,19 +46,23 @@ export class FileUploader {
|
||||
public _nextIndex:number = 0;
|
||||
public autoUpload:any;
|
||||
public authTokenHeader: string;
|
||||
public response: EventEmitter<any>;
|
||||
|
||||
public options:FileUploaderOptions = {
|
||||
autoUpload: false,
|
||||
isHTML5: true,
|
||||
filters: [],
|
||||
removeAfterUpload: false,
|
||||
disableMultipart: false
|
||||
disableMultipart: false,
|
||||
formatDataFunction: function (item:FileItem) { return item._file; },
|
||||
formatDataFunctionIsAsync: false
|
||||
};
|
||||
|
||||
protected _failFilterIndex:number;
|
||||
|
||||
public constructor(options:FileUploaderOptions) {
|
||||
this.setOptions(options);
|
||||
this.response = new EventEmitter<any>();
|
||||
}
|
||||
|
||||
public setOptions(options:FileUploaderOptions):void {
|
||||
@@ -291,6 +298,7 @@ export class FileUploader {
|
||||
}
|
||||
|
||||
protected _xhrTransport(item:FileItem):any {
|
||||
let that = this;
|
||||
let xhr = item._xhr = new XMLHttpRequest();
|
||||
let sendable:any;
|
||||
this._onBeforeUploadItem(item);
|
||||
@@ -315,7 +323,7 @@ export class FileUploader {
|
||||
});
|
||||
}
|
||||
} else {
|
||||
sendable = item._file;
|
||||
sendable = this.options.formatDataFunction(item);
|
||||
}
|
||||
|
||||
xhr.upload.onprogress = (event:any) => {
|
||||
@@ -357,7 +365,18 @@ export class FileUploader {
|
||||
if (this.authToken) {
|
||||
xhr.setRequestHeader(this.authTokenHeader, this.authToken);
|
||||
}
|
||||
xhr.send(sendable);
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState == XMLHttpRequest.DONE) {
|
||||
that.response.emit(xhr.responseText)
|
||||
}
|
||||
}
|
||||
if (this.options.formatDataFunctionIsAsync) {
|
||||
sendable.then(
|
||||
(result:any) => xhr.send(JSON.stringify(result))
|
||||
);
|
||||
} else {
|
||||
xhr.send(sendable);
|
||||
}
|
||||
this._render();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user