mirror of
https://github.com/akveo/ngx-admin.git
synced 2025-12-22 10:20:14 +01:00
fix(picture uploader): ngx-uploader integration
This commit is contained in:
parent
be9c677c06
commit
65dcb90f05
4 changed files with 21 additions and 28 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
import {Component, ViewEncapsulation} from '@angular/core';
|
import {Component, ViewEncapsulation} from '@angular/core';
|
||||||
|
import { NgUploaderOptions } from 'ngx-uploader';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'layouts',
|
selector: 'layouts',
|
||||||
|
|
@ -12,8 +13,9 @@ export class Layouts {
|
||||||
public profile:any = {
|
public profile:any = {
|
||||||
picture: 'assets/img/app/profile/Nasta.png'
|
picture: 'assets/img/app/profile/Nasta.png'
|
||||||
};
|
};
|
||||||
public uploaderOptions:any = {
|
public uploaderOptions:NgUploaderOptions = {
|
||||||
// url: 'http://website.com/upload'
|
// url: 'http://website.com/upload'
|
||||||
|
url: '',
|
||||||
};
|
};
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
|
|
||||||
|
|
@ -1,54 +1,40 @@
|
||||||
import {Component, ViewChild, Input, Output, EventEmitter, ElementRef, Renderer} from '@angular/core';
|
import {Component, ViewChild, Input, Output, EventEmitter, ElementRef, Renderer} from '@angular/core';
|
||||||
import {Ng2Uploader} from 'ng2-uploader/ng2-uploader';
|
import { NgUploaderOptions } from 'ngx-uploader';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'ba-picture-uploader',
|
selector: 'ba-picture-uploader',
|
||||||
styleUrls: ['./baPictureUploader.scss'],
|
styleUrls: ['./baPictureUploader.scss'],
|
||||||
templateUrl: './baPictureUploader.html',
|
templateUrl: './baPictureUploader.html',
|
||||||
providers: [Ng2Uploader]
|
|
||||||
})
|
})
|
||||||
export class BaPictureUploader {
|
export class BaPictureUploader {
|
||||||
|
|
||||||
@Input() defaultPicture:string = '';
|
@Input() defaultPicture:string = '';
|
||||||
@Input() picture:string = '';
|
@Input() picture:string = '';
|
||||||
|
|
||||||
@Input() uploaderOptions:any = {};
|
@Input() uploaderOptions:NgUploaderOptions = { url: '' };
|
||||||
@Input() canDelete:boolean = true;
|
@Input() canDelete:boolean = true;
|
||||||
|
|
||||||
onUpload:EventEmitter<any> = new EventEmitter();
|
@Output() onUpload = new EventEmitter<any>();
|
||||||
onUploadCompleted:EventEmitter<any> = new EventEmitter();
|
@Output() onUploadCompleted = new EventEmitter<any>();
|
||||||
|
|
||||||
@ViewChild('fileUpload') public _fileUpload:ElementRef;
|
@ViewChild('fileUpload') public _fileUpload:ElementRef;
|
||||||
|
|
||||||
public uploadInProgress:boolean = false;
|
public uploadInProgress:boolean;
|
||||||
|
|
||||||
constructor(private renderer:Renderer, protected _uploader:Ng2Uploader) {
|
constructor(private renderer: Renderer) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public ngOnInit():void {
|
beforeUpload(uploadingFile): void {
|
||||||
if (this._canUploadOnServer()) {
|
|
||||||
setTimeout(() => {
|
|
||||||
this._uploader.setOptions(this.uploaderOptions);
|
|
||||||
});
|
|
||||||
|
|
||||||
this._uploader._emitter.subscribe((data) => {
|
|
||||||
this._onUpload(data);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
console.warn('Please specify url parameter to be able to upload the file on the back-end');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public onFiles():void {
|
|
||||||
let files = this._fileUpload.nativeElement.files;
|
let files = this._fileUpload.nativeElement.files;
|
||||||
|
|
||||||
if (files.length) {
|
if (files.length) {
|
||||||
const file = files[0];
|
const file = files[0];
|
||||||
this._changePicture(file);
|
this._changePicture(file);
|
||||||
|
|
||||||
if (this._canUploadOnServer()) {
|
if (!this._canUploadOnServer()) {
|
||||||
|
uploadingFile.setAbort();
|
||||||
|
} else {
|
||||||
this.uploadInProgress = true;
|
this.uploadInProgress = true;
|
||||||
this._uploader.addFilesToQueue(files);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
<div class="picture-group" [ngClass]="{uploading: uploadInProgress}">
|
<div class="picture-group" [ngClass]="{uploading: uploadInProgress}">
|
||||||
<div class="picture-wrapper" (click)="bringFileSelector();">
|
<div class="picture-wrapper" (click)="bringFileSelector();">
|
||||||
<img src="{{ picture }}" *ngIf="picture">
|
<img [src]="picture" *ngIf="picture">
|
||||||
<img src="{{ defaultPicture }}" ngIf="{{!picture && defaultPicture}}">
|
<img [src]="defaultPicture" *ngIf="!picture && defaultPicture">
|
||||||
|
|
||||||
<div class="loading" *ngIf="uploadInProgress">
|
<div class="loading" *ngIf="uploadInProgress">
|
||||||
<div class="spinner">
|
<div class="spinner">
|
||||||
|
|
@ -12,5 +12,8 @@
|
||||||
</div>
|
</div>
|
||||||
<i class="ion-ios-close-outline" (click)="removePicture();" *ngIf="picture && canDelete"></i>
|
<i class="ion-ios-close-outline" (click)="removePicture();" *ngIf="picture && canDelete"></i>
|
||||||
<a href class="change-picture" (click)="bringFileSelector();">Change profile Picture</a>
|
<a href class="change-picture" (click)="bringFileSelector();">Change profile Picture</a>
|
||||||
<input #fileUpload type="file" hidden="true" id="uploadFile" (change)="onFiles()">
|
<input #fileUpload ngFileSelect [options]="uploaderOptions"
|
||||||
|
(onUpload)="_onUpload($event)"
|
||||||
|
(beforeUpload)="beforeUpload($event)"
|
||||||
|
type="file" hidden="true">
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import { NgModule, ModuleWithProviders } from '@angular/core';
|
||||||
import { CommonModule } from '@angular/common';
|
import { CommonModule } from '@angular/common';
|
||||||
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
|
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
|
||||||
import { RouterModule } from '@angular/router';
|
import { RouterModule } from '@angular/router';
|
||||||
|
import { NgUploaderModule } from 'ngx-uploader';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
BaThemeConfig
|
BaThemeConfig
|
||||||
|
|
@ -107,6 +108,7 @@ const NGA_VALIDATORS = [
|
||||||
RouterModule,
|
RouterModule,
|
||||||
FormsModule,
|
FormsModule,
|
||||||
ReactiveFormsModule,
|
ReactiveFormsModule,
|
||||||
|
NgUploaderModule,
|
||||||
],
|
],
|
||||||
exports: [
|
exports: [
|
||||||
...NGA_PIPES,
|
...NGA_PIPES,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue