StateService.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import VectorType from "../enum/VectorType.js";
  2. import SelectState from "../enum/SelectState.js";
  3. import VectorCategory from "../enum/VectorCategory.js";
  4. import { dataService } from "./DataService.js";
  5. export default class StateService {
  6. constructor() {
  7. this.eventName = null;
  8. this.selectItem = null; //选中
  9. this.focusItem = null; //点击
  10. this.draggingItem = null; //拖拽
  11. }
  12. getEventName() {
  13. return this.eventName;
  14. }
  15. setEventName(eventName) {
  16. this.eventName = eventName;
  17. }
  18. clearEventName() {
  19. this.eventName = null;
  20. }
  21. // type表示类型,state默认是select,但是有的元素需要知道选中的是哪个顶点
  22. setSelectItem(vectorId, type, state, dir) {
  23. this.selectItem = {};
  24. this.selectItem.vectorId = vectorId;
  25. this.selectItem.type = type;
  26. this.selectItem.state = state;
  27. this.selectItem.dir = dir;
  28. if (type == VectorType.Line) {
  29. const line = dataService.getLine(vectorId);
  30. this.selectItem.category = line.getCategory();
  31. } else if (type == VectorType.Point) {
  32. const point = dataService.getPoint(vectorId);
  33. this.selectItem.category = point.getCategory();
  34. }
  35. // console.log("选中的元素:" + JSON.stringify(this.selectItem));
  36. }
  37. setSelectState(state) {
  38. if (!state || !this.selectItem) {
  39. return;
  40. }
  41. this.selectItem.state = state;
  42. }
  43. getSelectItem() {
  44. return this.selectItem;
  45. }
  46. clearSelectItem() {
  47. this.selectItem = null;
  48. }
  49. getDraggingItem() {
  50. return this.draggingItem;
  51. }
  52. setDraggingItem(draggingItem) {
  53. this.draggingItem = draggingItem;
  54. if (this.draggingItem == null) {
  55. return;
  56. } else if (this.draggingItem.type == VectorType.Line) {
  57. const line = dataService.getLine(this.draggingItem.vectorId);
  58. if (!line) {
  59. return;
  60. }
  61. this.draggingItem.category = line.getCategory();
  62. } else if (this.draggingItem.type == VectorType.Point) {
  63. const point = dataService.getPoint(this.draggingItem.vectorId);
  64. this.draggingItem.category = point.getCategory();
  65. }
  66. }
  67. clearDraggingItem() {
  68. this.draggingItem = null;
  69. }
  70. getFocusItem() {
  71. return this.focusItem;
  72. }
  73. setFocusItem(focusItem) {
  74. this.focusItem = {};
  75. for (let key in focusItem) {
  76. this.focusItem[key] = focusItem[key];
  77. }
  78. }
  79. clearFocusItem() {
  80. this.focusItem = null;
  81. }
  82. clearItems() {
  83. this.selectItem = null;
  84. this.focusItem = null;
  85. this.draggingItem = null;
  86. }
  87. clear() {
  88. this.clearItems();
  89. this.clearEventName();
  90. }
  91. }
  92. const stateService = new StateService();
  93. window.stateService = stateService;
  94. export { stateService };