import {Component, Input, OnInit} from '@angular/core'; import {properties} from '../../../environments/environment'; import {UtilitiesService} from '../../openaireLibrary/services/utilities.service'; import {Subscription} from 'rxjs'; declare var UIkit; @Component({ selector: 'background-upload', template: `
upload an image
Position
Top Center Bottom
` }) export class BackgroundUploadComponent implements OnInit { @Input() label:string = ""; @Input() background; @Input() oldBackground; // @Input() light:boolean; @Input() communityId:string = ""; public file: File; // public photo: string | ArrayBuffer; private maxsize: number = 2000 * 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') { (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() { if (this.background.imageFile) { this.subscriptions.push(this.utilsService.deletePhoto(properties.utilsService + '/delete/community/' + this.communityId + "/" +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/community/"+ this.communityId + "/" + encodeURIComponent(this.communityId+"-"+this.label)+"?big=1", 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' }); })); } } }