all files / Github/bpmn-js/lib/features/modeling/behavior/ ToggleElementCollapseBehaviour.js

91.11% Statements 41/45
83.33% Branches 15/18
100% Functions 10/10
91.11% Lines 41/45
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145                                        1128×       11× 67×               11× 11×       11×   11×   11×   11× 11×   11× 11×             11×         10×               1128×   21× 21×   21×       21×   11×     11×     10×       1128×                   1128× 21× 21×     21×     10×       11×     21×                                   11× 67×    
import inherits from 'inherits';
 
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
 
import {
  getBusinessObject,
  is
} from '../../../util/ModelUtil';
 
import {
  computeChildrenBBox
} from 'diagram-js/lib/features/resize/ResizeUtil';
 
 
var LOW_PRIORITY = 500;
 
 
export default function ToggleElementCollapseBehaviour(
    eventBus, elementFactory, modeling,
    resize) {
 
  CommandInterceptor.call(this, eventBus);
 
 
  function hideEmptyLables(children) {
    if (children.length) {
      children.forEach(function(child) {
        Iif (child.type === 'label' && !child.businessObject.name) {
          child.hidden = true;
        }
      });
    }
  }
 
  function expandedBounds(shape, defaultSize) {
    var children = shape.children,
        newBounds = defaultSize,
        visibleElements,
        visibleBBox;
 
    visibleElements = filterVisible(children).concat([ shape ]);
 
    visibleBBox = computeChildrenBBox(visibleElements);
 
    Eif (visibleBBox) {
      // center to visibleBBox with max(defaultSize, childrenBounds)
      newBounds.width = Math.max(visibleBBox.width, newBounds.width);
      newBounds.height = Math.max(visibleBBox.height, newBounds.height);
 
      newBounds.x = visibleBBox.x + (visibleBBox.width - newBounds.width) / 2;
      newBounds.y = visibleBBox.y + (visibleBBox.height - newBounds.height) / 2;
    } else {
      // center to collapsed shape with defaultSize
      newBounds.x = shape.x + (shape.width - newBounds.width) / 2;
      newBounds.y = shape.y + (shape.height - newBounds.height) / 2;
    }
 
    return newBounds;
  }
 
  function collapsedBounds(shape, defaultSize) {
 
    return {
      x: shape.x + (shape.width - defaultSize.width) / 2,
      y: shape.y + (shape.height - defaultSize.height) / 2,
      width: defaultSize.width,
      height: defaultSize.height
    };
  }
 
  this.executed([ 'shape.toggleCollapse' ], LOW_PRIORITY, function(e) {
 
    var context = e.context,
        shape = context.shape;
 
    Iif (!is(shape, 'bpmn:SubProcess')) {
      return;
    }
 
    if (!shape.collapsed) {
      // all children got made visible through djs, hide empty labels
      hideEmptyLables(shape.children);
 
      // remove collapsed marker
      getBusinessObject(shape).di.isExpanded = true;
    } else {
      // place collapsed marker
      getBusinessObject(shape).di.isExpanded = false;
    }
  });
 
  this.reverted([ 'shape.toggleCollapse' ], LOW_PRIORITY, function(e) {
 
    var context = e.context;
    var shape = context.shape;
 
 
    // revert removing/placing collapsed marker
    if (!shape.collapsed) {
      getBusinessObject(shape).di.isExpanded = true;
 
    } else {
      getBusinessObject(shape).di.isExpanded = false;
    }
  });
 
  this.postExecuted([ 'shape.toggleCollapse' ], LOW_PRIORITY, function(e) {
    var shape = e.context.shape,
        defaultSize = elementFactory._getDefaultSize(shape),
        newBounds;
 
    if (shape.collapsed) {
 
      // resize to default size of collapsed shapes
      newBounds = collapsedBounds(shape, defaultSize);
    } else {
 
      // resize to bounds of max(visible children, defaultSize)
      newBounds = expandedBounds(shape, defaultSize);
    }
 
    modeling.resizeShape(shape, newBounds, null, {
      autoResize: shape.collapsed ? false : 'nwse'
    });
  });
 
}
 
 
inherits(ToggleElementCollapseBehaviour, CommandInterceptor);
 
ToggleElementCollapseBehaviour.$inject = [
  'eventBus',
  'elementFactory',
  'modeling'
];
 
 
// helpers //////////////////////
 
function filterVisible(elements) {
  return elements.filter(function(e) {
    return !e.hidden;
  });
}