MediaWiki:Common.js: Difference between revisions

From PRS
Jump to navigation Jump to search
No edit summary
No edit summary
 
(11 intermediate revisions by the same user not shown)
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 ── */
/* ============================================================
$(function () {
  Training Video – Video source loader + Synced Transcript
  ============================================================ */
 
mw.hook('wikipage.content').add(function () {
 
     if ($('.training-video-wrap').length === 0) return;
     if ($('.training-video-wrap').length === 0) return;


     function startWatching() {
     var $video = $('.training-video-wrap video');
        var video = document.querySelector('.mw-file-element');
    if ($video.length === 0) return;
        if (!video) {
 
             setTimeout(startWatching, 100);
    var videoEl = $video[0];
             return;
    var filename = $video.attr('data-file');
 
    if (!filename) return;
 
    /* ── Use MediaWiki API to get the real file URL ── */
    $.ajax({
        url: '/wiki/api.php',
        data: {
            action: 'query',
            titles: 'File:' + filename,
            prop: 'imageinfo',
            iiprop: 'url',
            format: 'json'
        },
        dataType: 'json',
        success: function (data) {
             var pages = data.query.pages;
            var page = pages[Object.keys(pages)[0]];
             if (page.imageinfo && page.imageinfo[0]) {
                var url = page.imageinfo[0].url;
                videoEl.src = url;
                videoEl.load();
            }
         }
         }
    });
    /* ── Format seconds → m:ss ── */
    function fmt(s) {
        s = Math.floor(s);
        return Math.floor(s / 60) + ':' + ('0' + (s % 60)).slice(-2);
    }
    var $entries  = $('.ts-entry');
    var $body    = $('.ts-scroll-body');
    var $markers  = $('.ts-progress-marker');
    var $fill    = $('.ts-progress-fill');
    var $timeDisp = $('.ts-time-display');
    /* ── Collect timestamp data from DOM ── */
    var timestamps = [];
    $entries.each(function () {
        timestamps.push(parseInt($(this).data('time'), 10));
    });


         console.log('=== TMH DIAGNOSTIC STARTED ===');
    /* ── Highlight the active transcript entry ── */
         console.log('Initial video attributes:');
    function highlightActive() {
         for (var i = 0; i < video.attributes.length; i++) {
         var cur = videoEl.currentTime;
             console.log(' ' + video.attributes[i].name + '="' + video.attributes[i].value + '"');
         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);
        });
        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);
            }
         }
         }
    }


        /* Watch for any attribute changes on the video element */
    /* ── Update progress bar ── */
        var observer = new MutationObserver(function (mutations) {
    function updateProgress() {
            mutations.forEach(function (mutation) {
        if (!videoEl.duration) return;
                if (mutation.type === 'attributes') {
        var pct = (videoEl.currentTime / videoEl.duration) * 100;
                    console.log('ATTR CHANGED: ' + mutation.attributeName + ' = "' + mutation.target.getAttribute(mutation.attributeName) + '" (was: "' + mutation.oldValue + '")');
        $fill.css('width', pct + '%');
                }
        $timeDisp.text(fmt(videoEl.currentTime) + ' / ' + fmt(videoEl.duration));
                if (mutation.type === 'childList') {
    }
                    console.log('CHILD ADDED/REMOVED on: ' + mutation.target.className);
 
                }
    /* ── Bind native video events ── */
             });
    $video.on('timeupdate', function () {
        highlightActive();
        updateProgress();
    });
 
    $video.on('loadedmetadata', function () {
        $timeDisp.text('0:00 / ' + fmt(videoEl.duration));
        $markers.each(function () {
            var t = parseInt($(this).data('time'), 10);
             $(this).css('left', ((t / videoEl.duration) * 100) + '%');
         });
         });
    });


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


        /* Also watch the parent container */
    /* ── Progress bar click → seek ── */
        var parent = document.querySelector('.mw-tmh-player');
    $('.ts-progress-track').on('click', function (e) {
         if (parent) {
         var rect  = this.getBoundingClientRect();
            observer.observe(parent, {
        var ratio = Math.max(0, Math.min(1, (e.clientX - rect.left) / rect.width));
                attributes: true,
        videoEl.currentTime = ratio * videoEl.duration;
                attributeOldValue: true,
    });
                childList: true,
                subtree: false
            });
        }


         console.log('=== NOW WATCHING FOR CHANGES ===');
    /* ── 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();
     });


    startWatching();
});
});
/* ============================================================ */

Latest revision as of 02:27, 23 April 2026

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

/* ============================================================
   Training Video – Video source loader + Synced Transcript
   ============================================================ */

mw.hook('wikipage.content').add(function () {

    if ($('.training-video-wrap').length === 0) return;

    var $video = $('.training-video-wrap video');
    if ($video.length === 0) return;

    var videoEl = $video[0];
    var filename = $video.attr('data-file');

    if (!filename) return;

    /* ── Use MediaWiki API to get the real file URL ── */
    $.ajax({
        url: '/wiki/api.php',
        data: {
            action: 'query',
            titles: 'File:' + filename,
            prop: 'imageinfo',
            iiprop: 'url',
            format: 'json'
        },
        dataType: 'json',
        success: function (data) {
            var pages = data.query.pages;
            var page = pages[Object.keys(pages)[0]];
            if (page.imageinfo && page.imageinfo[0]) {
                var url = page.imageinfo[0].url;
                videoEl.src = url;
                videoEl.load();
            }
        }
    });

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

    var $entries  = $('.ts-entry');
    var $body     = $('.ts-scroll-body');
    var $markers  = $('.ts-progress-marker');
    var $fill     = $('.ts-progress-fill');
    var $timeDisp = $('.ts-time-display');

    /* ── 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);
        });
        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));
        $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();
    });

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