all files / common/user-interaction/ tooltip.js

92.34% Statements 193/209
86.81% Branches 204/235
97.14% Functions 34/35
93.6% Lines 190/203
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 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          1188× 1188× 1188× 1188× 1188× 1188×   2237×   819× 819×     819× 819× 819× 819× 546×   273×   546× 546× 546× 546× 546× 546× 546×   232× 232×     232× 209×     23×   232× 232× 197× 197×           232×       883× 883× 360× 360× 179× 179×   179×       55× 55× 55× 22×   55×     594× 594× 198×   198×   594× 594×     55× 22×   55×       545× 545× 545× 545× 545× 545× 511×   511×     506× 1077× 506×     63× 63× 33×   15×       18×     30× 22×   22×               503× 503×                 547× 759× 759× 365×       547× 547× 547× 547× 547×   547×       547× 430×                                                                   547× 547× 547×     67×       430×     117× 117× 117× 117× 117× 117× 117× 117× 117× 117× 117× 117× 117× 117× 117×   117× 117×     547×       547× 547× 759× 759×   547×   759× 143×     616×         547× 316×   547×                             337× 337× 337× 93× 265×       646×   72× 72× 72× 72× 36× 27× 25×              
/* 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", "@syncfusion/ej2-base", "../../common/utils/helper", "../../common/utils/helper", "../../chart/utils/get-data", "@syncfusion/ej2-svg-base", "../../chart/user-interaction/selection"], function (require, exports, ej2_base_1, helper_1, helper_2, get_data_1, ej2_svg_base_1, selection_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var BaseTooltip = (function (_super) {
        __extends(BaseTooltip, _super);
        function BaseTooltip(chart) {
            var _this = _super.call(this, chart) || this;
            _this.element = _this.chart.element;
            _this.textStyle = chart.tooltip.textStyle;
            _this.control = chart;
            _this.template = chart.tooltip.template;
            return _this;
        }
        BaseTooltip.prototype.getElement = function (id) {
            return document.getElementById(id);
        };
        BaseTooltip.prototype.getTooltipElement = function (isTooltip) {
            this.inverted = this.chart.requireInvertedAxis;
            this.header = (this.control.tooltip.header === null) ?
                ((this.control.tooltip.shared) ? '${point.x}' : '${series.name}')
                : (this.control.tooltip.header);
            this.formattedText = [];
            var tooltipDiv = document.getElementById(this.chart.element.id + '_tooltip');
            var isStockChart = this.chart.element.id.indexOf('stockChart') > -1;
            if (!isTooltip && !tooltipDiv || isStockChart) {
                return this.createElement();
            }
            return null;
        };
        BaseTooltip.prototype.createElement = function () {
            var tooltipDiv = document.createElement('div');
            tooltipDiv.id = this.element.id + '_tooltip';
            tooltipDiv.className = 'ejSVGTooltip';
            tooltipDiv.style.pointerEvents = 'none';
            tooltipDiv.style.position = 'absolute';
            tooltipDiv.style.zIndex = '1';
            return tooltipDiv;
        };
        BaseTooltip.prototype.pushData = function (data, isFirst, tooltipDiv, isChart, enable3D) {
            Eif (data.series.enableTooltip) {
                Iif (enable3D) {
                    this.currentPoints.push(data);
                }
                else if (isChart) {
                    this.currentPoints.push(data);
                }
                else {
                    this.currentPoints.push(data);
                }
                this.stopAnimation();
                if (tooltipDiv && !document.getElementById(tooltipDiv.id)) {
                    Eif (!this.chart.stockChart) {
                        document.getElementById(this.element.id + '_Secondary_Element').appendChild(tooltipDiv);
                    }
                    else {
                        document.getElementById(this.chart.stockChart.element.id + '_Secondary_Element').appendChild(tooltipDiv);
                    }
                }
                return true;
            }
            return false;
        };
        BaseTooltip.prototype.removeHighlight = function () {
            var item;
            for (var i = 0, len = this.previousPoints.length; i < len; i++) {
                item = this.previousPoints[i];
                if (item.series.isRectSeries || this.chart.tooltip.enableHighlight) {
                    Eif (item.series.visible) {
                        this.highlightPoint(item.series, item.point.index, false);
                    }
                    continue;
                }
            }
        };
        BaseTooltip.prototype.animateHighlight = function (targetOpacity, targetElement, duration, targetStrokeWidth) {
            var initialOpacity = parseFloat(targetElement.getAttribute('opacity'));
            var initialStrokeWidth = null;
            if (targetStrokeWidth !== null) {
                initialStrokeWidth = parseFloat(targetElement.getAttribute('stroke-width'));
            }
            new ej2_base_1.Animation({}).animate(targetElement, {
                duration: duration,
                progress: function (args) {
                    targetElement.style.animation = '';
                    if (targetStrokeWidth !== null) {
                        var newStrokeWidth = initialStrokeWidth
                            + (args.timeStamp / args.duration) * (targetStrokeWidth - initialStrokeWidth);
                        targetElement.setAttribute('stroke-width', Math.max(newStrokeWidth, targetStrokeWidth).toString());
                    }
                    var newOpacity = initialOpacity + (args.timeStamp / args.duration) * (targetOpacity - initialOpacity);
                    targetElement.setAttribute('opacity', Math.min(newOpacity, targetOpacity).toString());
                },
                end: function () {
                    if (targetStrokeWidth !== null) {
                        targetElement.setAttribute('stroke-width', targetStrokeWidth.toString());
                    }
                    targetElement.setAttribute('opacity', targetOpacity.toString());
                }
            });
        };
        BaseTooltip.prototype.highlightPoint = function (series, pointIndex, highlight) {
            var _this = this;
            var element = this.getElement(this.element.id + '_Series_' + series.index + '_Point_' + pointIndex);
            var selectionModule = this.control.accumulationSelectionModule;
            var isAccumulation = this.chart.getModuleName() === 'accumulationchart';
            var isSelectedElement = selectionModule && selectionModule.selectedDataIndexes.length > 0 ? true : false;
            if ((element) || (!series.isRectSeries)) {
                Eif ((!isSelectedElement || isSelectedElement && element.getAttribute('class')
                    && element.getAttribute('class').indexOf('_ej2_chart_selection_series_') === -1) || (!series.isRectSeries)) {
                    if (series.isRectSeries && this.chart.highlightColor !== '' && !ej2_base_1.isNullOrUndefined(this.chart.highlightColor)) {
                        element.setAttribute('fill', (highlight && this.chart.highlightColor !== 'transparent' ? this.chart.highlightColor : series.pointColorMapping !== '' ? (series.points[0]).color : series.points[pointIndex].color || series.interior));
                    }
                    else {
                        var visibleSeriesLength = this.chart.visibleSeries.
                            filter(function (series) { return series.visible; }).length;
                        if ((this.control.highlightMode === 'None') && (this.chart.tooltip.enableHighlight) && ((!this.chart.tooltip.shared && visibleSeriesLength > 1) || (isAccumulation))) {
                            var target_1 = this.element.id + '_Series_' + series.index + '_Point_' + pointIndex;
                            var _loop_1 = function (currentSeries) {
                                var seriesElementsGroupCollections = [];
                                var currentSeriesWidth = typeof currentSeries.width === 'number' ? currentSeries.width : parseFloat(currentSeries.width);
                                seriesElementsGroupCollections = isAccumulation
                                    ? [this_1.getElement(this_1.chart.element.id + '_Series_' + currentSeries.index)]
                                    : new selection_1.Selection(this_1.chart).getSeriesElements(currentSeries);
                                seriesElementsGroupCollections.forEach(function (seriesElementsGroup) {
                                    seriesElementsGroup.childNodes.forEach(function (seriesElement) {
                                        Eif (isAccumulation ? !seriesElement.getAttribute('class') : (!seriesElement.getAttribute('class') && !seriesElementsGroup.getAttribute('class'))) {
                                            if (highlight && _this.chart.highlightColor !== 'transparent' && seriesElement.id !== '') {
                                                if (isAccumulation ?
                                                    seriesElement.id === target_1 : currentSeries.index === series.index) {
                                                    _this.animateHighlight(currentSeries.opacity, seriesElement, _this.chart.tooltip.duration, ((!series.isRectSeries || (seriesElement.id.indexOf('border') > -1)) && (!isAccumulation)) ?
                                                        (currentSeriesWidth + currentSeriesWidth / 2) : null);
                                                }
                                                else {
                                                    _this.animateHighlight(0.25, seriesElement, _this.chart.tooltip.duration, ((!series.isRectSeries || (seriesElement.id.indexOf('border') > -1)) && (!isAccumulation)) ? currentSeriesWidth : null);
                                                }
                                            }
                                            else if (!_this.currentPoints[0] && seriesElement.id !== '') {
                                                var targetOpacity = seriesElement.id.indexOf('border') > -1 ? 1 :
                                                    seriesElement.id.indexOf('Symbol') > -1 ? currentSeries.marker.opacity : currentSeries.opacity;
                                                _this.animateHighlight(targetOpacity, seriesElement, _this.chart.tooltip.duration, ((!series.isRectSeries || (seriesElement.id.indexOf('border') > -1)) && (!isAccumulation)) ? currentSeriesWidth : null);
                                            }
                                        }
                                    });
                                });
                            };
                            var this_1 = this;
                            for (var _i = 0, _a = this.chart.visibleSeries; _i < _a.length; _i++) {
                                var currentSeries = _a[_i];
                                _loop_1(currentSeries);
                            }
                        }
                        else Eif (series.isRectSeries) {
                            element.setAttribute('opacity', (highlight && this.chart.highlightColor !== 'transparent' ? series.opacity / 2 : series.opacity).toString());
                        }
                    }
                }
                else {
                    element.setAttribute('opacity', series.opacity.toString());
                }
            }
        };
        BaseTooltip.prototype.highlightPoints = function () {
            for (var _i = 0, _a = this.currentPoints; _i < _a.length; _i++) {
                var item = _a[_i];
                if ((item.series.isRectSeries || this.chart.tooltip.enableHighlight) && item.series.category === 'Series') {
                    this.highlightPoint(item.series, item.point.index, true);
                }
            }
        };
        BaseTooltip.prototype.createTooltip = function (chart, isFirst, location, clipLocation, point, shapes, offset, bounds, crosshairEnabled, extraPoints, templatePoint, customTemplate) {
            Iif (crosshairEnabled === void 0) { crosshairEnabled = false; }
            Iif (extraPoints === void 0) { extraPoints = null; }
            Iif (templatePoint === void 0) { templatePoint = null; }
            var series = this.currentPoints[0].series;
            var tooltipModule = chart.tooltipModule || chart.tooltip3DModule ||
                chart.accumulationTooltipModule;
            Iif (!tooltipModule || location === null) {
                helper_2.removeElement(this.chart.element.id + '_tooltip');
                return;
            }
            if (isFirst) {
                this.svgTooltip = new ej2_svg_base_1.Tooltip({
                    opacity: chart.tooltip.opacity ? chart.tooltip.opacity : ((this.chart.theme === 'Material3' || this.chart.theme === 'Material3Dark' || this.chart.theme.indexOf('Bootstrap5') > -1) ? 1 : 0.75),
                    header: this.headerText,
                    content: this.text,
                    fill: chart.tooltip.fill,
                    border: chart.tooltip.border,
                    enableAnimation: chart.tooltip.enableAnimation,
                    location: location,
                    shared: this.control.tooltip.shared,
                    crosshair: crosshairEnabled,
                    shapes: shapes,
                    clipBounds: this.chart.chartAreaType === 'PolarRadar' ? new helper_1.ChartLocation(0, 0) : clipLocation,
                    areaBounds: bounds,
                    palette: this.findPalette(),
                    template: customTemplate || this.template,
                    data: templatePoint,
                    theme: chart.theme,
                    offset: offset,
                    textStyle: chart.tooltip.textStyle,
                    isNegative: (series.isRectSeries && series.type !== 'Waterfall' && point && point.y < 0),
                    inverted: this.chart.requireInvertedAxis && series.isRectSeries,
                    arrowPadding: this.text.length > 1 || this.chart.stockChart || (this.chart.tooltip.location.x !== null
                        || this.chart.tooltip.location.y !== null) ? 0 : 7,
                    availableSize: chart.availableSize,
                    duration: this.chart.tooltip.duration,
                    isCanvas: this.chart.enableCanvas,
                    isFixed: (this.chart.tooltip.location.x !== null || this.chart.tooltip.location.y !== null),
                    isTextWrap: chart.tooltip.enableTextWrap && chart.getModuleName() === 'chart',
                    blazorTemplate: { name: 'Template', parent: this.chart.tooltip },
                    controlInstance: this.chart,
                    enableRTL: chart.enableRtl,
                    controlName: 'Chart',
                    allowHighlight: chart.getModuleName() === 'chart' && !series.marker.allowHighlight,
                    tooltipRender: function () {
                        tooltipModule.removeHighlight();
                        tooltipModule.highlightPoints();
                        tooltipModule.updatePreviousPoint(extraPoints);
                    },
                    animationComplete: function (args) {
                        if (args.tooltip.fadeOuted) {
                            tooltipModule.fadeOut(tooltipModule.previousPoints);
                        }
                    }
                });
                this.svgTooltip.appendTo(this.getElement(this.element.id + '_tooltip'));
            }
            else {
                Eif (this.svgTooltip) {
                    this.svgTooltip.location = location;
                    this.svgTooltip.content = this.text;
                    this.svgTooltip.header = this.headerText;
                    this.svgTooltip.offset = offset;
                    this.svgTooltip.palette = this.findPalette();
                    this.svgTooltip.shapes = shapes;
                    this.svgTooltip.data = templatePoint;
                    this.svgTooltip.template = this.template;
                    this.svgTooltip.controlName = 'Chart';
                    this.svgTooltip.crosshair = crosshairEnabled;
                    this.svgTooltip.textStyle = chart.tooltip.textStyle;
                    this.svgTooltip.isNegative = (series.isRectSeries && series.type !== 'Waterfall' && point && point.y < 0);
                    this.svgTooltip.clipBounds = this.chart.chartAreaType === 'PolarRadar' ? new helper_1.ChartLocation(0, 0) : clipLocation;
                    this.svgTooltip.arrowPadding = this.text.length > 1 || this.chart.stockChart || (this.chart.tooltip.location.x !== null
                        || this.chart.tooltip.location.y !== null) ? 0 : 7;
                    this.svgTooltip.allowHighlight = chart.getModuleName() === 'chart' && !series.marker.allowHighlight;
                    this.svgTooltip.dataBind();
                }
            }
            Iif (this.chart.isReact) {
                this.chart.renderReactTemplates();
            }
        };
        BaseTooltip.prototype.findPalette = function () {
            var colors = [];
            for (var _i = 0, _a = this.currentPoints; _i < _a.length; _i++) {
                var data = _a[_i];
                colors.push(this.findColor(data, data.series));
            }
            return colors;
        };
        BaseTooltip.prototype.findColor = function (data, series) {
            if (series.isRectSeries && (series.type === 'Candle' || series.type === 'Hilo' || series.type === 'HiloOpenClose')) {
                return data.point.color;
            }
            else {
                return (data.point.color && data.point.color !== '#ffffff' ? data.point.color
                    : data.point.interior) ||
                    series.marker.fill || series.interior;
            }
        };
        BaseTooltip.prototype.updatePreviousPoint = function (extraPoints) {
            if (extraPoints) {
                this.currentPoints = this.currentPoints.concat(extraPoints);
            }
            this.previousPoints = ej2_base_1.extend([], this.currentPoints, null, true);
        };
        BaseTooltip.prototype.fadeOut = function (data) {
            var svgElement = this.chart.enableCanvas ? this.getElement(this.element.id + '_tooltip_group') :
                this.getElement(this.element.id + '_tooltip_svg') || this.getElement(this.element.id + '_tooltipparent_template');
            var isTooltip = (svgElement && parseInt(svgElement.getAttribute('opacity'), 10) > 0);
            if (!isTooltip) {
                this.valueX = null;
                this.valueY = null;
                this.currentPoints = [];
                this.removeHighlight();
                this.removeHighlightedMarker(data, true);
                this.svgTooltip = null;
                this.control.trigger('animationComplete', {});
            }
        };
        BaseTooltip.prototype.removeHighlightedMarker = function (data, fadeOut) {
            if (this.chart.markerRender) {
                for (var _i = 0, data_1 = data; _i < data_1.length; _i++) {
                    var item = data_1[_i];
                    helper_2.removeElement(this.element.id + '_Series_' + item.series.index +
                        '_Point_' + item.point.index + '_Trackball');
                    this.chart.markerRender.removeHighlightedMarker(item.series, item.point, fadeOut);
                }
            }
            this.previousPoints = [];
        };
        BaseTooltip.prototype.removeText = function () {
            this.textElements = [];
            var element = this.getElement(this.element.id + '_tooltip_group');
            if (element && element.childNodes.length > 0) {
                while (element.lastChild && element.childNodes.length !== 1) {
                    element.removeChild(element.lastChild);
                }
            }
        };
        BaseTooltip.prototype.stopAnimation = function () {
            helper_2.stopTimer(this.toolTipInterval);
        };
        BaseTooltip.prototype.removeTooltip = function (duration) {
            var _this = this;
            var tooltipElement = this.getElement(this.element.id + '_tooltip');
            this.stopAnimation();
            if (tooltipElement && this.previousPoints.length > 0) {
                this.toolTipInterval = +setTimeout(function () {
                    if (_this.svgTooltip) {
                        _this.svgTooltip.fadeOut();
                    }
                }, duration);
            }
        };
        return BaseTooltip;
    }(get_data_1.ChartData));
    exports.BaseTooltip = BaseTooltip;
});