Skip to content

Commit 48ea2c7

Browse files
TaroEldLordMidas
authored andcommitted
refactor: improve stack functions usage and pass-through data
- More consistent usage of the stack functions - Factor out pass-through data to proper member
1 parent 9ae43ca commit 48ea2c7

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

ui/mods/msu/nested_tooltips.js

+20-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ MSU.NestedTooltip = {
22
__regexp : /(?:\[|[)tooltip=([\w\.]+?)\.(.+?)(?:\]|])(.*?)(?:\[|[)\/tooltip(?:\]|])/gm,
33
__imgRegexp : /(?:\[|[)imgtooltip=([\w\.]+?)\.(.+?)(?:\]|])(.*?)(?:\[|[)\/imgtooltip(?:\]|])/gm,
44
__tooltipStack : [],
5+
__passThroughData : {},
56
__getTooltipHideDelay : function(){ return MSU.getSettingValue(MSU.ID, "hideDelay")}, // default 100,
67
__getTooltipShowDelay : function(){ return MSU.getSettingValue(MSU.ID, "showDelay")}, // default 200,
78
__getTooltipLockDelay : function(){ return MSU.getSettingValue(MSU.ID, "lockDelay")}, // default 1000,
@@ -100,18 +101,19 @@ MSU.NestedTooltip = {
100101
// ghetto clone to get new ref
101102
_tooltipParams = JSON.parse(JSON.stringify(_tooltipParams));
102103

103-
if (this.__tooltipStack.length > 0)
104+
if (!this.isStackEmpty())
104105
{
105106
// check if this is within the same chain of nested tooltips, or if we need to clear the stack and start a new chain
106-
if (this.__tooltipStack[this.__tooltipStack.length -1].tooltip.container.find(_sourceContainer).length === 0)
107+
if (this.getTopOfStack().tooltip.container.find(_sourceContainer).length === 0)
107108
{
108109
self.clearStack();
109110
}
110111
// If we already have tooltips in the stack, we want to fetch the one from the first tooltip that will have received the entityId from the vanilla function
111112
else
112113
{
113-
$.each(this.__tooltipStack[0].dataToPass, function(_key, _value)
114+
$.each(self.__passThroughData, function(_key, _value)
114115
{
116+
// don't overwrite parameters we already have
115117
if (_key in _tooltipParams)
116118
return;
117119
_tooltipParams[_key] = _value;
@@ -139,6 +141,9 @@ MSU.NestedTooltip = {
139141
},
140142
updateStack : function ()
141143
{
144+
// descends the stack and removes tooltips until it finds one that should remain:
145+
// - Either its source is hovered and it's still on the screen
146+
// - Or the resulting tooltip is hovered and still on the screen
142147
for (var i = this.__tooltipStack.length - 1; i >= 0; i--)
143148
{
144149
var pairData = this.__tooltipStack[i];
@@ -162,7 +167,7 @@ MSU.NestedTooltip = {
162167
{
163168
return this.isStackEmpty() ? null : this.__tooltipStack[this.__tooltipStack.length - 1];
164169
},
165-
removeTopTooltip : function()
170+
removeTopOfStack : function()
166171
{
167172
this.removeTooltip(this.__tooltipStack[this.__tooltipStack.length-1]);
168173
},
@@ -171,6 +176,11 @@ MSU.NestedTooltip = {
171176
this.cleanSourceContainer(_pairData.source.container);
172177
this.cleanTooltipContainer(_pairData.tooltip.container);
173178
this.__tooltipStack.pop();
179+
if (this.isStackEmpty())
180+
{
181+
// clear refs out of data
182+
this.__passThroughData = {};
183+
}
174184
},
175185
cleanSourceContainer : function(_sourceContainer)
176186
{
@@ -214,20 +224,18 @@ MSU.NestedTooltip = {
214224
source : sourceData,
215225
tooltip : tooltipData
216226
}
227+
this.__tooltipStack.push(stackData);
217228

218229
// Add data that we'll want to pass to any nested tooltips, such as entityId
219-
if (this.__tooltipStack.length == 0)
230+
if (this.isStackEmpty())
220231
{
221-
var dataToPass = {};
222232
$.each(_tooltipParams, function(_key, _value)
223233
{
224234
if (_key === "contentType" || _key === "elementId")
225235
return;
226-
dataToPass[_key] = _value;
236+
self.__passThroughData[_key] = _value;
227237
})
228-
stackData.dataToPass = dataToPass;
229238
}
230-
this.__tooltipStack.push(stackData);
231239

232240
this.addTooltipLockHandler(tooltipContainer, _sourceContainer);
233241

@@ -329,10 +337,10 @@ MSU.NestedTooltip = {
329337
if (_event.which == 1)
330338
{
331339
_event.stopPropagation();
332-
self.removeTopTooltip();
333-
if (self.__tooltipStack.length > 0)
340+
self.removeTopOfStack();
341+
if (!self.isStackEmpty())
334342
{
335-
self.__tooltipStack[self.__tooltipStack.length-1].tooltip.container.trigger('mouseenter.msu-tooltip-container');
343+
self.getTopOfStack().tooltip.container.trigger('mouseenter.msu-tooltip-container');
336344
}
337345
}
338346
});

0 commit comments

Comments
 (0)