all files / diagram/utility/ container-util.js

98.01% Statements 295/301
88.46% Branches 161/182
100% Functions 19/19
98.01% Lines 295/301
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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420   23× 23×   46× 46× 46× 46× 46× 46× 46× 12×     23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 18× 18×   23× 23× 13× 21× 21× 21× 21×   21× 16×   21×   20×   21×   21×   21× 21× 21×   21× 21×       23× 23× 23×   23× 23× 23× 23×   23×     23×   23×   23×   23×   23×   13× 13× 13× 11× 11× 11× 11×     18× 18× 18×               18× 18×   18× 18× 18× 18× 18× 18× 18× 18× 18× 36× 36× 36×   36× 18×     18× 18×   18× 18× 18×   24× 24× 24× 24× 24× 24× 24× 24× 24×                               29× 29× 29× 29× 53× 53× 34× 23×     11×         29×   19× 19× 19× 19× 19× 19× 19× 19×   19×   12×   19×   17×   19×                 12× 12× 40×     11× 11×       58× 58× 56× 90× 90× 90× 90× 90× 90× 90×   20×               11× 11×         11×       42× 82× 82× 12× 12×     70× 30×                                                          
define(["require", "exports", "../core/elements/diagram-element", "../enum/enum", "../objects/node", "./../utility/base-util", "../core/elements/text-element", "../core/containers/canvas", "../primitives/size", "./dom-util", "../core/elements/path-element"], function (require, exports, diagram_element_1, enum_1, node_1, base_util_1, text_element_1, canvas_1, size_1, dom_util_1, path_element_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    function initContainerWrapper(content, node, diagram) {
        content = getContainerElement(content, node, diagram);
        return content;
    }
    exports.initContainerWrapper = initContainerWrapper;
    function setStyle(child, node) {
        child.style.fill = node.style.fill;
        child.style.strokeColor = node.style.strokeColor;
        child.style.strokeWidth = node.style.strokeWidth;
        child.style.strokeDashArray = node.style.strokeDashArray;
        child.style.opacity = node.style.opacity;
        child.style.gradient = node.style.gradient;
        if ((node.constraints & enum_1.NodeConstraints.Shadow) !== 0 && node.visible) {
            child.shadow = node.shadow;
        }
    }
    function getContainerElement(content, node, diagram) {
        var container = new canvas_1.Canvas();
        setStyle(container, node);
        container.id = node.id + '_container';
        var size = getSize(node, content);
        node.width = size.width;
        node.height = size.height;
        content.id = node.id + '_content';
        content.width = size.width;
        content.height = size.height;
        setStyle(content, node);
        container.children = [content];
        if (node.shape.header && node.shape.hasHeader) {
            var header = initHeader(diagram, node);
            container.children.push(header);
        }
        var containerShapeObj = (node.shape);
        if (node.shape.children && node.shape.children.length > 0) {
            for (var i = 0; i < containerShapeObj.children.length; i++) {
                var child = diagram.nameTable[containerShapeObj.children[parseInt(i.toString(), 10)]];
                Eif (child && (!child.parentId || child.parentId === node.id)) {
                    child.parentId = node.id;
                    if (!child.margin.left) {
                        child.margin.left = 10;
                    }
                    if (!child.margin.top) {
                        child.margin.top = 10;
                    }
                    if (node.shape.header && node.shape.hasHeader && !diagram.isLoading
                        && !(diagram.diagramActions & enum_1.DiagramAction.UndoRedo)) {
                        child.margin.top += node.shape.header.height;
                    }
                    var nodeOffsetX = (node.offsetX - node.width * node.pivot.x) +
                        (child.margin.left + child.width * child.pivot.x);
                    var nodeOffsetY = (node.offsetY - node.height * node.pivot.y) +
                        (child.margin.top + child.height * child.pivot.y);
                    Eif (child.offsetX !== nodeOffsetX || child.offsetY !== nodeOffsetY) {
                        child.offsetX = nodeOffsetX;
                        child.offsetY = nodeOffsetY;
                    }
                    container.children.push(child.wrapper);
                    updateIndex(diagram, child);
                }
            }
        }
        container.width = node.width;
        container.height = node.height;
        return container;
    }
    function getSize(node, content) {
        var size = new size_1.Size(node.width, node.height);
        Eif (!(content instanceof path_element_1.PathElement)) {
            size.width = size.width || 50;
            size.height = size.height || 50;
        }
        Iif (content.actualSize.width && content.actualSize.height) {
            return content.actualSize;
        }
        if (node.maxWidth !== undefined) {
            size.width = Math.min(size.width, node.maxWidth);
        }
        if (node.maxHeight !== undefined) {
            size.height = Math.min(size.height, node.maxHeight);
        }
        if (node.minWidth !== undefined) {
            size.width = Math.max(size.width, node.minWidth);
        }
        if (node.minHeight !== undefined) {
            size.height = Math.max(size.height, node.minHeight);
        }
        return size;
    }
    function setSizeForContainer(newObject, oldObject, object, wrapper, diagram) {
        wrapper.children[0].width = newObject.width;
        wrapper.children[0].height = newObject.height;
        if (object.shape.header && object.shape.hasHeader) {
            var header = diagram.nameTable[object.id + object.shape.header.id];
            header.wrapper.children[0].width = newObject.width;
            header.wrapper.measure(new size_1.Size(header.wrapper.bounds.width, header.wrapper.bounds.height));
            header.wrapper.arrange(header.wrapper.desiredSize);
        }
    }
    exports.setSizeForContainer = setSizeForContainer;
    function initHeader(diagram, object) {
        var shape = object.shape;
        shape.header.id = shape.header.id || base_util_1.randomId();
        var node = {
            id: object.id + shape.header.id,
            annotations: [base_util_1.cloneObject(shape.header.annotation)],
            style: shape.header.style,
            offsetX: object.offsetX, offsetY: object.offsetY,
            height: shape.header.height,
            width: object.width
        };
        var wrapper = getHeaderWrapper(diagram, object, node, true);
        return wrapper;
    }
    function getHeaderWrapper(diagram, parent, object, isHeader) {
        var node = new node_1.Node(diagram, 'nodes', object, true);
        node.parentId = parent.id;
        node.isHeader = isHeader;
        node.constraints &= ~(enum_1.NodeConstraints.InConnect | enum_1.NodeConstraints.OutConnect);
        node.constraints |= enum_1.NodeConstraints.HideThumbs;
        diagram.initObject(node);
        diagram.nodes.push(node);
        Eif (node.wrapper.children.length > 0) {
            for (var i = 0; i < node.wrapper.children.length; i++) {
                var child = node.wrapper.children[parseInt(i.toString(), 10)];
                Eif (child instanceof diagram_element_1.DiagramElement) {
                    child.isCalculateDesiredSize = false;
                }
                if (child instanceof text_element_1.TextElement) {
                    child.canConsiderBounds = false;
                }
            }
            node.wrapper.measure(new size_1.Size(undefined, undefined));
            node.wrapper.arrange(node.wrapper.desiredSize);
        }
        node.wrapper.measure(new size_1.Size(undefined, undefined));
        node.wrapper.arrange(node.wrapper.desiredSize);
        return node.wrapper;
    }
    function updateIndex(diagram, source) {
        var childNode = source;
        var nodeindex = diagram.getIndex(childNode, childNode.id);
        var layerIndex = diagram.layers.indexOf(diagram.commandHandler.getObjectLayer(source.id));
        var layer = diagram.layers[parseInt(layerIndex.toString(), 10)];
        diagram.nodes.splice(Number(nodeindex), 1);
        delete layer.zIndexTable[childNode.zIndex];
        childNode.zIndex = layer.objectZIndex + 1;
        diagram.nodes.push(childNode);
        layer.zIndexTable[childNode.zIndex] = childNode.id;
    }
    function dropContainerChild(target, source, diagram) {
        var container = diagram.nameTable[target.id].shape;
        Iif (diagram.currentSymbol && target.shape.type === 'Container') {
            source.parentId = target.id;
            return;
        }
        container.children = container.children || [];
        var redoElement = base_util_1.cloneObject(source);
        var sources = diagram.nameTable[source.id].wrapper;
        var targetWrapper = diagram.nameTable[target.id].wrapper;
        sources.margin.top = (sources.offsetY - (sources.actualSize.height / 2))
            - (target.offsetY - (target.actualSize.height / 2));
        sources.margin.left = (sources.offsetX - (sources.actualSize.width / 2))
            - (target.offsetX - (target.actualSize.width / 2));
        diagram.nameTable[source.id].parentId = target.id;
        container.children.push(source.id);
        targetWrapper.children[0].children.push(diagram.nameTable[source.id].wrapper);
        var bound = getChildrenBound(target, source.id, diagram);
        adjustContainerSize(bound, source, diagram, false);
        if (sources.margin.left < 0) {
            sources.margin.left = 10;
        }
        if (sources.margin.top < 0 || (container.hasHeader && (sources.margin.top < container.header.height))) {
            sources.margin.top = 10;
            Eif (container.hasHeader) {
                sources.margin.top += container.header.height;
            }
        }
        targetWrapper.measure(new size_1.Size(undefined, undefined));
        targetWrapper.arrange(targetWrapper.desiredSize);
        var obj = base_util_1.cloneObject(source);
        var entry = {
            type: 'PositionChanged', undoObject: { nodes: [redoElement] },
            redoObject: { nodes: [obj] }, category: 'Internal'
        };
        diagram.addHistoryEntry(entry);
        Eif (diagram.mode === 'SVG') {
            if (source.zIndex < target.zIndex) {
                diagram.moveSvgNode(source.id);
                updateIndex(diagram, source);
            }
            var parent_1 = dom_util_1.getDiagramElement(target.id + '_groupElement');
            parent_1.appendChild(dom_util_1.getDiagramElement(source.id + '_groupElement'));
        }
    }
    exports.dropContainerChild = dropContainerChild;
    function getChildrenBound(node, excludeChild, diagram) {
        var children = node.shape.children;
        var bound;
        Eif (children && children.length) {
            for (var _i = 0; _i < children.length; _i++) {
                var i = children[parseInt(_i.toString(), 10)];
                if (excludeChild !== i) {
                    if (!bound) {
                        bound = diagram.nameTable['' + i].wrapper.bounds;
                    }
                    else {
                        bound = diagram.nameTable['' + i].wrapper.bounds.uniteRect(bound);
                    }
                }
            }
        }
        return bound || diagram.nameTable['' + excludeChild].wrapper.bounds;
    }
    exports.getChildrenBound = getChildrenBound;
    function adjustContainerSize(bound, obj, diagram, isDrag) {
        var diffX;
        var diffY;
        var node = diagram.nameTable[obj.parentId];
        var pivot = { x: 0.5, y: 0.5 };
        var actualSize = node.wrapper.actualSize;
        var headerHeight = 0;
        Eif (node.shape.hasHeader) {
            headerHeight = node.shape.header.height;
        }
        if ((node.wrapper.bounds.left + obj.margin.left + obj.width) > (node.wrapper.bounds.right)) {
            pivot.x = 0;
            diffX = (obj.wrapper.margin.left + obj.wrapper.bounds.width) / actualSize.width;
        }
        else if (obj.margin.left < 0) {
            pivot.x = 1;
            diffX = (obj.wrapper.bounds.x + obj.wrapper.bounds.width) / node.wrapper.bounds.x;
        }
        if ((node.wrapper.bounds.top + obj.margin.top + obj.height) > (node.wrapper.bounds.bottom)) {
            pivot.y = 0;
            diffY = (obj.wrapper.margin.top + obj.wrapper.bounds.height) / actualSize.height;
        }
        else if (obj.margin.top < headerHeight && !isDrag) {
            pivot.y = 1;
            diffY = (obj.wrapper.bounds.y + obj.wrapper.bounds.height) / node.wrapper.bounds.y;
        }
        if ((diffX > 0 || diffY > 0) && (node.constraints & enum_1.NodeConstraints.Resize)) {
            diagram.commandHandler.scale(diagram.nameTable[obj.parentId], diffX || 1, diffY || 1, pivot);
        }
    }
    exports.adjustContainerSize = adjustContainerSize;
    function dragContainerChild(obj, diagram, tx, ty) {
        var node = diagram.nameTable[(obj).parentId];
        var headerHeight = 0;
        Eif (node.shape.hasHeader) {
            headerHeight = node.shape.header.height;
        }
        var newMargin = {
            top: (obj.margin.top + ty) - headerHeight >= 0 ? obj.margin.top + ty : obj.margin.top,
            left: obj.margin.left + tx >= 0 ? obj.margin.left + tx : obj.margin.left
        };
        if (newMargin.top !== obj.margin.top || newMargin.left !== obj.margin.left) {
            diagram.nodePropertyChange(obj, {}, { margin: newMargin });
        }
        var bound = getChildrenBound(node, obj.id, diagram);
        adjustContainerSize(bound, obj, diagram, true);
        node.wrapper.measure(new size_1.Size(undefined, undefined));
        node.wrapper.arrange(node.wrapper.desiredSize);
        diagram.updateSelector();
        updateContainerDocks(node, diagram);
    }
    exports.dragContainerChild = dragContainerChild;
    function updateChildWrapper(node, diagram) {
        var containerChildren = diagram.nameTable[node.parentId].wrapper.children[0].children;
        for (var i = 0; i < containerChildren.length; i++) {
            if (containerChildren[parseInt(i.toString(), 10)].id === node.wrapper.id &&
                (containerChildren[parseInt(i.toString(), 10)].offsetX === node.wrapper.offsetX ||
                    containerChildren[parseInt(i.toString(), 10)].offsetY === node.wrapper.offsetY)) {
                containerChildren[parseInt(i.toString(), 10)] = node.wrapper;
                break;
            }
        }
    }
    exports.updateChildWrapper = updateChildWrapper;
    function updateContainerDocks(obj, diagram) {
        var childTable = obj.shape.children;
        if (childTable) {
            for (var _a = 0, childTable_1 = childTable; _a < childTable_1.length; _a++) {
                var i = childTable_1[_a];
                var actualObject = diagram.nameTable["" + i];
                Eif (actualObject) {
                    diagram.updateConnectorEdges(actualObject);
                    actualObject.wrapper.measure(new size_1.Size(actualObject.wrapper.width, actualObject.wrapper.height));
                    actualObject.wrapper.arrange(actualObject.wrapper.desiredSize);
                    if (actualObject.shape.children
                        && actualObject.shape.children.length) {
                        updateContainerDocks(actualObject, diagram);
                    }
                }
            }
        }
        else {
            return;
        }
    }
    exports.updateContainerDocks = updateContainerDocks;
    function removeChildFromContainer(currentObj, diagram) {
        var element = diagram.nameTable[currentObj.parentId];
        if (currentObj.shape.type === 'Container') {
            if (currentObj.shape.children && currentObj.shape.children.length > 0) {
                var children = currentObj.shape.children;
                for (var j = children.length - 1; j >= 0; j--) {
                    diagram.remove(diagram.nameTable[children[parseInt(j.toString(), 10)]]);
                }
            }
            if (currentObj.shape.hasHeader) {
                diagram.remove(diagram.nameTable[currentObj.id + currentObj.shape.header.id]);
            }
        }
        if (element) {
            diagram.removeDependentConnector(currentObj);
            var children = element.shape.children;
            removeGElement(element.wrapper, currentObj.id, diagram, true);
            if (children) {
                var childIndex = children.indexOf(currentObj.id);
                children.splice(childIndex, 1);
            }
        }
    }
    exports.removeChildFromContainer = removeChildFromContainer;
    function removeGElement(wrapper, name, diagram, isDelete) {
        for (var _a = 0, _b = wrapper.children; _a < _b.length; _a++) {
            var i = _b[_a];
            if (i.id === name) {
                wrapper.children.splice(wrapper.children.indexOf(i), 1);
                if (!isDelete) {
                    var element = document.getElementById(i.id + '_groupElement');
                    var diagramLayer = document.getElementById(diagram.element.id + '_diagramLayer');
                    var parent_2 = element.parentElement;
                    parent_2.removeChild(element);
                    diagramLayer.appendChild(element);
                }
            }
            else if (i.children) {
                removeGElement(i, name, diagram, isDelete);
            }
        }
    }
    exports.removeGElement = removeGElement;
    function addContainerChild(child, parentId, diagram) {
        var id = child.id;
        var node = diagram.nameTable["" + id];
        var undoElement = base_util_1.cloneObject(node);
        if (!node) {
            diagram.add(child);
            node = diagram.nameTable["" + id];
        }
        child.parentId = parentId;
        var parentContainer = diagram.nameTable["" + parentId];
        var containerShape = parentContainer.shape;
        if (!containerShape.children) {
            containerShape.children = [];
        }
        Eif (node && parentContainer) {
            node.parentId = parentId;
            if (node.margin.top <= 0 || (parentContainer.shape.hasHeader &&
                node.margin.top < parentContainer.shape.header.height)) {
                node.margin.top = 10;
                Eif (parentContainer.shape.hasHeader) {
                    node.margin.top += parentContainer.shape.header.height;
                }
            }
            if (node.margin.left <= 0) {
                node.margin.left = 10;
            }
            var nodeOffsetX = (parentContainer.offsetX - parentContainer.width * parentContainer.pivot.x) +
                (node.margin.left + node.width * node.pivot.x);
            var nodeOffsetY = (parentContainer.offsetY - parentContainer.height * parentContainer.pivot.y) +
                (node.margin.top + node.height * node.pivot.y);
            if (node.offsetX !== nodeOffsetX || node.offsetY !== nodeOffsetY) {
                node.offsetX = nodeOffsetX;
                node.offsetY = nodeOffsetY;
            }
            var children = containerShape.children;
            if (children.indexOf(id) < 0) {
                children.push(id);
            }
            parentContainer.wrapper.children.push(node.wrapper);
            parentContainer.wrapper.measure(new size_1.Size());
            parentContainer.wrapper.arrange(parentContainer.wrapper.desiredSize);
            diagram.refreshDiagramLayer();
            Iif (!(diagram.diagramActions & enum_1.DiagramAction.UndoRedo) && (!diagram.historyManager.currentEntry ||
                diagram.historyManager.currentEntry.type !== 'CollectionChanged')) {
                var obj = base_util_1.cloneObject(node);
                var entry = {
                    type: 'PositionChanged',
                    undoObject: { nodes: [undoElement] },
                    redoObject: { nodes: [obj] },
                    category: 'Internal'
                };
                diagram.addHistoryEntry(entry);
            }
            Eif (diagram.mode === 'SVG') {
                var childElement = dom_util_1.getDiagramElement(parentId + '_groupElement');
                childElement.appendChild(dom_util_1.getDiagramElement(child.id + '_groupElement'));
            }
        }
    }
    exports.addContainerChild = addContainerChild;
    function removeChild(node, diagram) {
        var id = node.id;
        var parent = diagram.nameTable[node.parentId];
        var children = parent.shape.children;
        removeGElement(parent.wrapper, id, diagram, true);
        var childrenIndex = children.indexOf(id);
        children.splice(childrenIndex, 1);
        node.parentId = '';
        diagram.refreshDiagramLayer();
        diagram.updateSelector();
    }
    exports.removeChild = removeChild;
});