Skip to content

Commit

Permalink
chore(demo): update demo
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuri committed Oct 1, 2017
1 parent ec5deaf commit 6bce625
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 197 deletions.
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './src/ngx-uploader/module/ngx-uploader.module';
export * from './src/ngx-uploader/classes/ngx-uploader.class';
export { UploadFile, UploadInput, UploadOutput, UploadProgress, UploadStatus, UploaderOptions } from './src/ngx-uploader/classes/interfaces';
export * from './src/ngx-uploader/directives/ng-file-select.directive';
export * from './src/ngx-uploader/directives/ng-file-drop.directive';
12 changes: 11 additions & 1 deletion src/components/app-home/app-home.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@
</span>
</div>
<div class="browser-container">
<app-progress-chart [percent]="'20'"></app-progress-chart>
<div class="drop-container" ngFileDrop [options]="options" (uploadOutput)="onUploadOutput($event)" [uploadInput]="uploadInput" [ngClass]="{ 'is-drop-over': dragOver, 'is-hidden': uploading }">
<h1>Drag & Drop</h1>
<p>
<span>your files here or</span>
<label class="upload-button">
<input type="file" ngFileSelect [options]="options" (uploadOutput)="onUploadOutput($event)" [uploadInput]="uploadInput" multiple>
browse
</label>
</p>
</div>
<app-progress-chart [percent]="percent" *ngIf="uploading" [class.is-hidden]="!uploading"></app-progress-chart>
</div>
</div>
</div>
Expand Down
36 changes: 20 additions & 16 deletions src/components/app-home/app-home.component.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { Component, EventEmitter } from '@angular/core';
import { UploadOutput, UploadInput, UploadFile, humanizeBytes, UploaderOptions } from '../../../';

interface FormData {
concurrency: number;
autoUpload: boolean;
verbose: boolean;
}
import { UploadOutput, UploadInput, UploadFile, humanizeBytes, UploaderOptions, UploadStatus } from '../../../';

