Selection.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import {
  2. isArray,
  3. forEach
  4. } from 'min-dash';
  5. /**
  6. * A service that offers the current selection in a diagram.
  7. * Offers the api to control the selection, too.
  8. *
  9. * @class
  10. *
  11. * @param {EventBus} eventBus the event bus
  12. */
  13. export default function Selection(eventBus, canvas) {
  14. this._eventBus = eventBus;
  15. this._canvas = canvas;
  16. this._selectedElements = [];
  17. var self = this;
  18. eventBus.on([ 'shape.remove', 'connection.remove' ], function(e) {
  19. var element = e.element;
  20. self.deselect(element);
  21. });
  22. eventBus.on([ 'diagram.clear', 'root.set' ], function(e) {
  23. self.select(null);
  24. });
  25. }
  26. Selection.$inject = [ 'eventBus', 'canvas' ];
  27. Selection.prototype.deselect = function(element) {
  28. var selectedElements = this._selectedElements;
  29. var idx = selectedElements.indexOf(element);
  30. if (idx !== -1) {
  31. var oldSelection = selectedElements.slice();
  32. selectedElements.splice(idx, 1);
  33. this._eventBus.fire('selection.changed', { oldSelection: oldSelection, newSelection: selectedElements });
  34. }
  35. };
  36. Selection.prototype.get = function() {
  37. return this._selectedElements;
  38. };
  39. Selection.prototype.isSelected = function(element) {
  40. return this._selectedElements.indexOf(element) !== -1;
  41. };
  42. /**
  43. * This method selects one or more elements on the diagram.
  44. *
  45. * By passing an additional add parameter you can decide whether or not the element(s)
  46. * should be added to the already existing selection or not.
  47. *
  48. * @method Selection#select
  49. *
  50. * @param {Object|Object[]} elements element or array of elements to be selected
  51. * @param {boolean} [add] whether the element(s) should be appended to the current selection, defaults to false
  52. */
  53. Selection.prototype.select = function(elements, add) {
  54. var selectedElements = this._selectedElements,
  55. oldSelection = selectedElements.slice();
  56. if (!isArray(elements)) {
  57. elements = elements ? [ elements ] : [];
  58. }
  59. var canvas = this._canvas;
  60. var rootElement = canvas.getRootElement();
  61. elements = elements.filter(function(element) {
  62. var elementRoot = canvas.findRoot(element);
  63. return rootElement === elementRoot;
  64. });
  65. // selection may be cleared by passing an empty array or null
  66. // to the method
  67. if (add) {
  68. forEach(elements, function(element) {
  69. if (selectedElements.indexOf(element) !== -1) {
  70. // already selected
  71. return;
  72. } else {
  73. selectedElements.push(element);
  74. }
  75. });
  76. } else {
  77. this._selectedElements = selectedElements = elements.slice();
  78. }
  79. this._eventBus.fire('selection.changed', { oldSelection: oldSelection, newSelection: selectedElements });
  80. };