import { Widget } from "@hpcc-js/common"; import { format as d3Format } from "d3-format"; import { tip } from "./Tooltip.ts"; import "../src/ITooltip.css"; declare const event: object; // Use old school class declaration as this is a mixin --- export function ITooltip(this: any) { this.tooltip = tip(); if (this.tooltipLabelFormat_exists()) { this._labelFormatter = d3Format(this.tooltipLabelFormat() as string); } if (this.tooltipValueFormat_exists()) { this._valueFormatter = d3Format(this.tooltipValueFormat() as string); } if (this.layerEnter) { const layerEnter = this.layerEnter; this.layerEnter = function (_base, svgElement, _domElement) { if (!this._parentOverlay) { this._parentOverlay = _base._parentOverlay; } this.tooltipEnter(svgElement); layerEnter.apply(this, arguments); }; const layerUpdate = this.layerUpdate; this.layerUpdate = function (_base) { layerUpdate.apply(this, arguments); this.tooltipUpdate(); }; const layerExit = this.layerExit; this.layerExit = function (_base) { this.tooltipExit(); layerExit.apply(this, arguments); }; } else { const enter = this.enter; this.enter = function (_domNode, element) { this.tooltipEnter(element); enter.apply(this, arguments); }; const update = this.update; this.update = function (_domNode, _element) { update.apply(this, arguments); this.tooltipUpdate(); }; const exit = this.exit; this.exit = function (_domNode, _element) { this.tooltipExit(); exit.apply(this, arguments); }; } } ITooltip.prototype = Object.create(Widget.prototype); ITooltip.prototype.constructor = ITooltip; // abstract target(): any; // abstract target(_: any): this; ITooltip.prototype.tooltipEnter = function (element) { const overlayElement = this.parentOverlay(); if (!overlayElement.empty()) { this.tooltip.rootElement(overlayElement.node().parentNode); } element.call(this.tooltip); }; ITooltip.prototype.tooltipUpdate = function () { this.tooltip.offset(() => { if (event && this.tooltipFollowMouse()) { const d3tipElement: HTMLDivElement = document.querySelector(".d3-tip"); // d3Tip offers no reference to the '.d3-tip' element...? d3tipElement.style.display = "block"; d3tipElement.style.left = this.tooltipOffset() + ((event as any).clientX) + "px"; d3tipElement.style.top = (event as any).clientY + "px"; return []; } switch (this.tooltip.direction()()) { case "e": return [0, this.tooltipOffset()]; default: return [-this.tooltipOffset(), 0]; } }); let classed = this.tooltip.attr("class"); if (classed) { classed = classed.split(" notick").join("") + (this.tooltipTick() ? "" : " notick") + (this.tooltipStyle() === "none" ? " hidden" : ""); classed = classed.split(" ") .filter(function (_class) { return _class.indexOf("ITooltip-tooltipStyle-") !== 0; }) .join(" ") ; classed += " ITooltip-tooltipStyle-" + this.tooltipStyle(); this.tooltip .attr("class", classed) ; } }; ITooltip.prototype.tooltipExit = function () { if (this.tooltip) { this.tooltip.destroy(); } }; ITooltip.prototype._tooltipHTML = function (d) { return d; }; ITooltip.prototype.tooltipHTML = function (_) { return this.tooltip.html(_); }; ITooltip.prototype.tooltipFormat = function (opts: { label?: string | number, series?: string | number, value?: Date | string | number, arr?: Array<{ color: string, label: string, value: string }> } = {}) { opts.label = opts.label === undefined ? "" : opts.label; if (this._labelFormatter) { opts.label = this._labelFormatter(opts.label) || ""; } else if (this.formatData && this.parseData) { opts.label = this.formatData(this.parseData(opts.label)); } opts.series = opts.series || ""; if (opts.value instanceof Date) { opts.value = opts.value || ""; } else if (this._valueFormatter) { opts.value = this._valueFormatter(opts.value) || ""; } else if (this.formatValue && this.parseValue) { opts.value = this.formatValue(this.parseValue(opts.value)); } switch (this.tooltipStyle()) { case "none": break; case "series-table": let html = '
| ' + opts.label + " | |
|---|---|
| ";
html += '';
html += ' ' + row.label + " ";
html += " | ";
html += '' + row.value + " | ";
html += "
| ${obj[titleKey]} |
|---|