all files / utility/ dom-util.js

77.11% Statements 219/284
63.24% Branches 86/136
88.89% Functions 16/18
77.11% Lines 219/284
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 421 422 423 424 425 426 427 428         540× 540× 540×                 540×   540×                         42×   42×     42× 32×   32×   32× 32× 32×   32×   32×   19×       13× 13×         32×   32× 32×   10×                                                               22×                 22× 22× 22× 22× 22× 22× 22× 22× 22× 22× 22× 22× 22× 22× 22× 22× 22×   22× 22× 22× 22× 22× 22× 20× 63× 63×         63×       63× 63×   63×           19×         22×   19× 19× 19× 19× 19× 19× 19× 19× 19× 19× 19× 19× 19× 19× 26× 26× 26× 177× 177×               177× 177× 177× 177× 12×   177× 26× 26×       26×     151× 26× 26×     177× 177×       19×                                                           22× 22× 22× 22× 22× 58× 58× 58×   53× 47×       47×             58× 58× 58×             58× 58×     22×     22× 22× 22× 22× 22× 22× 22× 22×     22× 22×                                                                                
define(["require", "exports", "../primitives/rect", "../primitives/size", "./base-util", "../primitives/matrix", "@syncfusion/ej2-base"], function (require, exports, rect_1, size_1, base_util_1, matrix_1, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    /**
     * Defines the functionalities that need to access DOM
     */
    function getChildNode(node) {
        var child;
        var collection = [];
        Iif (ej2_base_1.Browser.info.name === 'msie' || ej2_base_1.Browser.info.name === 'edge') {
            for (var i = 0; i < node.childNodes.length; i++) {
                child = node.childNodes[parseInt(i.toString(), 10)];
                if (child.nodeType === 1) {
                    collection.push(child);
                }
            }
        }
        else {
            collection = node.children;
        }
        return collection;
    }
    exports.getChildNode = getChildNode;
    function translatePoints(element, points) {
        var translatedPts = [];
        for (var _i = 0, points_1 = points; _i < points_1.length; _i++) {
            var point = points_1[_i];
            var pt1 = {
                x: element.offsetX - element.actualSize.width * element.pivot.x + point.x,
                y: element.offsetY - element.actualSize.height * element.pivot.y + point.y
            };
            var matrix = void 0;
            var angle = element.rotateAngle + element.parentTransform;
            Iif (angle) {
                matrix = matrix_1.identityMatrix();
                matrix_1.rotateMatrix(matrix, angle, element.offsetX, element.offsetY);
            }
            Iif (matrix) {
                pt1 = matrix_1.transformPointByMatrix(matrix, pt1);
            }
            translatedPts.push(pt1);
        }
        return translatedPts;
    }
    exports.translatePoints = translatePoints;
    /** @private */
    function measurePath(data) {
        var path = 'pathTable';
        // eslint-disable-next-line
        if (!window[path]) {
            // eslint-disable-next-line
            window[path] = {};
        }
        if (data) {
            var measureElement = 'measureElement';
            // eslint-disable-next-line
            window[measureElement].style.visibility = 'visible';
            // eslint-disable-next-line
            var svg = window[measureElement].children[2];
            var element = getChildNode(svg)[0];
            element.setAttribute('d', data);
            //let bounds: SVGRect = element.getBBox();
            var bounds = void 0;
            // eslint-disable-next-line
            if (window[path][data]) {
                // eslint-disable-next-line
                bounds = window[path][data];
            }
            else {
                // eslint-disable-next-line
                window[path][data] = bounds = element.getBBox();
                Iif ((bounds.x === 0 || bounds.y === 0) && (bounds.width === 0 || bounds.height === 0)) {
                    // eslint-disable-next-line
                    window[path][data] = bounds = getBBox(data);
                }
            }
            var svgBounds = new rect_1.Rect(bounds.x, bounds.y, bounds.width, bounds.height);
            // eslint-disable-next-line
            window[measureElement].style.visibility = 'hidden';
            return svgBounds;
        }
        return new rect_1.Rect(0, 0, 0, 0);
    }
    exports.measurePath = measurePath;
    // tslint:disable-next-line
    function getBBox(path) {
        var xmin = 0;
        var xmax = 0;
        var ymin = 0;
        var ymax = 0;
        // tslint:disable-next-line
        var currentValue;
        // tslint:disable-next-line
        var currentpath = path;
        currentpath = currentpath.replace(/[a-z].*/g, ' ').replace(/[\sA-Z]+/gi, ' ').trim().split(' ');
        for (var i = 0; i < currentpath.length; i++) {
            if (currentpath[parseInt(i.toString(), 10)].length > 1) {
                currentValue = currentpath[parseInt(i.toString(), 10)].split(',');
                xmin = xmax = currentValue[0];
                ymin = ymax = currentValue[1];
            }
        }
        for (var i = 0; i < currentpath.length; i++) {
            currentValue = currentpath[parseInt(i.toString(), 10)].split(',');
            if (!currentValue[1]) {
                currentValue[0] = xmin;
                currentValue[1] = ymin;
            }
            xmin = Math.min(xmin, currentValue[0]);
            xmax = Math.max(xmax, currentValue[0]);
            ymin = Math.min(ymin, currentValue[1]);
            ymax = Math.max(ymax, currentValue[1]);
        }
        return { x: xmin, y: ymin, width: xmax - xmin, height: ymax - ymin };
    }
    function getTextOptions(element, maxWidth) {
        var options = {
            fill: element.style.fill, stroke: element.style.strokeColor, angle: element.rotateAngle + element.parentTransform,
            pivotX: element.pivot.x, pivotY: element.pivot.y, strokeWidth: element.style.strokeWidth,
            dashArray: element.style.strokeDashArray, opacity: element.style.opacity,
            visible: element.visible, id: element.id,
            width: maxWidth || element.actualSize.width, height: element.actualSize.height,
            x: element.offsetX - element.actualSize.width * element.pivot.x + 0.5,
            y: element.offsetY - element.actualSize.height * element.pivot.y + 0.5
        };
        options.fontSize = element.style.fontSize;
        options.fontFamily = element.style.fontFamily;
        options.textOverflow = element.style.textOverflow;
        options.textDecoration = element.style.textDecoration;
        options.doWrap = element.doWrap;
        options.whiteSpace = base_util_1.whiteSpaceToString(element.style.whiteSpace, element.style.textWrapping);
        options.content = element.content;
        options.textWrapping = element.style.textWrapping;
        options.breakWord = base_util_1.wordBreakToString(element.style.textWrapping);
        options.textAlign = base_util_1.textAlignToString(element.style.textAlign);
        options.color = element.style.color;
        options.italic = element.style.italic;
        options.bold = element.style.bold;
        options.dashArray = '';
        options.strokeWidth = 0;
        options.fill = '';
        return options;
    }
    function wrapSvgText(text, textValue) {
        var childNodes = [];
        var k = 0;
        var txtValue;
        var bounds1;
        var content = textValue || text.content;
        if (text.whiteSpace !== 'nowrap' && text.whiteSpace !== 'pre') {
            if (text.breakWord === 'breakall') {
                txtValue = '';
                txtValue += content[0];
                for (k = 0; k < content.length; k++) {
                    bounds1 = base_util_1.bBoxText(txtValue, text);
                    Iif (bounds1 >= text.width && txtValue.length > 0) {
                        childNodes[childNodes.length] = { text: txtValue, x: 0, dy: 0, width: bounds1 };
                        txtValue = '';
                    }
                    else {
                        txtValue = txtValue + (content[k + 1] || '');
                        // if (txtValue.indexOf('\n') > -1) {
                        //     txtValue = txtValue.replace('\n', '');
                        // }
                        var width = base_util_1.bBoxText(txtValue, text);
                        if ((Math.ceil(width) + 2 >= text.width && txtValue.length > 0) || (txtValue.indexOf('\n') > -1)) {
                            childNodes[childNodes.length] = { text: txtValue, x: 0, dy: 0, width: width };
                            txtValue = '';
                        }
                        if (k === content.length - 1 && txtValue.length > 0) {
                            childNodes[childNodes.length] = { text: txtValue, x: 0, dy: 0, width: width };
                            txtValue = '';
                        }
                    }
                }
            }
            else {
                childNodes = wordWrapping(text, textValue);
            }
        }
        else {
            childNodes[childNodes.length] = { text: content, x: 0, dy: 0, width: base_util_1.bBoxText(content, text) };
        }
        return childNodes;
    }
    function wordWrapping(text, textValue) {
        var childNodes = [];
        var txtValue = '';
        var j = 0;
        var i = 0;
        var wrap = text.whiteSpace !== 'nowrap' ? true : false;
        var content = textValue || text.content;
        var bounds1;
        var eachLine = content.split('\n');
        var txt;
        var words;
        var newText;
        var existingWidth;
        var existingText;
        for (j = 0; j < eachLine.length; j++) {
            txt = '';
            words = text.textWrapping !== 'NoWrap' ? eachLine[parseInt(j.toString(), 10)].split(' ') : eachLine;
            for (i = 0; i < words.length; i++) {
                bounds1 = base_util_1.bBoxText(words[parseInt(i.toString(), 10)], text);
                Iif (bounds1 > text.width && words[parseInt(i.toString(), 10)].length > 0 && text.textWrapping !== 'NoWrap') {
                    if (eachLine.length > 1) {
                        words[parseInt(i.toString(), 10)] = words[parseInt(i.toString(), 10)] + '\n';
                    }
                    text.content = words[parseInt(i.toString(), 10)];
                    childNodes = wrapText(text, txtValue, childNodes);
                }
                else {
                    txtValue += (((i !== 0 || words.length === 1) && wrap && txtValue.length > 0) ? ' ' : '') + words[parseInt(i.toString(), 10)];
                    newText = txtValue + (words[i + 1] || '');
                    var width = base_util_1.bBoxText(newText, text);
                    if (eachLine.length > 1 && i === words.length - 1) {
                        txtValue = txtValue + '\n';
                    }
                    if (Math.floor(width) > text.width - 2 && txtValue.length > 0) {
                        textValue = txtValue;
                        childNodes[childNodes.length] = {
                            text: (txtValue.indexOf('\n') === -1) ? txtValue + ' ' : textValue, x: 0, dy: 0,
                            width: newText === txtValue ? width : (txtValue === existingText) ? existingWidth : base_util_1.bBoxText(txtValue, text)
                        };
                        txtValue = '';
                    }
                    else {
                        if (i === words.length - 1) {
                            childNodes[childNodes.length] = { text: txtValue, x: 0, dy: 0, width: width };
                            txtValue = '';
                        }
                    }
                    existingText = newText;
                    existingWidth = width;
                }
            }
        }
        return childNodes;
    }
    function wrapText(txt, textValue, childNode) {
        var k = 0;
        var txtValue;
        var bounds1;
        var content = textValue || txt.content;
        txtValue = '';
        txtValue += content[0];
        for (k = 0; k < content.length; k++) {
            bounds1 = base_util_1.bBoxText(txtValue, txt);
            if (bounds1 >= txt.width && txtValue.length > 0) {
                childNode[childNode.length] = { text: txtValue, x: 0, dy: 0, width: bounds1 };
                txtValue = '';
            }
            else {
                txtValue = txtValue + (content[k + 1] || '');
                var width = base_util_1.bBoxText(txtValue, txt);
                if ((Math.ceil(width) + 2 >= txt.width && txtValue.length > 0) || (txtValue.indexOf('\n') > -1)) {
                    txtValue = txtValue.slice(0, -1);
                    childNode[childNode.length] = { text: txtValue, x: 0, dy: 0, width: width };
                    txtValue = content[k + 1] || '';
                }
                if (k === content.length - 1 && txtValue.length > 0) {
                    childNode[childNode.length] = { text: txtValue, x: 0, dy: 0, width: width };
                    txtValue = '';
                }
            }
        }
        return childNode;
    }
    function wrapSvgTextAlign(text, childNodes) {
        var wrapBounds = { x: 0, width: 0 };
        var k = 0;
        var txtWidth;
        var width;
        for (k = 0; k < childNodes.length; k++) {
            txtWidth = childNodes[parseInt(k.toString(), 10)].width;
            width = txtWidth;
            if (text.textAlign === 'left') {
                txtWidth = 0;
            }
            else if (text.textAlign === 'center') {
                Iif (txtWidth > text.width && (text.textOverflow === 'Ellipsis' || text.textOverflow === 'Clip')) {
                    txtWidth = 0;
                }
                else {
                    txtWidth = -txtWidth / 2;
                }
            }
            else Eif (text.textAlign === 'right') {
                txtWidth = -txtWidth;
            }
            else {
                txtWidth = childNodes.length > 1 ? 0 : -txtWidth / 2;
            }
            childNodes[parseInt(k.toString(), 10)].dy = text.fontSize * 1.2;
            childNodes[parseInt(k.toString(), 10)].x = txtWidth;
            Iif (!wrapBounds) {
                wrapBounds = {
                    x: txtWidth,
                    width: width
                };
            }
            else {
                wrapBounds.x = Math.min(wrapBounds.x, txtWidth);
                wrapBounds.width = Math.max(wrapBounds.width, width);
            }
        }
        return wrapBounds;
    }
    /** @private */
    function measureText(text, style, content, maxWidth, textValue) {
        var bounds = new size_1.Size(0, 0);
        var childNodes;
        var wrapBounds;
        var options = getTextOptions(text, maxWidth);
        text.childNodes = childNodes = wrapSvgText(options, textValue);
        text.wrapBounds = wrapBounds = wrapSvgTextAlign(options, childNodes);
        bounds.width = wrapBounds.width;
        Iif (text.wrapBounds.width >= maxWidth && options.textOverflow !== 'Wrap') {
            bounds.width = maxWidth;
        }
        bounds.height = childNodes.length * text.style.fontSize * 1.2;
        return bounds;
    }
    exports.measureText = measureText;
    /** @private */
    function getDiagramElement(elementId, contentId) {
        var diagramElement;
        var element;
        Iif (contentId) {
            element = document.getElementById(contentId);
        }
        diagramElement = (element) ? element.querySelector('#' + elementId) : document.getElementById(elementId);
        return diagramElement;
    }
    exports.getDiagramElement = getDiagramElement;
    /** @private */
    function createHtmlElement(elementType, attribute) {
        var element = ej2_base_1.createElement(elementType);
        setAttributeHtml(element, attribute);
        return element;
    }
    exports.createHtmlElement = createHtmlElement;
    /** @private */
    function setAttributeHtml(element, attributes) {
        var keys = Object.keys(attributes);
        for (var i = 0; i < keys.length; i++) {
            element.setAttribute(keys[parseInt(i.toString(), 10)], attributes[keys[parseInt(i.toString(), 10)]]);
        }
    }
    exports.setAttributeHtml = setAttributeHtml;
    /**
     * @private
     */
    function getAdornerLayerSvg(diagramId, index) {
        var adornerLayerSvg = null;
        var diagramElement = getDiagramElement(diagramId + index + '_diagramAdornerLayer');
        var elementcoll;
        Iif (diagramElement) {
            elementcoll = diagramElement.getElementsByClassName('e-adorner-layer' + index);
            adornerLayerSvg = elementcoll[0];
        }
        return adornerLayerSvg;
    }
    exports.getAdornerLayerSvg = getAdornerLayerSvg;
    /** @private */
    function getSelectorElement(diagramId, index) {
        var adornerLayer = null;
        var adornerSvg = getAdornerLayerSvg(diagramId, index);
        Iif (adornerSvg) {
            adornerLayer = adornerSvg.getElementById(diagramId + '_SelectorElement');
        }
        return adornerLayer;
    }
    exports.getSelectorElement = getSelectorElement;
    /** @private */
    function createMeasureElements() {
        var measureElement = 'measureElement';
        // eslint-disable-next-line
        if (!window[measureElement]) {
            var divElement = createHtmlElement('div', {
                id: 'measureElement',
                style: 'visibility:hidden ; height: 0px ; width: 0px; overflow: hidden;'
            });
            var text = createHtmlElement('span', { 'style': 'display:inline-block ; line-height: normal' });
            divElement.appendChild(text);
            var imageElement = void 0;
            imageElement = createHtmlElement('img', {});
            divElement.appendChild(imageElement);
            var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
            svg.setAttribute('xlink', 'http://www.w3.org/1999/xlink');
            divElement.appendChild(svg);
            var element = document.createElementNS('http://www.w3.org/2000/svg', 'path');
            svg.appendChild(element);
            var data = document.createTextNode('');
            var tSpan = document.createElementNS('http://www.w3.org/2000/svg', 'text');
            tSpan.setAttributeNS('http://www.w3.org/XML/1998/namespace', 'xml:space', 'preserve');
            svg.appendChild(tSpan);
            // eslint-disable-next-line
            window[measureElement] = divElement;
            // eslint-disable-next-line
            window[measureElement].usageCount = 1;
            document.body.appendChild(divElement);
        }
        else {
            // eslint-disable-next-line
            window[measureElement].usageCount += 1;
        }
    }
    exports.createMeasureElements = createMeasureElements;
    /** @private */
    function measureImage(source, contentSize) {
        var measureElement = 'measureElement';
        // eslint-disable-next-line
        window[measureElement].style.visibility = 'visible';
        // eslint-disable-next-line
        var imageElement = window[measureElement].children[1];
        imageElement.setAttribute('src', source);
        var bounds = imageElement.getBoundingClientRect();
        var width = bounds.width;
        var height = bounds.height;
        contentSize = new size_1.Size(width, height);
        // eslint-disable-next-line
        window[measureElement].style.visibility = 'hidden';
        return contentSize;
    }
    exports.measureImage = measureImage;
});