argos/dmp-frontend/src/app/ui/description/editor/description-form/description-form-annotation...

76 lines
2.3 KiB
TypeScript

import { Injectable } from '@angular/core';
import { Annotation } from '@annotation-service/core/model/annotation.model';
import { AnnotationLookup } from '@annotation-service/core/query/annotation.lookup';
import { AnnotationService } from '@annotation-service/services/http/annotation.service';
import { AnnotationEntityType } from '@app/core/common/enum/annotation-entity-type';
import { SnackBarNotificationLevel, UiNotificationService } from '@app/core/services/notification/ui-notification-service';
import { BaseService } from '@common/base/base.service';
import { Guid } from '@common/types/guid';
import { TranslateService } from '@ngx-translate/core';
import { takeUntil } from 'rxjs/operators';
import { nameof } from 'ts-simple-nameof';
@Injectable({
providedIn: 'any',
})
export class DescriptionFormAnnotationService extends BaseService {
private entityId: Guid;
private annotationsPerAnchor: Map<string, number>;
constructor(
private annotationService: AnnotationService,
private uiNotificationService: UiNotificationService,
private language: TranslateService
) {
super();
}
init(entityId: Guid) {
this.entityId = entityId;
this.refreshAnnotations();
}
public getCount(anchor: string) {
if (this.annotationsPerAnchor.has(anchor)) {
return this.annotationsPerAnchor.get(anchor);
} else {
return 0;
}
}
public refreshAnnotations(onSuccess?: () => void) {
const lookup: AnnotationLookup = new AnnotationLookup();
lookup.entityIds = [this.entityId];
lookup.entityTypes = [AnnotationEntityType.Description];
lookup.project = {
fields: [
nameof<Annotation>(x => x.id),
nameof<Annotation>(x => x.anchor),
]
};
this.annotationService.query(lookup)
.pipe(takeUntil(this._destroyed))
.subscribe(
data => {
this.annotationsPerAnchor = new Map();
for (const item of data.items) {
if (!this.annotationsPerAnchor.has(item.anchor)) {
this.annotationsPerAnchor.set(item.anchor, 0);
}
this.annotationsPerAnchor.set(item.anchor, this.annotationsPerAnchor.get(item.anchor) + 1);
}
onSuccess ? onSuccess() : null;
},
error => this.onCallbackError(error),
);
}
private onCallbackError(error: any) {
this.uiNotificationService.snackBarNotification(this.language.instant(error.message), SnackBarNotificationLevel.Error);
}
}