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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | 1× 1× 1189× 1189× 801× 801× 801× 801× 801× 801× 1189× 46× 1648× 1415× 233× 233× 213× 20× 20× 20× 20× 20× 20× 9× 9× 7× 7× 3× 3× 1× 1× 20× 20× 1× 1× 21× 21× 21× 21× 3× 18× 21× 84× 21× 212× 212× 45× 179× 224× 212× 233× 233× 233× 233× 21× 212× 212× 21× 21× 212× 848× 212× 184× 28× 457× 233× | import inherits from 'inherits'; import { getOrientation, getMid, asTRBL } from 'diagram-js/lib/layout/LayoutUtil'; import { substract } from 'diagram-js/lib/util/Math'; import { hasExternalLabel } from '../../../util/LabelUtil'; import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor'; var ALIGNMENTS = [ 'top', 'bottom', 'left', 'right' ]; var ELEMENT_LABEL_DISTANCE = 10; /** * A component that makes sure that external labels are added * together with respective elements and properly updated (DI wise) * during move. * * @param {EventBus} eventBus * @param {Modeling} modeling */ export default function AdaptiveLabelPositioningBehavior(eventBus, modeling) { CommandInterceptor.call(this, eventBus); this.postExecuted([ 'connection.create', 'connection.layout', 'connection.updateWaypoints' ], function(event) { var context = event.context, connection = context.connection; var source = connection.source, target = connection.target; checkLabelAdjustment(source); checkLabelAdjustment(target); }); this.postExecuted([ 'label.create' ], function(event) { checkLabelAdjustment(event.context.shape.labelTarget); }); function checkLabelAdjustment(element) { // skip non-existing labels if (!hasExternalLabel(element)) { return; } var optimalPosition = getOptimalPosition(element); // no optimal position found if (!optimalPosition) { return; } adjustLabelPosition(element, optimalPosition); } function adjustLabelPosition(element, orientation) { var elementMid = getMid(element), label = element.label, labelMid = getMid(label); var elementTrbl = asTRBL(element); var newLabelMid; switch (orientation) { case 'top': newLabelMid = { x: elementMid.x, y: elementTrbl.top - ELEMENT_LABEL_DISTANCE - label.height / 2 }; break; case 'left': newLabelMid = { x: elementTrbl.left - ELEMENT_LABEL_DISTANCE - label.width / 2, y: elementMid.y }; break; case 'bottom': newLabelMid = { x: elementMid.x, y: elementTrbl.bottom + ELEMENT_LABEL_DISTANCE + label.height / 2 }; break; case 'right': newLabelMid = { x: elementTrbl.right + ELEMENT_LABEL_DISTANCE + label.width / 2, y: elementMid.y }; break; } var delta = substract(newLabelMid, labelMid); modeling.moveShape(label, delta); } } inherits(AdaptiveLabelPositioningBehavior, CommandInterceptor); AdaptiveLabelPositioningBehavior.$inject = [ 'eventBus', 'modeling' ]; // helpers ////////////////////// /** * Return alignments which are taken by a boundary's host element * * @param {Shape} element * * @return {Array<String>} */ function getTakenHostAlignments(element) { var hostElement = element.host, elementMid = getMid(element), hostOrientation = getOrientation(elementMid, hostElement); var freeAlignments; // check whether there is a multi-orientation, e.g. 'top-left' if (hostOrientation.indexOf('-') >= 0) { freeAlignments = hostOrientation.split('-'); } else { freeAlignments = [ hostOrientation ]; } var takenAlignments = ALIGNMENTS.filter(function(alignment) { return freeAlignments.indexOf(alignment) === -1; }); return takenAlignments; } /** * Return alignments which are taken by related connections * * @param {Shape} element * * @return {Array<String>} */ function getTakenConnectionAlignments(element) { var elementMid = getMid(element); var takenAlignments = [].concat( element.incoming.map(function(c) { return c.waypoints[c.waypoints.length - 2 ]; }), element.outgoing.map(function(c) { return c.waypoints[1]; }) ).map(function(point) { return getApproximateOrientation(elementMid, point); }); return takenAlignments; } /** * Return the optimal label position around an element * or _undefined_, if none was found. * * @param {Shape} element * * @return {String} positioning identifier */ function getOptimalPosition(element) { var labelMid = getMid(element.label); var elementMid = getMid(element); var labelOrientation = getApproximateOrientation(elementMid, labelMid); if (!isAligned(labelOrientation)) { return; } var takenAlignments = getTakenConnectionAlignments(element); if (element.host) { var takenHostAlignments = getTakenHostAlignments(element); takenAlignments = takenAlignments.concat(takenHostAlignments); } var freeAlignments = ALIGNMENTS.filter(function(alignment) { return takenAlignments.indexOf(alignment) === -1; }); // NOTHING TO DO; label already aligned a.O.K. if (freeAlignments.indexOf(labelOrientation) !== -1) { return; } return freeAlignments[0]; } function getApproximateOrientation(p0, p1) { return getOrientation(p1, p0, 5); } function isAligned(orientation) { return ALIGNMENTS.indexOf(orientation) !== -1; } |