{"version":3,"file":"vega-tooltip.js","sources":["../src/formatValue.ts","../src/style.ts","../src/defaults.ts","../src/position.ts","../src/Handler.ts","../src/index.ts"],"sourcesContent":["import {isArray, isObject, isString} from 'vega-util';\n\n/**\n * Format the value to be shown in the tooltip.\n *\n * @param value The value to show in the tooltip.\n * @param valueToHtml Function to convert a single cell value to an HTML string\n */\nexport function formatValue(value: any, valueToHtml: (value: any) => string, maxDepth: number): string {\n if (isArray(value)) {\n return `[${value.map((v) => valueToHtml(isString(v) ? v : stringify(v, maxDepth))).join(', ')}]`;\n }\n\n if (isObject(value)) {\n let content = '';\n\n const {title, image, ...rest} = value as any;\n\n if (title) {\n content += `

${valueToHtml(title)}

`;\n }\n\n if (image) {\n content += ``;\n }\n\n const keys = Object.keys(rest);\n if (keys.length > 0) {\n content += '';\n for (const key of keys) {\n let val = (rest as any)[key];\n\n // ignore undefined properties\n if (val === undefined) {\n continue;\n }\n\n if (isObject(val)) {\n val = stringify(val, maxDepth);\n }\n\n content += ``;\n }\n content += `
${valueToHtml(key)}:${valueToHtml(val)}
`;\n }\n\n return content || '{}'; // show empty object if there are no properties\n }\n\n return valueToHtml(value);\n}\n\nexport function replacer(maxDepth: number) {\n const stack: any[] = [];\n\n return function (this: any, key: string, value: any) {\n if (typeof value !== 'object' || value === null) {\n return value;\n }\n const pos = stack.indexOf(this) + 1;\n stack.length = pos;\n if (stack.length > maxDepth) {\n return '[Object]';\n }\n if (stack.indexOf(value) >= 0) {\n return '[Circular]';\n }\n stack.push(value);\n return value;\n };\n}\n\n/**\n * Stringify any JS object to valid JSON\n */\nexport function stringify(obj: any, maxDepth: number) {\n return JSON.stringify(obj, replacer(maxDepth));\n}\n","// generated with build-style.sh\nexport default `#vg-tooltip-element {\n visibility: hidden;\n padding: 8px;\n position: fixed;\n z-index: 1000;\n font-family: sans-serif;\n font-size: 11px;\n border-radius: 3px;\n box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);\n /* The default theme is the light theme. */\n background-color: rgba(255, 255, 255, 0.95);\n border: 1px solid #d9d9d9;\n color: black;\n}\n#vg-tooltip-element.visible {\n visibility: visible;\n}\n#vg-tooltip-element h2 {\n margin-top: 0;\n margin-bottom: 10px;\n font-size: 13px;\n}\n#vg-tooltip-element img {\n max-width: 200px;\n max-height: 200px;\n}\n#vg-tooltip-element table {\n border-spacing: 0;\n}\n#vg-tooltip-element table tr {\n border: none;\n}\n#vg-tooltip-element table tr td {\n overflow: hidden;\n text-overflow: ellipsis;\n padding-top: 2px;\n padding-bottom: 2px;\n}\n#vg-tooltip-element table tr td.key {\n color: #808080;\n max-width: 150px;\n text-align: right;\n padding-right: 4px;\n}\n#vg-tooltip-element table tr td.value {\n display: block;\n max-width: 300px;\n max-height: 7em;\n text-align: left;\n}\n#vg-tooltip-element.dark-theme {\n background-color: rgba(32, 32, 32, 0.9);\n border: 1px solid #f5f5f5;\n color: white;\n}\n#vg-tooltip-element.dark-theme td.key {\n color: #bfbfbf;\n}\n`;\n","import {formatValue} from './formatValue';\nimport defaultStyle from './style';\n\nconst EL_ID = 'vg-tooltip-element';\n\nexport const DEFAULT_OPTIONS = {\n /**\n * X offset.\n */\n offsetX: 10,\n\n /**\n * Y offset.\n */\n offsetY: 10,\n\n /**\n * ID of the tooltip element.\n */\n id: EL_ID,\n\n /**\n * ID of the tooltip CSS style.\n */\n styleId: 'vega-tooltip-style',\n\n /**\n * The name of the theme. You can use the CSS class called [THEME]-theme to style the tooltips.\n *\n * There are two predefined themes: \"light\" (default) and \"dark\".\n */\n theme: 'light',\n\n /**\n * Do not use the default styles provided by Vega Tooltip. If you enable this option, you need to use your own styles. It is not necessary to disable the default style when using a custom theme.\n */\n disableDefaultStyle: false,\n\n /**\n * HTML sanitizer function that removes dangerous HTML to prevent XSS.\n *\n * This should be a function from string to string. You may replace it with a formatter such as a markdown formatter.\n */\n sanitize: escapeHTML,\n\n /**\n * The maximum recursion depth when printing objects in the tooltip.\n */\n maxDepth: 2,\n\n /**\n * A function to customize the rendered HTML of the tooltip.\n * @param value A value string, or object of value strings keyed by field\n * @param sanitize The `sanitize` function from `options.sanitize`\n * @returns {string} The returned string will become the `innerHTML` of the tooltip element\n */\n formatTooltip: formatValue,\n};\n\nexport type Options = Partial;\n\n/**\n * Escape special HTML characters.\n *\n * @param value A value to convert to string and HTML-escape.\n */\nexport function escapeHTML(value: any): string {\n return String(value).replace(/&/g, '&').replace(/ window.innerWidth) {\n x = +event.clientX - offsetX - tooltipBox.width;\n }\n\n let y = event.clientY + offsetY;\n if (y + tooltipBox.height > window.innerHeight) {\n y = +event.clientY - offsetY - tooltipBox.height;\n }\n\n return {x, y};\n}\n","import {TooltipHandler} from 'vega-typings';\n\nimport {createDefaultStyle, DEFAULT_OPTIONS, Options} from './defaults';\nimport {calculatePosition} from './position';\n\n/**\n * The tooltip handler class.\n */\nexport class Handler {\n /**\n * The handler function. We bind this to this function in the constructor.\n */\n public call: TooltipHandler;\n\n /**\n * Complete tooltip options.\n */\n private options: Required;\n\n /**\n * The tooltip html element.\n */\n private el: HTMLElement | null;\n\n /**\n * Create the tooltip handler and initialize the element and style.\n *\n * @param options Tooltip Options\n */\n constructor(options?: Options) {\n this.options = {...DEFAULT_OPTIONS, ...options};\n const elementId = this.options.id;\n this.el = null;\n\n // bind this to call\n this.call = this.tooltipHandler.bind(this);\n\n // prepend a default stylesheet for tooltips to the head\n if (!this.options.disableDefaultStyle && !document.getElementById(this.options.styleId)) {\n const style = document.createElement('style');\n style.setAttribute('id', this.options.styleId);\n style.innerHTML = createDefaultStyle(elementId);\n\n const head = document.head;\n if (head.childNodes.length > 0) {\n head.insertBefore(style, head.childNodes[0]);\n } else {\n head.appendChild(style);\n }\n }\n }\n\n /**\n * The tooltip handler function.\n */\n private tooltipHandler(handler: any, event: MouseEvent, item: any, value: any) {\n // console.log(handler, event, item, value);\n\n // append a div element that we use as a tooltip unless it already exists\n this.el = document.getElementById(this.options.id);\n if (!this.el) {\n this.el = document.createElement('div');\n this.el.setAttribute('id', this.options.id);\n this.el.classList.add('vg-tooltip');\n\n const tooltipContainer = document.fullscreenElement ?? document.body;\n tooltipContainer.appendChild(this.el);\n }\n\n // hide tooltip for null, undefined, or empty string values\n if (value == null || value === '') {\n this.el.classList.remove('visible', `${this.options.theme}-theme`);\n return;\n }\n\n // set the tooltip content\n this.el.innerHTML = this.options.formatTooltip(value, this.options.sanitize, this.options.maxDepth);\n\n // make the tooltip visible\n this.el.classList.add('visible', `${this.options.theme}-theme`);\n\n const {x, y} = calculatePosition(\n event,\n this.el.getBoundingClientRect(),\n this.options.offsetX,\n this.options.offsetY\n );\n\n this.el.setAttribute('style', `top: ${y}px; left: ${x}px`);\n }\n}\n","import pkg from '../package.json';\nimport {View} from 'vega-typings';\n\nimport {Options} from './defaults';\nimport {Handler} from './Handler';\n\nconst version = pkg.version;\n\nexport * from './defaults';\nexport * from './formatValue';\nexport * from './position';\nexport * from './Handler';\nexport {version};\n\n/**\n * Create a tooltip handler and register it with the provided view.\n *\n * @param view The Vega view.\n * @param opt Tooltip options.\n */\nexport default function (view: View, opt?: Options) {\n const handler = new Handler(opt);\n\n view.tooltip(handler.call).run();\n\n return handler;\n}\n"],"names":["isArray","isString","isObject"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAEA;;;;;EAKG;WACa,WAAW,CAAC,KAAU,EAAE,WAAmC,EAAE,QAAgB,EAAA;EAC3F,IAAA,IAAIA,gBAAO,CAAC,KAAK,CAAC,EAAE;EAClB,QAAA,OAAO,WAAI,KAAK,CAAC,GAAG,CAAC,UAAC,CAAC,EAAA,EAAK,OAAA,WAAW,CAACC,iBAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,CAArD,EAAqD,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