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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | 802× 802× 799× 802× 649× 649× 36× 649× 870× 870× 124× 154× 154× 154× 423× 423× 423× 415× 378× 154× 3× 151× 348× 1× 348× 348× 348× 348× 348× 348× 348× 315× 87× 38× 87× 24× 87× 364× 364× 9× 355× 355× 355× 355× 348× 348× 1517× 1517× 736× 1517× 2684× 2684× 713× 2684× 2684× 2684× 2684× 2684× 1562× 2684× 1694× 2684× 2230× 2684× 2003× 1517× 9× 9× 94× 94× 35× 94× 4× 94× 4× 94× 47× 22× 25× 2× 9× 1068× 234× 834× 827× 7× 6486× 1740× | import { assign, isArray, isNumber, isObject, isUndefined, groupBy, forEach } from 'min-dash'; /** * Adds an element to a collection and returns true if the * element was added. * * @param {Array<Object>} elements * @param {Object} e * @param {Boolean} unique */ export function add(elements, e, unique) { var canAdd = !unique || elements.indexOf(e) === -1; if (canAdd) { elements.push(e); } return canAdd; } /** * Iterate over each element in a collection, calling the iterator function `fn` * with (element, index, recursionDepth). * * Recurse into all elements that are returned by `fn`. * * @param {Object|Array<Object>} elements * @param {Function} fn iterator function called with (element, index, recursionDepth) * @param {Number} [depth] maximum recursion depth */ export function eachElement(elements, fn, depth) { depth = depth || 0; if (!isArray(elements)) { elements = [ elements ]; } forEach(elements, function(s, i) { var filter = fn(s, i, depth); if (isArray(filter) && filter.length) { eachElement(filter, fn, depth + 1); } }); } /** * Collects self + child elements up to a given depth from a list of elements. * * @param {djs.model.Base|Array<djs.model.Base>} elements the elements to select the children from * @param {Boolean} unique whether to return a unique result set (no duplicates) * @param {Number} maxDepth the depth to search through or -1 for infinite * * @return {Array<djs.model.Base>} found elements */ export function selfAndChildren(elements, unique, maxDepth) { var result = [], processedChildren = []; eachElement(elements, function(element, i, depth) { add(result, element, unique); var children = element.children; // max traversal depth not reached yet if (maxDepth === -1 || depth < maxDepth) { // children exist && children not yet processed if (children && add(processedChildren, children, unique)) { return children; } } }); return result; } /** * Return self + direct children for a number of elements * * @param {Array<djs.model.Base>} elements to query * @param {Boolean} allowDuplicates to allow duplicates in the result set * * @return {Array<djs.model.Base>} the collected elements */ export function selfAndDirectChildren(elements, allowDuplicates) { return selfAndChildren(elements, !allowDuplicates, 1); } /** * Return self + ALL children for a number of elements * * @param {Array<djs.model.Base>} elements to query * @param {Boolean} allowDuplicates to allow duplicates in the result set * * @return {Array<djs.model.Base>} the collected elements */ export function selfAndAllChildren(elements, allowDuplicates) { return selfAndChildren(elements, !allowDuplicates, -1); } /** * Gets the the closure for all selected elements, * their enclosed children and connections. * * @param {Array<djs.model.Base>} elements * @param {Boolean} [isTopLevel=true] * @param {Object} [existingClosure] * * @return {Object} newClosure */ export function getClosure(elements, isTopLevel, closure) { if (isUndefined(isTopLevel)) { isTopLevel = true; } Iif (isObject(isTopLevel)) { closure = isTopLevel; isTopLevel = true; } closure = closure || {}; var allShapes = copyObject(closure.allShapes), allConnections = copyObject(closure.allConnections), enclosedElements = copyObject(closure.enclosedElements), enclosedConnections = copyObject(closure.enclosedConnections); var topLevel = copyObject( closure.topLevel, isTopLevel && groupBy(elements, function(e) { return e.id; }) ); function handleConnection(c) { if (topLevel[c.source.id] && topLevel[c.target.id]) { topLevel[c.id] = [ c ]; } // not enclosed as a child, but maybe logically // (connecting two moved elements?) if (allShapes[c.source.id] && allShapes[c.target.id]) { enclosedConnections[c.id] = enclosedElements[c.id] = c; } allConnections[c.id] = c; } function handleElement(element) { enclosedElements[element.id] = element; if (element.waypoints) { // remember connection enclosedConnections[element.id] = allConnections[element.id] = element; } else { // remember shape allShapes[element.id] = element; // remember all connections forEach(element.incoming, handleConnection); forEach(element.outgoing, handleConnection); // recurse into children return element.children; } } eachElement(elements, handleElement); return { allShapes: allShapes, allConnections: allConnections, topLevel: topLevel, enclosedConnections: enclosedConnections, enclosedElements: enclosedElements }; } /** * Returns the surrounding bbox for all elements in * the array or the element primitive. * * @param {Array<djs.model.Shape>|djs.model.Shape} elements * @param {Boolean} stopRecursion */ export function getBBox(elements, stopRecursion) { stopRecursion = !!stopRecursion; if (!isArray(elements)) { elements = [elements]; } var minX, minY, maxX, maxY; forEach(elements, function(element) { // If element is a connection the bbox must be computed first var bbox = element; if (element.waypoints && !stopRecursion) { bbox = getBBox(element.waypoints, true); } var x = bbox.x, y = bbox.y, height = bbox.height || 0, width = bbox.width || 0; if (x < minX || minX === undefined) { minX = x; } if (y < minY || minY === undefined) { minY = y; } if ((x + width) > maxX || maxX === undefined) { maxX = x + width; } if ((y + height) > maxY || maxY === undefined) { maxY = y + height; } }); return { x: minX, y: minY, height: maxY - minY, width: maxX - minX }; } /** * Returns all elements that are enclosed from the bounding box. * * * If bbox.(width|height) is not specified the method returns * all elements with element.x/y > bbox.x/y * * If only bbox.x or bbox.y is specified, method return all elements with * e.x > bbox.x or e.y > bbox.y * * @param {Array<djs.model.Shape>} elements List of Elements to search through * @param {djs.model.Shape} bbox the enclosing bbox. * * @return {Array<djs.model.Shape>} enclosed elements */ export function getEnclosedElements(elements, bbox) { var filteredElements = {}; forEach(elements, function(element) { var e = element; if (e.waypoints) { e = getBBox(e); } if (!isNumber(bbox.y) && (e.x > bbox.x)) { filteredElements[element.id] = element; } if (!isNumber(bbox.x) && (e.y > bbox.y)) { filteredElements[element.id] = element; } if (e.x > bbox.x && e.y > bbox.y) { if (isNumber(bbox.width) && isNumber(bbox.height) && e.width + e.x < bbox.width + bbox.x && e.height + e.y < bbox.height + bbox.y) { filteredElements[element.id] = element; } else if (!isNumber(bbox.width) || !isNumber(bbox.height)) { filteredElements[element.id] = element; } } }); return filteredElements; } export function getType(element) { if ('waypoints' in element) { return 'connection'; } if ('x' in element) { return 'shape'; } return 'root'; } export function isFrameElement(element) { return !!(element && element.isFrame); } // helpers /////////////////////////////// function copyObject(src1, src2) { return assign({}, src1 || {}, src2 || {}); } |