MediaWiki:Gadget-TimelinePreview.js

From Angelina Jordan Wiki
Revision as of 22:11, 11 November 2025 by Most2dot0 (talk | contribs) (retain bold and italics formatting for text of stripped links)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// Gadget: Timeline hover previews (no suppression; system-popup replace + fallback custom popup)
// Requires: mediawiki.api, jquery, mediawiki.util

mw.loader.using(['mediawiki.api', 'jquery', 'mediawiki.util']).then(function () {
    var api = new mw.Api();
    var timelineCache = null;

	function capitalizeFirst(str) {
	    return str.charAt(0).toUpperCase() + str.slice(1);
	}

    // Custom popup (fallback) - simple white box that won't try to hide system popups
    var customPopup = $('<div>')
        .attr('id', 'timeline-preview-fallback')
        .css({
            position: 'absolute',
            zIndex: 1100,
            background: '#fff',
            border: '1px solid #a2a9b1',
            borderRadius: '2px',
	        padding: '0.75em',  // inner margin
	        fontWeight: '450',
	        fontSize: '0.85em', // <- new smaller font            boxShadow: '0 1px 3px rgba(0,0,0,0.2)',
            maxWidth: '360px',
            display: 'none'
        })
        .appendTo(document.body);

    function loadTimeline() {
        if (timelineCache) return $.Deferred().resolve();
        return api.get({
            action: 'parse',
            page: 'Timeline',
            prop: 'text',
            formatversion: 2
        }).then(function (data) {
            var html = $('<div>').html(data.parse.text);
            timelineCache = {};
            html.find('dt').each(function () {
                var dt = $(this);
                var id = dt.find('span[id]').attr('id');
                if (!id) return;
                var ddList = [];
                var el = dt.next();
                while (el.length && el.prop('tagName') === 'DD') {
                    var clone = el.clone();
                    // strip citation superscripts
                    clone.find('sup.reference').remove();
					// replace links with their inner HTML (preserves <b>, <i>, etc.)
					clone.find('a').replaceWith(function () {
					    return $(this).html();
					});
                    ddList.push(clone.html());
                    el = el.next();
                }
                timelineCache[id] = capitalizeFirst(ddList.join('<br>').trim()); // <- capitalize
            });
        });
    }

    // Heuristic: identify system popup elements when they are added
	function replaceSystemPopupContent(popupNode, html) {
	    try {
	        var $p = $(popupNode);
	        var extract = $p.find('.mwe-popups-extract, .popups-extract, .mwe-popups-container').first();
	
	        // Wrap our content in a small padding container
	        var contentWithPadding = $('<div>').css({ padding: '1.5em', fontWeight: '450' }).html(html);
	
	        if (extract && extract.length) {
	            extract.html(contentWithPadding);
	        } else {
	            // fallback: try replace an inner container, otherwise replace entire popup
	            var inner = $p.find('*').first();
	            if (inner && inner.length) inner.html(contentWithPadding);
	            else $p.html(contentWithPadding);
	        }
	    } catch (e) {
	        // ignore errors
	    }
	}

    // Observe once for system popup nodes being added; disconnect after first match or timeout
    function watchForSystemPopupOnce(html, timeoutMs, onFound) {
        timeoutMs = timeoutMs || 1500;
        var found = false;
        var observer = new MutationObserver(function (mutations, obs) {
            for (var i = 0; i < mutations.length; i++) {
                var m = mutations[i];
                for (var j = 0; j < m.addedNodes.length; j++) {
                    var node = m.addedNodes[j];
                    if (node.nodeType !== 1) continue;
                    var cls = node.className || '';
                    if (/\bmwe-popups\b/.test(cls) || /\bpopups\b/.test(cls) || /\bpopups-container\b/.test(cls)) {
                        found = true;
                        replaceSystemPopupContent(node, html);
                        if (typeof onFound === 'function') onFound(node);
                        obs.disconnect();
                        return;
                    }
                    var $node = $(node);
                    var foundEl = $node.find('.mwe-popups, .popups, .popups-container, .mwe-popups-extract, .popups-extract').first();
                    if (foundEl && foundEl.length) {
                        found = true;
                        replaceSystemPopupContent(foundEl.get(0), html);
                        if (typeof onFound === 'function') onFound(foundEl.get(0));
                        obs.disconnect();
                        return;
                    }
                }
            }
        });
        observer.observe(document.body, { childList: true, subtree: true });
        var to = setTimeout(function () {
            if (!found) {
                try { observer.disconnect(); } catch (e) {}
            }
        }, timeoutMs);
        return {
            disconnect: function () { clearTimeout(to); try { observer.disconnect(); } catch (e) {} }
        };
    }

    function showFallbackPopup($link, html) {
        customPopup.html(html).show();
        var offset = $link.offset();
        customPopup.css({
            top: offset.top + $link.outerHeight() + 6,
            left: offset.left
        });
    }
    function hideFallbackPopup() { customPopup.hide(); }

    function onLinkEnter() {
        var $link = $(this);
        var href = $link.attr('href') || '';
        var match = href.match(/(?:https?:\/\/[^\/]+)?\/wiki\/Timeline#(\d{4}-\d{2}-\d{2})$/)
          || href.match(/\/Timeline#(\d{4}-\d{2}-\d{2})$/)
          || href.match(/^#(\d{4}-\d{2}-\d{2})$/);
        if (!match) return;
        var id = match[1];

        loadTimeline().then(function () {
            var content = timelineCache && timelineCache[id];
            if (!content) return;

            // Start watching for a system popup to replace. If found, we replace it and hide the fallback.
            var handle = watchForSystemPopupOnce(content, 2000, function () {
				clearTimeout(fallbackTimer);                
				hideFallbackPopup();
            });

            // Fallback: show our custom popup after a short delay if no system popup yet
			var isTimelinePage = mw.config.get('wgPageName') === 'Timeline';
			var fallbackDelay = isTimelinePage ? 100 : 1000; // shorter timeout on Timeline page
			
			var fallbackTimer = setTimeout(function () {
			    showFallbackPopup($link, content);
			}, fallbackDelay);

            // cleanup on leave
            $link.one('mouseleave.timelineCleanup', function () {
                clearTimeout(fallbackTimer);
                try { handle.disconnect(); } catch (e) {}
                hideFallbackPopup();
                $link.off('mouseleave.timelineCleanup');
            });
        });
    }

    function onLinkLeave() {
        hideFallbackPopup();
        // remove any one-time cleanup that may not have fired
        $(this).off('mouseleave.timelineCleanup');
    }

	var timelineLinkSelector = 'a[href*="Timeline#"], a[href^="#"]';
	
	// Delegated event binding: catches all current and future links
	$(document)
	    .on('mouseenter.timelinePreview', timelineLinkSelector, onLinkEnter)
	    .on('mouseleave.timelinePreview', timelineLinkSelector, onLinkLeave);
});