SelectionBehavior.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {
  2. hasPrimaryModifier
  3. } from '../../util/Mouse';
  4. import {
  5. find
  6. } from 'min-dash';
  7. export default function SelectionBehavior(
  8. eventBus, selection, canvas,
  9. elementRegistry) {
  10. eventBus.on('create.end', 500, function(e) {
  11. // select the created shape after a
  12. // successful create operation
  13. if (e.context.canExecute) {
  14. selection.select(e.context.shape);
  15. }
  16. });
  17. eventBus.on('connect.end', 500, function(e) {
  18. // select the connect end target
  19. // after a connect operation
  20. if (e.context.canExecute && e.context.target) {
  21. selection.select(e.context.target);
  22. }
  23. });
  24. eventBus.on('shape.move.end', 500, function(e) {
  25. var previousSelection = e.previousSelection || [];
  26. var shape = elementRegistry.get(e.context.shape.id);
  27. // make sure at least the main moved element is being
  28. // selected after a move operation
  29. var inSelection = find(previousSelection, function(selectedShape) {
  30. return shape.id === selectedShape.id;
  31. });
  32. if (!inSelection) {
  33. selection.select(shape);
  34. }
  35. });
  36. // Shift + click selection
  37. eventBus.on('element.click', function(event) {
  38. var element = event.element;
  39. // do not select the root element
  40. // or connections
  41. if (element === canvas.getRootElement()) {
  42. element = null;
  43. }
  44. var isSelected = selection.isSelected(element),
  45. isMultiSelect = selection.get().length > 1;
  46. // mouse-event: SELECTION_KEY
  47. var add = hasPrimaryModifier(event);
  48. // select OR deselect element in multi selection
  49. if (isSelected && isMultiSelect) {
  50. if (add) {
  51. return selection.deselect(element);
  52. } else {
  53. return selection.select(element);
  54. }
  55. } else
  56. if (!isSelected) {
  57. selection.select(element, add);
  58. } else {
  59. selection.deselect(element);
  60. }
  61. });
  62. }
  63. SelectionBehavior.$inject = [
  64. 'eventBus',
  65. 'selection',
  66. 'canvas',
  67. 'elementRegistry'
  68. ];