Outline.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { getBBox } from '../../util/Elements';
  2. var LOW_PRIORITY = 500;
  3. import {
  4. append as svgAppend,
  5. attr as svgAttr,
  6. create as svgCreate
  7. } from 'tiny-svg';
  8. import {
  9. query as domQuery
  10. } from 'min-dom';
  11. import {
  12. assign
  13. } from 'min-dash';
  14. /**
  15. * @class
  16. *
  17. * A plugin that adds an outline to shapes and connections that may be activated and styled
  18. * via CSS classes.
  19. *
  20. * @param {EventBus} eventBus
  21. * @param {Styles} styles
  22. * @param {ElementRegistry} elementRegistry
  23. */
  24. export default function Outline(eventBus, styles, elementRegistry) {
  25. this.offset = 6;
  26. var OUTLINE_STYLE = styles.cls('djs-outline', [ 'no-fill' ]);
  27. var self = this;
  28. function createOutline(gfx, bounds) {
  29. var outline = svgCreate('rect');
  30. svgAttr(outline, assign({
  31. x: 10,
  32. y: 10,
  33. width: 100,
  34. height: 100
  35. }, OUTLINE_STYLE));
  36. svgAppend(gfx, outline);
  37. return outline;
  38. }
  39. // A low priortity is necessary, because outlines of labels have to be updated
  40. // after the label bounds have been updated in the renderer.
  41. eventBus.on([ 'shape.added', 'shape.changed' ], LOW_PRIORITY, function(event) {
  42. var element = event.element,
  43. gfx = event.gfx;
  44. var outline = domQuery('.djs-outline', gfx);
  45. if (!outline) {
  46. outline = createOutline(gfx, element);
  47. }
  48. self.updateShapeOutline(outline, element);
  49. });
  50. eventBus.on([ 'connection.added', 'connection.changed' ], function(event) {
  51. var element = event.element,
  52. gfx = event.gfx;
  53. var outline = domQuery('.djs-outline', gfx);
  54. if (!outline) {
  55. outline = createOutline(gfx, element);
  56. }
  57. self.updateConnectionOutline(outline, element);
  58. });
  59. }
  60. /**
  61. * Updates the outline of a shape respecting the dimension of the
  62. * element and an outline offset.
  63. *
  64. * @param {SVGElement} outline
  65. * @param {djs.model.Base} element
  66. */
  67. Outline.prototype.updateShapeOutline = function(outline, element) {
  68. svgAttr(outline, {
  69. x: -this.offset,
  70. y: -this.offset,
  71. width: element.width + this.offset * 2,
  72. height: element.height + this.offset * 2
  73. });
  74. };
  75. /**
  76. * Updates the outline of a connection respecting the bounding box of
  77. * the connection and an outline offset.
  78. *
  79. * @param {SVGElement} outline
  80. * @param {djs.model.Base} element
  81. */
  82. Outline.prototype.updateConnectionOutline = function(outline, connection) {
  83. var bbox = getBBox(connection);
  84. svgAttr(outline, {
  85. x: bbox.x - this.offset,
  86. y: bbox.y - this.offset,
  87. width: bbox.width + this.offset * 2,
  88. height: bbox.height + this.offset * 2
  89. });
  90. };
  91. Outline.$inject = ['eventBus', 'styles', 'elementRegistry'];