Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 55 additions & 41 deletions packages/core/src/view/handler/TooltipHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,38 @@ import EventSource from '../event/EventSource';
class TooltipHandler implements GraphPlugin {
static pluginId = 'TooltipHandler';

/**
* Creates the tooltip element and appends it to the document body.
*
*/
init() {
if (document.body) {
this.div = document.createElement('div');
this.div.className = 'mxTooltip';
this.div.style.visibility = 'hidden';
document.body.appendChild(this.div);

InternalEvent.addGestureListeners(this.div, (evt) => {
const source = getSource(evt);
// @ts-ignore nodeName may exist
if (source && source.nodeName !== 'A') {
this.hideTooltip();
}
});

// Hides tooltips and resets tooltip timer if mouse leaves container
InternalEvent.addListener(
this.graph.getContainer(),
'mouseleave',
(evt: MouseEvent) => {
if (this.div !== evt.relatedTarget) {
this.hide();
}
}
);
}
}

/**
* Constructs an event handler that displays tooltips.
*
Expand All @@ -49,35 +81,9 @@ class TooltipHandler implements GraphPlugin {
constructor(graph: Graph) {
this.graph = graph;
this.graph.addMouseListener(this);

this.div = document.createElement('div');
this.div.className = 'mxTooltip';
this.div.style.visibility = 'hidden';

document.body.appendChild(this.div);

InternalEvent.addGestureListeners(this.div, (evt) => {
const source = getSource(evt);

// @ts-ignore nodeName may exist
if (source && source.nodeName !== 'A') {
this.hideTooltip();
}
});

// Hides tooltips and resets tooltip timer if mouse leaves container
InternalEvent.addListener(
this.graph.getContainer(),
'mouseleave',
(evt: MouseEvent) => {
if (this.div !== evt.relatedTarget) {
this.hide();
}
}
);
}

div: HTMLElement;
div?: HTMLElement | null;

/**
* Specifies the zIndex for the tooltip and its shadow.
Expand Down Expand Up @@ -231,7 +237,7 @@ class TooltipHandler implements GraphPlugin {
restart &&
this.isEnabled() &&
state &&
this.div.style.visibility === 'hidden'
(!this.div || this.div.style.visibility == 'hidden')
) {
const node = me.getSource();
const x = me.getX();
Expand Down Expand Up @@ -280,8 +286,10 @@ class TooltipHandler implements GraphPlugin {
* Hides the tooltip.
*/
hideTooltip() {
this.div.style.visibility = 'hidden';
this.div.innerHTML = '';
if (this.div) {
this.div.style.visibility = 'hidden';
this.div.innerHTML = '';
}
}

/**
Expand All @@ -291,20 +299,22 @@ class TooltipHandler implements GraphPlugin {
show(tip: HTMLElement | string | null, x: number, y: number) {
if (!this.destroyed && tip && tip !== '') {
const origin = getScrollOrigin();

this.div.style.zIndex = String(this.zIndex);
this.div.style.left = `${x + origin.x}px`;
this.div.style.top = `${y + TOOLTIP_VERTICAL_OFFSET + origin.y}px`;
if (!this.div) {
this.init();
}
this.div!.style.zIndex = String(this.zIndex);
this.div!.style.left = `${x + origin.x}px`;
this.div!.style.top = `${y + TOOLTIP_VERTICAL_OFFSET + origin.y}px`;

if (!isNode(tip)) {
this.div.innerHTML = (tip as string).replace(/\n/g, '<br>');
this.div!.innerHTML = (tip as string).replace(/\n/g, '<br>');
} else {
this.div.innerHTML = '';
this.div.appendChild(tip as HTMLElement);
this.div!.innerHTML = '';
this.div!.appendChild(tip as HTMLElement);
}

this.div.style.visibility = '';
fit(this.div);
this.div!.style.visibility = '';
fit(this.div!);
}
}

Expand All @@ -313,14 +323,18 @@ class TooltipHandler implements GraphPlugin {
*/
onDestroy() {
if (!this.destroyed) {
this.resetTimer();
this.graph.removeMouseListener(this);
InternalEvent.release(this.div);
if (this.div) {
InternalEvent.release(this.div);
}

if (this.div.parentNode) {
if (this.div?.parentNode) {
this.div.parentNode.removeChild(this.div);
}

this.destroyed = true;
this.div = null;
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/html/.storybook/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ const resetMaxGraphConfigs = (): void => {
// This function is a workaround to destroy mxGraph elements that are not released by the previous story.
// See https://github.com/maxGraph/maxGraph/issues/400
function destroyUnreleasedElements() {
document.querySelectorAll('.mxPopupMenu,.mxWindow').forEach((e) => e.remove());
document
.querySelectorAll('.mxPopupMenu,.mxTooltip,.mxWindow')
.forEach((e) => e.remove());
}

const preview: Preview = {
Expand Down