This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/lib/tooltip.js.es6
Joffrey JAFFEUX 9554d9c56a
UX: tooltips and improvements to new dashboard
- tooltips
- revert chart title UI
- reduce period chooser font-size
- localize dates of data points
- fix a bug where multiple reports were loaded at the same time
- fix a bug where % was not showing anymore
- remove spacing at the top
- remove loadingTitle feature (Loading...%report name%) incompatible with new hijack design
2018-05-16 16:45:21 +02:00

74 lines
1.7 KiB
JavaScript

export function showTooltip() {
const fadeSpeed = 300;
const tooltipID = "#discourse-tooltip";
const $this = $(this);
const $parent = $this.offsetParent();
const content = $this.attr("data-tooltip");
const retina = window.devicePixelRatio && window.devicePixelRatio > 1 ? "class='retina'" : "";
let pos = $this.offset();
const delta = $parent.offset();
pos.top -= delta.top;
pos.left -= delta.left;
$(tooltipID).fadeOut(fadeSpeed).remove();
$(this).after(`
<div id="discourse-tooltip" ${retina}>
<div class="tooltip-pointer"></div>
<div class="tooltip-content">${content}</div>
</div>
`);
$(window).on("click.discourse", (event) => {
if ($(event.target).closest(tooltipID).length === 0) {
$(tooltipID).remove();
$(window).off("click.discourse");
}
return true;
});
const $tooltip = $(tooltipID);
$tooltip.css({top: 0, left: 0});
let left = (pos.left - ($tooltip.width() / 2) + $this.width()/2);
if (left < 0) {
$tooltip.find(".tooltip-pointer").css({
"margin-left": left*2 + "px"
});
left = 0;
}
// also do a right margin fix
const parentWidth = $parent.width();
if (left + $tooltip.width() > parentWidth) {
let oldLeft = left;
left = parentWidth - $tooltip.width();
$tooltip.find(".tooltip-pointer").css({
"margin-left": (oldLeft - left) * 2 + "px"
});
}
$tooltip.css({
top: pos.top + 5 + "px",
left: left + "px"
});
$tooltip.fadeIn(fadeSpeed);
return false;
}
export function registerTooltip(jqueryContext) {
if (jqueryContext.length) {
jqueryContext.on('click', showTooltip);
}
}
export function unregisterTooltip(jqueryContext) {
if (jqueryContext.length) {
jqueryContext.off('click');
}
}