feat(layouts): add file uploader (#842)

This commit is contained in:
pornmongkon 2017-04-12 23:11:49 +07:00 committed by Lex Zhukov
parent 1be434b437
commit 7d03461efb
8 changed files with 82 additions and 2 deletions

View file

@ -16,6 +16,11 @@ export class Layouts {
url: '',
};
public fileUploaderOptions:NgUploaderOptions = {
// url: 'http://website.com/upload'
url: '',
};
constructor() {
}

View file

@ -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>

View file

@ -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'];
}
}

View file

@ -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>

View file

@ -0,0 +1 @@
export * from './baFileUploader.component';

View file

@ -12,3 +12,4 @@ export * from './baFullCalendar';
export * from './baPictureUploader';
export * from './baCheckbox';
export * from './baMultiCheckbox';
export * from './baFileUploader';

View file

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