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 251 252 253 254 255 256 257 258 259 260 261 | 1× 1× 1× 84× 84× 84× 14× 22× 4× 44× 8× 8× 8× 280× 280× 280× 280× 248× 280× 32× 280× 70× 70× 70× 35× 35× 35× 35× 35× 15× 9× 6× 12× 1× 1× 1× 1× 11× 12× 21× 6× 15× 15× 23× 23× 23× 7× | import { filter, isNumber } from 'min-dash'; var max = Math.max, min = Math.min; var DEFAULT_CHILD_BOX_PADDING = 20; import { getBBox } from '../../util/Elements'; import { asTRBL, asBounds } from '../../layout/LayoutUtil'; /** * Substract a TRBL from another * * @param {TRBL} trblA * @param {TRBL} trblB * * @return {TRBL} */ export function substractTRBL(trblA, trblB) { return { top: trblA.top - trblB.top, right: trblA.right - trblB.right, bottom: trblA.bottom - trblB.bottom, left: trblA.left - trblB.left }; } /** * Resize the given bounds by the specified delta from a given anchor point. * * @param {Bounds} bounds the bounding box that should be resized * @param {String} direction in which the element is resized (nw, ne, se, sw) * @param {Point} delta of the resize operation * * @return {Bounds} resized bounding box */ export function resizeBounds(bounds, direction, delta) { var dx = delta.x, dy = delta.y; switch (direction) { case 'nw': return { x: bounds.x + dx, y: bounds.y + dy, width: bounds.width - dx, height: bounds.height - dy }; case 'sw': return { x: bounds.x + dx, y: bounds.y, width: bounds.width - dx, height: bounds.height + dy }; case 'ne': return { x: bounds.x, y: bounds.y + dy, width: bounds.width + dx, height: bounds.height - dy }; case 'se': return { x: bounds.x, y: bounds.y, width: bounds.width + dx, height: bounds.height + dy }; default: throw new Error('unrecognized direction: ' + direction); } } /** * Resize the given bounds by applying the passed * { top, right, bottom, left } delta. * * @param {Bounds} bounds * @param {TRBL} trblResize * * @return {Bounds} */ export function resizeTRBL(bounds, resize) { return { x: bounds.x + (resize.left || 0), y: bounds.y + (resize.top || 0), width: bounds.width - (resize.left || 0) + (resize.right || 0), height: bounds.height - (resize.top || 0) + (resize.bottom || 0) }; } export function reattachPoint(bounds, newBounds, point) { var sx = bounds.width / newBounds.width, sy = bounds.height / newBounds.height; return { x: Math.round((newBounds.x + newBounds.width / 2)) - Math.floor(((bounds.x + bounds.width / 2) - point.x) / sx), y: Math.round((newBounds.y + newBounds.height / 2)) - Math.floor(((bounds.y + bounds.height / 2) - point.y) / sy) }; } function applyConstraints(attr, trbl, resizeConstraints) { var value = trbl[attr], minValue = resizeConstraints.min && resizeConstraints.min[attr], maxValue = resizeConstraints.max && resizeConstraints.max[attr]; if (isNumber(minValue)) { value = (/top|left/.test(attr) ? min : max)(value, minValue); } if (isNumber(maxValue)) { value = (/top|left/.test(attr) ? max : min)(value, maxValue); } return value; } export function ensureConstraints(currentBounds, resizeConstraints) { Iif (!resizeConstraints) { return currentBounds; } var currentTrbl = asTRBL(currentBounds); return asBounds({ top: applyConstraints('top', currentTrbl, resizeConstraints), right: applyConstraints('right', currentTrbl, resizeConstraints), bottom: applyConstraints('bottom', currentTrbl, resizeConstraints), left: applyConstraints('left', currentTrbl, resizeConstraints) }); } export function getMinResizeBounds(direction, currentBounds, minDimensions, childrenBounds) { var currentBox = asTRBL(currentBounds); var minBox = { top: /n/.test(direction) ? currentBox.bottom - minDimensions.height : currentBox.top, left: /w/.test(direction) ? currentBox.right - minDimensions.width : currentBox.left, bottom: /s/.test(direction) ? currentBox.top + minDimensions.height : currentBox.bottom, right: /e/.test(direction) ? currentBox.left + minDimensions.width : currentBox.right }; var childrenBox = childrenBounds ? asTRBL(childrenBounds) : minBox; var combinedBox = { top: min(minBox.top, childrenBox.top), left: min(minBox.left, childrenBox.left), bottom: max(minBox.bottom, childrenBox.bottom), right: max(minBox.right, childrenBox.right) }; return asBounds(combinedBox); } function asPadding(mayBePadding, defaultValue) { if (typeof mayBePadding !== 'undefined') { return mayBePadding; } else { return DEFAULT_CHILD_BOX_PADDING; } } export function addPadding(bbox, padding) { var left, right, top, bottom; if (typeof padding === 'object') { left = asPadding(padding.left); right = asPadding(padding.right); top = asPadding(padding.top); bottom = asPadding(padding.bottom); } else { left = right = top = bottom = asPadding(padding); } return { x: bbox.x - left, y: bbox.y - top, width: bbox.width + left + right, height: bbox.height + top + bottom }; } /** * Is the given element part of the resize * targets min boundary box? * * This is the default implementation which excludes * connections and labels. * * @param {djs.model.Base} element */ function isBBoxChild(element) { // exclude connections if (element.waypoints) { return false; } // exclude labels Iif (element.type === 'label') { return false; } return true; } /** * Return children bounding computed from a shapes children * or a list of prefiltered children. * * @param {djs.model.Shape|Array<djs.model.Shape>} shapeOrChildren * @param {Number|Object} padding * * @return {Bounds} */ export function computeChildrenBBox(shapeOrChildren, padding) { var elements; // compute based on shape Eif (shapeOrChildren.length === undefined) { // grab all the children that are part of the // parents children box elements = filter(shapeOrChildren.children, isBBoxChild); } else { elements = shapeOrChildren; } if (elements.length) { return addPadding(getBBox(elements), padding); } } |