Skip to content

Commit

Permalink
refactor(2.0.0): bump to 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuri committed Dec 22, 2016
1 parent 30923f0 commit 5871821
Show file tree
Hide file tree
Showing 11 changed files with 410 additions and 256 deletions.
12 changes: 11 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { NgFileDropDirective } from './src/directives/ng-file-drop';
import { NgFileSelectDirective } from './src/directives/ng-file-select';

export * from './src/directives/ng-file-drop';
export * from './src/directives/ng-file-select';
export * from './src/services/ng2-uploader';

export { Ng2UploaderModule } from './src/module/ng2-uploader.module';
export { Ng2UploaderOptions, UploadedFile, UploadRejected } from './src/classes';

export { Ng2UploaderModule } from './src/module/ng2-uploader.module';

export const UPLOAD_DIRECTIVES: any[] = [
NgFileSelectDirective,
NgFileDropDirective
];
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ng2-uploader",
"description": "Angular2 File Uploader",
"version": "1.6.2",
"version": "2.0.0",
"license": "MIT",
"main": "index.js",
"typings": "index.d.ts",
Expand All @@ -25,18 +25,18 @@
"uploader"
],
"devDependencies": {
"@angular/common": "^2.2.3",
"@angular/compiler": "^2.2.3",
"@angular/compiler-cli": "^2.2.3",
"@angular/core": "^2.2.3",
"@angular/platform-browser": "^2.2.3",
"@angular/platform-browser-dynamic": "^2.2.3",
"@angular/platform-server": "^2.2.3",
"@angular/common": "^2.4.1",
"@angular/compiler": "^2.4.1",
"@angular/compiler-cli": "^2.4.1",
"@angular/core": "^2.4.1",
"@angular/platform-browser": "^2.4.1",
"@angular/platform-browser-dynamic": "^2.4.1",
"@angular/platform-server": "^2.4.1",
"@types/core-js": "^0.9.35",
"@types/node": "^6.0.51",
"reflect-metadata": "^0.1.8",
"rxjs": "5.0.0-rc.4",
"typescript": "2.1.4",
"zone.js": "^0.7.2"
"@types/node": "^6.0.52",
"reflect-metadata": "^0.1.9",
"rxjs": "5.0.1",
"typescript": "2.0.10",
"zone.js": "^0.7.4"
}
}
3 changes: 3 additions & 0 deletions src/classes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './ng2-uploader-options.class';
export * from './uploaded-file.class';
export * from './upload-rejected.class';
65 changes: 65 additions & 0 deletions src/classes/ng2-uploader-options.class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
export interface INg2UploaderOptions {
url: string;
cors?: boolean;
withCredentials?: boolean;
multiple?: boolean;
maxUploads?: number;
data?: any;
autoUpload?: boolean;
multipart?: any;
method?: 'POST' | 'GET';
customHeaders?: any;
encodeHeaders?: boolean;
authTokenPrefix?: string;
authToken?: string;
fieldName?: string;
fieldReset?: boolean;
previewUrl?: string;
calculateSpeed?: boolean;
filterExtensions?: boolean;
allowedExtensions?: string[];
}

export class Ng2UploaderOptions implements INg2UploaderOptions {
url: string;
cors?: boolean;
withCredentials?: boolean;
multiple?: boolean;
maxUploads?: number;
data?: any;
autoUpload?: boolean;
multipart?: any;
method?: 'POST' | 'GET';
customHeaders?: any;
encodeHeaders?: boolean;
authTokenPrefix?: string;
authToken?: string;
fieldName?: string;
fieldReset?: boolean;
previewUrl?: string;
calculateSpeed?: boolean;
filterExtensions?: boolean;
allowedExtensions?: string[];

constructor(obj: INg2UploaderOptions) {
this.url = obj.url != null ? obj.url : '';
this.cors = obj.cors != null ? obj.cors : true;
this.withCredentials = obj.withCredentials != null ? obj.withCredentials : this.withCredentials;
this.multiple = obj.multiple != null ? obj.multiple : true;
this.maxUploads = obj.maxUploads != null ? obj.maxUploads : 10;
this.data = obj.data != null ? obj.data : {};
this.autoUpload = obj && obj.autoUpload ? obj.autoUpload : true;
this.multipart = obj.multipart != null ? obj.multipart : false;
this.method = obj.method != null ? obj.method : 'POST';
this.customHeaders = obj.customHeaders != null ? obj.customHeaders : { };
this.encodeHeaders = obj.encodeHeaders != null ? obj.encodeHeaders : false;
this.authTokenPrefix = obj.authTokenPrefix != null ? obj.authTokenPrefix : 'Bearer';
this.authToken = obj.authToken != null ? obj.authToken : null;
this.fieldName = obj.fieldName != null ? obj.fieldName : 'file';
this.fieldReset = obj.fieldReset != null ? obj.fieldReset : null;
this.previewUrl = obj.previewUrl != null ? obj.previewUrl : null;
this.calculateSpeed = obj.calculateSpeed != null ? obj.calculateSpeed : true;
this.filterExtensions = obj.filterExtensions != null ? obj.filterExtensions : false;
this.allowedExtensions = obj && obj.allowedExtensions ? obj.allowedExtensions : [];
}
}
7 changes: 7 additions & 0 deletions src/classes/upload-rejected.class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class UploadRejected {
public static get EXTENSION_NOT_ALLOWED():string { return 'ExtensionNotAllowed'; }
public static get MAX_SIZE_EXCEEDED():string { return 'MaxSizeExceeded'; }

file: any;
reason: string;
}
72 changes: 72 additions & 0 deletions src/classes/uploaded-file.class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
export class UploadedFile {
id: string;
status: number;
statusText: string;
progress: Object;
originalName: string;
size: number;
response: string;
done: boolean;
error: boolean;
abort: boolean;
startTime: number;
endTime: number;
speedAverage: number;
speedAverageHumanized: string;

constructor(id: string, originalName: string, size: number) {
this.id = id;
this.originalName = originalName;
this.size = size;
this.progress = {
loaded: 0,
total: 0,
percent: 0,
speed: 0,
speedHumanized: null
};
this.done = false;
this.error = false;
this.abort = false;
this.startTime = new Date().getTime();
this.endTime = 0;
this.speedAverage = 0;
this.speedAverageHumanized = null;
}

setProgres(progress: Object): void {
this.progress = progress;
}

setError(): void {
this.error = true;
this.done = true;
}

setAbort(): void {
this.abort = true;
this.done = true;
}

onFinished(status: number, statusText: string, response: string): void {
this.endTime = new Date().getTime();
this.speedAverage = this.size / (this.endTime - this.startTime) * 1000;
this.speedAverage = parseInt(<any>this.speedAverage, 10);
this.speedAverageHumanized = this.humanizeBytes(this.speedAverage);
this.status = status;
this.statusText = statusText;
this.response = response;
this.done = true;
}

humanizeBytes(bytes: number): string {
if (bytes === 0) {
return '0 Byte';
}
let k = 1024;
const sizes: string[] = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB'];
let i: number = Math.floor(Math.log(bytes) / Math.log(k));

return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i] + '/s';
}
}
81 changes: 38 additions & 43 deletions src/directives/ng-file-drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,33 @@ import {
EventEmitter,
Input,
Output,
HostListener
HostListener,
Inject,
OnChanges,
OnInit
} from '@angular/core';
import { Ng2Uploader, UploadRejected, UploadedFile } from '../services/ng2-uploader';
import { Ng2UploaderService } from '../services/ng2-uploader';
import { INg2UploaderOptions, Ng2UploaderOptions, UploadedFile, UploadRejected } from '../classes';

