Merge pull request #903 from adrianfaciu/chore/docs

Updating docs and formatting files
This commit was merged in pull request #903.
This commit is contained in:
Adrian Fâciu
2017-10-07 13:00:53 +03:00
committed by GitHub
8 changed files with 251 additions and 271 deletions

View File

@@ -25,6 +25,9 @@ Easy to use Angular2 directives for files upload ([demo](http://valor-software.g
- `uploader` - (`FileUploader`) - uploader object. See using in [demo](https://github.com/valor-software/ng2-file-upload/blob/master/demo/components/file-upload/simple-demo.ts) - `uploader` - (`FileUploader`) - uploader object. See using in [demo](https://github.com/valor-software/ng2-file-upload/blob/master/demo/components/file-upload/simple-demo.ts)
### Events
- `onFileSelected` - fires when files are selected and added to the uploader queue
## API for `ng2FileDrop` ## API for `ng2FileDrop`
### Properties ### Properties
@@ -39,6 +42,7 @@ Easy to use Angular2 directives for files upload ([demo](http://valor-software.g
4. `itemAlias` - item alias (form name redefenition) 4. `itemAlias` - item alias (form name redefenition)
5. `formatDataFunction` - Function to modify the request body. 'DisableMultipart' must be 'true' for this function to be called. 5. `formatDataFunction` - Function to modify the request body. 'DisableMultipart' must be 'true' for this function to be called.
6. `formatDataFunctionIsAsync` - Informs if the function sent in 'formatDataFunction' is asynchronous. Defaults to false. 6. `formatDataFunctionIsAsync` - Informs if the function sent in 'formatDataFunction' is asynchronous. Defaults to false.
7. `parametersBeforeFiles` - States if additional parameters should be appended before or after the file. Defaults to false.
### Events ### Events

View File

@@ -28,6 +28,10 @@ import { FileSelectDirective, FileDropDirective, FileUploader } from 'ng2-file-u
4. `itemAlias` - item alias (form name redefenition) 4. `itemAlias` - item alias (form name redefenition)
5. `formatDataFunction` - Function to modify the request body. 'DisableMultipart' must be 'true' for this function to be called. 5. `formatDataFunction` - Function to modify the request body. 'DisableMultipart' must be 'true' for this function to be called.
6. `formatDataFunctionIsAsync` - Informs if the function sent in 'formatDataFunction' is asynchronous. Defaults to false. 6. `formatDataFunctionIsAsync` - Informs if the function sent in 'formatDataFunction' is asynchronous. Defaults to false.
7. `parametersBeforeFiles` - States if additional parameters should be appended before or after the file. Defaults to false.
### Events
- `onFileSelected` - fires when files are selected and added to the uploader queue
## FileDrop API ## FileDrop API
@@ -40,4 +44,4 @@ import { FileSelectDirective, FileDropDirective, FileUploader } from 'ng2-file-u
- `fileOver` - it fires during 'over' and 'out' events for Drop Area; returns `boolean`: `true` if file is over Drop Area, `false` in case of out. - `fileOver` - it fires during 'over' and 'out' events for Drop Area; returns `boolean`: `true` if file is over Drop Area, `false` in case of out.
See using in [ts demo](https://github.com/valor-software/ng2-file-upload/blob/master/demo/components/file-upload/simple-demo.ts) and See using in [ts demo](https://github.com/valor-software/ng2-file-upload/blob/master/demo/components/file-upload/simple-demo.ts) and
[html demo](https://github.com/valor-software/ng2-file-upload/blob/master/demo/components/file-upload/simple-demo.html) [html demo](https://github.com/valor-software/ng2-file-upload/blob/master/demo/components/file-upload/simple-demo.html)
- `onFileDrop` - it fires after a file has been dropped on a Drop Area; you can pass in `$event` to get the list of files that were dropped. i.e. `(onFileDrop)="dropped($event)"` - `onFileDrop` - it fires after a file has been dropped on a Drop Area; you can pass in `$event` to get the list of files that were dropped. i.e. `(onFileDrop)="dropped($event)"`

View File

@@ -2,30 +2,30 @@ import { FileLikeObject } from './file-like-object.class';
import { FileUploader, ParsedResponseHeaders, FileUploaderOptions } from './file-uploader.class'; import { FileUploader, ParsedResponseHeaders, FileUploaderOptions } from './file-uploader.class';
export class FileItem { export class FileItem {
public file:FileLikeObject; public file: FileLikeObject;
public _file:File; public _file: File;
public alias:string; public alias: string;
public url:string = '/'; public url: string = '/';
public method:string; public method: string;
public headers:any = []; public headers: any = [];
public withCredentials:boolean = true; public withCredentials: boolean = true;
public formData:any = []; public formData: any = [];
public isReady:boolean = false; public isReady: boolean = false;
public isUploading:boolean = false; public isUploading: boolean = false;
public isUploaded:boolean = false; public isUploaded: boolean = false;
public isSuccess:boolean = false; public isSuccess: boolean = false;
public isCancel:boolean = false; public isCancel: boolean = false;
public isError:boolean = false; public isError: boolean = false;
public progress:number = 0; public progress: number = 0;
public index:number = void 0; public index: number = void 0;
public _xhr:XMLHttpRequest; public _xhr: XMLHttpRequest;
public _form:any; public _form: any;
protected uploader:FileUploader; protected uploader: FileUploader;
protected some:File; protected some: File;
protected options:FileUploaderOptions; protected options: FileUploaderOptions;
public constructor(uploader:FileUploader, some:File, options:FileUploaderOptions) { public constructor(uploader: FileUploader, some: File, options: FileUploaderOptions) {
this.uploader = uploader; this.uploader = uploader;
this.some = some; this.some = some;
this.options = options; this.options = options;
@@ -38,7 +38,7 @@ export class FileItem {
this.url = uploader.options.url; this.url = uploader.options.url;
} }
public upload():void { public upload(): void {
try { try {
this.uploader.uploadItem(this); this.uploader.uploadItem(this);
} catch (e) { } catch (e) {
@@ -47,43 +47,43 @@ export class FileItem {
} }
} }
public cancel():void { public cancel(): void {
this.uploader.cancelItem(this); this.uploader.cancelItem(this);
} }
public remove():void { public remove(): void {
this.uploader.removeFromQueue(this); this.uploader.removeFromQueue(this);
} }
public onBeforeUpload():void { public onBeforeUpload(): void {
return void 0; return void 0;
} }
public onBuildForm(form:any):any { public onBuildForm(form: any): any {
return {form}; return { form };
} }
public onProgress(progress:number):any { public onProgress(progress: number): any {
return {progress}; return { progress };
} }
public onSuccess(response:string, status:number, headers:ParsedResponseHeaders):any { public onSuccess(response: string, status: number, headers: ParsedResponseHeaders): any {
return {response, status, headers}; return { response, status, headers };
} }
public onError(response:string, status:number, headers:ParsedResponseHeaders):any { public onError(response: string, status: number, headers: ParsedResponseHeaders): any {
return {response, status, headers}; return { response, status, headers };
} }
public onCancel(response:string, status:number, headers:ParsedResponseHeaders):any { public onCancel(response: string, status: number, headers: ParsedResponseHeaders): any {
return {response, status, headers}; return { response, status, headers };
} }
public onComplete(response:string, status:number, headers:ParsedResponseHeaders):any { public onComplete(response: string, status: number, headers: ParsedResponseHeaders): any {
return {response, status, headers}; return { response, status, headers };
} }
public _onBeforeUpload():void { public _onBeforeUpload(): void {
this.isReady = true; this.isReady = true;
this.isUploading = true; this.isUploading = true;
this.isUploaded = false; this.isUploaded = false;
@@ -94,16 +94,16 @@ export class FileItem {
this.onBeforeUpload(); this.onBeforeUpload();
} }
public _onBuildForm(form:any):void { public _onBuildForm(form: any): void {
this.onBuildForm(form); this.onBuildForm(form);
} }
public _onProgress(progress:number):void { public _onProgress(progress: number): void {
this.progress = progress; this.progress = progress;
this.onProgress(progress); this.onProgress(progress);
} }
public _onSuccess(response:string, status:number, headers:ParsedResponseHeaders):void { public _onSuccess(response: string, status: number, headers: ParsedResponseHeaders): void {
this.isReady = false; this.isReady = false;
this.isUploading = false; this.isUploading = false;
this.isUploaded = true; this.isUploaded = true;
@@ -115,7 +115,7 @@ export class FileItem {
this.onSuccess(response, status, headers); this.onSuccess(response, status, headers);
} }
public _onError(response:string, status:number, headers:ParsedResponseHeaders):void { public _onError(response: string, status: number, headers: ParsedResponseHeaders): void {
this.isReady = false; this.isReady = false;
this.isUploading = false; this.isUploading = false;
this.isUploaded = true; this.isUploaded = true;
@@ -127,7 +127,7 @@ export class FileItem {
this.onError(response, status, headers); this.onError(response, status, headers);
} }
public _onCancel(response:string, status:number, headers:ParsedResponseHeaders):void { public _onCancel(response: string, status: number, headers: ParsedResponseHeaders): void {
this.isReady = false; this.isReady = false;
this.isUploading = false; this.isUploading = false;
this.isUploaded = false; this.isUploaded = false;
@@ -139,7 +139,7 @@ export class FileItem {
this.onCancel(response, status, headers); this.onCancel(response, status, headers);
} }
public _onComplete(response:string, status:number, headers:ParsedResponseHeaders):void { public _onComplete(response: string, status: number, headers: ParsedResponseHeaders): void {
this.onComplete(response, status, headers); this.onComplete(response, status, headers);
if (this.uploader.options.removeAfterUpload) { if (this.uploader.options.removeAfterUpload) {
@@ -147,7 +147,7 @@ export class FileItem {
} }
} }
public _prepareToUploading():void { public _prepareToUploading(): void {
this.index = this.index || ++this.uploader._nextIndex; this.index = this.index || ++this.uploader._nextIndex;
this.isReady = true; this.isReady = true;
} }

View File

@@ -1,32 +1,31 @@
function isElement(node:any):boolean { function isElement(node: any): boolean {
return !!(node && (node.nodeName || node.prop && node.attr && node.find)); return !!(node && (node.nodeName || node.prop && node.attr && node.find));
} }
export class FileLikeObject { export class FileLikeObject {
public lastModifiedDate:any; public lastModifiedDate: any;
public size:any; public size: any;
public type:string; public type: string;
public name:string; public name: string;
public rawFile:string; public rawFile: string;
public constructor(fileOrInput:any) { public constructor(fileOrInput: any) {
this.rawFile = fileOrInput; this.rawFile = fileOrInput;
let isInput = isElement(fileOrInput); let isInput = isElement(fileOrInput);
let fakePathOrObject = isInput ? fileOrInput.value : fileOrInput; let fakePathOrObject = isInput ? fileOrInput.value : fileOrInput;
let postfix = typeof fakePathOrObject === 'string' ? 'FakePath' : 'Object'; let postfix = typeof fakePathOrObject === 'string' ? 'FakePath' : 'Object';
let method = '_createFrom' + postfix; let method = '_createFrom' + postfix;
(this as any)[method](fakePathOrObject); (this as any)[ method ](fakePathOrObject);
} }
public _createFromFakePath(path:string):void { public _createFromFakePath(path: string): void {
this.lastModifiedDate = void 0; this.lastModifiedDate = void 0;
this.size = void 0; this.size = void 0;
this.type = 'like/' + path.slice(path.lastIndexOf('.') + 1).toLowerCase(); this.type = 'like/' + path.slice(path.lastIndexOf('.') + 1).toLowerCase();
this.name = path.slice(path.lastIndexOf('/') + path.lastIndexOf('\\') + 2); this.name = path.slice(path.lastIndexOf('/') + path.lastIndexOf('\\') + 2);
} }
public _createFromObject(object:{size:number, type:string, name:string}):void { public _createFromObject(object: { size: number, type: string, name: string }): void {
// this.lastModifiedDate = copy(object.lastModifiedDate);
this.size = object.size; this.size = object.size;
this.type = object.type; this.type = object.type;
this.name = object.name; this.name = object.name;

View File

@@ -2,12 +2,10 @@ import { Directive, EventEmitter, ElementRef, Input, HostListener, Output } from
import { FileUploader } from './file-uploader.class'; import { FileUploader } from './file-uploader.class';
// todo: filters
@Directive({ selector: '[ng2FileSelect]' }) @Directive({ selector: '[ng2FileSelect]' })
export class FileSelectDirective { export class FileSelectDirective {
@Input() public uploader: FileUploader; @Input() public uploader: FileUploader;
@Output() public onFileSelected:EventEmitter<File[]> = new EventEmitter<File[]>(); @Output() public onFileSelected: EventEmitter<File[]> = new EventEmitter<File[]>();
protected element: ElementRef; protected element: ElementRef;
@@ -27,23 +25,17 @@ export class FileSelectDirective {
return !!this.element.nativeElement.attributes.multiple; return !!this.element.nativeElement.attributes.multiple;
} }
@HostListener('change', ['$event']) @HostListener('change')
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 files = this.element.nativeElement.files;
let options = this.getOptions(); let options = this.getOptions();
let filters = this.getFilters(); let filters = this.getFilters();
// if(!this.uploader.isHTML5) this.destroy();
this.uploader.addToQueue(files, options, filters); this.uploader.addToQueue(files, options, filters);
this.onFileSelected.emit(files); this.onFileSelected.emit(files);
if (this.isEmptyAfterSelection()) { if (this.isEmptyAfterSelection()) {
// todo
this.element.nativeElement.value = ''; this.element.nativeElement.value = '';
/*this.element.nativeElement
.replaceWith(this.element = this.element.nativeElement.clone(true)); // IE fix*/
} }
} }
} }

View File

@@ -1,6 +1,6 @@
export class FileType { export class FileType {
/* MS office */ /* MS office */
public static mime_doc:string[] = [ public static mime_doc: string[] = [
'application/msword', 'application/msword',
'application/msword', 'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
@@ -8,7 +8,7 @@ export class FileType {
'application/vnd.ms-word.document.macroEnabled.12', 'application/vnd.ms-word.document.macroEnabled.12',
'application/vnd.ms-word.template.macroEnabled.12' 'application/vnd.ms-word.template.macroEnabled.12'
]; ];
public static mime_xsl:string[] = [ public static mime_xsl: string[] = [
'application/vnd.ms-excel', 'application/vnd.ms-excel',
'application/vnd.ms-excel', 'application/vnd.ms-excel',
'application/vnd.ms-excel', 'application/vnd.ms-excel',
@@ -19,7 +19,7 @@ export class FileType {
'application/vnd.ms-excel.addin.macroEnabled.12', 'application/vnd.ms-excel.addin.macroEnabled.12',
'application/vnd.ms-excel.sheet.binary.macroEnabled.12' 'application/vnd.ms-excel.sheet.binary.macroEnabled.12'
]; ];
public static mime_ppt:string[] = [ public static mime_ppt: string[] = [
'application/vnd.ms-powerpoint', 'application/vnd.ms-powerpoint',
'application/vnd.ms-powerpoint', 'application/vnd.ms-powerpoint',
'application/vnd.ms-powerpoint', 'application/vnd.ms-powerpoint',
@@ -34,7 +34,7 @@ export class FileType {
]; ];
/* PSD */ /* PSD */
public static mime_psd:string[] = [ public static mime_psd: string[] = [
'image/photoshop', 'image/photoshop',
'image/x-photoshop', 'image/x-photoshop',
'image/psd', 'image/psd',
@@ -44,7 +44,7 @@ export class FileType {
]; ];
/* Compressed files */ /* Compressed files */
public static mime_compress:string[] = [ public static mime_compress: string[] = [
'application/x-gtar', 'application/x-gtar',
'application/x-gcompress', 'application/x-gcompress',
'application/compress', 'application/compress',
@@ -53,7 +53,7 @@ export class FileType {
'application/octet-stream' 'application/octet-stream'
]; ];
public static getMimeClass(file:any):string { public static getMimeClass(file: any): string {
let mimeClass = 'application'; let mimeClass = 'application';
if (this.mime_psd.indexOf(file.type) !== -1) { if (this.mime_psd.indexOf(file.type) !== -1) {
mimeClass = 'image'; mimeClass = 'image';
@@ -81,8 +81,8 @@ export class FileType {
return mimeClass; return mimeClass;
} }
public static fileTypeDetection(inputFilename:string):string { public static fileTypeDetection(inputFilename: string): string {
let types:{[key:string]:string} = { let types: { [ key: string ]: string } = {
'jpg': 'image', 'jpg': 'image',
'jpeg': 'image', 'jpeg': 'image',
'tif': 'image', 'tif': 'image',
@@ -144,11 +144,11 @@ export class FileType {
if (chunks.length < 2) { if (chunks.length < 2) {
return 'application'; return 'application';
} }
let extension = chunks[chunks.length - 1].toLowerCase(); let extension = chunks[ chunks.length - 1 ].toLowerCase();
if (types[extension] === undefined) { if (types[ extension ] === undefined) {
return 'application'; return 'application';
} else { } else {
return types[extension]; return types[ extension ];
} }
} }
} }

View File

@@ -5,9 +5,9 @@ import { FileDropDirective } from './file-drop.directive';
import { FileSelectDirective } from './file-select.directive'; import { FileSelectDirective } from './file-select.directive';
@NgModule({ @NgModule({
imports: [CommonModule], imports: [ CommonModule ],
declarations: [FileDropDirective, FileSelectDirective], declarations: [ FileDropDirective, FileSelectDirective ],
exports: [FileDropDirective, FileSelectDirective] exports: [ FileDropDirective, FileSelectDirective ]
}) })
export class FileUploadModule { export class FileUploadModule {
} }

View File

@@ -3,103 +3,106 @@ import { FileLikeObject } from './file-like-object.class';
import { FileItem } from './file-item.class'; import { FileItem } from './file-item.class';
import { FileType } from './file-type.class'; import { FileType } from './file-type.class';
function isFile(value:any):boolean { function isFile(value: any): boolean {
return (File && value instanceof File); return (File && value instanceof File);
} }
// function isFileLikeObject(value:any) {
export interface Headers { export interface Headers {
name:string; name: string;
value:string; value: string;
} }
export type ParsedResponseHeaders = {[headerFieldName:string]:string}; export type ParsedResponseHeaders = { [ headerFieldName: string ]: string };
export type FilterFunction = {name:string, fn:(item?:FileLikeObject, options?:FileUploaderOptions)=>boolean}; export type FilterFunction = {
name: string,
fn: (item?: FileLikeObject, options?: FileUploaderOptions) => boolean
};
export interface FileUploaderOptions { export interface FileUploaderOptions {
allowedMimeType?:Array<string>; allowedMimeType?: string[];
allowedFileType?:Array<string>; allowedFileType?: string[];
autoUpload?:boolean; autoUpload?: boolean;
isHTML5?:boolean; isHTML5?: boolean;
filters?:Array<FilterFunction>; filters?: FilterFunction[];
headers?:Array<Headers>; headers?: Headers[];
method?:string; method?: string;
authToken?:string; authToken?: string;
maxFileSize?:number; maxFileSize?: number;
queueLimit?:number; queueLimit?: number;
removeAfterUpload?:boolean; removeAfterUpload?: boolean;
url?:string; url?: string;
disableMultipart?:boolean; disableMultipart?: boolean;
itemAlias?: string; itemAlias?: string;
authTokenHeader?: string; authTokenHeader?: string;
additionalParameter?:{[key: string]: any}; additionalParameter?: { [ key: string ]: any };
formatDataFunction?:Function; parametersBeforeFiles?: boolean;
formatDataFunctionIsAsync?:boolean; formatDataFunction?: Function;
formatDataFunctionIsAsync?: boolean;
} }
export class FileUploader { export class FileUploader {
public authToken:string; public authToken: string;
public isUploading:boolean = false; public isUploading: boolean = false;
public queue:Array<FileItem> = []; public queue: FileItem[] = [];
public progress:number = 0; public progress: number = 0;
public _nextIndex:number = 0; public _nextIndex: number = 0;
public autoUpload:any; public autoUpload: any;
public authTokenHeader: string; public authTokenHeader: string;
public response: EventEmitter<any>; public response: EventEmitter<any>;
public options:FileUploaderOptions = { public options: FileUploaderOptions = {
autoUpload: false, autoUpload: false,
isHTML5: true, isHTML5: true,
filters: [], filters: [],
removeAfterUpload: false, removeAfterUpload: false,
disableMultipart: false, disableMultipart: false,
formatDataFunction: function (item:FileItem) { return item._file; }, formatDataFunction: (item: FileItem) => item._file,
formatDataFunctionIsAsync: false formatDataFunctionIsAsync: false
}; };
protected _failFilterIndex:number; protected _failFilterIndex: number;
public constructor(options:FileUploaderOptions) { public constructor(options: FileUploaderOptions) {
this.setOptions(options); this.setOptions(options);
this.response = new EventEmitter<any>(); this.response = new EventEmitter<any>();
} }
public setOptions(options:FileUploaderOptions):void { public setOptions(options: FileUploaderOptions): void {
this.options = Object.assign(this.options, options); this.options = Object.assign(this.options, options);
this.authToken = options.authToken; this.authToken = options.authToken;
this.authTokenHeader = options.authTokenHeader || 'Authorization'; this.authTokenHeader = options.authTokenHeader || 'Authorization';
this.autoUpload = options.autoUpload; this.autoUpload = options.autoUpload;
this.options.filters.unshift({name: 'queueLimit', fn: this._queueLimitFilter}); this.options.filters.unshift({ name: 'queueLimit', fn: this._queueLimitFilter });
if (this.options.maxFileSize) { if (this.options.maxFileSize) {
this.options.filters.unshift({name: 'fileSize', fn: this._fileSizeFilter}); this.options.filters.unshift({ name: 'fileSize', fn: this._fileSizeFilter });
} }
if (this.options.allowedFileType) { if (this.options.allowedFileType) {
this.options.filters.unshift({name: 'fileType', fn: this._fileTypeFilter}); this.options.filters.unshift({ name: 'fileType', fn: this._fileTypeFilter });
} }
if (this.options.allowedMimeType) { if (this.options.allowedMimeType) {
this.options.filters.unshift({name: 'mimeType', fn: this._mimeTypeFilter}); this.options.filters.unshift({ name: 'mimeType', fn: this._mimeTypeFilter });
} }
for(let i = 0; i < this.queue.length; i++) { for (let i = 0; i < this.queue.length; i++) {
this.queue[i].url = this.options.url; this.queue[ i ].url = this.options.url;
} }
// this.options.filters.unshift({name: 'folder', fn: this._folderFilter});
} }
public addToQueue(files:File[], options?:FileUploaderOptions, filters?:FilterFunction[]|string):void { public addToQueue(files: File[], options?: FileUploaderOptions, filters?: FilterFunction[] | string): void {
let list:File[] = []; let list: File[] = [];
for (let file of files) { for (let file of files) {
list.push(file); list.push(file);
} }
let arrayOfFilters = this._getFilters(filters); let arrayOfFilters = this._getFilters(filters);
let count = this.queue.length; let count = this.queue.length;
let addedFileItems:FileItem[] = []; let addedFileItems: FileItem[] = [];
list.map((some:File) => { list.map((some: File) => {
if (!options) { if (!options) {
options = this.options; options = this.options;
} }
@@ -111,7 +114,7 @@ export class FileUploader {
this.queue.push(fileItem); this.queue.push(fileItem);
this._onAfterAddingFile(fileItem); this._onAfterAddingFile(fileItem);
} else { } else {
let filter = arrayOfFilters[this._failFilterIndex]; let filter = arrayOfFilters[ this._failFilterIndex ];
this._onWhenAddingFileFailed(temp, filter, options); this._onWhenAddingFileFailed(temp, filter, options);
} }
}); });
@@ -125,9 +128,9 @@ export class FileUploader {
} }
} }
public removeFromQueue(value:FileItem):void { public removeFromQueue(value: FileItem): void {
let index = this.getIndexOfItem(value); let index = this.getIndexOfItem(value);
let item = this.queue[index]; let item = this.queue[ index ];
if (item.isUploading) { if (item.isUploading) {
item.cancel(); item.cancel();
} }
@@ -135,149 +138,144 @@ export class FileUploader {
this.progress = this._getTotalProgress(); this.progress = this._getTotalProgress();
} }
public clearQueue():void { public clearQueue(): void {
while (this.queue.length) { while (this.queue.length) {
this.queue[0].remove(); this.queue[ 0 ].remove();
} }
this.progress = 0; this.progress = 0;
} }
public uploadItem(value:FileItem):void { public uploadItem(value: FileItem): void {
let index = this.getIndexOfItem(value); let index = this.getIndexOfItem(value);
let item = this.queue[index]; let item = this.queue[ index ];
let transport = this.options.isHTML5 ? '_xhrTransport' : '_iframeTransport'; let transport = this.options.isHTML5 ? '_xhrTransport' : '_iframeTransport';
item._prepareToUploading(); item._prepareToUploading();
if (this.isUploading) { if (this.isUploading) {
return; return;
} }
this.isUploading = true; this.isUploading = true;
(this as any)[transport](item); (this as any)[ transport ](item);
} }
public cancelItem(value:FileItem):void { public cancelItem(value: FileItem): void {
let index = this.getIndexOfItem(value); let index = this.getIndexOfItem(value);
let item = this.queue[index]; let item = this.queue[ index ];
let prop = this.options.isHTML5 ? item._xhr : item._form; let prop = this.options.isHTML5 ? item._xhr : item._form;
if (item && item.isUploading) { if (item && item.isUploading) {
prop.abort(); prop.abort();
} }
} }
public uploadAll():void { public uploadAll(): void {
let items = this.getNotUploadedItems().filter((item:FileItem) => !item.isUploading); let items = this.getNotUploadedItems().filter((item: FileItem) => !item.isUploading);
if (!items.length) { if (!items.length) {
return; return;
} }
items.map((item:FileItem) => item._prepareToUploading()); items.map((item: FileItem) => item._prepareToUploading());
items[0].upload(); items[ 0 ].upload();
} }
public cancelAll():void { public cancelAll(): void {
let items = this.getNotUploadedItems(); let items = this.getNotUploadedItems();
items.map((item:FileItem) => item.cancel()); items.map((item: FileItem) => item.cancel());
} }
public isFile(value:any):boolean { public isFile(value: any): boolean {
return isFile(value); return isFile(value);
} }
public isFileLikeObject(value:any):boolean { public isFileLikeObject(value: any): boolean {
return value instanceof FileLikeObject; return value instanceof FileLikeObject;
} }
public getIndexOfItem(value:any):number { public getIndexOfItem(value: any): number {
return typeof value === 'number' ? value : this.queue.indexOf(value); return typeof value === 'number' ? value : this.queue.indexOf(value);
} }
public getNotUploadedItems():Array<any> { public getNotUploadedItems(): any[] {
return this.queue.filter((item:FileItem) => !item.isUploaded); return this.queue.filter((item: FileItem) => !item.isUploaded);
} }
public getReadyItems():Array<any> { public getReadyItems(): any[] {
return this.queue return this.queue
.filter((item:FileItem) => (item.isReady && !item.isUploading)) .filter((item: FileItem) => (item.isReady && !item.isUploading))
.sort((item1:any, item2:any) => item1.index - item2.index); .sort((item1: any, item2: any) => item1.index - item2.index);
} }
public destroy():void { public destroy(): void {
return void 0;
/*forEach(this._directives, (key) => {
forEach(this._directives[key], (object) => {
object.destroy();
});
});*/
}
public onAfterAddingAll(fileItems:any):any {
return {fileItems};
}
public onBuildItemForm(fileItem:FileItem, form:any):any {
return {fileItem, form};
}
public onAfterAddingFile(fileItem:FileItem):any {
return {fileItem};
}
public onWhenAddingFileFailed(item:FileLikeObject, filter:any, options:any):any {
return {item, filter, options};
}
public onBeforeUploadItem(fileItem:FileItem):any {
return {fileItem};
}
public onProgressItem(fileItem:FileItem, progress:any):any {
return {fileItem, progress};
}
public onProgressAll(progress:any):any {
return {progress};
}
public onSuccessItem(item:FileItem, response:string, status:number, headers:ParsedResponseHeaders):any {
return {item, response, status, headers};
}
public onErrorItem(item:FileItem, response:string, status:number, headers:ParsedResponseHeaders):any {
return {item, response, status, headers};
}
public onCancelItem(item:FileItem, response:string, status:number, headers:ParsedResponseHeaders):any {
return {item, response, status, headers};
}
public onCompleteItem(item:FileItem, response:string, status:number, headers:ParsedResponseHeaders):any {
return {item, response, status, headers};
}
public onCompleteAll():any {
return void 0; return void 0;
} }
public _mimeTypeFilter(item:FileLikeObject):boolean { public onAfterAddingAll(fileItems: any): any {
return { fileItems };
}
public onBuildItemForm(fileItem: FileItem, form: any): any {
return { fileItem, form };
}
public onAfterAddingFile(fileItem: FileItem): any {
return { fileItem };
}
public onWhenAddingFileFailed(item: FileLikeObject, filter: any, options: any): any {
return { item, filter, options };
}
public onBeforeUploadItem(fileItem: FileItem): any {
return { fileItem };
}
public onProgressItem(fileItem: FileItem, progress: any): any {
return { fileItem, progress };
}
public onProgressAll(progress: any): any {
return { progress };
}
public onSuccessItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): any {
return { item, response, status, headers };
}
public onErrorItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): any {
return { item, response, status, headers };
}
public onCancelItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): any {
return { item, response, status, headers };
}
public onCompleteItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): any {
return { item, response, status, headers };
}
public onCompleteAll(): any {
return void 0;
}
public _mimeTypeFilter(item: FileLikeObject): boolean {
return !(this.options.allowedMimeType && this.options.allowedMimeType.indexOf(item.type) === -1); return !(this.options.allowedMimeType && this.options.allowedMimeType.indexOf(item.type) === -1);
} }
public _fileSizeFilter(item:FileLikeObject):boolean { public _fileSizeFilter(item: FileLikeObject): boolean {
return !(this.options.maxFileSize && item.size > this.options.maxFileSize); return !(this.options.maxFileSize && item.size > this.options.maxFileSize);
} }
public _fileTypeFilter(item:FileLikeObject):boolean { public _fileTypeFilter(item: FileLikeObject): boolean {
return !(this.options.allowedFileType && return !(this.options.allowedFileType &&
this.options.allowedFileType.indexOf(FileType.getMimeClass(item)) === -1); this.options.allowedFileType.indexOf(FileType.getMimeClass(item)) === -1);
} }
public _onErrorItem(item:FileItem, response:string, status:number, headers:ParsedResponseHeaders):void { public _onErrorItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): void {
item._onError(response, status, headers); item._onError(response, status, headers);
this.onErrorItem(item, response, status, headers); this.onErrorItem(item, response, status, headers);
} }
public _onCompleteItem(item:FileItem, response:string, status:number, headers:ParsedResponseHeaders):void { public _onCompleteItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): void {
item._onComplete(response, status, headers); item._onComplete(response, status, headers);
this.onCompleteItem(item, response, status, headers); this.onCompleteItem(item, response, status, headers);
let nextItem = this.getReadyItems()[0]; let nextItem = this.getReadyItems()[ 0 ];
this.isUploading = false; this.isUploading = false;
if (nextItem) { if (nextItem) {
nextItem.upload(); nextItem.upload();
@@ -288,26 +286,21 @@ export class FileUploader {
this._render(); this._render();
} }
protected _headersGetter(parsedHeaders:ParsedResponseHeaders):any { protected _headersGetter(parsedHeaders: ParsedResponseHeaders): any {
return (name:any):any => { return (name: any): any => {
if (name) { if (name) {
return parsedHeaders[name.toLowerCase()] || void 0; return parsedHeaders[ name.toLowerCase() ] || void 0;
} }
return parsedHeaders; return parsedHeaders;
}; };
} }
protected _xhrTransport(item:FileItem):any { protected _xhrTransport(item: FileItem): any {
let that = this; let that = this;
let xhr = item._xhr = new XMLHttpRequest(); let xhr = item._xhr = new XMLHttpRequest();
let sendable:any; let sendable: any;
this._onBeforeUploadItem(item); this._onBeforeUploadItem(item);
// todo
/*item.formData.map(obj => {
obj.map((value, key) => {
form.append(key, value);
});
});*/
if (typeof item._file.size !== 'number') { if (typeof item._file.size !== 'number') {
throw new TypeError('The file specified is no longer valid'); throw new TypeError('The file specified is no longer valid');
} }
@@ -315,11 +308,15 @@ export class FileUploader {
sendable = new FormData(); sendable = new FormData();
this._onBuildItemForm(item, sendable); this._onBuildItemForm(item, sendable);
const appendFile = () => sendable.append(item.alias, item._file, item.file.name);
if (!this.options.parametersBeforeFiles) {
appendFile();
}
// For AWS, Additional Parameters must come BEFORE Files // For AWS, Additional Parameters must come BEFORE Files
if (this.options.additionalParameter !== undefined) { if (this.options.additionalParameter !== undefined) {
Object.keys(this.options.additionalParameter).forEach((key:string) => { Object.keys(this.options.additionalParameter).forEach((key: string) => {
let paramVal = this.options.additionalParameter[key]; let paramVal = this.options.additionalParameter[ key ];
// Allow an additional parameter to include the filename // Allow an additional parameter to include the filename
if (typeof paramVal === 'string' && paramVal.indexOf('{{file_name}}') >= 0) { if (typeof paramVal === 'string' && paramVal.indexOf('{{file_name}}') >= 0) {
paramVal = paramVal.replace('{{file_name}}', item.file.name); paramVal = paramVal.replace('{{file_name}}', item.file.name);
@@ -328,12 +325,14 @@ export class FileUploader {
}); });
} }
sendable.append(item.alias, item._file, item.file.name); if (this.options.parametersBeforeFiles) {
appendFile();
}
} else { } else {
sendable = this.options.formatDataFunction(item); sendable = this.options.formatDataFunction(item);
} }
xhr.upload.onprogress = (event:any) => { xhr.upload.onprogress = (event: any) => {
let progress = Math.round(event.lengthComputable ? event.loaded * 100 / event.total : 0); let progress = Math.round(event.lengthComputable ? event.loaded * 100 / event.total : 0);
this._onProgressItem(item, progress); this._onProgressItem(item, progress);
}; };
@@ -342,7 +341,7 @@ export class FileUploader {
let response = this._transformResponse(xhr.response, headers); let response = this._transformResponse(xhr.response, headers);
let gist = this._isSuccessCode(xhr.status) ? 'Success' : 'Error'; let gist = this._isSuccessCode(xhr.status) ? 'Success' : 'Error';
let method = '_on' + gist + 'Item'; let method = '_on' + gist + 'Item';
(this as any)[method](item, response, xhr.status, headers); (this as any)[ method ](item, response, xhr.status, headers);
this._onCompleteItem(item, response, xhr.status, headers); this._onCompleteItem(item, response, xhr.status, headers);
}; };
xhr.onerror = () => { xhr.onerror = () => {
@@ -372,14 +371,14 @@ export class FileUploader {
if (this.authToken) { if (this.authToken) {
xhr.setRequestHeader(this.authTokenHeader, this.authToken); xhr.setRequestHeader(this.authTokenHeader, this.authToken);
} }
xhr.onreadystatechange = function() { xhr.onreadystatechange = function () {
if (xhr.readyState == XMLHttpRequest.DONE) { if (xhr.readyState == XMLHttpRequest.DONE) {
that.response.emit(xhr.responseText) that.response.emit(xhr.responseText)
} }
} }
if (this.options.formatDataFunctionIsAsync) { if (this.options.formatDataFunctionIsAsync) {
sendable.then( sendable.then(
(result:any) => xhr.send(JSON.stringify(result)) (result: any) => xhr.send(JSON.stringify(result))
); );
} else { } else {
xhr.send(sendable); xhr.send(sendable);
@@ -387,7 +386,7 @@ export class FileUploader {
this._render(); this._render();
} }
protected _getTotalProgress(value:number = 0):number { protected _getTotalProgress(value: number = 0): number {
if (this.options.removeAfterUpload) { if (this.options.removeAfterUpload) {
return value; return value;
} }
@@ -398,7 +397,7 @@ export class FileUploader {
return Math.round(uploaded * ratio + current); return Math.round(uploaded * ratio + current);
} }
protected _getFilters(filters:FilterFunction[]|string):FilterFunction[] { protected _getFilters(filters: FilterFunction[] | string): FilterFunction[] {
if (!filters) { if (!filters) {
return this.options.filters; return this.options.filters;
} }
@@ -408,93 +407,77 @@ export class FileUploader {
if (typeof filters === 'string') { if (typeof filters === 'string') {
let names = filters.match(/[^\s,]+/g); let names = filters.match(/[^\s,]+/g);
return this.options.filters return this.options.filters
.filter((filter:any) => names.indexOf(filter.name) !== -1); .filter((filter: any) => names.indexOf(filter.name) !== -1);
} }
return this.options.filters; return this.options.filters;
} }
protected _render():any { protected _render(): any {
return void 0; return void 0;
// todo: ?
} }
// protected _folderFilter(item:FileItem):boolean { protected _queueLimitFilter(): boolean {
// return !!(item.size || item.type);
// }
protected _queueLimitFilter():boolean {
return this.options.queueLimit === undefined || this.queue.length < this.options.queueLimit; return this.options.queueLimit === undefined || this.queue.length < this.options.queueLimit;
} }
protected _isValidFile(file:FileLikeObject, filters:FilterFunction[], options:FileUploaderOptions):boolean { protected _isValidFile(file: FileLikeObject, filters: FilterFunction[], options: FileUploaderOptions): boolean {
this._failFilterIndex = -1; this._failFilterIndex = -1;
return !filters.length ? true : filters.every((filter:FilterFunction) => { return !filters.length ? true : filters.every((filter: FilterFunction) => {
this._failFilterIndex++; this._failFilterIndex++;
return filter.fn.call(this, file, options); return filter.fn.call(this, file, options);
}); });
} }
protected _isSuccessCode(status:number):boolean { protected _isSuccessCode(status: number): boolean {
return (status >= 200 && status < 300) || status === 304; return (status >= 200 && status < 300) || status === 304;
} }
/* tslint:disable */ protected _transformResponse(response: string, headers: ParsedResponseHeaders): string {
protected _transformResponse(response:string, headers:ParsedResponseHeaders):string {
// todo: ?
/*var headersGetter = this._headersGetter(headers);
forEach($http.defaults.transformResponse, (transformFn) => {
response = transformFn(response, headersGetter);
});*/
return response; return response;
} }
/* tslint:enable */ protected _parseHeaders(headers: string): ParsedResponseHeaders {
protected _parseHeaders(headers:string):ParsedResponseHeaders { let parsed: any = {};
let parsed:any = {}; let key: any;
let key:any; let val: any;
let val:any; let i: any;
let i:any;
if (!headers) { if (!headers) {
return parsed; return parsed;
} }
headers.split('\n').map((line:any) => { headers.split('\n').map((line: any) => {
i = line.indexOf(':'); i = line.indexOf(':');
key = line.slice(0, i).trim().toLowerCase(); key = line.slice(0, i).trim().toLowerCase();
val = line.slice(i + 1).trim(); val = line.slice(i + 1).trim();
if (key) { if (key) {
parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val; parsed[ key ] = parsed[ key ] ? parsed[ key ] + ', ' + val : val;
} }
}); });
return parsed; return parsed;
} }
/*protected _iframeTransport(item:FileItem) { protected _onWhenAddingFileFailed(item: FileLikeObject, filter: any, options: any): void {
// todo: implement it later
}*/
protected _onWhenAddingFileFailed(item:FileLikeObject, filter:any, options:any):void {
this.onWhenAddingFileFailed(item, filter, options); this.onWhenAddingFileFailed(item, filter, options);
} }
protected _onAfterAddingFile(item:FileItem):void { protected _onAfterAddingFile(item: FileItem): void {
this.onAfterAddingFile(item); this.onAfterAddingFile(item);
} }
protected _onAfterAddingAll(items:any):void { protected _onAfterAddingAll(items: any): void {
this.onAfterAddingAll(items); this.onAfterAddingAll(items);
} }
protected _onBeforeUploadItem(item:FileItem):void { protected _onBeforeUploadItem(item: FileItem): void {
item._onBeforeUpload(); item._onBeforeUpload();
this.onBeforeUploadItem(item); this.onBeforeUploadItem(item);
} }
protected _onBuildItemForm(item:FileItem, form:any):void { protected _onBuildItemForm(item: FileItem, form: any): void {
item._onBuildForm(form); item._onBuildForm(form);
this.onBuildItemForm(item, form); this.onBuildItemForm(item, form);
} }
protected _onProgressItem(item:FileItem, progress:any):void { protected _onProgressItem(item: FileItem, progress: any): void {
let total = this._getTotalProgress(progress); let total = this._getTotalProgress(progress);
this.progress = total; this.progress = total;
item._onProgress(progress); item._onProgress(progress);
@@ -503,14 +486,12 @@ export class FileUploader {
this._render(); this._render();
} }
/* tslint:disable */ protected _onSuccessItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): void {
protected _onSuccessItem(item:FileItem, response:string, status:number, headers:ParsedResponseHeaders):void {
item._onSuccess(response, status, headers); item._onSuccess(response, status, headers);
this.onSuccessItem(item, response, status, headers); this.onSuccessItem(item, response, status, headers);
} }
/* tslint:enable */ protected _onCancelItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): void {
protected _onCancelItem(item:FileItem, response:string, status:number, headers:ParsedResponseHeaders):void {
item._onCancel(response, status, headers); item._onCancel(response, status, headers);
this.onCancelItem(item, response, status, headers); this.onCancelItem(item, response, status, headers);
} }