Skip to content

Commit feadeed

Browse files
lewisjbmarcortw
authored andcommitted
feat(timeline): refactor tooltip to only use one dom-element (visjs#2662)
1 parent 1c40890 commit feadeed

File tree

6 files changed

+22
-43
lines changed

6 files changed

+22
-43
lines changed

lib/timeline/component/ItemSet.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ function ItemSet(body, options) {
157157
this.selection = []; // list with the ids of all selected nodes
158158
this.stackDirty = true; // if true, all items will be restacked on next redraw
159159

160+
this.popup = null;
161+
160162
this.touchParams = {}; // stores properties while dragging
161163
this.groupTouchParams = {};
162164
// create the HTML DOM
@@ -889,12 +891,6 @@ ItemSet.prototype.removeItem = function(id) {
889891
// remove by id here, it is possible that an item has no id defined
890892
// itself, so better not delete by the item itself
891893
dataset.remove(id);
892-
893-
// Remove it's popup
894-
if (itemObj.popup) {
895-
itemObj.popup.destroy();
896-
itemObj.popup = null;
897-
}
898894
}
899895
});
900896
}
@@ -1899,17 +1895,26 @@ ItemSet.prototype._onMouseOver = function (event) {
18991895
return;
19001896
}
19011897

1902-
if (item.getTitle()) {
1903-
if (item.popup == null) {
1904-
item.setPopup(new Popup(this.body.dom.root, this.options.tooltip.overflowMethod || 'flip'));
1898+
var title = item.getTitle();
1899+
if (title) {
1900+
if (this.popup == null) {
1901+
this.popup = new Popup(this.body.dom.root,
1902+
this.options.tooltip.overflowMethod || 'flip');
19051903
}
19061904

1905+
this.popup.setText(title);
19071906
var container = this.body.dom.centerContainer;
1908-
item.popup.setPosition(
1907+
this.popup.setPosition(
19091908
event.clientX - util.getAbsoluteLeft(container) + container.offsetLeft,
19101909
event.clientY - util.getAbsoluteTop(container) + container.offsetTop
19111910
);
1912-
item.popup.show();
1911+
this.popup.show();
1912+
} else {
1913+
// Hovering over item without a title, hide popup
1914+
// Needed instead of _just_ in _onMouseOut due to #2572
1915+
if (this.popup != null) {
1916+
this.popup.hide();
1917+
}
19131918
}
19141919

19151920
this.body.emitter.emit('itemover', {
@@ -1928,8 +1933,8 @@ ItemSet.prototype._onMouseOut = function (event) {
19281933
return;
19291934
}
19301935

1931-
if (item.popup != null) {
1932-
item.popup.hide();
1936+
if (this.popup != null) {
1937+
this.popup.hide();
19331938
}
19341939

19351940
this.body.emitter.emit('itemout', {
@@ -1942,14 +1947,14 @@ ItemSet.prototype._onMouseMove = function (event) {
19421947
if (!item) return;
19431948

19441949
if (this.options.tooltip.followMouse) {
1945-
if (item.popup) {
1946-
if (!item.popup.hidden) {
1950+
if (this.popup) {
1951+
if (!this.popup.hidden) {
19471952
var container = this.body.dom.centerContainer;
1948-
item.popup.setPosition(
1953+
this.popup.setPosition(
19491954
event.clientX - util.getAbsoluteLeft(container) + container.offsetLeft,
19501955
event.clientY - util.getAbsoluteTop(container) + container.offsetTop
19511956
);
1952-
item.popup.show(); // Redraw
1957+
this.popup.show(); // Redraw
19531958
}
19541959
}
19551960
}

lib/timeline/component/item/BackgroundItem.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ BackgroundItem.prototype.redraw = function() {
100100
// - the item is selected/deselected
101101
if (this.dirty) {
102102
this._updateContents(this.dom.content);
103-
this._updateTitle();
104103
this._updateDataAttributes(this.dom.content);
105104
this._updateStyle(this.dom.box);
106105

lib/timeline/component/item/BoxItem.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ BoxItem.prototype.redraw = function() {
118118
// - the item is selected/deselected
119119
if (this.dirty) {
120120
this._updateContents(this.dom.content);
121-
this._updateTitle();
122121
this._updateDataAttributes(this.dom.box);
123122
this._updateStyle(this.dom.box);
124123

lib/timeline/component/item/Item.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ function Item (data, conversion, options) {
2323
this.displayed = false;
2424
this.groupShowing = true;
2525
this.dirty = true;
26-
this.popup = null;
2726

2827
this.top = null;
2928
this.right = null;
@@ -397,18 +396,6 @@ Item.prototype._updateContents = function (element) {
397396
}
398397
};
399398

400-
/**
401-
* Set HTML contents for the item
402-
* @private
403-
*/
404-
Item.prototype._updateTitle = function () {
405-
if (this.data.title != null) {
406-
if (this.popup != null) {
407-
this.popup.setText(this.data.title || '');
408-
}
409-
}
410-
};
411-
412399
/**
413400
* Process dataAttributes timeline option and set as data- attributes on dom.content
414401
* @param {Element} element HTML element to which the attributes will be attached
@@ -498,13 +485,4 @@ Item.prototype.getTitle = function () {
498485
return this.data.title;
499486
};
500487

501-
/**
502-
* Set the popup object, and update the title
503-
* @param {Popup} popup
504-
*/
505-
Item.prototype.setPopup = function (popup) {
506-
this.popup = popup;
507-
this._updateTitle();
508-
};
509-
510488
module.exports = Item;

lib/timeline/component/item/PointItem.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ PointItem.prototype.redraw = function() {
9696
// - the item is selected/deselected
9797
if (this.dirty) {
9898
this._updateContents(this.dom.content);
99-
this._updateTitle();
10099
this._updateDataAttributes(this.dom.point);
101100
this._updateStyle(this.dom.point);
102101

lib/timeline/component/item/RangeItem.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ RangeItem.prototype.redraw = function() {
100100
// - the item is selected/deselected
101101
if (this.dirty) {
102102
this._updateContents(this.dom.content);
103-
this._updateTitle();
104103
this._updateDataAttributes(this.dom.box);
105104
this._updateStyle(this.dom.box);
106105

0 commit comments

Comments
 (0)