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

96.13% Statements 149/155
89.86% Branches 124/138
100% Functions 14/14
96.05% Lines 146/152
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 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          53057× 53057× 53057×   239775× 239775× 239775× 231900× 515726× 515726× 79476× 73468× 73468×     436250× 436250×   515726× 515726× 125336×   515726× 515726× 515726× 515726× 71682× 71682× 71342×     444384× 428480× 231742×     196738×       231900× 231742× 231742× 231742× 231742× 231742×     239775× 239775× 239775× 239775× 239775× 239775×   162854× 162854× 159344× 159344× 159344× 394575× 394575× 259386× 259386× 5166×   81×     259386× 259386× 259386× 259386× 259386× 259386× 173577× 173577× 77712× 77712×     259386× 85809× 85809×     173577×   259386× 259386×   394575× 838×     393737× 44311× 44311×   349426× 343418× 343418×         162854× 162854× 162854× 162854×   81× 37×       44×   13×       31×   30×           28×       23×           85809×     1875× 1875×   27× 27×     83907× 83907×   85809×     78× 78×   1883× 1883×     83848× 83848×   85809×   173577× 173577× 173577×     98343× 98343×     75007× 75007×   204× 204×   173577×     96123× 96123×     75184× 75184×   2247× 2247×   173577×        
/* 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", "./container", "../elements/diagram-element", "../../utility/base-util", "../../enum/enum", "../../primitives/size", "../../primitives/rect", "../elements/text-element", "../elements/image-element", "../elements/path-element"], function (require, exports, container_1, diagram_element_1, base_util_1, enum_1, size_1, rect_1, text_element_1, image_element_1, path_element_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Canvas = (function (_super) {
        __extends(Canvas, _super);
        function Canvas() {
            var _this = _super !== null && _super.apply(this, arguments) || this;
            _this.measureChildren = undefined;
            return _this;
        }
        Canvas.prototype.measure = function (availableSize, id, callback) {
            var desired = undefined;
            var desiredBounds = undefined;
            if (this.hasChildren()) {
                for (var _i = 0, _a = this.children; _i < _a.length; _i++) {
                    var child = _a[_i];
                    if (child instanceof text_element_1.TextElement) {
                        if (child.canMeasure) {
                            availableSize.width = availableSize.width || this.maxWidth || this.minWidth;
                            child.measure(availableSize);
                        }
                    }
                    else Eif (!(child instanceof text_element_1.TextElement)) {
                        child.measure(availableSize, id, callback);
                    }
                    var childSize = child.desiredSize.clone();
                    if (child.rotateAngle !== 0) {
                        childSize = base_util_1.rotateSize(childSize, child.rotateAngle);
                    }
                    var right = childSize.width + child.margin.right;
                    var bottom = childSize.height + child.margin.bottom;
                    var childBounds = new rect_1.Rect(child.margin.left, child.margin.top, right, bottom);
                    if (child.float) {
                        var position = child.getAbsolutePosition(childSize);
                        if (position !== undefined) {
                            continue;
                        }
                    }
                    if ((!(child instanceof text_element_1.TextElement)) || (child instanceof text_element_1.TextElement && child.canConsiderBounds)) {
                        if (desiredBounds === undefined) {
                            desiredBounds = childBounds;
                        }
                        else {
                            desiredBounds.uniteRect(childBounds);
                        }
                    }
                }
                if (desiredBounds) {
                    var leftMargin = 0;
                    var topMargin = 0;
                    leftMargin = Math.max(desiredBounds.left, 0);
                    topMargin = Math.max(desiredBounds.top, 0);
                    desired = new size_1.Size(desiredBounds.width + leftMargin, desiredBounds.height + topMargin);
                }
            }
            desired = _super.prototype.validateDesiredSize.call(this, desired, availableSize);
            _super.prototype.stretchChildren.call(this, desired);
            desired.width += this.padding.left + this.padding.right;
            desired.height += this.padding.top + this.padding.bottom;
            this.desiredSize = desired;
            return desired;
        };
        Canvas.prototype.arrange = function (desiredSize, isStack) {
            this.outerBounds = new rect_1.Rect();
            if (this.hasChildren()) {
                var y = this.offsetY - desiredSize.height * this.pivot.y + this.padding.top;
                var x = this.offsetX - desiredSize.width * this.pivot.x + this.padding.left;
                for (var _i = 0, _a = this.children; _i < _a.length; _i++) {
                    var child = _a[_i];
                    if ((child.transform & enum_1.Transform.Parent) !== 0) {
                        child.parentTransform = this.parentTransform + this.rotateAngle;
                        if (this.flip !== enum_1.FlipDirection.None || this.elementActions & enum_1.ElementAction.ElementIsGroup) {
                            if ((this.flip === enum_1.FlipDirection.Horizontal || this.flip === enum_1.FlipDirection.Vertical) &&
                                !(child.elementActions & enum_1.ElementAction.ElementIsPort)) {
                                this.setParentTransform(child);
                            }
                        }
                        var childSize = child.desiredSize.clone();
                        var topLeft = void 0;
                        var center = { x: 0, y: 0 };
                        var childX = x;
                        var childY = y;
                        if (child.relativeMode === 'Point') {
                            var position = child.getAbsolutePosition(desiredSize);
                            if (position !== undefined) {
                                childX += position.x;
                                childY += position.y;
                            }
                        }
                        if (child.relativeMode === 'Object') {
                            topLeft = this.alignChildBasedOnParent(child, childSize, desiredSize, childX, childY);
                            child.flip = this.flip;
                        }
                        else {
                            topLeft = this.alignChildBasedOnaPoint(child, childX, childY);
                        }
                        center = { x: topLeft.x + childSize.width / 2, y: topLeft.y + childSize.height / 2 };
                        _super.prototype.findChildOffsetFromCenter.call(this, child, center);
                    }
                    if (isStack && (child.horizontalAlignment === 'Stretch' || child.verticalAlignment === 'Stretch')) {
                        child.arrange(desiredSize);
                    }
                    else {
                        if (child instanceof text_element_1.TextElement && child.canMeasure) {
                            child.arrange(child.desiredSize);
                            this.outerBounds.uniteRect(child.outerBounds);
                        }
                        else if (!(child instanceof text_element_1.TextElement)) {
                            child.arrange(child.desiredSize);
                            this.outerBounds.uniteRect(child.outerBounds);
                        }
                    }
                }
            }
            this.actualSize = desiredSize;
            this.updateBounds();
            this.outerBounds.uniteRect(this.bounds);
            return desiredSize;
        };
        Canvas.prototype.setParentTransform = function (child) {
            if (this.flipMode === 'All' || child instanceof image_element_1.ImageElement) {
                Iif (child.parentTransform > 0) {
                    child.parentTransform = -child.parentTransform;
                }
            }
            else if (!(child instanceof image_element_1.ImageElement || child instanceof text_element_1.TextElement || child instanceof path_element_1.PathElement)
                && child instanceof diagram_element_1.DiagramElement) {
                Iif (child.parentTransform > 0) {
                    child.parentTransform = -child.parentTransform;
                }
            }
            else if (child instanceof text_element_1.TextElement && child.position === undefined) {
                child.parentTransform = -child.parentTransform;
            }
            else if (this.flipMode === 'None') {
                Iif (!(child.elementActions & enum_1.ElementAction.ElementIsPort) && child instanceof path_element_1.PathElement) {
                    if (child.parentTransform > 0) {
                        child.parentTransform = -child.parentTransform;
                    }
                }
            }
            else if (child instanceof path_element_1.PathElement) {
                Iif (child.parentTransform > 0) {
                    child.parentTransform = -child.parentTransform;
                }
            }
            else if (this.flipMode === 'LabelText' || this.flipMode === 'LabelAndLabelText' || this.flipMode === 'PortAndLabelText') {
                Eif (child instanceof text_element_1.TextElement) {
                    Iif (child.parentTransform > 0) {
                        child.parentTransform = -child.parentTransform;
                    }
                }
            }
        };
        Canvas.prototype.alignChildBasedOnParent = function (child, childSize, parentSize, x, y) {
            switch (child.horizontalAlignment) {
                case 'Auto':
                case 'Left':
                    x += child.margin.left;
                    break;
                case 'Right':
                    x += parentSize.width - childSize.width - child.margin.right;
                    break;
                case 'Stretch':
                case 'Center':
                    x += parentSize.width / 2 - childSize.width / 2;
                    break;
            }
            switch (child.verticalAlignment) {
                case 'Auto':
                case 'Top':
                    y += child.margin.top;
                    break;
                case 'Bottom':
                    y += parentSize.height - childSize.height - child.margin.bottom;
                    break;
                case 'Stretch':
                case 'Center':
                    y += parentSize.height / 2 - childSize.height / 2;
                    break;
            }
            return { x: x, y: y };
        };
        Canvas.prototype.alignChildBasedOnaPoint = function (child, x, y) {
            x += child.margin.left - child.margin.right;
            y += child.margin.top - child.margin.bottom;
            switch (child.horizontalAlignment) {
                case 'Auto':
                case 'Left':
                    x = child.inversedAlignment ? x : (x - child.desiredSize.width);
                    break;
                case 'Stretch':
                case 'Center':
                    x -= child.desiredSize.width * child.pivot.x;
                    break;
                case 'Right':
                    x = child.inversedAlignment ? (x - child.desiredSize.width) : x;
                    break;
            }
            switch (child.verticalAlignment) {
                case 'Auto':
                case 'Top':
                    y = child.inversedAlignment ? y : (y - child.desiredSize.height);
                    break;
                case 'Stretch':
                case 'Center':
                    y -= child.desiredSize.height * child.pivot.y;
                    break;
                case 'Bottom':
                    y = child.inversedAlignment ? (y - child.desiredSize.height) : y;
                    break;
            }
            return { x: x, y: y };
        };
        return Canvas;
    }(container_1.Container));
    exports.Canvas = Canvas;
});