MediaWiki:Common.js: Difference between revisions

From PRS
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load. */


/* ── DIAGNOSTIC: Watch what TMH does to the video element ── */
/* ── Prevent TMH from removing native video controls ── */
$(function () {
$(function () {
     if ($('.training-video-wrap').length === 0) return;
     if ($('.training-video-wrap').length === 0) return;


     function startWatching() {
     function startGuarding() {
         var video = document.querySelector('.mw-file-element');
         var video = document.querySelector('.mw-file-element');
         if (!video) {
         if (!video) {
             setTimeout(startWatching, 100);
             setTimeout(startGuarding, 100);
             return;
             return;
         }
         }


        console.log('=== TMH DIAGNOSTIC STARTED ===');
         /* Watch for TMH removing the controls attribute and restore it immediately */
        console.log('Initial video attributes:');
        for (var i = 0; i < video.attributes.length; i++) {
            console.log('  ' + video.attributes[i].name + '="' + video.attributes[i].value + '"');
        }
 
         /* Watch for any attribute changes on the video element */
         var observer = new MutationObserver(function (mutations) {
         var observer = new MutationObserver(function (mutations) {
             mutations.forEach(function (mutation) {
             mutations.forEach(function (mutation) {
                 if (mutation.type === 'attributes') {
                 if (mutation.attributeName === 'controls') {
                     console.log('ATTR CHANGED: ' + mutation.attributeName + ' = "' + mutation.target.getAttribute(mutation.attributeName) + '" (was: "' + mutation.oldValue + '")');
                     var current = video.getAttribute('controls');
                }
                    if (current === null) {
                if (mutation.type === 'childList') {
                        video.setAttribute('controls', '');
                    console.log('CHILD ADDED/REMOVED on: ' + mutation.target.className);
                    }
                 }
                 }
             });
             });
Line 32: Line 26:
         observer.observe(video, {
         observer.observe(video, {
             attributes: true,
             attributes: true,
             attributeOldValue: true,
             attributeFilter: ['controls']
            childList: true,
            subtree: true
         });
         });


         /* Also watch the parent container */
         /* Make sure controls are set initially */
         var parent = document.querySelector('.mw-tmh-player');
         video.setAttribute('controls', '');
         if (parent) {
    }
             observer.observe(parent, {
 
                attributes: true,
    startGuarding();
                attributeOldValue: true,
});
                 childList: true,
 
                subtree: false
/* ============================================================
             });
  Training Video – Synced Transcript Sidebar
  ============================================================ */
$(function () {
    /* Only run on pages that contain a training video wrapper */
    if ($('.training-video-wrap').length === 0) return;
 
    /* ── Find elements ── */
    var $video      = $('.training-video-wrap video');
    var $entries    = $('.ts-entry');
    var $body      = $('.ts-scroll-body');
    var $markers    = $('.ts-progress-marker');
    var $fill      = $('.ts-progress-fill');
    var $timeDisp  = $('.ts-time-display');
 
    if ($video.length === 0) return; // no video on this page
 
    var videoEl = $video[0];
 
    /* ── Format seconds → m:ss ── */
    function fmt(s) {
        s = Math.floor(s);
        return Math.floor(s / 60) + ':' + ('0' + (s % 60)).slice(-2);
    }
 
    /* ── Collect timestamp data from DOM ── */
    var timestamps = [];
    $entries.each(function () {
        timestamps.push(parseInt($(this).data('time'), 10));
    });
 
    /* ── Highlight the active transcript entry ── */
    function highlightActive() {
        var cur = videoEl.currentTime;
        var activeIdx = 0;
         for (var i = 0; i < timestamps.length; i++) {
            if (cur >= timestamps[i]) activeIdx = i;
        }
        $entries.each(function (i) {
             $(this).toggleClass('ts-active', i === activeIdx);
        });
        /* Auto-scroll transcript panel to keep active line visible */
        var $active = $entries.eq(activeIdx);
        if ($active.length && $body.length) {
            var entryTop      = $active.position().top;
            var bodyScrollTop = $body.scrollTop();
            var bodyHeight    = $body.height();
            if (entryTop < 0 || entryTop > bodyHeight - 60) {
                 $body.animate({ scrollTop: bodyScrollTop + entryTop - 60 }, 200);
             }
         }
         }
    }


         console.log('=== NOW WATCHING FOR CHANGES ===');
    /* ── Update progress bar ── */
    function updateProgress() {
        if (!videoEl.duration) return;
        var pct = (videoEl.currentTime / videoEl.duration) * 100;
        $fill.css('width', pct + '%');
         $timeDisp.text(fmt(videoEl.currentTime) + ' / ' + fmt(videoEl.duration));
     }
     }


     startWatching();
     /* ── Bind native video events ── */
    $video.on('timeupdate', function () {
        highlightActive();
        updateProgress();
    });
 
    $video.on('loadedmetadata', function () {
        $timeDisp.text('0:00 / ' + fmt(videoEl.duration));
        /* Position progress bar markers once duration is known */
        $markers.each(function () {
            var t = parseInt($(this).data('time'), 10);
            $(this).css('left', ((t / videoEl.duration) * 100) + '%');
        });
    });
 
    /* ── Transcript entry click → seek video ── */
    $entries.on('click', function () {
        var t = parseInt($(this).data('time'), 10);
        videoEl.currentTime = t;
        if (videoEl.paused) videoEl.play();
        highlightActive();
    });
 
    /* ── Progress bar click → seek ── */
    $('.ts-progress-track').on('click', function (e) {
        var rect  = this.getBoundingClientRect();
        var ratio = Math.max(0, Math.min(1, (e.clientX - rect.left) / rect.width));
        videoEl.currentTime = ratio * videoEl.duration;
    });
 
    /* ── Marker click → seek ── */
    $markers.on('click', function (e) {
        e.stopPropagation();
        var t = parseInt($(this).data('time'), 10);
        videoEl.currentTime = t;
        if (videoEl.paused) videoEl.play();
    });
 
});
});
/* ============================================================ */

Revision as of 08:19, 22 April 2026

/* Any JavaScript here will be loaded for all users on every page load. */

/* ── Prevent TMH from removing native video controls ── */
$(function () {
    if ($('.training-video-wrap').length === 0) return;

    function startGuarding() {
        var video = document.querySelector('.mw-file-element');
        if (!video) {
            setTimeout(startGuarding, 100);
            return;
        }

        /* Watch for TMH removing the controls attribute and restore it immediately */
        var observer = new MutationObserver(function (mutations) {
            mutations.forEach(function (mutation) {
                if (mutation.attributeName === 'controls') {
                    var current = video.getAttribute('controls');
                    if (current === null) {
                        video.setAttribute('controls', '');
                    }
                }
            });
        });

        observer.observe(video, {
            attributes: true,
            attributeFilter: ['controls']
        });

        /* Make sure controls are set initially */
        video.setAttribute('controls', '');
    }

    startGuarding();
});

/* ============================================================
   Training Video – Synced Transcript Sidebar
   ============================================================ */
$(function () {
    /* Only run on pages that contain a training video wrapper */
    if ($('.training-video-wrap').length === 0) return;

    /* ── Find elements ── */
    var $video      = $('.training-video-wrap video');
    var $entries    = $('.ts-entry');
    var $body       = $('.ts-scroll-body');
    var $markers    = $('.ts-progress-marker');
    var $fill       = $('.ts-progress-fill');
    var $timeDisp   = $('.ts-time-display');

    if ($video.length === 0) return; // no video on this page

    var videoEl = $video[0];

    /* ── Format seconds → m:ss ── */
    function fmt(s) {
        s = Math.floor(s);
        return Math.floor(s / 60) + ':' + ('0' + (s % 60)).slice(-2);
    }

    /* ── Collect timestamp data from DOM ── */
    var timestamps = [];
    $entries.each(function () {
        timestamps.push(parseInt($(this).data('time'), 10));
    });

    /* ── Highlight the active transcript entry ── */
    function highlightActive() {
        var cur = videoEl.currentTime;
        var activeIdx = 0;
        for (var i = 0; i < timestamps.length; i++) {
            if (cur >= timestamps[i]) activeIdx = i;
        }
        $entries.each(function (i) {
            $(this).toggleClass('ts-active', i === activeIdx);
        });
        /* Auto-scroll transcript panel to keep active line visible */
        var $active = $entries.eq(activeIdx);
        if ($active.length && $body.length) {
            var entryTop      = $active.position().top;
            var bodyScrollTop = $body.scrollTop();
            var bodyHeight    = $body.height();
            if (entryTop < 0 || entryTop > bodyHeight - 60) {
                $body.animate({ scrollTop: bodyScrollTop + entryTop - 60 }, 200);
            }
        }
    }

    /* ── Update progress bar ── */
    function updateProgress() {
        if (!videoEl.duration) return;
        var pct = (videoEl.currentTime / videoEl.duration) * 100;
        $fill.css('width', pct + '%');
        $timeDisp.text(fmt(videoEl.currentTime) + ' / ' + fmt(videoEl.duration));
    }

    /* ── Bind native video events ── */
    $video.on('timeupdate', function () {
        highlightActive();
        updateProgress();
    });

    $video.on('loadedmetadata', function () {
        $timeDisp.text('0:00 / ' + fmt(videoEl.duration));
        /* Position progress bar markers once duration is known */
        $markers.each(function () {
            var t = parseInt($(this).data('time'), 10);
            $(this).css('left', ((t / videoEl.duration) * 100) + '%');
        });
    });

    /* ── Transcript entry click → seek video ── */
    $entries.on('click', function () {
        var t = parseInt($(this).data('time'), 10);
        videoEl.currentTime = t;
        if (videoEl.paused) videoEl.play();
        highlightActive();
    });

    /* ── Progress bar click → seek ── */
    $('.ts-progress-track').on('click', function (e) {
        var rect  = this.getBoundingClientRect();
        var ratio = Math.max(0, Math.min(1, (e.clientX - rect.left) / rect.width));
        videoEl.currentTime = ratio * videoEl.duration;
    });

    /* ── Marker click → seek ── */
    $markers.on('click', function (e) {
        e.stopPropagation();
        var t = parseInt($(this).data('time'), 10);
        videoEl.currentTime = t;
        if (videoEl.paused) videoEl.play();
    });

});
/* ============================================================ */