@Component({
selector: 'app-home',
Expand All @@ -19,24 +13,17 @@ export class AppHomeComponent {
dragOver: boolean;
options: UploaderOptions;
percent: number;
uploading: boolean;

constructor() {
this.uploading = false;
this.options = { concurrency: 1 };

this.formData = {
concurrency: 1,
autoUpload: false,
verbose: true
};

this.files = [];
this.uploadInput = new EventEmitter<UploadInput>();
this.humanizeBytes = humanizeBytes;
}

onUploadOutput(output: UploadOutput): void {
console.log(output);

if (output.type === 'allAddedToQueue') {
const event: UploadInput = {
type: 'uploadAll',
Expand All @@ -49,6 +36,7 @@ export class AppHomeComponent {
} else if (output.type === 'addedToQueue' && typeof output.file !== 'undefined') {
this.files.push(output.file);
} else if (output.type === 'uploading' && typeof output.file !== 'undefined') {
this.uploading = true;
const index = this.files.findIndex(file => typeof output.file !== 'undefined' && file.id === output.file.id);
this.files[index] = output.file;
} else if (output.type === 'removed') {
Expand All @@ -60,6 +48,22 @@ export class AppHomeComponent {
} else if (output.type === 'drop') {
this.dragOver = false;
}

let uploadInProgress = false;
let percent = 0;

this.files.forEach(file => {
if (file.progress.status !== UploadStatus.Done) {
uploadInProgress = true;
}

if (file.progress.data) {
percent += file.progress.data.percentage;
}
});

this.uploading = uploadInProgress;
this.percent = percent / this.files.length;
}

startUpload(): void {
Expand Down
63 changes: 63 additions & 0 deletions src/ngx-uploader/classes/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Subscription } from 'rxjs/Subscription';

export interface UploaderOptions {
concurrency: number;
}

export interface BlobFile extends Blob {
name: string;
}

export enum UploadStatus {
Queue,
Uploading,
Done,
Cancelled
}

export interface UploadProgress {
status: UploadStatus;
data?: {
percentage: number;
speed: number;
speedHuman: string;
startTime: number | null;
endTime: number | null;
eta: number | null;
etaHuman: string | null;
};
}

export interface UploadFile {
id: string;
fileIndex: number;
lastModifiedDate: Date;
name: string;
size: number;
type: string;
form: FormData;
progress: UploadProgress;
response?: any;
responseStatus?: number;
sub?: Subscription | any;
nativeFile?: File;
}

export interface UploadOutput {
type: 'addedToQueue' | 'allAddedToQueue' | 'uploading' | 'done' | 'start' | 'cancelled' | 'dragOver' | 'dragOut' | 'drop' | 'removed' | 'removedAll';
file?: UploadFile;
nativeFile?: File;
}

export interface UploadInput {
type: 'uploadAll' | 'uploadFile' | 'cancel' | 'cancelAll' | 'remove' | 'removeAll';
url?: string;
method?: string;
id?: string;
fieldName?: string;
fileIndex?: number;
file?: UploadFile;
data?: { [key: string]: string | Blob };
headers?: { [key: string]: string };
withCredentials?: boolean;
}
63 changes: 1 addition & 62 deletions src/ngx-uploader/classes/ngx-uploader.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,7 @@ import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import { Subscription } from 'rxjs/Subscription';
import 'rxjs/add/operator/mergeMap';

export interface BlobFile extends Blob {
name: string;
}

export enum UploadStatus {
Queue,
Uploading,
Done,
Cancelled
}

export interface UploaderOptions {
concurrency: number;
}

export interface UploadProgress {
status: UploadStatus;
data?: {
percentage: number;
speed: number;
speedHuman: string;
startTime: number | null;
endTime: number | null;
eta: number | null;
etaHuman: string | null;
};
}

export interface UploadFile {
id: string;
fileIndex: number;
lastModifiedDate: Date;
name: string;
size: number;
type: string;
form: FormData;
progress: UploadProgress;
response?: any;
responseStatus?: number;
sub?: Subscription | any;
nativeFile?: File;
}

export interface UploadOutput {
type: 'addedToQueue' | 'allAddedToQueue' | 'uploading' | 'done' | 'start' | 'cancelled' | 'dragOver' | 'dragOut' | 'drop' | 'removed' | 'removedAll';
file?: UploadFile;
nativeFile?: File;
}

export interface UploadInput {
type: 'uploadAll' | 'uploadFile' | 'cancel' | 'cancelAll' | 'remove' | 'removeAll';
url?: string;
method?: string;
id?: string;
fieldName?: string;
fileIndex?: number;
file?: UploadFile;
data?: { [key: string]: string | Blob };
headers?: { [key: string]: string };
withCredentials?: boolean;
}
import { UploaderOptions, UploadFile, UploadOutput, UploadInput, UploadStatus, BlobFile } from './interfaces';

export function humanizeBytes(bytes: number): string {
if (bytes === 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/ngx-uploader/directives/ng-file-drop.directive.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Directive, ElementRef, EventEmitter, Input, Output, OnInit, OnDestroy, HostListener } from '@angular/core';
import { isPlatformServer } from '@angular/common';
import { NgUploaderService, UploadOutput, UploadInput, UploadFile, UploaderOptions } from '../classes/ngx-uploader.class';
import { UploadOutput, UploadInput, UploadFile, UploaderOptions, NgUploaderService } from '../../../';

@Directive({
selector: '[ngFileDrop]'
Expand Down
2 changes: 1 addition & 1 deletion src/ngx-uploader/directives/ng-file-select.directive.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Directive, ElementRef, EventEmitter, Input, Output, OnInit, OnDestroy } from '@angular/core';
import { isPlatformServer } from '@angular/common';
import { NgUploaderService, UploadOutput, UploadInput, UploadFile, UploaderOptions } from '../classes/ngx-uploader.class';
import { UploadOutput, UploadInput, UploadFile, UploaderOptions, NgUploaderService } from '../../../';

@Directive({
selector: '[ngFileSelect]'
Expand Down
Loading

0 comments on commit 6bce625

Please sign in to comment.