Outline.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. rx: 4,
  34. width: 100,
  35. height: 100
  36. }, OUTLINE_STYLE));
  37. svgAppend(gfx, outline);
  38. return outline;
  39. }
  40. // A low priortity is necessary, because outlines of labels have to be updated
  41. // after the label bounds have been updated in the renderer.
  42. eventBus.on([ 'shape.added', 'shape.changed' ], LOW_PRIORITY, function(event) {
  43. var element = event.element,
  44. gfx = event.gfx;
  45. var outline = domQuery('.djs-outline', gfx);
  46. if (!outline) {
  47. outline = createOutline(gfx, element);
  48. }
  49. self.updateShapeOutline(outline, element);
  50. });
  51. eventBus.on([ 'connection.added', 'connection.changed' ], function(event) {
  52. var element = event.element,
  53. gfx = event.gfx;
  54. var outline = domQuery('.djs-outline', gfx);
  55. if (!outline) {
  56. outline = createOutline(gfx, element);
  57. }
  58. self.updateConnectionOutline(outline, element);
  59. });
  60. }
  61. /**
  62. * Updates the outline of a shape respecting the dimension of the
  63. * element and an outline offset.
  64. *
  65. * @param {SVGElement} outline
  66. * @param {djs.model.Base} element
  67. */
  68. Outline.prototype.updateShapeOutline = function(outline, element) {
  69. svgAttr(outline, {
  70. x: -this.offset,
  71. y: -this.offset,
  72. width: element.width + this.offset * 2,
  73. height: element.height + this.offset * 2
  74. });
  75. };
  76. /**
  77. * Updates the outline of a connection respecting the bounding box of
  78. * the connection and an outline offset.
  79. *
  80. * @param {SVGElement} outline
  81. * @param {djs.model.Base} element
  82. */
  83. Outline.prototype.updateConnectionOutline = function(outline, connection) {
  84. var bbox = getBBox(connection);
  85. svgAttr(outline, {
  86. x: bbox.x - this.offset,
  87. y: bbox.y - this.offset,
  88. width: bbox.width + this.offset * 2,
  89. height: bbox.height + this.offset * 2
  90. });
  91. };
  92. Outline.$inject = [ 'eventBus', 'styles', 'elementRegistry' ];