Compare commits

..

2 Commits

Author SHA1 Message Date
Gabrijel Škoro
44d804319a Add promise support on filters 2017-02-28 09:44:19 +01:00
Dmitriy Shekhovtsov
cf12851e1d chore(readme): badges updates 2017-01-18 15:19:20 +02:00
2 changed files with 26 additions and 16 deletions

View File

@@ -1,15 +1,9 @@
# ng2-file-upload [![npm version](https://badge.fury.io/js/ng2-file-upload.svg)](http://badge.fury.io/js/ng2-file-upload)
# ng2-file-upload [![npm version](https://badge.fury.io/js/ng2-file-upload.svg)](http://badge.fury.io/js/ng2-file-upload) [![npm downloads](https://img.shields.io/npm/dm/ng2-file-upload.svg)](https://npmjs.org/ng2-file-upload)[![slack](https://ngx-slack.herokuapp.com/badge.svg)](https://ngx-slack.herokuapp.com)
Easy to use Angular2 directives for files upload ([demo](http://valor-software.github.io/ng2-file-upload/))
Follow me [![twitter](https://img.shields.io/twitter/follow/valorkin.svg?style=social&label=%20valorkin)](https://twitter.com/valorkin) to be notified about new releases.
[![Angular 2 Style Guide](https://mgechev.github.io/angular2-style-guide/images/badge.svg)](https://github.com/mgechev/angular2-style-guide)
[![Build Status](https://travis-ci.org/valor-software/ng2-file-upload.svg?branch=master)](https://travis-ci.org/valor-software/ng2-file-upload)
[![Code Climate](https://codeclimate.com/github/valor-software/ng2-file-upload/badges/gpa.svg)](https://codeclimate.com/github/valor-software/ng2-file-upload)
[![Join the chat at https://gitter.im/valor-software/ng2-bootstrap](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/valor-software/ng2-bootstrap?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![Build Status](https://travis-ci.org/valor-software/ng2-file-upload.svg?branch=development)](https://travis-ci.org/valor-software/ng2-file-upload)
[![Dependency Status](https://david-dm.org/valor-software/ng2-file-upload.svg)](https://david-dm.org/valor-software/ng2-file-upload)
[![devDependency Status](https://david-dm.org/valor-software/ng2-file-upload/dev-status.svg)](https://david-dm.org/valor-software/ng2-file-upload#info=devDependencies)
[![Throughput Graph](https://graphs.waffle.io/valor-software/ng2-file-upload/throughput.svg)](https://waffle.io/valor-software/ng2-file-upload/metrics)
## Quick start

View File

@@ -13,7 +13,7 @@ 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|Promise<boolean>};
export interface FileUploaderOptions {
allowedMimeType?:Array<string>;
@@ -98,15 +98,15 @@ export class FileUploader {
}
let temp = new FileLikeObject(some);
if (this._isValidFile(temp, arrayOfFilters, options)) {
this._isValidFile(temp, arrayOfFilters, options).then(() => {
let fileItem = new FileItem(this, some, options);
addedFileItems.push(fileItem);
this.queue.push(fileItem);
this._onAfterAddingFile(fileItem);
} else {
}).catch(() => {
let filter = arrayOfFilters[this._failFilterIndex];
this._onWhenAddingFileFailed(temp, filter, options);
}
});
});
if (this.queue.length !== count) {
this._onAfterAddingAll(addedFileItems);
@@ -400,11 +400,27 @@ export class FileUploader {
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): Promise<boolean> {
if (!filters.length) {
return Promise.resolve(true);
}
this._failFilterIndex = -1;
return !filters.length ? true : filters.every((filter:FilterFunction) => {
this._failFilterIndex++;
return filter.fn.call(this, file, options);
return Promise.all(
filters.map((filter: FilterFunction) => {
const isValid: boolean = filter.fn.call(this, file, options);
return Promise.resolve(isValid);
})
).then((values) => {
const isValid = values.every((value: boolean) => {
this._failFilterIndex++;
return value;
})
return isValid ? Promise.resolve(isValid) : Promise.reject(isValid);
});
}