all files / linear-gauge/user-interaction/ tooltip.js

98.84% Statements 171/173
90% Branches 153/170
100% Functions 15/15
98.84% Lines 171/173
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   123× 123× 123× 123× 123×   936× 936× 936× 936× 936× 739× 739× 739× 739× 739×     197× 197× 197× 197×   936× 936× 936× 56× 56× 56× 56× 56× 56× 56× 56×               56× 56× 56× 56× 56× 56×   56× 56× 56×   880× 39× 39× 39× 39× 39× 39× 39× 39× 39× 39× 39×   39×               39× 39× 39× 39× 39× 39×   39× 39× 39×   841×     839× 839×     95× 95× 95×     95×                     95× 95×   95×   95× 95× 95× 27×   95× 95× 95× 95× 95×       95×     92×       92× 92×   95×   95× 95×               95×         95×                                     95×     95×   95× 95× 19×     76×   95×   95× 95× 95× 95× 95× 95× 95× 95× 89× 89× 89×       95× 95× 95× 95× 95× 95× 95×   95× 19× 19× 14×               76× 76× 47×       29×     95× 95×   744× 744× 744× 744×   123×     123× 123×   121×   118× 118×   386×   121× 121× 121× 121× 121× 60×   121× 121× 121× 121× 120×        
define(["require", "exports", "@syncfusion/ej2-base", "../model/constant", "@syncfusion/ej2-svg-base", "../utils/helper", "../utils/helper"], function (require, exports, ej2_base_1, constant_1, ej2_svg_base_1, helper_1, helper_2) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var GaugeTooltip = (function () {
        function GaugeTooltip(gauge) {
            this.gauge = gauge;
            this.element = gauge.element;
            this.tooltip = gauge.tooltip;
            this.tooltipId = this.gauge.element.id + '_LinearGauge_Tooltip';
            this.addEventListener();
        }
        GaugeTooltip.prototype.renderTooltip = function (e) {
            var pageX;
            var pageY;
            var target;
            var touchArg;
            if (e.type.indexOf('touch') !== -1) {
                this.isTouch = true;
                touchArg = e;
                pageX = touchArg.changedTouches[0].pageX;
                pageY = touchArg.changedTouches[0].pageY;
                target = touchArg.target;
            }
            else {
                this.isTouch = e.pointerType === 'touch';
                pageX = e.pageX;
                pageY = e.pageY;
                target = e.target;
            }
            var tooltipEle;
            var tooltipContent;
            if (target.id.indexOf('Pointer') > -1 && this.gauge.tooltip.type.indexOf('Pointer') > -1) {
                this.pointerElement = target;
                var areaRect = this.gauge.element.getBoundingClientRect();
                var current = helper_2.getPointer(this.pointerElement, this.gauge);
                this.currentAxis = current.axis;
                this.axisIndex = current.axisIndex;
                this.currentPointer = current.pointer;
                var customTooltipFormat = this.tooltip.format && this.tooltip.format.match('{value}') !== null;
                var tooltipStyle = {
                    size: this.tooltip.textStyle.size,
                    color: this.tooltip.textStyle.color,
                    fontFamily: this.tooltip.textStyle.fontFamily,
                    fontWeight: this.tooltip.textStyle.fontWeight,
                    fontStyle: this.tooltip.textStyle.fontStyle,
                    opacity: this.tooltip.textStyle.opacity
                };
                tooltipStyle.color = tooltipStyle.color || this.gauge.themeStyle.tooltipFontColor;
                tooltipStyle.size = tooltipStyle.size || this.gauge.themeStyle.tooltipFontSize;
                tooltipStyle.fontFamily = tooltipStyle.fontFamily || this.gauge.themeStyle.fontFamily;
                tooltipStyle.fontWeight = tooltipStyle.fontWeight || this.gauge.themeStyle.labelWeight;
                tooltipStyle.opacity = tooltipStyle.opacity || this.gauge.themeStyle.tooltipTextOpacity;
                tooltipContent = customTooltipFormat ? helper_1.textFormatter(this.tooltip.format, { value: this.currentPointer.currentValue }, this.gauge) :
                    helper_1.formatValue(this.currentPointer.currentValue, this.gauge).toString();
                tooltipEle = this.tooltipCreate(tooltipEle);
                this.tooltipRender(tooltipContent, target, tooltipEle, e, areaRect, pageX, pageY, tooltipStyle);
                this.gauge.renderReactTemplates();
            }
            else if (target.id.indexOf('Range') > -1 && this.gauge.tooltip.type.indexOf('Range') > -1) {
                this.pointerElement = target;
                var areaRect = this.gauge.element.getBoundingClientRect();
                var current = helper_2.getPointer(this.pointerElement, this.gauge);
                this.currentAxis = current.axis;
                this.axisIndex = current.axisIndex;
                var rangePosition = Number(target.id.charAt(target.id.length - 1));
                this.currentRange = this.currentAxis.ranges[rangePosition];
                var startData = (this.currentRange.start).toString();
                var endData = (this.currentRange.end).toString();
                var rangeTooltipFormat = this.gauge.tooltip.rangeSettings.format || this.currentAxis.labelStyle.format;
                var customTooltipFormat = rangeTooltipFormat && (rangeTooltipFormat.match('{end}') !== null ||
                    rangeTooltipFormat.match('{start}') !== null);
                var rangeTooltipStyle = {
                    size: this.tooltip.rangeSettings.textStyle.size,
                    color: this.tooltip.rangeSettings.textStyle.color,
                    fontFamily: this.tooltip.rangeSettings.textStyle.fontFamily,
                    fontWeight: this.tooltip.rangeSettings.textStyle.fontWeight,
                    fontStyle: this.tooltip.rangeSettings.textStyle.fontStyle,
                    opacity: this.tooltip.rangeSettings.textStyle.opacity
                };
                rangeTooltipStyle.color = rangeTooltipStyle.color || this.gauge.themeStyle.tooltipFontColor;
                rangeTooltipStyle.size = rangeTooltipStyle.size || this.gauge.themeStyle.tooltipFontSize;
                rangeTooltipStyle.fontFamily = rangeTooltipStyle.fontFamily || this.gauge.themeStyle.fontFamily;
                rangeTooltipStyle.fontWeight = rangeTooltipStyle.fontWeight || this.gauge.themeStyle.labelWeight;
                rangeTooltipStyle.opacity = rangeTooltipStyle.opacity || this.gauge.themeStyle.tooltipTextOpacity;
                tooltipContent = customTooltipFormat ? rangeTooltipFormat.replace(/{start}/g, startData).replace(/{end}/g, endData) :
                    'Start : ' + startData + '<br>' + 'End : ' + endData;
                tooltipEle = this.tooltipCreate(tooltipEle);
                this.tooltipRender(tooltipContent, target, tooltipEle, e, areaRect, pageX, pageY, rangeTooltipStyle);
                this.gauge.renderReactTemplates();
            }
            else if ((target.id === (this.element.id + '_LinearGaugeTitle')) && (target.textContent.indexOf('...') > -1)) {
                helper_1.showTooltip(this.gauge.title, this.gauge);
            }
            else {
                helper_1.removeTooltip();
                this.gauge.clearTemplate();
            }
        };
        GaugeTooltip.prototype.tooltipRender = function (tooltipContent, target, tooltipEle, e, areaRect, pageX, pageY, tooltipStyle) {
            var _this = this;
            var location = this.getTooltipLocation();
            if ((this.tooltip.rangeSettings.showAtMousePosition && target.id.indexOf('Range') > -1) ||
                (this.tooltip.showAtMousePosition && target.id.indexOf('Pointer') > -1)) {
                location = helper_1.getMousePosition(pageX, pageY, this.gauge.svgObject);
            }
            var args = {
                name: constant_1.tooltipRender,
                cancel: false,
                gauge: this.gauge,
                event: e,
                location: location,
                content: tooltipContent,
                tooltip: this.tooltip,
                axis: this.currentAxis,
                pointer: this.currentPointer
            };
            var tooltipPos = this.getTooltipPosition();
            location.y += ((this.tooltip.rangeSettings.template && tooltipPos === 'Top') ||
                (this.tooltip.template && tooltipPos === 'Top')) ? 20 : 0;
            location.x += ((this.tooltip.rangeSettings.template && tooltipPos === 'Right') ||
                (this.tooltip.template && tooltipPos === 'Right')) ? 20 : 0;
            this.gauge.trigger(constant_1.tooltipRender, args, function () {
                var template = (target.id.indexOf('Range') > -1) ? args.tooltip.rangeSettings.template : args.tooltip.template;
                if (template !== null && Object.keys(template).length === 1 && typeof template !== 'function') {
                    template = template[Object.keys(template)[0]];
                }
                Eif (!args.cancel) {
                    var fillColor = (target.id.indexOf('Range') > -1) ? _this.tooltip.rangeSettings.fill : _this.tooltip.fill;
                    _this.svgTooltip = _this.svgCreate(_this.svgTooltip, args, _this.gauge, areaRect, fillColor, template, tooltipPos, location, target, tooltipStyle);
                    _this.svgTooltip.opacity = _this.gauge.themeStyle.tooltipFillOpacity || _this.svgTooltip.opacity;
                    _this.svgTooltip.appendTo(tooltipEle);
                }
            });
        };
        GaugeTooltip.prototype.tooltipCreate = function (tooltipEle) {
            if (document.getElementById(this.tooltipId)) {
                tooltipEle = document.getElementById(this.tooltipId);
            }
            else {
                tooltipEle = ej2_base_1.createElement('div', {
                    id: this.tooltipId,
                    className: 'EJ2-LinearGauge-Tooltip'
                });
                tooltipEle.style.cssText = 'position: absolute;pointer-events:none;z-index: 3;';
                document.getElementById(this.gauge.element.id + '_Secondary_Element').appendChild(tooltipEle);
            }
            return tooltipEle;
        };
        GaugeTooltip.prototype.svgCreate = function (svgTooltip, args, gauge, areaRect, fill, template, tooltipPos, location, target, textStyle) {
            var tooltipBorder = (target.id.indexOf('Range') > -1) ? args.tooltip.rangeSettings.border : args.tooltip.border;
            textStyle = {
                color: args.tooltip.textStyle.color || textStyle.color,
                fontFamily: args.tooltip.textStyle.fontFamily || textStyle.fontFamily,
                fontStyle: args.tooltip.textStyle.fontStyle || textStyle.fontStyle,
                fontWeight: args.tooltip.textStyle.fontWeight || textStyle.fontWeight,
                opacity: args.tooltip.textStyle.opacity || textStyle.opacity,
                size: args.tooltip.textStyle.size || textStyle.size
            };
            var borderStyle = {
                color: tooltipBorder.color || this.gauge.themeStyle.tooltipBorderColor || 'transparent',
                width: tooltipBorder.width || this.gauge.themeStyle.tooltipBorderWidth || 0,
                dashArray: tooltipBorder.dashArray
            };
            svgTooltip = new ej2_svg_base_1.Tooltip({
                enable: true,
                header: '',
                data: { value: args.content },
                template: template,
                content: [ej2_base_1.SanitizeHtmlHelper.sanitize(args.content)],
                shapes: [],
                location: args.location,
                palette: [],
                inverted: !(args.gauge.orientation === 'Horizontal'),
                enableAnimation: args.tooltip.enableAnimation,
                fill: fill || gauge.themeStyle.tooltipFillColor,
                availableSize: gauge.availableSize,
                areaBounds: new helper_1.Rect((this.gauge.orientation === 'Vertical') ? location.x : areaRect.left - this.element.getBoundingClientRect().left, (this.gauge.orientation === 'Vertical') ? areaRect.top : (tooltipPos === 'Bottom') ? areaRect.top : location.y, tooltipPos === 'Right' ? Math.abs(areaRect.left - location.x) : areaRect.width, areaRect.height),
                textStyle: textStyle,
                border: borderStyle,
                theme: args.gauge.theme,
                enableShadow: true
            });
            Iif (gauge.isVue || gauge.isVue3) {
                svgTooltip.controlInstance = gauge;
            }
            return svgTooltip;
        };
        GaugeTooltip.prototype.getTooltipPosition = function () {
            var position;
            if (this.gauge.orientation === 'Vertical') {
                position = (!this.currentAxis.opposedPosition) ? 'Left' : 'Right';
            }
            else {
                position = (this.currentAxis.opposedPosition) ? 'Top' : 'Bottom';
            }
            return position;
        };
        GaugeTooltip.prototype.getTooltipLocation = function () {
            var lineX;
            var lineY;
            var x;
            var y;
            var lineId = this.gauge.element.id + '_AxisLine_' + this.axisIndex;
            var tickID = this.gauge.element.id + '_MajorTicksLine_' + this.axisIndex;
            var lineBounds;
            if (helper_1.getElement(lineId)) {
                lineBounds = helper_1.getElement(lineId).getBoundingClientRect();
                lineX = lineBounds.left;
                lineY = lineBounds.top;
            }
            else {
                lineBounds = helper_1.getElement(tickID).getBoundingClientRect();
                lineX = (!this.currentAxis.opposedPosition) ? (lineBounds.left + lineBounds.width) : lineBounds.left;
                lineY = (!this.currentAxis.opposedPosition) ? (lineBounds.top + lineBounds.height) : lineBounds.top;
            }
            var bounds = this.pointerElement.getBoundingClientRect();
            var elementRect = this.gauge.element.getBoundingClientRect();
            x = bounds.left - elementRect.left;
            y = bounds.top - elementRect.top;
            var height = bounds.height;
            var width = bounds.width;
            var tooltipPosition = (this.pointerElement.id.indexOf('Range') > -1) ? this.tooltip.rangeSettings.position :
                this.tooltip.position;
            if (this.gauge.orientation === 'Vertical') {
                x = (lineX - elementRect.left);
                if (this.pointerElement.id.indexOf('Range') > -1 || this.pointerElement.id.indexOf('BarPointer') > -1) {
                    y = (!this.currentAxis.isInversed) ? ((tooltipPosition === 'End') ? y : ((tooltipPosition === 'Start') ?
                        y + height : y + (height / 2))) : ((tooltipPosition === 'End') ? y + height : ((tooltipPosition === 'Start') ?
                        y + height : y + (height / 2)));
                }
                else {
                    y = (this.currentPointer.type === 'Marker') ? y + (height / 2) : (!this.currentAxis.isInversed) ? y : y + height;
                }
            }
            else {
                y = (lineY - elementRect.top);
                if (this.pointerElement.id.indexOf('Range') > -1 || this.pointerElement.id.indexOf('BarPointer') > -1) {
                    x = (!this.currentAxis.isInversed) ? ((tooltipPosition === 'End') ? x + width : ((tooltipPosition === 'Start') ?
                        x : x + (width / 2))) : ((tooltipPosition === 'End') ? x : ((tooltipPosition === 'Start') ? x + width : x + (width / 2)));
                }
                else {
                    x = (this.currentPointer.type === 'Marker') ? (x + width / 2) : (!this.currentAxis.isInversed) ? x + width : x;
                }
            }
            var location = new helper_1.GaugeLocation(x, y);
            return location;
        };
        GaugeTooltip.prototype.mouseUpHandler = function (e) {
            helper_1.removeTooltip();
            this.renderTooltip(e);
            clearTimeout(this.clearTimeout);
            this.clearTimeout = setTimeout(helper_1.removeTooltip.bind(this), 2000);
        };
        GaugeTooltip.prototype.addEventListener = function () {
            Iif (this.gauge.isDestroyed) {
                return;
            }
            this.gauge.on(ej2_base_1.Browser.touchMoveEvent, this.renderTooltip, this);
            this.gauge.on(ej2_base_1.Browser.touchEndEvent, this.mouseUpHandler, this);
        };
        GaugeTooltip.prototype.removeEventListener = function () {
            if (this.gauge.isDestroyed) {
                return;
            }
            this.gauge.off(ej2_base_1.Browser.touchMoveEvent, this.renderTooltip);
            this.gauge.off(ej2_base_1.Browser.touchEndEvent, this.mouseUpHandler);
        };
        GaugeTooltip.prototype.getModuleName = function () {
            return 'Tooltip';
        };
        GaugeTooltip.prototype.destroy = function () {
            this.element = null;
            this.currentAxis = null;
            this.currentPointer = null;
            this.currentRange = null;
            if (!ej2_base_1.isNullOrUndefined(this.svgTooltip)) {
                this.svgTooltip.destroy();
            }
            this.svgTooltip = null;
            this.pointerElement = null;
            this.tooltip = null;
            this.removeEventListener();
            this.gauge = null;
        };
        return GaugeTooltip;
    }());
    exports.GaugeTooltip = GaugeTooltip;
});