From 80033ef7473989f63758effab258a2d7e6ccdc2f Mon Sep 17 00:00:00 2001 From: Eli Fenton Date: Mon, 15 Jan 2024 08:46:25 -0500 Subject: [PATCH] Code clean-up --- script.js | 107 ++++++++++++++++++++++++++---------------------------- 1 file changed, 51 insertions(+), 56 deletions(-) diff --git a/script.js b/script.js index 6b154ed..c8ec2e5 100644 --- a/script.js +++ b/script.js @@ -4,11 +4,11 @@ * @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. @@ -16,23 +16,24 @@ var autotooltip = function($) { * @private */ var _init = function() { - if (!tt) { - tt = $(''); - // 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 = $(''); + // 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); }; @@ -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; @@ -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'}); }; /** @@ -68,39 +72,18 @@ 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. @@ -108,9 +91,21 @@ var autotooltip = function($) { * @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); }, @@ -118,9 +113,9 @@ var autotooltip = function($) { * 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); } };