Skip to content

Commit

Permalink
Code clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
Eli Fenton committed Jan 15, 2024
1 parent 7b1b751 commit 80033ef
Showing 1 changed file with 51 additions and 56 deletions.
107 changes: 51 additions & 56 deletions script.js
Expand Up @@ -4,35 +4,36 @@
* @type {{show:function, hide:function}}
*/
var autotooltip = function($) {
var timer;
var MAX_WIDTH = 500;
var moveCount = 0;
var isVisible;
var tt;
var m_tt;
var m_visible;
var m_timer;
var m_moveCounter = 0;

/**
* Initialize the module.
*
* @private
*/
var _init = function() {
if (!tt) {
tt = $('<div class="plugin-autotooltip_tip" role="tooltip"></div>');
// Cover the various templates.
var container = $('.dokuwiki .bodyContent, .dokuwiki .wiki-content, #dokuwiki__content');
// Use the root .dokuwiki if we have to, though we might lose some font information.
if (!container.length) {
container = $('.dokuwiki');
}
// In case the template is really strange.
if (!container.length) {
container = $('body');
}
container.first().append(tt);
if (m_tt) {
return;
}

m_tt = $('<div class="plugin-autotooltip_tip" role="tooltip"></div>');
// Cover the various templates.
var container = $('.dokuwiki .bodyContent, .dokuwiki .wiki-content, #dokuwiki__content');
// Use the root .dokuwiki if we have to, though we might lose some font information.
if (!container.length) {
container = $('.dokuwiki');
}
// In case the template is really strange.
if (!container.length) {
container = $('body');
}
container.first().append(m_tt);

$(document).on('mousemove', _moveThrottled);
_init = function() {}; // Only once.
$(document).on('mousemove', _moveDebounced);
};


Expand All @@ -43,7 +44,10 @@ var autotooltip = function($) {
* @private
*/
var _move = function(e) {
var top = Math.max(e.pageY - window.scrollY - tt.outerHeight() - 4, 8);
if (!m_visible) {
return;
}
var top = Math.max(e.pageY - window.scrollY - m_tt.outerHeight() - 4, 8);
var left = Math.max(e.pageX + 4, 8);
var right = '';
var winWidth = window.innerWidth;
Expand All @@ -59,7 +63,7 @@ var autotooltip = function($) {
width = Math.min(winWidth - e.pageX - 4, MAX_WIDTH);
}

tt.css({top: top + 'px', left: left, right: right, width: 'auto', 'max-width': width + 'px'});
m_tt.css({top: top + 'px', left: left, right: right, width: 'auto', 'max-width': width + 'px'});
};

/**
Expand All @@ -68,59 +72,50 @@ var autotooltip = function($) {
* @param {MouseEvent} e
* @private
*/
var _moveThrottled = function(e) {
if (isVisible) {
var localMoveCount = ++moveCount;
requestAnimationFrame(function() {
if (localMoveCount == moveCount) {
_move(e);
}
});
var _moveDebounced = function(e) {
if (!m_visible) {
return;
}
var closureCounter = ++m_moveCounter;
requestAnimationFrame(function() {
if (closureCounter == m_moveCounter) {
_move(e);
}
});
};


/**
* Show the tooltip with the given HTML.
*
* @param {MouseEvent} evt
* @param {String} html - The HTML content of the tooltip.
* @param {String} classes - CSS classes to add.
* @param {int} delay - Delay, in ms.
* @private
*/
var _show = function(evt, html, classes, delay) {
delay = parseInt(delay) || 50;
tt.html(html).attr('class', 'plugin-autotooltip_tip ' + classes);
_move(evt);
isVisible = true;
clearInterval(timer);
timer = setTimeout(function() {
tt.addClass('plugin-autotooltip--visible');
}, delay);
};


return {
/**
* Show a tooltip.
*
* @param {MouseEvent} evt
*/
show: function(evt) {
var elt = evt.currentTarget;
m_visible = true;
_init();
_show(evt, $('.plugin-autotooltip-hidden-tip', elt).html(), $('.plugin-autotooltip-hidden-classes', elt).text(), $(elt).attr('data-delay'));

var elt = evt.currentTarget;
m_tt
.html($('.plugin-autotooltip-hidden-tip', elt).html())
.attr('class', 'plugin-autotooltip_tip .plugin-autotooltip-hidden-classes');
// This isn't strictly needed because of the attachment to document.mousemove,
// but it forces proper initial placement when the mouse is moving rapidly (so
// move is throttled).
_move(evt);
clearInterval(m_timer);
m_timer = setTimeout(function() {
m_tt.addClass('plugin-autotooltip--visible');
}, parseInt($(elt).attr('data-delay')) || 50);
},


/**
* Hide the tooltip.
*/
hide: function() {
isVisible = false;
timer = setTimeout(function() {
tt.removeClass('plugin-autotooltip--visible');
m_visible = false;
m_timer = setTimeout(function() {
m_tt.removeClass('plugin-autotooltip--visible');
}, 50);
}
};
Expand Down

0 comments on commit 80033ef

Please sign in to comment.