123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- import VectorType from "../enum/VectorType.js";
- import SelectState from "../enum/SelectState.js";
- import VectorCategory from "../enum/VectorCategory.js";
- import { dataService } from "./DataService.js";
- export default class StateService {
- constructor() {
- this.eventName = null;
- this.selectItem = null; //选中
- this.focusItem = null; //点击
- this.draggingItem = null; //拖拽
- }
- getEventName() {
- return this.eventName;
- }
- setEventName(eventName) {
- this.eventName = eventName;
- }
- clearEventName() {
- this.eventName = null;
- }
- // type表示类型,state默认是select,但是有的元素需要知道选中的是哪个顶点
- setSelectItem(vectorId, type, state, dir) {
- this.selectItem = {};
- this.selectItem.vectorId = vectorId;
- this.selectItem.type = type;
- this.selectItem.state = state;
- this.selectItem.dir = dir;
- if (type == VectorType.Line) {
- const line = dataService.getLine(vectorId);
- this.selectItem.category = line.getCategory();
- } else if (type == VectorType.Point) {
- const point = dataService.getPoint(vectorId);
- this.selectItem.category = point.getCategory();
- }
- // console.log("选中的元素:" + JSON.stringify(this.selectItem));
- }
- setSelectState(state) {
- if (!state || !this.selectItem) {
- return;
- }
- this.selectItem.state = state;
- }
- getSelectItem() {
- return this.selectItem;
- }
- clearSelectItem() {
- this.selectItem = null;
- }
- getDraggingItem() {
- return this.draggingItem;
- }
- setDraggingItem(draggingItem) {
- this.draggingItem = draggingItem;
- if (this.draggingItem == null) {
- return;
- } else if (this.draggingItem.type == VectorType.Line) {
- const line = dataService.getLine(this.draggingItem.vectorId);
- if (!line) {
- return;
- }
- this.draggingItem.category = line.getCategory();
- } else if (this.draggingItem.type == VectorType.Point) {
- const point = dataService.getPoint(this.draggingItem.vectorId);
- this.draggingItem.category = point.getCategory();
- }
- }
- clearDraggingItem() {
- this.draggingItem = null;
- }
- getFocusItem() {
- return this.focusItem;
- }
- setFocusItem(focusItem) {
- this.focusItem = {};
- for (let key in focusItem) {
- this.focusItem[key] = focusItem[key];
- }
- }
- clearFocusItem() {
- this.focusItem = null;
- }
- clearItems() {
- this.selectItem = null;
- this.focusItem = null;
- this.draggingItem = null;
- }
- clear() {
- this.clearItems();
- this.clearEventName();
- }
- }
- const stateService = new StateService();
- window.stateService = stateService;
- export { stateService };
|