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)
### Events
- `onFileSelected` - fires when files are selected and added to the uploader queue
## API for `ng2FileDrop`
### 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)
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.
7. `parametersBeforeFiles` - States if additional parameters should be appended before or after the file. Defaults to false.
### Events

View File

@@ -28,6 +28,10 @@ import { FileSelectDirective, FileDropDirective, FileUploader } from 'ng2-file-u
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.
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

View File

@@ -26,7 +26,6 @@ export class FileLikeObject {
}
public _createFromObject(object: { size: number, type: string, name: string }): void {
// this.lastModifiedDate = copy(object.lastModifiedDate);
this.size = object.size;
this.type = object.type;
this.name = object.name;

View File

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

View File

@@ -6,7 +6,7 @@ import { FileType } from './file-type.class';
function isFile(value: any): boolean {
return (File && value instanceof File);
}
// function isFileLikeObject(value:any) {
export interface Headers {
name: string;
value: string;
@@ -14,15 +14,18 @@ export interface Headers {
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 {
allowedMimeType?:Array<string>;
allowedFileType?:Array<string>;
allowedMimeType?: string[];
allowedFileType?: string[];
autoUpload?: boolean;
isHTML5?: boolean;
filters?:Array<FilterFunction>;
headers?:Array<Headers>;
filters?: FilterFunction[];
headers?: Headers[];
method?: string;
authToken?: string;
maxFileSize?: number;
@@ -33,6 +36,7 @@ export interface FileUploaderOptions {
itemAlias?: string;
authTokenHeader?: string;
additionalParameter?: { [ key: string ]: any };
parametersBeforeFiles?: boolean;
formatDataFunction?: Function;
formatDataFunctionIsAsync?: boolean;
}
@@ -41,7 +45,7 @@ export class FileUploader {
public authToken: string;
public isUploading: boolean = false;
public queue:Array<FileItem> = [];
public queue: FileItem[] = [];
public progress: number = 0;
public _nextIndex: number = 0;
public autoUpload: any;
@@ -54,7 +58,7 @@ export class FileUploader {
filters: [],
removeAfterUpload: false,
disableMultipart: false,
formatDataFunction: function (item:FileItem) { return item._file; },
formatDataFunction: (item: FileItem) => item._file,
formatDataFunctionIsAsync: false
};
@@ -88,7 +92,6 @@ export class FileUploader {
for (let i = 0; i < this.queue.length; i++) {
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 {
@@ -189,11 +192,11 @@ export class FileUploader {
return typeof value === 'number' ? value : this.queue.indexOf(value);
}
public getNotUploadedItems():Array<any> {
public getNotUploadedItems(): any[] {
return this.queue.filter((item: FileItem) => !item.isUploaded);
}
public getReadyItems():Array<any> {
public getReadyItems(): any[] {
return this.queue
.filter((item: FileItem) => (item.isReady && !item.isUploading))
.sort((item1: any, item2: any) => item1.index - item2.index);
@@ -201,11 +204,6 @@ export class FileUploader {
public destroy(): void {
return void 0;
/*forEach(this._directives, (key) => {
forEach(this._directives[key], (object) => {
object.destroy();
});
});*/
}
public onAfterAddingAll(fileItems: any): any {
@@ -302,12 +300,7 @@ export class FileUploader {
let xhr = item._xhr = new XMLHttpRequest();
let sendable: any;
this._onBeforeUploadItem(item);
// todo
/*item.formData.map(obj => {
obj.map((value, key) => {
form.append(key, value);
});
});*/
if (typeof item._file.size !== 'number') {
throw new TypeError('The file specified is no longer valid');
}
@@ -315,6 +308,10 @@ export class FileUploader {
sendable = new FormData();
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
if (this.options.additionalParameter !== undefined) {
@@ -328,7 +325,9 @@ export class FileUploader {
});
}
sendable.append(item.alias, item._file, item.file.name);
if (this.options.parametersBeforeFiles) {
appendFile();
}
} else {
sendable = this.options.formatDataFunction(item);
}
@@ -415,13 +414,8 @@ export class FileUploader {
protected _render(): any {
return void 0;
// todo: ?
}
// protected _folderFilter(item:FileItem):boolean {
// return !!(item.size || item.type);
// }
protected _queueLimitFilter(): boolean {
return this.options.queueLimit === undefined || this.queue.length < this.options.queueLimit;
}
@@ -438,17 +432,10 @@ export class FileUploader {
return (status >= 200 && status < 300) || status === 304;
}
/* tslint:disable */
protected _transformResponse(response: string, headers: ParsedResponseHeaders): string {
// todo: ?
/*var headersGetter = this._headersGetter(headers);
forEach($http.defaults.transformResponse, (transformFn) => {
response = transformFn(response, headersGetter);
});*/
return response;
}
/* tslint:enable */
protected _parseHeaders(headers: string): ParsedResponseHeaders {
let parsed: any = {};
let key: any;
@@ -468,10 +455,6 @@ export class FileUploader {
return parsed;
}
/*protected _iframeTransport(item:FileItem) {
// todo: implement it later
}*/
protected _onWhenAddingFileFailed(item: FileLikeObject, filter: any, options: any): void {
this.onWhenAddingFileFailed(item, filter, options);
}
@@ -503,13 +486,11 @@ export class FileUploader {
this._render();
}
/* tslint:disable */
protected _onSuccessItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): void {
item._onSuccess(response, status, headers);
this.onSuccessItem(item, response, status, headers);
}
/* tslint:enable */
protected _onCancelItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): void {
item._onCancel(response, status, headers);
this.onCancelItem(item, response, status, headers);