connect-admin/src/app/pages/customization/background.component.ts

150 lines
5.1 KiB
TypeScript

import {Component, Input, OnInit} from '@angular/core';
import {CustomizationOptions} from '../../openaireLibrary/connect/community/CustomizationOptions';
import {properties} from '../../../environments/environment';
import {UtilitiesService} from '../../openaireLibrary/services/utilities.service';
import {Subscription} from 'rxjs';
declare var UIkit;
@Component({
selector: 'background',
template: `
<color [color]="background.color"
[label]="label" [light]="light"
(colorChange)=
" background.color = $event;"></color>
<div *ngIf="upload" >
<input #file id="photo" type="file" class="uk-hidden" (change)="fileChangeEvent($event)"/>
<div *ngIf="!background.imageFile" class=" uk-width-1-1"
style="margin-top: 7px;">
<div class="uk-grid uk-flex uk-flex-middle" uk-grid>
<div class=" uk-width-1-1 uk-flex uk-flex-center">
<button class="uk-button uk-button-secondary uk-flex uk-flex-middle uk-flex-wrap"
(click)="file.click()">
<icon name="cloud_upload" [flex]="true"></icon>
<span class="uk-margin-small-left">Upload a file</span>
</button>
</div>
</div>
</div>
<div *ngIf="background.imageFile" class="uk-width-1-1 uk-flex uk-flex-middle">
<div class="uk-card uk-card-default uk-text-center ">
<img class="" [src]="background.imageFile.indexOf('data:')==-1?(properties.utilsService + '/download/'+background.imageFile):background.imageFile">
</div>
<div class="uk-margin-left">
<button (click)="removePhoto()" uk-tooltip="Remove" class="uk-button-secondary outlined uk-icon-button">
<icon name="remove"></icon>
</button>
</div>
<div class="uk-margin-small-left">
<button class="uk-button-secondary uk-icon-button" (click)="file.click()" uk-tooltip="Edit">
<icon name="edit"></icon>
</button>
</div>
</div>
</div>
`
})
export class BackgroundComponent implements OnInit {
@Input() label:string = "";
@Input() background;
@Input() oldBackground;
@Input() light:boolean;
@Input() upload:boolean = false;
@Input() communityId:string = "";
public file: File;
// public photo: string | ArrayBuffer;
private maxsize: number = 200 * 1024;
properties;
private subscriptions: any[] = [];
constructor(private utilsService: UtilitiesService) {
}
ngOnInit() {
this.properties = properties;
}
ngOnDestroy() {
this.subscriptions.forEach(subscription => {
if (subscription instanceof Subscription) {
subscription.unsubscribe();
}
});
}
removePhoto() {
if (typeof document != 'undefined') {
(<HTMLInputElement>document.getElementById("photo")).value = "";
}
// this.initPhoto();
console.log(this.background.imageFile + " " + this.oldBackground.imageFile)
if(this.background.imageFile != this.oldBackground.imageFile){
this.deletePhoto();
}
this.background.imageFile = null;
this.file = null;
}
public deletePhoto() {
console.log("deletePhoto")
if (this.background.imageFile) {
console.log("deletePhoto@@")
this.subscriptions.push(this.utilsService.deletePhoto(properties.utilsService + '/delete/stakeholder/' +this.background.imageFile).subscribe());
}
}
fileChangeEvent(event) {
if (event.target.files && event.target.files[0]) {
this.file = event.target.files[0];
if (this.file.type !== 'image/png' && this.file.type !== 'image/jpeg') {
UIkit.notification('You must choose a file with type: image/png or image/jpeg!', {
status: 'danger',
timeout: 6000,
pos: 'bottom-right'
});
this.removePhoto();
} else if (this.file.size > this.maxsize) {
UIkit.notification('File exceeds size\'s limit ('+this.maxsize/1024+'KB)! ', {
status: 'danger',
timeout: 6000,
pos: 'bottom-right'
});
this.removePhoto();
} else {
/*const reader = new FileReader();
reader.readAsDataURL(this.file);
reader.onload = () => {
this.background.imageFile = reader.result;
};*/
this.save();
}
}
}
public save() {
if (this.file) {
this.subscriptions.push(this.utilsService.uploadPhoto(this.properties.utilsService + "/upload/stakeholder/" + encodeURIComponent(this.communityId+"-"+this.label), this.file).subscribe(res => {
this.deletePhoto();
this.removePhoto();
this.background.imageFile = res.filename;
}, error => {
UIkit.notification("An error has been occurred during upload your image. Try again later", {
status: 'danger',
timeout: 6000,
pos: 'bottom-right'
});
}));
}
}
}