fix(zone.js): error in Safari, Added Typings (#221)
* Changed tsconfig.json to match tsconfig schema * Change package.json to include all necessary packages for this package * Added type for EventEmitter * Fix for zone.js error "More tasks executed then were scheduled" in Safari, explicit call of NgZone not required in zone.js ^0.6.12 * Added type information for methods in FileUploader * Added type information for methods in FileItem * Added type information for methods in FileUploader * Removed codelyzer / eslint according to PR valor-software/ng2-file-upload#221
This commit was merged in pull request #221.
This commit is contained in:
committed by
Dmitriy Shekhovtsov
parent
ec1e5ae556
commit
db77e89235
@@ -6,7 +6,7 @@ import { FileUploader } from './file-uploader.class';
|
||||
export class FileDropDirective {
|
||||
@Input() public uploader:FileUploader;
|
||||
@Output() public fileOver:EventEmitter<any> = new EventEmitter();
|
||||
@Output() public onFileDrop:EventEmitter<File[]> = new EventEmitter();
|
||||
@Output() public onFileDrop:EventEmitter<File[]> = new EventEmitter<File[]>();
|
||||
|
||||
private element:ElementRef;
|
||||
public constructor(element:ElementRef) {
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import {NgZone} from '@angular/core';
|
||||
|
||||
import {FileLikeObject} from './file-like-object.class';
|
||||
import {FileUploader} from './file-uploader.class';
|
||||
import {FileUploader, ParsedResponseHeaders, FileUploaderOptions} from './file-uploader.class';
|
||||
|
||||
export class FileItem {
|
||||
public file:FileLikeObject;
|
||||
@@ -20,28 +18,28 @@ export class FileItem {
|
||||
public isError:boolean = false;
|
||||
public progress:number = 0;
|
||||
public index:number = void 0;
|
||||
private _zone:NgZone;
|
||||
public _xhr:XMLHttpRequest;
|
||||
public _form:any;
|
||||
|
||||
private uploader:FileUploader;
|
||||
private some:any;
|
||||
private options:any;
|
||||
private some:File;
|
||||
private options:FileUploaderOptions;
|
||||
|
||||
public constructor(uploader:FileUploader, some:any, options:any) {
|
||||
public constructor(uploader:FileUploader, some:File, options:FileUploaderOptions) {
|
||||
this.uploader = uploader;
|
||||
this.some = some;
|
||||
this.options = options;
|
||||
this.file = new FileLikeObject(some);
|
||||
this._file = some;
|
||||
this.url = uploader.options.url;
|
||||
this._zone = new NgZone({ enableLongStackTrace: false });
|
||||
}
|
||||
|
||||
public upload():void {
|
||||
try {
|
||||
this.uploader.uploadItem(this);
|
||||
} catch (e) {
|
||||
this.uploader._onCompleteItem(this, '', 0, []);
|
||||
this.uploader._onErrorItem(this, '', 0, []);
|
||||
this.uploader._onCompleteItem(this, '', 0, {});
|
||||
this.uploader._onErrorItem(this, '', 0, {});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,19 +63,19 @@ export class FileItem {
|
||||
return {progress};
|
||||
}
|
||||
|
||||
public onSuccess(response:any, status:any, headers:any):any {
|
||||
public onSuccess(response:string, status:number, headers:ParsedResponseHeaders):any {
|
||||
return {response,status,headers};
|
||||
}
|
||||
|
||||
public onError(response:any, status:any, headers:any):any {
|
||||
public onError(response:string, status:number, headers:ParsedResponseHeaders):any {
|
||||
return {response,status,headers};
|
||||
}
|
||||
|
||||
public onCancel(response:any, status:any, headers:any):any {
|
||||
public onCancel(response:string, status:number, headers:ParsedResponseHeaders):any {
|
||||
return {response,status,headers};
|
||||
}
|
||||
|
||||
public onComplete(response:any, status:any, headers:any):any {
|
||||
public onComplete(response:string, status:number, headers:ParsedResponseHeaders):any {
|
||||
return {response,status,headers};
|
||||
}
|
||||
|
||||
@@ -97,13 +95,11 @@ export class FileItem {
|
||||
}
|
||||
|
||||
public _onProgress(progress:number):void {
|
||||
this._zone.run(() => {
|
||||
this.progress = progress;
|
||||
});
|
||||
this.progress = progress;
|
||||
this.onProgress(progress);
|
||||
}
|
||||
|
||||
public _onSuccess(response:any, status:any, headers:any):void {
|
||||
public _onSuccess(response:string, status:number, headers:ParsedResponseHeaders):void {
|
||||
this.isReady = false;
|
||||
this.isUploading = false;
|
||||
this.isUploaded = true;
|
||||
@@ -115,7 +111,7 @@ export class FileItem {
|
||||
this.onSuccess(response, status, headers);
|
||||
}
|
||||
|
||||
public _onError(response:any, status:any, headers:any):void {
|
||||
public _onError(response:string, status:number, headers:ParsedResponseHeaders):void {
|
||||
this.isReady = false;
|
||||
this.isUploading = false;
|
||||
this.isUploaded = true;
|
||||
@@ -127,7 +123,7 @@ export class FileItem {
|
||||
this.onError(response, status, headers);
|
||||
}
|
||||
|
||||
public _onCancel(response:any, status:any, headers:any):void {
|
||||
public _onCancel(response:string, status:number, headers:ParsedResponseHeaders):void {
|
||||
this.isReady = false;
|
||||
this.isUploading = false;
|
||||
this.isUploaded = false;
|
||||
@@ -139,7 +135,7 @@ export class FileItem {
|
||||
this.onCancel(response, status, headers);
|
||||
}
|
||||
|
||||
public _onComplete(response:any, status:any, headers:any):void {
|
||||
public _onComplete(response:string, status:number, headers:ParsedResponseHeaders):void {
|
||||
this.onComplete(response, status, headers);
|
||||
|
||||
if (this.uploader.options.removeAfterUpload) {
|
||||
|
||||
@@ -11,13 +11,18 @@ export interface Headers {
|
||||
value:string;
|
||||
}
|
||||
|
||||
export type ParsedResponseHeaders = {[headerFieldName:string]:string};
|
||||
|
||||
export type FilterFunction = {name:string, fn:(item?:FileLikeObject, options?:FileUploaderOptions)=>boolean};
|
||||
|
||||
export interface FileUploaderOptions {
|
||||
allowedMimeType?:Array<string>;
|
||||
allowedFileType?:Array<string>;
|
||||
autoUpload?:boolean;
|
||||
isHTML5?:boolean;
|
||||
filters?:Array<any>;
|
||||
filters?:Array<FilterFunction>;
|
||||
headers?:Array<Headers>;
|
||||
authToken?:string;
|
||||
maxFileSize?:number;
|
||||
queueLimit?:number;
|
||||
removeAfterUpload?:boolean;
|
||||
@@ -28,7 +33,7 @@ export class FileUploader {
|
||||
|
||||
public authToken:string;
|
||||
public isUploading:boolean = false;
|
||||
public queue:Array<any> = [];
|
||||
public queue:Array<FileItem> = [];
|
||||
public progress:number = 0;
|
||||
public _nextIndex:number = 0;
|
||||
public autoUpload:any;
|
||||
@@ -42,11 +47,11 @@ export class FileUploader {
|
||||
|
||||
private _failFilterIndex:number;
|
||||
|
||||
public constructor(options:any) {
|
||||
public constructor(options:FileUploaderOptions) {
|
||||
this.setOptions(options);
|
||||
}
|
||||
|
||||
public setOptions(options:any):void {
|
||||
public setOptions(options:FileUploaderOptions):void {
|
||||
this.options = Object.assign(this.options, options);
|
||||
|
||||
this.authToken = options.authToken;
|
||||
@@ -68,15 +73,15 @@ export class FileUploader {
|
||||
// this.options.filters.unshift({name: 'folder', fn: this._folderFilter});
|
||||
}
|
||||
|
||||
public addToQueue(files:any[], options?:any, filters?:any):void {
|
||||
let list:any[] = [];
|
||||
public addToQueue(files:File[], options?:FileUploaderOptions, filters?:FilterFunction[]|string):void {
|
||||
let list:File[] = [];
|
||||
for (let file of files) {
|
||||
list.push(file);
|
||||
}
|
||||
let arrayOfFilters = this._getFilters(filters);
|
||||
let count = this.queue.length;
|
||||
let addedFileItems:any[] = [];
|
||||
list.map((some:any) => {
|
||||
let addedFileItems:FileItem[] = [];
|
||||
list.map((some:File) => {
|
||||
if (!options) {
|
||||
options = this.options;
|
||||
}
|
||||
@@ -102,7 +107,7 @@ export class FileUploader {
|
||||
}
|
||||
}
|
||||
|
||||
public removeFromQueue(value:any):void {
|
||||
public removeFromQueue(value:FileItem):void {
|
||||
let index = this.getIndexOfItem(value);
|
||||
let item = this.queue[index];
|
||||
if (item.isUploading) {
|
||||
@@ -131,27 +136,27 @@ export class FileUploader {
|
||||
(this as any)[transport](item);
|
||||
}
|
||||
|
||||
public cancelItem(value:any):void {
|
||||
public cancelItem(value:FileItem):void {
|
||||
let index = this.getIndexOfItem(value);
|
||||
let item = this.queue[index];
|
||||
let prop = this.options.isHTML5 ? '_xhr' : '_form';
|
||||
let prop = this.options.isHTML5 ? item._xhr : item._form;
|
||||
if (item && item.isUploading) {
|
||||
item[prop].abort();
|
||||
prop.abort();
|
||||
}
|
||||
}
|
||||
|
||||
public uploadAll():void {
|
||||
let items = this.getNotUploadedItems().filter((item:any) => !item.isUploading);
|
||||
let items = this.getNotUploadedItems().filter((item:FileItem) => !item.isUploading);
|
||||
if (!items.length) {
|
||||
return;
|
||||
}
|
||||
items.map((item:any) => item._prepareToUploading());
|
||||
items.map((item:FileItem) => item._prepareToUploading());
|
||||
items[0].upload();
|
||||
}
|
||||
|
||||
public cancelAll():void {
|
||||
let items = this.getNotUploadedItems();
|
||||
items.map((item:any) => item.cancel());
|
||||
items.map((item:FileItem) => item.cancel());
|
||||
}
|
||||
|
||||
public isFile(value:any):boolean {
|
||||
@@ -167,12 +172,12 @@ export class FileUploader {
|
||||
}
|
||||
|
||||
public getNotUploadedItems():Array<any> {
|
||||
return this.queue.filter((item:any) => !item.isUploaded);
|
||||
return this.queue.filter((item:FileItem) => !item.isUploaded);
|
||||
}
|
||||
|
||||
public getReadyItems():Array<any> {
|
||||
return this.queue
|
||||
.filter((item:any) => (item.isReady && !item.isUploading))
|
||||
.filter((item:FileItem) => (item.isReady && !item.isUploading))
|
||||
.sort((item1:any, item2:any) => item1.index - item2.index);
|
||||
}
|
||||
|
||||
@@ -189,23 +194,23 @@ export class FileUploader {
|
||||
return {fileItems};
|
||||
}
|
||||
|
||||
public onBuildItemForm(fileItem:any, form:any):any {
|
||||
public onBuildItemForm(fileItem:FileItem, form:any):any {
|
||||
return {fileItem, form};
|
||||
}
|
||||
|
||||
public onAfterAddingFile(fileItem:any):any {
|
||||
public onAfterAddingFile(fileItem:FileItem):any {
|
||||
return {fileItem};
|
||||
}
|
||||
|
||||
public onWhenAddingFileFailed(item:any, filter:any, options:any):any {
|
||||
public onWhenAddingFileFailed(item:FileLikeObject, filter:any, options:any):any {
|
||||
return {item, filter, options};
|
||||
}
|
||||
|
||||
public onBeforeUploadItem(fileItem:any):any {
|
||||
public onBeforeUploadItem(fileItem:FileItem):any {
|
||||
return {fileItem};
|
||||
}
|
||||
|
||||
public onProgressItem(fileItem:any, progress:any):any {
|
||||
public onProgressItem(fileItem:FileItem, progress:any):any {
|
||||
return {fileItem, progress};
|
||||
}
|
||||
|
||||
@@ -213,19 +218,19 @@ export class FileUploader {
|
||||
return {progress};
|
||||
}
|
||||
|
||||
public onSuccessItem(item:any, response:any, status:any, headers:any):any {
|
||||
public onSuccessItem(item:FileItem, response:string, status:number, headers:ParsedResponseHeaders):any {
|
||||
return {item, response, status, headers};
|
||||
}
|
||||
|
||||
public onErrorItem(item:any, response:any, status:any, headers:any):any {
|
||||
public onErrorItem(item:FileItem, response:string, status:number, headers:ParsedResponseHeaders):any {
|
||||
return {item, response, status, headers};
|
||||
}
|
||||
|
||||
public onCancelItem(item:any, response:any, status:any, headers:any):any {
|
||||
public onCancelItem(item:FileItem, response:string, status:number, headers:ParsedResponseHeaders):any {
|
||||
return {item, response, status, headers};
|
||||
}
|
||||
|
||||
public onCompleteItem(item:any, response:any, status:any, headers:any):any {
|
||||
public onCompleteItem(item:FileItem, response:string, status:number, headers:ParsedResponseHeaders):any {
|
||||
return {item, response, status, headers};
|
||||
}
|
||||
|
||||
@@ -233,25 +238,25 @@ export class FileUploader {
|
||||
return void 0;
|
||||
}
|
||||
|
||||
public _mimeTypeFilter(item:any):boolean {
|
||||
public _mimeTypeFilter(item:FileLikeObject):boolean {
|
||||
return !(this.options.allowedMimeType && this.options.allowedMimeType.indexOf(item.type) === -1);
|
||||
}
|
||||
|
||||
public _fileSizeFilter(item:any):boolean {
|
||||
public _fileSizeFilter(item:FileLikeObject):boolean {
|
||||
return !(this.options.maxFileSize && item.size > this.options.maxFileSize);
|
||||
}
|
||||
|
||||
public _fileTypeFilter(item:any):boolean {
|
||||
public _fileTypeFilter(item:FileLikeObject):boolean {
|
||||
return !(this.options.allowedFileType &&
|
||||
this.options.allowedFileType.indexOf(FileType.getMimeClass(item)) === -1);
|
||||
}
|
||||
|
||||
public _onErrorItem(item:any, response:any, status:any, headers:any):void {
|
||||
public _onErrorItem(item:FileItem, response:string, status:number, headers:ParsedResponseHeaders):void {
|
||||
item._onError(response, status, headers);
|
||||
this.onErrorItem(item, response, status, headers);
|
||||
}
|
||||
|
||||
public _onCompleteItem(item:any, response:any, status:any, headers:any):void {
|
||||
public _onCompleteItem(item:FileItem, response:string, status:number, headers:ParsedResponseHeaders):void {
|
||||
item._onComplete(response, status, headers);
|
||||
this.onCompleteItem(item, response, status, headers);
|
||||
let nextItem = this.getReadyItems()[0];
|
||||
@@ -265,8 +270,8 @@ export class FileUploader {
|
||||
this._render();
|
||||
}
|
||||
|
||||
protected _headersGetter(parsedHeaders:any):any {
|
||||
return (name:any) => {
|
||||
protected _headersGetter(parsedHeaders:ParsedResponseHeaders):any {
|
||||
return (name:any):any => {
|
||||
if (name) {
|
||||
return parsedHeaders[name.toLowerCase()] || void 0;
|
||||
}
|
||||
@@ -274,7 +279,7 @@ export class FileUploader {
|
||||
};
|
||||
}
|
||||
|
||||
protected _xhrTransport(item:any):any {
|
||||
protected _xhrTransport(item:FileItem):any {
|
||||
let xhr = item._xhr = new XMLHttpRequest();
|
||||
let form = new FormData();
|
||||
this._onBeforeUploadItem(item);
|
||||
@@ -343,7 +348,7 @@ export class FileUploader {
|
||||
return Math.round(uploaded * ratio + current);
|
||||
}
|
||||
|
||||
private _getFilters(filters:any):any {
|
||||
private _getFilters(filters:FilterFunction[]|string):FilterFunction[] {
|
||||
if (!filters) {
|
||||
return this.options.filters;
|
||||
}
|
||||
@@ -363,7 +368,7 @@ export class FileUploader {
|
||||
// todo: ?
|
||||
}
|
||||
|
||||
// private _folderFilter(item:any):boolean {
|
||||
// private _folderFilter(item:FileItem):boolean {
|
||||
// return !!(item.size || item.type);
|
||||
// }
|
||||
|
||||
@@ -371,20 +376,20 @@ export class FileUploader {
|
||||
return this.options.queueLimit === undefined || this.queue.length < this.options.queueLimit;
|
||||
}
|
||||
|
||||
private _isValidFile(file:any, filters:any, options:any):boolean {
|
||||
private _isValidFile(file:FileLikeObject, filters:FilterFunction[], options:FileUploaderOptions):boolean {
|
||||
this._failFilterIndex = -1;
|
||||
return !filters.length ? true : filters.every((filter:any) => {
|
||||
return !filters.length ? true : filters.every((filter:FilterFunction) => {
|
||||
this._failFilterIndex++;
|
||||
return filter.fn.call(this, file, options);
|
||||
});
|
||||
}
|
||||
|
||||
private _isSuccessCode(status:any):boolean {
|
||||
private _isSuccessCode(status:number):boolean {
|
||||
return (status >= 200 && status < 300) || status === 304;
|
||||
}
|
||||
|
||||
/* tslint:disable */
|
||||
private _transformResponse(response:any, headers:any):any {
|
||||
private _transformResponse(response:string, headers:ParsedResponseHeaders):string {
|
||||
// todo: ?
|
||||
/*var headersGetter = this._headersGetter(headers);
|
||||
forEach($http.defaults.transformResponse, (transformFn) => {
|
||||
@@ -394,7 +399,7 @@ export class FileUploader {
|
||||
}
|
||||
|
||||
/* tslint:enable */
|
||||
private _parseHeaders(headers:any):any {
|
||||
private _parseHeaders(headers:string):ParsedResponseHeaders {
|
||||
let parsed:any = {};
|
||||
let key:any;
|
||||
let val:any;
|
||||
@@ -413,15 +418,15 @@ export class FileUploader {
|
||||
return parsed;
|
||||
}
|
||||
|
||||
/*private _iframeTransport(item:any) {
|
||||
/*private _iframeTransport(item:FileItem) {
|
||||
// todo: implement it later
|
||||
}*/
|
||||
|
||||
private _onWhenAddingFileFailed(item:any, filter:any, options:any):void {
|
||||
private _onWhenAddingFileFailed(item:FileLikeObject, filter:any, options:any):void {
|
||||
this.onWhenAddingFileFailed(item, filter, options);
|
||||
}
|
||||
|
||||
private _onAfterAddingFile(item:any):void {
|
||||
private _onAfterAddingFile(item:FileItem):void {
|
||||
this.onAfterAddingFile(item);
|
||||
}
|
||||
|
||||
@@ -429,17 +434,17 @@ export class FileUploader {
|
||||
this.onAfterAddingAll(items);
|
||||
}
|
||||
|
||||
private _onBeforeUploadItem(item:any):void {
|
||||
private _onBeforeUploadItem(item:FileItem):void {
|
||||
item._onBeforeUpload();
|
||||
this.onBeforeUploadItem(item);
|
||||
}
|
||||
|
||||
private _onBuildItemForm(item:any, form:any):void {
|
||||
private _onBuildItemForm(item:FileItem, form:any):void {
|
||||
item._onBuildForm(form);
|
||||
this.onBuildItemForm(item, form);
|
||||
}
|
||||
|
||||
private _onProgressItem(item:any, progress:any):void {
|
||||
private _onProgressItem(item:FileItem, progress:any):void {
|
||||
let total = this._getTotalProgress(progress);
|
||||
this.progress = total;
|
||||
item._onProgress(progress);
|
||||
@@ -449,13 +454,13 @@ export class FileUploader {
|
||||
}
|
||||
|
||||
/* tslint:disable */
|
||||
private _onSuccessItem(item:any, response:any, status:any, headers:any):void {
|
||||
private _onSuccessItem(item:FileItem, response:string, status:number, headers:ParsedResponseHeaders):void {
|
||||
item._onSuccess(response, status, headers);
|
||||
this.onSuccessItem(item, response, status, headers);
|
||||
}
|
||||
|
||||
/* tslint:enable */
|
||||
private _onCancelItem(item:any, response:any, status:any, headers:any):void {
|
||||
private _onCancelItem(item:FileItem, response:string, status:number, headers:ParsedResponseHeaders):void {
|
||||
item._onCancel(response, status, headers);
|
||||
this.onCancelItem(item, response, status, headers);
|
||||
}
|
||||
|
||||
@@ -65,6 +65,7 @@
|
||||
"conventional-github-releaser": "1.1.2",
|
||||
"copy-webpack-plugin": "3.0.1",
|
||||
"cpy-cli": "1.0.1",
|
||||
"del": "^2.2.0",
|
||||
"del-cli": "0.2.0",
|
||||
"es6-promise": "3.1.2",
|
||||
"es6-shim": "0.35.0",
|
||||
|
||||
@@ -12,9 +12,6 @@
|
||||
"listFiles": false,
|
||||
"noLib": false
|
||||
},
|
||||
"exclude": [
|
||||
"node_modules"
|
||||
],
|
||||
"files": [
|
||||
"./typings/browser.d.ts",
|
||||
"./ng2-file-upload.ts"
|
||||
|
||||
Reference in New Issue
Block a user