all files / utility/ base-util.js

97.28% Statements 143/147
90.63% Branches 29/32
100% Functions 12/12
97.28% Lines 143/147
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                                 82× 82× 82× 82× 82× 82× 82× 82× 82× 82× 82× 82× 82× 82× 82×         82× 82× 82× 82× 82× 82× 82× 82× 82× 82× 82× 82×       82× 13× 13× 13× 13× 13× 13× 13× 13× 13× 13×     82× 82× 82× 82× 82× 82× 82× 82× 82×     45× 45×   35× 35×       45×     46× 46×       41× 41×     46×   508×   508×   508× 508× 508× 508×   508×   508× 508×         46×     46× 46×     33× 33×     46×     505× 245× 245× 245×   260×     10× 10× 10×          
define(["require", "exports", "../primitives/rect", "../primitives/matrix", "./dom-util", "../primitives/size"], function (require, exports, rect_1, matrix_1, dom_util_1, size_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    /**
     * Implements the basic functionalities
     */
    /** @private */
    function randomId() {
        var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz';
        var id = '';
        var num;
        for (var i = 0; i < 5; i++) {
            Eif ('crypto' in window && 'getRandomValues' in crypto) {
                var count = new Uint16Array(1);
                // tslint:disable-next-line:no-any
                var intCrypto = window.msCrypto || window.crypto;
                num = intCrypto.getRandomValues(count)[0] % (chars.length - 1);
            }
            else {
                num = Math.floor(Math.random() * chars.length);
            }
            Iif (i === 0 && num < 10) {
                i--;
                continue;
            }
            id += chars.substring(num, num + 1);
        }
        return id;
    }
    exports.randomId = randomId;
    /** @private */
    function cornersPointsBeforeRotation(ele) {
        var bounds = new rect_1.Rect();
        var top = ele.offsetY - ele.actualSize.height * ele.pivot.y;
        var bottom = ele.offsetY + ele.actualSize.height * (1 - ele.pivot.y);
        var left = ele.offsetX - ele.actualSize.width * ele.pivot.x;
        var right = ele.offsetX + ele.actualSize.width * (1 - ele.pivot.x);
        var topLeft = { x: left, y: top };
        var topCenter = { x: (left + right) / 2, y: top };
        var topRight = { x: right, y: top };
        var middleLeft = { x: left, y: (top + bottom) / 2 };
        var middleRight = { x: right, y: (top + bottom) / 2 };
        var bottomLeft = { x: left, y: bottom };
        var bottomCenter = { x: (left + right) / 2, y: bottom };
        var bottomRight = { x: right, y: bottom };
        bounds = rect_1.Rect.toBounds([topLeft, topRight, bottomLeft, bottomRight]);
        return bounds;
    }
    exports.cornersPointsBeforeRotation = cornersPointsBeforeRotation;
    /** @private */
    function rotateSize(size, angle) {
        var matrix = matrix_1.identityMatrix();
        matrix_1.rotateMatrix(matrix, angle, 0, 0);
        var topLeft = matrix_1.transformPointByMatrix(matrix, { x: 0, y: 0 });
        var topRight = matrix_1.transformPointByMatrix(matrix, { x: size.width, y: 0 });
        var bottomLeft = matrix_1.transformPointByMatrix(matrix, { x: 0, y: size.height });
        var bottomRight = matrix_1.transformPointByMatrix(matrix, { x: size.width, y: size.height });
        var minX = Math.min(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x);
        var minY = Math.min(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);
        var maxX = Math.max(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x);
        var maxY = Math.max(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);
        return new size_1.Size(maxX - minX, maxY - minY);
    }
    exports.rotateSize = rotateSize;
    /** @private */
    function getBounds(element) {
        var bounds = new rect_1.Rect();
        var corners;
        corners = cornersPointsBeforeRotation(element);
        var middleLeft = corners.middleLeft;
        var topCenter = corners.topCenter;
        var bottomCenter = corners.bottomCenter;
        var middleRight = corners.middleRight;
        var topLeft = corners.topLeft;
        var topRight = corners.topRight;
        var bottomLeft = corners.bottomLeft;
        var bottomRight = corners.bottomRight;
        element.corners = {
            topLeft: topLeft, topCenter: topCenter, topRight: topRight, middleLeft: middleLeft,
            middleRight: middleRight, bottomLeft: bottomLeft, bottomCenter: bottomCenter, bottomRight: bottomRight
        };
        if (element.rotateAngle !== 0 || element.parentTransform !== 0) {
            var matrix = matrix_1.identityMatrix();
            matrix_1.rotateMatrix(matrix, element.rotateAngle + element.parentTransform, element.offsetX, element.offsetY);
            element.corners.topLeft = topLeft = matrix_1.transformPointByMatrix(matrix, topLeft);
            element.corners.topCenter = topCenter = matrix_1.transformPointByMatrix(matrix, topCenter);
            element.corners.topRight = topRight = matrix_1.transformPointByMatrix(matrix, topRight);
            element.corners.middleLeft = middleLeft = matrix_1.transformPointByMatrix(matrix, middleLeft);
            element.corners.middleRight = middleRight = matrix_1.transformPointByMatrix(matrix, middleRight);
            element.corners.bottomLeft = bottomLeft = matrix_1.transformPointByMatrix(matrix, bottomLeft);
            element.corners.bottomCenter = bottomCenter = matrix_1.transformPointByMatrix(matrix, bottomCenter);
            element.corners.bottomRight = bottomRight = matrix_1.transformPointByMatrix(matrix, bottomRight);
            //Set corners based on rotate angle
        }
        bounds = rect_1.Rect.toBounds([topLeft, topRight, bottomLeft, bottomRight]);
        element.corners.left = bounds.left;
        element.corners.right = bounds.right;
        element.corners.top = bounds.top;
        element.corners.bottom = bounds.bottom;
        element.corners.center = bounds.center;
        element.corners.width = bounds.width;
        element.corners.height = bounds.height;
        return bounds;
    }
    exports.getBounds = getBounds;
    /** @private */
    function textAlignToString(value) {
        var state = '';
        switch (value) {
            case 'Center':
                state = 'center';
                break;
            case 'Left':
                state = 'left';
                break;
            case 'Right':
                state = 'right';
                break;
        }
        return state;
    }
    exports.textAlignToString = textAlignToString;
    /** @private */
    function wordBreakToString(value) {
        var state = '';
        switch (value) {
            case 'Wrap':
                state = 'breakall';
                break;
            case 'NoWrap':
                state = 'keepall';
                break;
            case 'WrapWithOverflow':
                state = 'normal';
                break;
            case 'LineThrough':
                state = 'line-through';
                break;
        }
        return state;
    }
    exports.wordBreakToString = wordBreakToString;
    function bBoxText(textContent, options) {
        var measureElement = 'measureElement';
        // eslint-disable-next-line
        window[measureElement].style.visibility = 'visible';
        // eslint-disable-next-line
        var svg = window[measureElement].children[2];
        var text = dom_util_1.getChildNode(svg)[1];
        text.textContent = textContent;
        text.setAttribute('style', 'font-size:' + options.fontSize + 'px; font-family:'
            + options.fontFamily + ';font-weight:' + (options.bold ? 'bold' : 'normal'));
        var bBox = text.getBBox().width;
        // eslint-disable-next-line
        window[measureElement].style.visibility = 'hidden';
        return bBox;
    }
    exports.bBoxText = bBoxText;
    /** @private */
    function middleElement(i, j) {
        var m = 0;
        m = (i + j) / 2;
        return m;
    }
    exports.middleElement = middleElement;
    /** @private */
    function whiteSpaceToString(value, wrap) {
        Iif (wrap === 'NoWrap' && value === 'PreserveAll') {
            return 'pre';
        }
        var state = '';
        switch (value) {
            case 'CollapseAll':
                state = 'nowrap';
                break;
            case 'CollapseSpace':
                state = 'pre-line';
                break;
            case 'PreserveAll':
                state = 'pre-wrap';
                break;
        }
        return state;
    }
    exports.whiteSpaceToString = whiteSpaceToString;
    /** @private */
    function rotatePoint(angle, pivotX, pivotY, point) {
        if (angle !== 0) {
            var matrix = matrix_1.identityMatrix();
            matrix_1.rotateMatrix(matrix, angle, pivotX, pivotY);
            return matrix_1.transformPointByMatrix(matrix, point);
        }
        return point;
    }
    exports.rotatePoint = rotatePoint;
    /** @private */
    function getOffset(topLeft, obj) {
        var offX = topLeft.x + obj.desiredSize.width * obj.pivot.x;
        var offY = topLeft.y + obj.desiredSize.height * obj.pivot.y;
        return {
            x: offX, y: offY
        };
    }
    exports.getOffset = getOffset;
});