all files / diagram/core/containers/ container.js

99.32% Statements 147/148
97.96% Branches 96/98
100% Functions 16/16
99.31% Lines 144/145
11 statements, 6 functions, 9 branches Ignored     
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          57298× 57298× 57298× 57298× 57298× 57298×   863869× 838679×   25190×   7717× 7717× 7717× 7717× 7717× 7717× 7717× 7717× 7712× 13207× 13207× 1193×   13207× 1164×   13207× 13207× 4823×   13207× 13207× 11686× 7587×     4099×     1521×               7712× 1007×       1007× 1007× 1007×   7712× 7595× 7595× 7595× 7595×   7712× 7595×     7717× 7717× 7717× 7717×   7717× 7717× 7717× 7717× 7595× 7595× 7595× 7595× 7595× 7595× 13090× 13090× 13090× 1404× 1404× 1404× 1396×     1404×   13090× 1404× 1404× 1404×   13090× 4706×   13090× 7595×     5495×         7717× 7717× 7717× 7717×   412843× 400083× 860795× 860795× 246150×   860795× 246168×   860795× 176894×         5556× 5556×   248909× 248909× 248909× 248909× 248909× 248909×   13207× 13207× 13207× 13207× 13207× 13207× 13207× 13207× 13207× 13207× 13207× 13207× 13207× 13207× 13207× 13207× 13207× 1906× 1906× 1906× 1906×   13207×        
/* istanbul ignore next */ 
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
define(["require", "exports", "../elements/diagram-element", "../../enum/enum", "../appearance", "../../primitives/size", "../../primitives/rect", "../../utility/base-util"], function (require, exports, diagram_element_1, enum_1, appearance_1, size_1, rect_1, base_util_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Container = (function (_super) {
        __extends(Container, _super);
        function Container() {
            var _this = _super !== null && _super.apply(this, arguments) || this;
            _this.padding = new appearance_1.Thickness(0, 0, 0, 0);
            _this.desiredBounds = undefined;
            _this.measureChildren = true;
            _this.prevRotateAngle = 0;
            return _this;
        }
        Container.prototype.hasChildren = function () {
            if (this.children !== undefined && this.children.length > 0) {
                return true;
            }
            return false;
        };
        Container.prototype.measure = function (availableSize, id, callback) {
            this.desiredBounds = undefined;
            var desired = undefined;
            var child;
            var center = { x: 0, y: 0 };
            var y;
            var x;
            var childBounds;
            if (this.hasChildren()) {
                for (var i = 0; i < this.children.length; i++) {
                    child = this.children[parseInt(i.toString(), 10)];
                    if (child.horizontalAlignment === 'Stretch' && !availableSize.width) {
                        availableSize.width = child.bounds.width;
                    }
                    if (child.verticalAlignment === 'Stretch' && !availableSize.height) {
                        availableSize.height = child.bounds.height;
                    }
                    var force = child.horizontalAlignment === 'Stretch' || child.verticalAlignment === 'Stretch';
                    if (this.measureChildren || force || (child instanceof Container && child.measureChildren !== undefined)) {
                        child.measure(availableSize, id, callback);
                    }
                    childBounds = this.GetChildrenBounds(child);
                    if (child.horizontalAlignment !== 'Stretch' && child.verticalAlignment !== 'Stretch') {
                        if (this.desiredBounds === undefined) {
                            this.desiredBounds = childBounds;
                        }
                        else {
                            this.desiredBounds.uniteRect(childBounds);
                        }
                    }
                    else if (this.actualSize && !this.actualSize.width && !this.actualSize.height &&
                        !child.preventContainer && child.horizontalAlignment === 'Stretch' && child.verticalAlignment === 'Stretch') {
                        Eif (this.desiredBounds === undefined) {
                            this.desiredBounds = child.bounds;
                        }
                        else {
                            this.desiredBounds.uniteRect(child.bounds);
                        }
                    }
                }
                if (this.desiredBounds !== undefined && this.rotateAngle !== 0) {
                    var offsetPt = {
                        x: this.desiredBounds.x + this.desiredBounds.width * this.pivot.x,
                        y: this.desiredBounds.y + this.desiredBounds.height * this.pivot.y
                    };
                    var newPoint = base_util_1.rotatePoint(this.rotateAngle, undefined, undefined, offsetPt);
                    this.desiredBounds.x = newPoint.x - this.desiredBounds.width * this.pivot.x;
                    this.desiredBounds.y = newPoint.y - this.desiredBounds.height * this.pivot.y;
                }
                if (this.desiredBounds !== undefined) {
                    this.desiredBounds.width += this.padding.left + this.padding.right;
                    this.desiredBounds.height += this.padding.top + this.padding.bottom;
                    this.desiredBounds.x -= this.padding.left;
                    this.desiredBounds.y -= this.padding.top;
                }
                if (this.desiredBounds) {
                    desired = new size_1.Size(this.desiredBounds.width, this.desiredBounds.height);
                }
            }
            desired = this.validateDesiredSize(desired, availableSize);
            this.stretchChildren(desired);
            this.desiredSize = desired;
            return desired;
        };
        Container.prototype.arrange = function (desiredSize) {
            var child;
            var bounds;
            var childBounds = this.desiredBounds;
            if (childBounds) {
                var x = this.offsetX;
                var y = this.offsetY;
                this.offsetX = childBounds.x + childBounds.width * this.pivot.x;
                this.offsetY = childBounds.y + childBounds.height * this.pivot.y;
                Eif (this.hasChildren()) {
                    for (var i = 0; i < this.children.length; i++) {
                        child = this.children[parseInt(i.toString(), 10)];
                        var arrange = false;
                        if (child.horizontalAlignment === 'Stretch') {
                            child.offsetX = this.offsetX;
                            child.parentTransform = this.parentTransform + this.rotateAngle;
                            if (this.flip && (this.elementActions & enum_1.ElementAction.ElementIsGroup)) {
                                child.parentTransform = (this.flip === 'Horizontal' || this.flip === 'Vertical') ?
                                    -child.parentTransform : child.parentTransform;
                            }
                            arrange = true;
                        }
                        if (child.verticalAlignment === 'Stretch') {
                            child.offsetY = this.offsetY;
                            child.parentTransform = this.parentTransform + this.rotateAngle;
                            arrange = true;
                        }
                        if (arrange || this.measureChildren || (child instanceof Container && child.measureChildren !== undefined)) {
                            child.arrange(child.desiredSize);
                        }
                        if (i === 0) {
                            this.outerBounds = child.outerBounds;
                        }
                        else {
                            this.outerBounds.uniteRect(child.outerBounds);
                        }
                    }
                }
            }
            this.actualSize = desiredSize;
            this.updateBounds();
            this.prevRotateAngle = this.rotateAngle;
            return desiredSize;
        };
        Container.prototype.stretchChildren = function (size) {
            if (this.hasChildren()) {
                for (var _i = 0, _a = this.children; _i < _a.length; _i++) {
                    var child = _a[_i];
                    if (child.horizontalAlignment === 'Stretch' || child.desiredSize.width === undefined) {
                        child.desiredSize.width = size.width - child.margin.left - child.margin.right;
                    }
                    if (child.verticalAlignment === 'Stretch' || child.desiredSize.height === undefined) {
                        child.desiredSize.height = size.height - child.margin.top - child.margin.bottom;
                    }
                    if (child instanceof Container) {
                        child.stretchChildren(child.desiredSize);
                    }
                }
            }
        };
        Container.prototype.applyPadding = function (size) {
            size.width += this.padding.left + this.padding.right;
            size.height += this.padding.top + this.padding.bottom;
        };
        Container.prototype.findChildOffsetFromCenter = function (child, center) {
            var topLeft = { x: center.x - child.desiredSize.width / 2, y: center.y - child.desiredSize.height / 2 };
            var offset = base_util_1.getOffset(topLeft, child);
            offset = base_util_1.rotatePoint(child.rotateAngle, center.x, center.y, offset);
            offset = base_util_1.rotatePoint(this.rotateAngle + this.parentTransform, this.offsetX, this.offsetY, offset);
            child.offsetX = offset.x;
            child.offsetY = offset.y;
        };
        Container.prototype.GetChildrenBounds = function (child) {
            var childBounds;
            var childSize = child.desiredSize.clone();
            var diffAngle = child.rotateAngle - this.rotateAngle;
            var refPoint = { x: child.offsetX, y: child.offsetY };
            var left = refPoint.x - childSize.width * child.pivot.x;
            var top = refPoint.y - childSize.height * child.pivot.y;
            var right = left + childSize.width;
            var bottom = top + childSize.height;
            var topLeft = { x: left, y: top };
            var topRight = { x: right, y: top };
            var bottomLeft = { x: left, y: bottom };
            var bottomRight = { x: right, y: bottom };
            topLeft = base_util_1.rotatePoint(child.rotateAngle, child.offsetX, child.offsetY, topLeft);
            topRight = base_util_1.rotatePoint(child.rotateAngle, child.offsetX, child.offsetY, topRight);
            bottomLeft = base_util_1.rotatePoint(child.rotateAngle, child.offsetX, child.offsetY, bottomLeft);
            bottomRight = base_util_1.rotatePoint(child.rotateAngle, child.offsetX, child.offsetY, bottomRight);
            if (this.rotateAngle !== 0) {
                topLeft = base_util_1.rotatePoint(-this.rotateAngle, undefined, undefined, topLeft);
                topRight = base_util_1.rotatePoint(-this.rotateAngle, undefined, undefined, topRight);
                bottomLeft = base_util_1.rotatePoint(-this.rotateAngle, undefined, undefined, bottomLeft);
                bottomRight = base_util_1.rotatePoint(-this.rotateAngle, undefined, undefined, bottomRight);
            }
            return rect_1.Rect.toBounds([topLeft, topRight, bottomLeft, bottomRight]);
        };
        return Container;
    }(diagram_element_1.DiagramElement));
    exports.Container = Container;
});