Skip to content

Commit

Permalink
change invalid handling
Browse files Browse the repository at this point in the history
  • Loading branch information
AckerApple committed Sep 6, 2017
1 parent 6d4c427 commit 863a212
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 54 deletions.
2 changes: 1 addition & 1 deletion demo/www/index.js

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions dist/file-upload/FileUploader.class.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ export declare class FileUploader {
autoUpload: any;
authTokenHeader: string;
options: FileUploaderOptions;
protected _failFilterIndex: number;
constructor(options?: FileUploaderOptions);
setOptions(options: FileUploaderOptions): void;
isFileValid(file: File): boolean;
isFilesValid(files: File[]): boolean;
getValidFiles(files: File[]): File[];
getInvalidFiles(files: File[]): {
file: File;
type: string;
}[];
addToQueue(files: File[], options?: FileUploaderOptions, filters?: FilterFunction[] | string): void;
removeFromQueue(value: FileItem): void;
clearQueue(): void;
Expand Down Expand Up @@ -85,8 +89,9 @@ export declare class FileUploader {
protected _getFilters(filters?: FilterFunction[] | string): FilterFunction[];
protected _render(): any;
protected _queueLimitFilter(): boolean;
protected _isValidFile(file: FileLikeObject, filters: FilterFunction[], options: FileUploaderOptions): boolean;
protected _isSuccessCode(status: number): boolean;
getFileFilterFailName(file: File | FileLikeObject): string;
_isValidFile(file: FileLikeObject, filters: FilterFunction[], options: FileUploaderOptions): boolean;
_isSuccessCode(status: number): boolean;
protected _transformResponse(response: string, headers: ParsedResponseHeaders): string;
protected _parseHeaders(headers: string): ParsedResponseHeaders;
protected _onWhenAddingFileFailed(item: FileLikeObject, filter: any, options: any): void;
Expand Down
39 changes: 30 additions & 9 deletions dist/file-upload/FileUploader.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@ var FileUploader = /** @class */ (function () {
}
return true;
};
FileUploader.prototype.getValidFiles = function (files) {
var rtn = [];
for (var x = files.length - 1; x >= 0; --x) {
if (this.isFileValid(files[x])) {
rtn.push(files[x]);
}
}
return rtn;
};
FileUploader.prototype.getInvalidFiles = function (files) {
var rtn = [];
for (var x = files.length - 1; x >= 0; --x) {
var failReason = this.getFileFilterFailName(files[x]);
if (failReason) {
rtn.push({ file: files[x], type: failReason });
}
}
return rtn;
};
FileUploader.prototype.addToQueue = function (files, options, filters) {
var _this = this;
var list = [];
Expand All @@ -64,7 +83,7 @@ var FileUploader = /** @class */ (function () {
var arrayOfFilters = this._getFilters(filters);
var count = this.queue.length;
var addedFileItems = [];
list.map(function (some) {
list.map(function (some, index) {
if (!options) {
options = _this.options;
}
Expand All @@ -77,7 +96,7 @@ var FileUploader = /** @class */ (function () {
_this._onAfterAddingFile(fileItem);
}
else {
var filter = arrayOfFilters[_this._failFilterIndex];
var filter = arrayOfFilters[index];
_this._onWhenAddingFileFailed(temp, filter, options);
}
});
Expand Down Expand Up @@ -349,16 +368,18 @@ var FileUploader = /** @class */ (function () {
FileUploader.prototype._queueLimitFilter = function () {
return this.options.queueLimit === undefined || this.queue.length < this.options.queueLimit;
};
FileUploader.prototype.getFileFilterFailName = function (file) {
for (var x = this.options.filters.length - 1; x >= 0; --x) {
if (!this.options.filters[x].fn.call(this, file, this.options)) {
return this.options.filters[x].name;
}
}
return;
};
FileUploader.prototype._isValidFile = function (file, filters, options) {
this._failFilterIndex = -1;
if (!filters.length)
return true;
for (var x = filters.length - 1; x >= 0; --x) {
if (!filters[x].fn.call(this, file, options)) {
return false;
}
}
return true;
return this.getFileFilterFailName(file) ? false : true;
};
FileUploader.prototype._isSuccessCode = function (status) {
return (status >= 200 && status < 300) || status === 304;
Expand Down
2 changes: 1 addition & 1 deletion dist/file-upload/FileUploader.class.metadata.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"__symbolic":"module","version":3,"metadata":{"Headers":{"__symbolic":"interface"},"FileUploaderOptions":{"__symbolic":"interface"},"FileUploader":{"__symbolic":"class","members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","name":"any"}]}],"setOptions":[{"__symbolic":"method"}],"isFileValid":[{"__symbolic":"method"}],"isFilesValid":[{"__symbolic":"method"}],"addToQueue":[{"__symbolic":"method"}],"removeFromQueue":[{"__symbolic":"method"}],"clearQueue":[{"__symbolic":"method"}],"uploadItem":[{"__symbolic":"method"}],"cancelItem":[{"__symbolic":"method"}],"uploadAll":[{"__symbolic":"method"}],"cancelAll":[{"__symbolic":"method"}],"isFile":[{"__symbolic":"method"}],"isFileLikeObject":[{"__symbolic":"method"}],"getIndexOfItem":[{"__symbolic":"method"}],"getNotUploadedItems":[{"__symbolic":"method"}],"getReadyItems":[{"__symbolic":"method"}],"destroy":[{"__symbolic":"method"}],"onAfterAddingAll":[{"__symbolic":"method"}],"onBuildItemForm":[{"__symbolic":"method"}],"onAfterAddingFile":[{"__symbolic":"method"}],"onWhenAddingFileFailed":[{"__symbolic":"method"}],"onBeforeUploadItem":[{"__symbolic":"method"}],"onProgressItem":[{"__symbolic":"method"}],"onProgressAll":[{"__symbolic":"method"}],"onSuccessItem":[{"__symbolic":"method"}],"onErrorItem":[{"__symbolic":"method"}],"onCancelItem":[{"__symbolic":"method"}],"onCompleteItem":[{"__symbolic":"method"}],"onCompleteAll":[{"__symbolic":"method"}],"_acceptFilter":[{"__symbolic":"method"}],"_mimeTypeFilter":[{"__symbolic":"method"}],"_fileSizeFilter":[{"__symbolic":"method"}],"_fileTypeFilter":[{"__symbolic":"method"}],"_onErrorItem":[{"__symbolic":"method"}],"_onCompleteItem":[{"__symbolic":"method"}],"_headersGetter":[{"__symbolic":"method"}],"getFormData":[{"__symbolic":"method"}],"_xhrTransport":[{"__symbolic":"method"}],"_getTotalProgress":[{"__symbolic":"method"}],"_getFilters":[{"__symbolic":"method"}],"_render":[{"__symbolic":"method"}],"_queueLimitFilter":[{"__symbolic":"method"}],"_isValidFile":[{"__symbolic":"method"}],"_isSuccessCode":[{"__symbolic":"method"}],"_transformResponse":[{"__symbolic":"method"}],"_parseHeaders":[{"__symbolic":"method"}],"_onWhenAddingFileFailed":[{"__symbolic":"method"}],"_onAfterAddingFile":[{"__symbolic":"method"}],"_onAfterAddingAll":[{"__symbolic":"method"}],"_onBeforeUploadItem":[{"__symbolic":"method"}],"_onBuildItemForm":[{"__symbolic":"method"}],"_onProgressItem":[{"__symbolic":"method"}],"_onSuccessItem":[{"__symbolic":"method"}],"_onCancelItem":[{"__symbolic":"method"}],"dataUrl":[{"__symbolic":"method"}]}}}},{"__symbolic":"module","version":1,"metadata":{"Headers":{"__symbolic":"interface"},"FileUploaderOptions":{"__symbolic":"interface"},"FileUploader":{"__symbolic":"class","members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","name":"any"}]}],"setOptions":[{"__symbolic":"method"}],"isFileValid":[{"__symbolic":"method"}],"isFilesValid":[{"__symbolic":"method"}],"addToQueue":[{"__symbolic":"method"}],"removeFromQueue":[{"__symbolic":"method"}],"clearQueue":[{"__symbolic":"method"}],"uploadItem":[{"__symbolic":"method"}],"cancelItem":[{"__symbolic":"method"}],"uploadAll":[{"__symbolic":"method"}],"cancelAll":[{"__symbolic":"method"}],"isFile":[{"__symbolic":"method"}],"isFileLikeObject":[{"__symbolic":"method"}],"getIndexOfItem":[{"__symbolic":"method"}],"getNotUploadedItems":[{"__symbolic":"method"}],"getReadyItems":[{"__symbolic":"method"}],"destroy":[{"__symbolic":"method"}],"onAfterAddingAll":[{"__symbolic":"method"}],"onBuildItemForm":[{"__symbolic":"method"}],"onAfterAddingFile":[{"__symbolic":"method"}],"onWhenAddingFileFailed":[{"__symbolic":"method"}],"onBeforeUploadItem":[{"__symbolic":"method"}],"onProgressItem":[{"__symbolic":"method"}],"onProgressAll":[{"__symbolic":"method"}],"onSuccessItem":[{"__symbolic":"method"}],"onErrorItem":[{"__symbolic":"method"}],"onCancelItem":[{"__symbolic":"method"}],"onCompleteItem":[{"__symbolic":"method"}],"onCompleteAll":[{"__symbolic":"method"}],"_acceptFilter":[{"__symbolic":"method"}],"_mimeTypeFilter":[{"__symbolic":"method"}],"_fileSizeFilter":[{"__symbolic":"method"}],"_fileTypeFilter":[{"__symbolic":"method"}],"_onErrorItem":[{"__symbolic":"method"}],"_onCompleteItem":[{"__symbolic":"method"}],"_headersGetter":[{"__symbolic":"method"}],"getFormData":[{"__symbolic":"method"}],"_xhrTransport":[{"__symbolic":"method"}],"_getTotalProgress":[{"__symbolic":"method"}],"_getFilters":[{"__symbolic":"method"}],"_render":[{"__symbolic":"method"}],"_queueLimitFilter":[{"__symbolic":"method"}],"_isValidFile":[{"__symbolic":"method"}],"_isSuccessCode":[{"__symbolic":"method"}],"_transformResponse":[{"__symbolic":"method"}],"_parseHeaders":[{"__symbolic":"method"}],"_onWhenAddingFileFailed":[{"__symbolic":"method"}],"_onAfterAddingFile":[{"__symbolic":"method"}],"_onAfterAddingAll":[{"__symbolic":"method"}],"_onBeforeUploadItem":[{"__symbolic":"method"}],"_onBuildItemForm":[{"__symbolic":"method"}],"_onProgressItem":[{"__symbolic":"method"}],"_onSuccessItem":[{"__symbolic":"method"}],"_onCancelItem":[{"__symbolic":"method"}],"dataUrl":[{"__symbolic":"method"}]}}}}]
[{"__symbolic":"module","version":3,"metadata":{"Headers":{"__symbolic":"interface"},"FileUploaderOptions":{"__symbolic":"interface"},"FileUploader":{"__symbolic":"class","members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","name":"any"}]}],"setOptions":[{"__symbolic":"method"}],"isFileValid":[{"__symbolic":"method"}],"isFilesValid":[{"__symbolic":"method"}],"getValidFiles":[{"__symbolic":"method"}],"getInvalidFiles":[{"__symbolic":"method"}],"addToQueue":[{"__symbolic":"method"}],"removeFromQueue":[{"__symbolic":"method"}],"clearQueue":[{"__symbolic":"method"}],"uploadItem":[{"__symbolic":"method"}],"cancelItem":[{"__symbolic":"method"}],"uploadAll":[{"__symbolic":"method"}],"cancelAll":[{"__symbolic":"method"}],"isFile":[{"__symbolic":"method"}],"isFileLikeObject":[{"__symbolic":"method"}],"getIndexOfItem":[{"__symbolic":"method"}],"getNotUploadedItems":[{"__symbolic":"method"}],"getReadyItems":[{"__symbolic":"method"}],"destroy":[{"__symbolic":"method"}],"onAfterAddingAll":[{"__symbolic":"method"}],"onBuildItemForm":[{"__symbolic":"method"}],"onAfterAddingFile":[{"__symbolic":"method"}],"onWhenAddingFileFailed":[{"__symbolic":"method"}],"onBeforeUploadItem":[{"__symbolic":"method"}],"onProgressItem":[{"__symbolic":"method"}],"onProgressAll":[{"__symbolic":"method"}],"onSuccessItem":[{"__symbolic":"method"}],"onErrorItem":[{"__symbolic":"method"}],"onCancelItem":[{"__symbolic":"method"}],"onCompleteItem":[{"__symbolic":"method"}],"onCompleteAll":[{"__symbolic":"method"}],"_acceptFilter":[{"__symbolic":"method"}],"_mimeTypeFilter":[{"__symbolic":"method"}],"_fileSizeFilter":[{"__symbolic":"method"}],"_fileTypeFilter":[{"__symbolic":"method"}],"_onErrorItem":[{"__symbolic":"method"}],"_onCompleteItem":[{"__symbolic":"method"}],"_headersGetter":[{"__symbolic":"method"}],"getFormData":[{"__symbolic":"method"}],"_xhrTransport":[{"__symbolic":"method"}],"_getTotalProgress":[{"__symbolic":"method"}],"_getFilters":[{"__symbolic":"method"}],"_render":[{"__symbolic":"method"}],"_queueLimitFilter":[{"__symbolic":"method"}],"getFileFilterFailName":[{"__symbolic":"method"}],"_isValidFile":[{"__symbolic":"method"}],"_isSuccessCode":[{"__symbolic":"method"}],"_transformResponse":[{"__symbolic":"method"}],"_parseHeaders":[{"__symbolic":"method"}],"_onWhenAddingFileFailed":[{"__symbolic":"method"}],"_onAfterAddingFile":[{"__symbolic":"method"}],"_onAfterAddingAll":[{"__symbolic":"method"}],"_onBeforeUploadItem":[{"__symbolic":"method"}],"_onBuildItemForm":[{"__symbolic":"method"}],"_onProgressItem":[{"__symbolic":"method"}],"_onSuccessItem":[{"__symbolic":"method"}],"_onCancelItem":[{"__symbolic":"method"}],"dataUrl":[{"__symbolic":"method"}]}}}},{"__symbolic":"module","version":1,"metadata":{"Headers":{"__symbolic":"interface"},"FileUploaderOptions":{"__symbolic":"interface"},"FileUploader":{"__symbolic":"class","members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","name":"any"}]}],"setOptions":[{"__symbolic":"method"}],"isFileValid":[{"__symbolic":"method"}],"isFilesValid":[{"__symbolic":"method"}],"getValidFiles":[{"__symbolic":"method"}],"getInvalidFiles":[{"__symbolic":"method"}],"addToQueue":[{"__symbolic":"method"}],"removeFromQueue":[{"__symbolic":"method"}],"clearQueue":[{"__symbolic":"method"}],"uploadItem":[{"__symbolic":"method"}],"cancelItem":[{"__symbolic":"method"}],"uploadAll":[{"__symbolic":"method"}],"cancelAll":[{"__symbolic":"method"}],"isFile":[{"__symbolic":"method"}],"isFileLikeObject":[{"__symbolic":"method"}],"getIndexOfItem":[{"__symbolic":"method"}],"getNotUploadedItems":[{"__symbolic":"method"}],"getReadyItems":[{"__symbolic":"method"}],"destroy":[{"__symbolic":"method"}],"onAfterAddingAll":[{"__symbolic":"method"}],"onBuildItemForm":[{"__symbolic":"method"}],"onAfterAddingFile":[{"__symbolic":"method"}],"onWhenAddingFileFailed":[{"__symbolic":"method"}],"onBeforeUploadItem":[{"__symbolic":"method"}],"onProgressItem":[{"__symbolic":"method"}],"onProgressAll":[{"__symbolic":"method"}],"onSuccessItem":[{"__symbolic":"method"}],"onErrorItem":[{"__symbolic":"method"}],"onCancelItem":[{"__symbolic":"method"}],"onCompleteItem":[{"__symbolic":"method"}],"onCompleteAll":[{"__symbolic":"method"}],"_acceptFilter":[{"__symbolic":"method"}],"_mimeTypeFilter":[{"__symbolic":"method"}],"_fileSizeFilter":[{"__symbolic":"method"}],"_fileTypeFilter":[{"__symbolic":"method"}],"_onErrorItem":[{"__symbolic":"method"}],"_onCompleteItem":[{"__symbolic":"method"}],"_headersGetter":[{"__symbolic":"method"}],"getFormData":[{"__symbolic":"method"}],"_xhrTransport":[{"__symbolic":"method"}],"_getTotalProgress":[{"__symbolic":"method"}],"_getFilters":[{"__symbolic":"method"}],"_render":[{"__symbolic":"method"}],"_queueLimitFilter":[{"__symbolic":"method"}],"getFileFilterFailName":[{"__symbolic":"method"}],"_isValidFile":[{"__symbolic":"method"}],"_isSuccessCode":[{"__symbolic":"method"}],"_transformResponse":[{"__symbolic":"method"}],"_parseHeaders":[{"__symbolic":"method"}],"_onWhenAddingFileFailed":[{"__symbolic":"method"}],"_onAfterAddingFile":[{"__symbolic":"method"}],"_onAfterAddingAll":[{"__symbolic":"method"}],"_onBeforeUploadItem":[{"__symbolic":"method"}],"_onBuildItemForm":[{"__symbolic":"method"}],"_onProgressItem":[{"__symbolic":"method"}],"_onSuccessItem":[{"__symbolic":"method"}],"_onCancelItem":[{"__symbolic":"method"}],"dataUrl":[{"__symbolic":"method"}]}}}}]
12 changes: 9 additions & 3 deletions dist/file-upload/ngf.directive.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ export declare class ngf {
forceFilename: string;
fileDropDisabled: boolean;
selectable: boolean;
ref: any;
ref: ngf;
refChange: EventEmitter<ngf>;
uploader: FileUploader;
lastTryInvalid: boolean;
lastTryInvalidChange: EventEmitter<boolean>;
lastInvalids: {
file: File;
type: string;
}[];
lastInvalidsChange: EventEmitter<{
file: File;
type: string;
}[]>;
fileUrl: string;
fileUrlChange: EventEmitter<string>;
file: File;
Expand Down
30 changes: 18 additions & 12 deletions dist/file-upload/ngf.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ var ngf = /** @class */ (function () {
this.selectable = false;
this.refChange = new core_1.EventEmitter();
this.uploader = new FileUploader_class_1.FileUploader({});
this.lastTryInvalid = false;
this.lastTryInvalidChange = new core_1.EventEmitter();
this.lastInvalids = [];
this.lastInvalidsChange = new core_1.EventEmitter();
this.fileUrlChange = new core_1.EventEmitter();
this.fileChange = new core_1.EventEmitter();
this.filesChange = new core_1.EventEmitter();
Expand Down Expand Up @@ -63,15 +63,21 @@ var ngf = /** @class */ (function () {
};
ngf.prototype.handleFiles = function (files) {
var _this = this;
this.lastTryInvalid = files.length && !this.uploader.isFilesValid(files);
this.lastTryInvalidChange.emit(this.lastTryInvalid);
if (!this.lastTryInvalid) {
this.uploader.addToQueue(files);
this.filesChange.emit(this.files = files);
if (files.length) {
this.fileChange.emit(this.file = files[0]);
var valids = this.uploader.getValidFiles(files);
if (files.length != valids.length) {
this.lastInvalids = this.uploader.getInvalidFiles(files);
this.lastInvalidsChange.emit(this.lastInvalids);
}
else {
this.lastInvalids = null;
}
if (valids.length) {
this.uploader.addToQueue(valids);
this.filesChange.emit(this.files = valids);
if (valids.length) {
this.fileChange.emit(this.file = valids[0]);
if (this.fileUrlChange.observers.length) {
this.uploader.dataUrl(files[0])
this.uploader.dataUrl(valids[0])
.then(function (url) { return _this.fileUrlChange.emit(url); });
}
}
Expand Down Expand Up @@ -146,8 +152,8 @@ var ngf = /** @class */ (function () {
'ref': [{ type: core_1.Input, args: ['ngf',] },],
'refChange': [{ type: core_1.Output, args: ['ngfChange',] },],
'uploader': [{ type: core_1.Input },],
'lastTryInvalid': [{ type: core_1.Input },],
'lastTryInvalidChange': [{ type: core_1.Output },],
'lastInvalids': [{ type: core_1.Input },],
'lastInvalidsChange': [{ type: core_1.Output },],
'fileUrl': [{ type: core_1.Input },],
'fileUrlChange': [{ type: core_1.Output },],
'file': [{ type: core_1.Input },],
Expand Down
Loading

0 comments on commit 863a212

Please sign in to comment.