DrilldownCentering.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { is } from '../../util/ModelUtil';
  2. /**
  3. * @typedef {import('diagram-js/lib/core/Canvas').default} Canvas
  4. * @typedef {import('diagram-js/lib/core/EventBus').default} EventBus
  5. */
  6. /**
  7. * Move collapsed subprocesses into view when drilling down.
  8. *
  9. * Zoom and scroll are saved in a session.
  10. *
  11. * @param {EventBus} eventBus
  12. * @param {Canvas} canvas
  13. */
  14. export default function DrilldownCentering(eventBus, canvas) {
  15. var currentRoot = null;
  16. var positionMap = new Map();
  17. eventBus.on('root.set', function(event) {
  18. var newRoot = event.element;
  19. var currentViewbox = canvas.viewbox();
  20. var storedViewbox = positionMap.get(newRoot);
  21. positionMap.set(currentRoot, {
  22. x: currentViewbox.x,
  23. y: currentViewbox.y,
  24. zoom: currentViewbox.scale
  25. });
  26. currentRoot = newRoot;
  27. // current root was replaced with a collaboration, we don't update the viewbox
  28. if (is(newRoot, 'bpmn:Collaboration') && !storedViewbox) {
  29. return;
  30. }
  31. storedViewbox = storedViewbox || { x: 0, y: 0, zoom: 1 };
  32. var dx = (currentViewbox.x - storedViewbox.x) * currentViewbox.scale,
  33. dy = (currentViewbox.y - storedViewbox.y) * currentViewbox.scale;
  34. if (dx !== 0 || dy !== 0) {
  35. canvas.scroll({
  36. dx: dx,
  37. dy: dy
  38. });
  39. }
  40. if (storedViewbox.zoom !== currentViewbox.scale) {
  41. canvas.zoom(storedViewbox.zoom, { x: 0, y: 0 });
  42. }
  43. });
  44. eventBus.on('diagram.clear', function() {
  45. positionMap.clear();
  46. currentRoot = null;
  47. });
  48. }
  49. DrilldownCentering.$inject = [ 'eventBus', 'canvas' ];
  50. /**
  51. * ES5 Map implementation. Works.
  52. */
  53. function Map() {
  54. this._entries = [];
  55. this.set = function(key, value) {
  56. var found = false;
  57. for (var k in this._entries) {
  58. if (this._entries[k][0] === key) {
  59. this._entries[k][1] = value;
  60. found = true;
  61. break;
  62. }
  63. }
  64. if (!found) {
  65. this._entries.push([ key, value ]);
  66. }
  67. };
  68. this.get = function(key) {
  69. for (var k in this._entries) {
  70. if (this._entries[k][0] === key) {
  71. return this._entries[k][1];
  72. }
  73. }
  74. return null;
  75. };
  76. this.clear = function() {
  77. this._entries.length = 0;
  78. };
  79. this.remove = function(key) {
  80. var idx = -1;
  81. for (var k in this._entries) {
  82. if (this._entries[k][0] === key) {
  83. idx = k;
  84. break;
  85. }
  86. }
  87. if (idx !== -1) {
  88. this._entries.splice(idx, 1);
  89. }
  90. };
  91. }