@Directive({
selector: '[ngFileDrop]'
})
export class NgFileDropDirective {

export class NgFileDropDirective implements OnChanges, OnInit {
@Input() options: Ng2UploaderOptions;
@Input() events: EventEmitter<any>;
@Output() onUpload: EventEmitter<any> = new EventEmitter();
@Output() onPreviewData: EventEmitter<any> = new EventEmitter();
@Output() onFileOver:EventEmitter<any> = new EventEmitter();
@Output() onUploadRejected: EventEmitter<UploadRejected> = new EventEmitter<UploadRejected>();
@Output() beforeUpload: EventEmitter<UploadedFile> = new EventEmitter<UploadedFile>();

_options:any;

@Input('options')
set options(value: any) {
this._options = value;
this.uploader.setOptions(this.options);
}

get options(): any {
return this._options;
}

files: any[] = [];
uploader: Ng2Uploader;

constructor(public el: ElementRef) {
this.uploader = new Ng2Uploader();
setTimeout(() => {
this.uploader.setOptions(this.options);
});
constructor(
@Inject(ElementRef) public el: ElementRef,
@Inject(Ng2UploaderService) public uploader: Ng2UploaderService) { }

ngOnInit() {
this.uploader._emitter.subscribe((data: any) => {
this.onUpload.emit(data);
if (data.done) {
Expand Down Expand Up @@ -69,6 +59,15 @@ export class NgFileDropDirective {
this.initEvents();
}

ngOnChanges() {
if (!this.options) {
return;
}

this.options = new Ng2UploaderOptions(this.options);
this.uploader.setOptions(this.options);
}

initEvents(): void {
if (typeof this.el.nativeElement.addEventListener === 'undefined') {
return;
Expand All @@ -95,32 +94,28 @@ export class NgFileDropDirective {
}, false);
}

filterFilesByExtension(): void {
this.files = this.files.filter(f => {
if (this.options.allowedExtensions.indexOf(f.type) !== -1) {
return true;
}

let ext: string = f.name.split('.').pop();
if (this.options.allowedExtensions.indexOf(ext) !== -1 ) {
return true;
}

this.onUploadRejected.emit({file: f, reason: UploadRejected.EXTENSION_NOT_ALLOWED});

return false;
});
}

@HostListener('change') onChange(): void {
if (!this.el.nativeElement.files || !this.el.nativeElement.files.length) {
this.files = this.el.nativeElement.files;
if (!this.files) {
console.log('return');
return;
}

this.files = Array.from(this.el.nativeElement.files);

if (this.options.filterExtensions && this.options.allowedExtensions) {
this.filterFilesByExtension();
this.files = this.files.filter(f => {
if (this.options.allowedExtensions.indexOf(f.type) !== -1) {
return true;
}

let ext: string = f.name.split('.').pop();
if (this.options.allowedExtensions.indexOf(ext) !== -1 ) {
return true;
}

this.onUploadRejected.emit({file: f, reason: UploadRejected.EXTENSION_NOT_ALLOWED});

return false;
});
}

if (this.files.length) {
Expand Down
Loading

0 comments on commit 5871821

Please sign in to comment.