FarmMapsLib/projects/common/src/fm/components/edit-image-modal/edit-image-modal.component.ts

74 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-03-01 21:03:18 +00:00
import { Component, OnInit,ViewChild,ElementRef,EventEmitter, Output } from '@angular/core';
import { HttpClient, HttpParams,HttpHeaders } from "@angular/common/http";
import {NgbModal} from "@ng-bootstrap/ng-bootstrap"
2021-03-01 15:56:48 +00:00
import { ImageCroppedEvent,LoadedImage } from 'ngx-image-cropper';
2021-03-01 21:03:18 +00:00
import {ImageService } from '../../services/image.service';
2021-03-01 15:56:48 +00:00
@Component({
selector: 'fm-edit-image-modal',
templateUrl: './edit-image-modal.component.html',
styleUrls: ['./edit-image-modal.component.scss']
})
export class EditImageModalComponent implements OnInit {
2021-03-01 21:03:18 +00:00
@Output() changed = new EventEmitter();
2021-03-01 15:56:48 +00:00
@ViewChild('upload_modal') modal:ElementRef;
2021-03-01 21:03:18 +00:00
constructor(private modalService: NgbModal,public imageService:ImageService) { }
2023-03-06 13:04:14 +00:00
isImageLoaded = false;
2021-03-01 21:03:18 +00:00
aspectRatio:number = 4/3;
imageChangedEvent: any = '';
2023-03-06 13:04:14 +00:00
croppedImage = '';
2021-03-01 21:03:18 +00:00
endpointUrl:string = null;
imageUrl:string = null;
2023-03-06 13:04:14 +00:00
maxWidth = 200;
roundImage = false;
imageType = "jpeg";
saveImage = true;
2021-03-01 15:56:48 +00:00
ngOnInit(): void {
}
open(endpoint:string,aspectRatio:number,roundImage?:boolean,maxWidth?:number,saveImage?:boolean, imageType?:string) {
2021-03-01 21:03:18 +00:00
this.endpointUrl = endpoint;
this.imageUrl = endpoint;
2021-03-01 15:56:48 +00:00
this.aspectRatio= aspectRatio;
2021-03-02 11:20:53 +00:00
this.roundImage = roundImage === undefined?this.roundImage:roundImage;
this.maxWidth = maxWidth === undefined?this.maxWidth:maxWidth;
this.imageType = imageType === undefined?this.imageType:imageType.substr(6);
2021-03-02 12:15:16 +00:00
this.saveImage = saveImage === undefined?this.saveImage:saveImage;
this.modalService.open(this.modal,{ size: 'lg' });
2021-03-01 15:56:48 +00:00
}
fileChangeEvent(event: any): void {
this.imageChangedEvent = event;
}
imageCropped(event: ImageCroppedEvent) {
this.croppedImage = event.base64;
}
imageLoaded(image: LoadedImage) {
2021-03-01 16:25:40 +00:00
this.isImageLoaded=true;
2021-03-01 15:56:48 +00:00
}
cropperReady() {
// cropper ready
}
loadImageFailed() {
// show message
}
2021-03-01 21:03:18 +00:00
save() {
if(this.croppedImage) {
2023-03-06 13:04:14 +00:00
const blob = this.imageService.dataUrltoBlob(this.croppedImage);
2021-03-02 15:51:16 +00:00
if(this.saveImage) {
this.imageService.putImage(this.endpointUrl,blob).subscribe(() => {
2021-03-02 12:15:16 +00:00
this.changed.emit({});
});
} else {
2021-03-02 15:51:16 +00:00
this.changed.emit({ "croppedImage": this.imageService.blobToFile(blob,"croppedimage.jpg")} );
2021-03-02 12:15:16 +00:00
}
this.modalService.dismissAll("Save");
2021-03-01 21:03:18 +00:00
}
}
2021-03-01 15:56:48 +00:00
}