bixi
Version:
企业级中后台前端解决方案
155 lines (141 loc) • 4.06 kB
text/typescript
import { CdkConnectedOverlay } from '@angular/cdk/overlay';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
EventEmitter,
Input,
Output,
TemplateRef,
ViewChild,
ViewEncapsulation
} from '@angular/core';
import { DEFAULT_TOOLTIP_POSITIONS, POSITION_MAP } from 'ng-zorro-antd/core/overlay';
import { ILabelingEvent, ISafeAny } from './label.type';
const OVERLAY_WIDTH = 468;
const OVERLAY_HEIGHT = 288;
export class BixiLabelModalComponent {
overlayWidth = OVERLAY_WIDTH;
overlayHeight = OVERLAY_HEIGHT;
placement = 'right';
isOpen = false;
_labeling: ILabelingEvent | null = null;
overlay!: CdkConnectedOverlay;
content: TemplateRef<ISafeAny> | undefined;
title: string;
dragBoundary: string;
set labeling(val: ILabelingEvent | null) {
this._labeling = val;
if (val) {
this.onShow();
} else {
this.onHide();
}
}
get labeling() {
return this._labeling;
}
// tslint:disable-next-line: no-output-native
close = new EventEmitter();
constructor(
private cdr: ChangeDetectorRef
) { }
get labels() {
return this.labeling ? (this.labeling.data) || [] : [];
}
onShow() {
if (this.isOpen && this.overlay) {
// 如果是二次标注的话,只需要调整位置就可以了
Promise.resolve().then(() => {
this.cdr.detectChanges();
this.overlay.overlayRef.updatePosition();
});
return;
}
this.isOpen = true;
this.cdr.detectChanges();
}
onHide() {
this.isOpen = false;
this.cdr.detectChanges();
}
onClose = () => {
this.onHide();
this.close.emit();
}
onDetach() {
// 其它方式关闭的话,也触发一次 cancel
if (this.isOpen) {
this.onClose();
}
}
get overlayStyle() {
return {
width: `${this.overlayWidth || 0}px`,
height: `${this.overlayHeight || 0}px`
};
}
get positions() {
return [POSITION_MAP[this.placement], ...DEFAULT_TOOLTIP_POSITIONS];
}
get originStyle() {
if (!this.labeling) {
return {
'width': '0px',
'height': '0px'
};
}
return {
position: 'absolute',
width: '20px',
height: '20px',
top: `${this.labeling.position.top || 0}px`,
left: `${this.labeling.position.left || 0}px`
};
}
}