FarmMapsLib/projects/common/src/fm/components/thumbnail/thumbnail.component.ts

86 lines
2.9 KiB
TypeScript
Raw Normal View History

import { Component,Input ,ViewChild,ElementRef,ChangeDetectorRef, Output,EventEmitter} from '@angular/core';
2021-02-15 10:34:21 +00:00
import { Store } from '@ngrx/store';
2021-03-01 15:56:48 +00:00
2021-02-25 18:57:09 +00:00
import { IListItem } from '../../models/list.item';
import { ImageService } from '../../services/image.service';
2021-02-15 10:34:21 +00:00
import { commonReducers,ItemTypeService } from '../../../public-api'
2021-03-01 15:56:48 +00:00
import { EditImageModalComponent} from '../edit-image-modal/edit-image-modal.component';
2021-03-01 21:03:18 +00:00
import { AppConfig } from "../../shared/app.config";
2021-02-15 10:34:21 +00:00
@Component({
selector: 'fm-thumbnail',
templateUrl: 'thumbnail.component.html',
styleUrls: ['thumbnail.component.scss']
})
export class ThumbnailComponent {
2021-02-25 18:57:09 +00:00
@Input() public item: IListItem;
2023-03-06 13:04:14 +00:00
@Input() public edit = false;
@Input() public save = true;
@Output() changed = new EventEmitter();
2021-02-25 18:57:09 +00:00
@ViewChild('thumbnail') el:ElementRef;
2021-03-01 15:56:48 +00:00
@ViewChild('modal') modal:EditImageModalComponent;
2021-02-15 10:34:21 +00:00
public image = null;
private endpoint = "";
public thumnailUrl = "";
constructor(public store: Store<commonReducers.State>, public itemTypeService: ItemTypeService,public appConfig: AppConfig,private changeDetector:ChangeDetectorRef,public imageService:ImageService) {
2021-02-15 10:34:21 +00:00
}
2021-02-25 18:57:09 +00:00
getThumbnailUrl(item:IListItem):string {
if(this.image == null && this.item.thumbnail) {
this.thumnailUrl = this.appConfig.getConfig("apiEndPoint")+item.url+'/thumbnail?v=' + Date.parse(item.updated);
} else if (this.image == null) {
this.thumnailUrl = "";
}
return this.thumnailUrl;
2021-02-25 18:57:09 +00:00
}
getFontSize():string {
if(this.el) {
2023-03-06 13:04:14 +00:00
const h = this.el.nativeElement.offsetHeight - (this.el.nativeElement.offsetHeight / 5 )
2021-02-25 18:57:09 +00:00
return h + "px";
} else {
return "1em";
}
}
getLineHeight():string {
if(this.el) {
return this.el.nativeElement.offsetHeight + "px";
} else {
return "1em";
}
}
canEdit():boolean {
return this.edit && this.item != null;
}
hasThumbnail():boolean {
return (this.item && this.item.thumbnail) || this.image != null;
}
2021-02-25 18:57:09 +00:00
onEditClick() {
this.endpoint = `${this.appConfig.getConfig("apiEndPoint")}/api/v1/items/${this.item.code}/thumbnail`;
this.modal.open(this.endpoint,4/3,false,200,false);
2021-03-01 21:03:18 +00:00
}
onChanged(event:any) {
this.image = event.croppedImage;
this.imageService.blobToDataUrl(event.croppedImage).then(url => {
this.thumnailUrl = url
this.changeDetector.detectChanges();
if(this.save) this.saveImage();
this.changed.emit(event.croppedImage);
});
2021-02-25 18:57:09 +00:00
}
saveImage() {
if(this.image) {
this.imageService.putImage(this.endpoint,this.image).subscribe(() => {});
}
}
2021-02-15 10:34:21 +00:00
}