SelectionHandler.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. class SelectionHandler {
  2. constructor(getUserId, userColors) {
  3. this.userColors = userColors;
  4. this.getUserId = getUserId;
  5. this.map = new Map();
  6. }
  7. install(graph, controller) {
  8. graph.selectionModel.addListener(mxEvent.CHANGE, (source, eventObj) => {
  9. if (listenerEnabled) {
  10. const {added, removed} = eventObj.properties;
  11. controller.addInput(
  12. "broadcast",
  13. // "selection_change",
  14. "in",
  15. [{
  16. userId: this.getUserId(),
  17. addedIds: removed ? removed.map(cell => cell.id) : [],
  18. removedIds: added ? added.map(cell => cell.id) : [],
  19. }],
  20. controller.wallclockToSimtime(),
  21. );
  22. }
  23. });
  24. controller.addMyOwnOutputListener({
  25. 'add': event => {
  26. if (event.name === "selection_change") {
  27. const [{userId, addedIds, removedIds}] = event.parameters;
  28. const color = this.userColors.getColor(userId);
  29. for (const cellId of addedIds) {
  30. const cell = graph.model.cells[cellId];
  31. const highlight = new mxCellHighlight(graph, color,
  32. 6); // width
  33. highlight.highlight(graph.view.getState(cell));
  34. this.map.set(cellId, highlight);
  35. }
  36. for (const cellId of removedIds) {
  37. const cell = graph.model.cells[cellId];
  38. const highlight = this.map.get(cellId);
  39. highlight.destroy();
  40. this.map.delete(cellId);
  41. }
  42. }
  43. }
  44. })
  45. }
  46. clearAll() {
  47. this.map.forEach(highlight => highlight.destroy());
  48. this.map.clear();
  49. }
  50. }