Add label method
This commit is contained in:
parent
407e3061df
commit
2915bd304c
@ -1,3 +1,6 @@
|
||||
import type { Model } from "./Model";
|
||||
import { ObjectID } from "./Renderer";
|
||||
|
||||
/**
|
||||
* 数据标签
|
||||
*/
|
||||
@ -6,7 +9,7 @@ class Label {
|
||||
/**
|
||||
* 唯一标识符
|
||||
*/
|
||||
public id: string;
|
||||
public id: ObjectID;
|
||||
|
||||
/**
|
||||
* 用户定义的名称
|
||||
@ -18,12 +21,18 @@ class Label {
|
||||
*/
|
||||
public color?: string;
|
||||
|
||||
/**
|
||||
* 所属模型
|
||||
*/
|
||||
public model: Model;
|
||||
|
||||
/**
|
||||
* 构造器
|
||||
* @param id 标签 ID
|
||||
* @param name 用户定义的名称
|
||||
*/
|
||||
public constructor(id: string, name?: string) {
|
||||
public constructor(model:Model, id: ObjectID, name?: string) {
|
||||
this.model = model;
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
@ -34,6 +43,16 @@ class Label {
|
||||
public equal(label: Label): boolean {
|
||||
return this === label || this.id === label.id;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否被删除
|
||||
*/
|
||||
public isDeleted(): boolean {
|
||||
for (let i = 0; i < this.model.labelPool.length; i++) {
|
||||
if (this.model.labelPool[i].equal(this)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -5,10 +5,14 @@ import { Range } from "./Range";
|
||||
import { Emitter, EventType, EventMixin } from "./Emitter";
|
||||
import { CtrlObject } from "./CtrlObject";
|
||||
import { ObjectID, AbstractRenderer } from "./Renderer";
|
||||
import { Label } from "./Label";
|
||||
|
||||
type ModelEvent = {
|
||||
groupAdd: Group;
|
||||
rangeAdd: Range;
|
||||
labelAdd: Label;
|
||||
labelDelete: Label;
|
||||
labelChange: Label[];
|
||||
objectAdd: CtrlObject;
|
||||
objectDelete: CtrlObject[];
|
||||
objectChange: CtrlObject[];
|
||||
@ -32,6 +36,51 @@ class Model extends Emitter<ModelEvent> {
|
||||
*/
|
||||
public objectPool: CtrlObject[] = [];
|
||||
|
||||
/**
|
||||
* 标签列表
|
||||
*/
|
||||
public labelPool: Label[] = [];
|
||||
|
||||
/**
|
||||
* 添加标签
|
||||
*/
|
||||
public addLabel(name: string): Label {
|
||||
console.log(`Model: Creat label with id ${this.idIndex}`);
|
||||
let label = new Label(this, this.nextId, name);
|
||||
this.labelPool.push(label);
|
||||
this.emit("labelAdd", label);
|
||||
this.emit("labelChange", this.labelPool);
|
||||
return label;
|
||||
}
|
||||
|
||||
/**
|
||||
* 搜索并删除一个 Label
|
||||
* @param name 搜索值
|
||||
*/
|
||||
public deleteLabel(name: Label | ObjectID) {
|
||||
let deletedLabel: Label | undefined;
|
||||
let index = 0;
|
||||
|
||||
for (let i = 0; i < this.labelPool.length; i++) {
|
||||
if (name instanceof Label) {
|
||||
if (this.labelPool[i].equal(name)) {
|
||||
deletedLabel = this.labelPool[i];
|
||||
index = i;
|
||||
}
|
||||
} else if (name === this.labelPool[i].id) {
|
||||
deletedLabel = this.labelPool[i];
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (deletedLabel) {
|
||||
this.labelPool.splice(index, 1);
|
||||
console.log(`Model: Delete label ${deletedLabel.name ?? deletedLabel.id}`);
|
||||
this.emit("labelDelete", deletedLabel);
|
||||
this.emit("labelChange", this.labelPool);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加组
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user