Skip to content

Commit

Permalink
feat(layouts): add file uploader (#842)
Browse files Browse the repository at this point in the history
  • Loading branch information
Guutong authored and lexzhukov committed Apr 12, 2017
1 parent 1be434b commit 7d03461
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/app/pages/forms/components/layouts/layouts.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export class Layouts {
url: '',
};

public fileUploaderOptions:NgUploaderOptions = {
// url: 'http://website.com/upload'
url: '',
};

constructor() {
}

Expand Down
5 changes: 5 additions & 0 deletions src/app/pages/forms/components/layouts/layouts.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,10 @@
<ba-picture-uploader [picture]="profile.picture" [defaultPicture]="defaultPicture" [uploaderOptions]="uploaderOptions"></ba-picture-uploader>
</ba-card>
</div>
<div class="col-md-6">
<ba-card title="File Uploader" baCardClass="with-scroll">
<ba-file-uploader [fileUploaderOptions]="fileUploaderOptions"></ba-file-uploader>
</ba-card>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Component, ViewChild, Input, Output, EventEmitter, ElementRef, Renderer } from '@angular/core';
import { NgUploaderOptions } from 'ngx-uploader';
@Component({
selector: 'ba-file-uploader',
styleUrls: ['./baFileUploader.scss'],
templateUrl: './baFileUploader.html',
})
export class BaFileUploader {
@Input() fileUploaderOptions: NgUploaderOptions = { url: '' };
@Output() onFileUpload = new EventEmitter<any>();
@Output() onFileUploadCompleted = new EventEmitter<any>();
@Input() defaultValue: string = '';

@ViewChild('fileUpload') public _fileUpload: ElementRef;
@ViewChild('inputText') public _inputText: ElementRef;

public uploadFileInProgress: boolean;
constructor(private renderer: Renderer) {
}

bringFileSelector(): boolean {
this.renderer.invokeElementMethod(this._fileUpload.nativeElement, 'click');
return false;
}

beforeFileUpload(uploadingFile): void {
let files = this._fileUpload.nativeElement.files;
if (files.length) {
const file = files[0];
this._onChangeFileSelect(files[0])
if (!this._canFleUploadOnServer()) {
uploadingFile.setAbort();
} else {
this.uploadFileInProgress = true;
}
}
}

_onChangeFileSelect(file) {
this._inputText.nativeElement.value = file.name
}

_onFileUpload(data): void {
if (data['done'] || data['abort'] || data['error']) {
this._onFileUploadCompleted(data);
} else {
this.onFileUpload.emit(data);
}
}

_onFileUploadCompleted(data): void {
this.uploadFileInProgress = false;
this.onFileUploadCompleted.emit(data);
}

_canFleUploadOnServer(): boolean {
return !!this.fileUploaderOptions['url'];
}
}
7 changes: 7 additions & 0 deletions src/app/theme/components/baFileUploader/baFileUploader.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<input #fileUpload ngFileSelect type="file" [options]="fileUploaderOptions" (onUpload)="_onFileUpload($event)" (beforeUpload)="beforeFileUpload($event)" hidden="true">
<div class="input-group" [ngClass]="{uploading: uploadFileInProgress}">
<input #inputText type="text" [value]="defaultValue" class="form-control" readonly>
<span class="input-group-btn">
<button class="btn btn-success" type="button" (click)="bringFileSelector();">Browse</button>
</span>
</div>
Empty file.
1 change: 1 addition & 0 deletions src/app/theme/components/baFileUploader/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './baFileUploader.component';
1 change: 1 addition & 0 deletions src/app/theme/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export * from './baFullCalendar';
export * from './baPictureUploader';
export * from './baCheckbox';
export * from './baMultiCheckbox';
export * from './baFileUploader';
6 changes: 4 additions & 2 deletions src/app/theme/nga.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import {
BaMultiCheckbox,
BaPageTop,
BaPictureUploader,
BaSidebar
BaSidebar,
BaFileUploader
} from './components';

import { BaCardBlur } from './components/baCard/baCardBlur.directive';
Expand Down Expand Up @@ -70,7 +71,8 @@ const NGA_COMPONENTS = [
BaMultiCheckbox,
BaPageTop,
BaPictureUploader,
BaSidebar
BaSidebar,
BaFileUploader
];

const NGA_DIRECTIVES = [
Expand Down

0 comments on commit 7d03461

Please sign in to comment.