DisabledCells.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. class DisabledCells {
  2. constructor() {
  3. this.disabledCells = new Set();
  4. }
  5. add(cell) {
  6. this.disabledCells.add(cell);
  7. }
  8. delete(cell) {
  9. this.disabledCells.delete(cell);
  10. }
  11. install(graph) {
  12. const self = this;
  13. // part #1: Intercepting mxGraph.fireMouseEvent
  14. const oldFireMouseEvent = graph.fireMouseEvent;
  15. graph.fireMouseEvent = function(evtName, me, sender) {
  16. if (me.state && self.disabledCells.has(me.state.cell.id)) {
  17. // clicked shape is disabled
  18. return;
  19. }
  20. oldFireMouseEvent.apply(this, arguments);
  21. }
  22. // part #2: Ignore double clicks on disabled cells
  23. const oldDblClick = graph.dblClick;
  24. graph.dblClick = function(evt, cell) {
  25. if (cell && self.disabledCells.has(cell.id)) {
  26. // clicked shape is disabled
  27. return;
  28. }
  29. oldDblClick.apply(this, arguments);
  30. }
  31. // part #3: Protect disabled cells from ever being selected
  32. const oldMxSelectionChange = mxSelectionChange; // override constructor :)
  33. mxSelectionChange = function(selectionModel, added, removed) {
  34. oldMxSelectionChange.apply(this, arguments);
  35. if (this.added) {
  36. this.added = this.added.filter(cell => !self.disabledCells.has(cell.id));
  37. }
  38. }
  39. mxSelectionChange.prototype = oldMxSelectionChange.prototype;
  40. }
  41. }
  42. module.exports = { DisabledCells };