DrilldownOverlayBehavior.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import inherits from 'inherits-browser';
  2. import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
  3. import { getBusinessObject, is } from '../../util/ModelUtil';
  4. import { classes, domify } from 'min-dom';
  5. import { getPlaneIdFromShape } from '../../util/DrilldownUtil';
  6. /**
  7. * @typedef {import('diagram-js/lib/core/Canvas').default} Canvas
  8. * @typedef {import('diagram-js/lib/core/ElementRegistry').default} ElementRegistry
  9. * @typedef {import('diagram-js/lib/core/EventBus').default} EventBus
  10. * @typedef {import('diagram-js/lib/features/overlays/Overlays').default} Overlays
  11. *
  12. * @typedef {import('../../model/Types').Element} Element
  13. * @typedef {import('../../model/Types').Parent} Parent
  14. * @typedef {import('../../model/Types').Shape} Shape
  15. */
  16. var LOW_PRIORITY = 250;
  17. var ARROW_DOWN_SVG = '<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M4.81801948,3.50735931 L10.4996894,9.1896894 L10.5,4 L12,4 L12,12 L4,12 L4,10.5 L9.6896894,10.4996894 L3.75735931,4.56801948 C3.46446609,4.27512627 3.46446609,3.80025253 3.75735931,3.50735931 C4.05025253,3.21446609 4.52512627,3.21446609 4.81801948,3.50735931 Z"/></svg>';
  18. var EMPTY_MARKER = 'bjs-drilldown-empty';
  19. /**
  20. * @param {Canvas} canvas
  21. * @param {EventBus} eventBus
  22. * @param {ElementRegistry} elementRegistry
  23. * @param {Overlays} overlays
  24. */
  25. export default function DrilldownOverlayBehavior(
  26. canvas, eventBus, elementRegistry, overlays
  27. ) {
  28. CommandInterceptor.call(this, eventBus);
  29. this._canvas = canvas;
  30. this._eventBus = eventBus;
  31. this._elementRegistry = elementRegistry;
  32. this._overlays = overlays;
  33. var self = this;
  34. this.executed('shape.toggleCollapse', LOW_PRIORITY, function(context) {
  35. var shape = context.shape;
  36. // Add overlay to the collapsed shape
  37. if (self._canDrillDown(shape)) {
  38. self._addOverlay(shape);
  39. } else {
  40. self._removeOverlay(shape);
  41. }
  42. }, true);
  43. this.reverted('shape.toggleCollapse', LOW_PRIORITY, function(context) {
  44. var shape = context.shape;
  45. // Add overlay to the collapsed shape
  46. if (self._canDrillDown(shape)) {
  47. self._addOverlay(shape);
  48. } else {
  49. self._removeOverlay(shape);
  50. }
  51. }, true);
  52. this.executed([ 'shape.create', 'shape.move', 'shape.delete' ], LOW_PRIORITY,
  53. function(context) {
  54. var oldParent = context.oldParent,
  55. newParent = context.newParent || context.parent,
  56. shape = context.shape;
  57. // Add overlay to the collapsed shape
  58. if (self._canDrillDown(shape)) {
  59. self._addOverlay(shape);
  60. }
  61. self._updateDrilldownOverlay(oldParent);
  62. self._updateDrilldownOverlay(newParent);
  63. self._updateDrilldownOverlay(shape);
  64. }, true);
  65. this.reverted([ 'shape.create', 'shape.move', 'shape.delete' ], LOW_PRIORITY,
  66. function(context) {
  67. var oldParent = context.oldParent,
  68. newParent = context.newParent || context.parent,
  69. shape = context.shape;
  70. // Add overlay to the collapsed shape
  71. if (self._canDrillDown(shape)) {
  72. self._addOverlay(shape);
  73. }
  74. self._updateDrilldownOverlay(oldParent);
  75. self._updateDrilldownOverlay(newParent);
  76. self._updateDrilldownOverlay(shape);
  77. }, true);
  78. eventBus.on('import.render.complete', function() {
  79. elementRegistry.filter(function(e) {
  80. return self._canDrillDown(e);
  81. }).map(function(el) {
  82. self._addOverlay(el);
  83. });
  84. });
  85. }
  86. inherits(DrilldownOverlayBehavior, CommandInterceptor);
  87. /**
  88. * @param {Shape} shape
  89. */
  90. DrilldownOverlayBehavior.prototype._updateDrilldownOverlay = function(shape) {
  91. var canvas = this._canvas;
  92. if (!shape) {
  93. return;
  94. }
  95. var root = canvas.findRoot(shape);
  96. if (root) {
  97. this._updateOverlayVisibility(root);
  98. }
  99. };
  100. /**
  101. * @param {Element} element
  102. *
  103. * @return {boolean}
  104. */
  105. DrilldownOverlayBehavior.prototype._canDrillDown = function(element) {
  106. var canvas = this._canvas;
  107. return is(element, 'bpmn:SubProcess') && canvas.findRoot(getPlaneIdFromShape(element));
  108. };
  109. /**
  110. * Update the visibility of the drilldown overlay. If the plane has no elements,
  111. * the drilldown will only be shown when the element is selected.
  112. *
  113. * @param {Parent} element The collapsed root or shape.
  114. */
  115. DrilldownOverlayBehavior.prototype._updateOverlayVisibility = function(element) {
  116. var overlays = this._overlays;
  117. var businessObject = getBusinessObject(element);
  118. var overlay = overlays.get({ element: businessObject.id, type: 'drilldown' })[0];
  119. if (!overlay) {
  120. return;
  121. }
  122. var hasFlowElements = businessObject
  123. && businessObject.get('flowElements')
  124. && businessObject.get('flowElements').length;
  125. classes(overlay.html).toggle(EMPTY_MARKER, !hasFlowElements);
  126. };
  127. /**
  128. * Add a drilldown button to the given element assuming the plane has the same
  129. * ID as the element.
  130. *
  131. * @param {Shape} element The collapsed shape.
  132. */
  133. DrilldownOverlayBehavior.prototype._addOverlay = function(element) {
  134. var canvas = this._canvas,
  135. overlays = this._overlays;
  136. var existingOverlays = overlays.get({ element: element, type: 'drilldown' });
  137. if (existingOverlays.length) {
  138. this._removeOverlay(element);
  139. }
  140. var button = domify('<button class="bjs-drilldown">' + ARROW_DOWN_SVG + '</button>');
  141. button.addEventListener('click', function() {
  142. canvas.setRootElement(canvas.findRoot(getPlaneIdFromShape(element)));
  143. });
  144. overlays.add(element, 'drilldown', {
  145. position: {
  146. bottom: -7,
  147. right: -8
  148. },
  149. html: button
  150. });
  151. this._updateOverlayVisibility(element);
  152. };
  153. DrilldownOverlayBehavior.prototype._removeOverlay = function(element) {
  154. var overlays = this._overlays;
  155. overlays.remove({
  156. element: element,
  157. type: 'drilldown'
  158. });
  159. };
  160. DrilldownOverlayBehavior.$inject = [
  161. 'canvas',
  162. 'eventBus',
  163. 'elementRegistry',
  164. 'overlays'
  165. ];