MediaWiki:Common.js
MediaWiki interface page
More actions
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.
/* BPM Wiki Interactive Waveform Audio Player */
( function () {
'use strict';
var activeAudio = null;
var allPlayers = [];
function formatTime( sec ) {
if ( !isFinite( sec ) || sec < 0 ) {
return '0:00';
}
var total = Math.floor( sec );
var m = Math.floor( total / 60 );
var s = total % 60;
return m + ':' + ( s < 10 ? '0' : '' ) + s;
}
function getColors( el ) {
var cs = window.getComputedStyle( el );
var rootCs = window.getComputedStyle( document.documentElement );
var played = rootCs.getPropertyValue( '--color-progressive' ).trim() || '#3366cc';
var hover = rootCs.getPropertyValue( '--color-progressive--hover' ).trim() || played;
var unplayed = rootCs.getPropertyValue( '--border-color-base' ).trim() || 'rgba(128,128,128,0.35)';
var isDark = document.documentElement.classList.contains( 'skin-theme-clientpref-night' ) ||
( document.documentElement.classList.contains( 'skin-theme-clientpref-os' ) &&
window.matchMedia && window.matchMedia( '(prefers-color-scheme: dark)' ).matches );
if ( !unplayed || unplayed === 'transparent' ) {
unplayed = isDark ? 'rgba(255,255,255,0.22)' : 'rgba(0,0,0,0.18)';
}
return { played: played, hover: hover, unplayed: unplayed, isDark: isDark };
}
function initPlayer( container ) {
if ( container.dataset.wfInit === '1' ) {
return;
}
container.dataset.wfInit = '1';
var audio = container.querySelector( 'audio.bpm-audio-native' );
var btn = container.querySelector( '.bpm-wf-btn' );
var wrap = container.querySelector( '.bpm-wf-wave-wrap' );
var canvas = container.querySelector( '.bpm-wf-canvas' );
var timeEl = container.querySelector( '.bpm-wf-time' );
if ( !audio || !btn || !wrap || !canvas || !timeEl ) {
return;
}
var peaks = [];
try {
peaks = JSON.parse( container.getAttribute( 'data-peaks' ) || '[]' );
} catch ( e ) {
peaks = [];
}
if ( !Array.isArray( peaks ) || peaks.length === 0 ) {
for ( var i = 0; i < 64; i++ ) {
peaks.push( 25 + Math.round( Math.abs( Math.sin( i * 0.45 ) ) * 55 ) );
}
}
var fallbackDur = parseFloat( container.getAttribute( 'data-duration' ) || '0' ) || 0;
var hoverRatio = -1;
var rafId = null;
var isScrubbing = false;
function getDuration() {
return ( isFinite( audio.duration ) && audio.duration > 0 ) ? audio.duration : fallbackDur;
}
function updateTime() {
var dur = getDuration();
var cur = audio.currentTime || 0;
timeEl.textContent = formatTime( cur ) + ( dur > 0 ? ' / ' + formatTime( dur ) : '' );
var pct = dur > 0 ? Math.round( ( cur / dur ) * 100 ) : 0;
wrap.setAttribute( 'aria-valuenow', String( pct ) );
}
function draw() {
var rect = wrap.getBoundingClientRect();
var width = Math.max( 1, Math.floor( rect.width ) );
var height = Math.max( 1, Math.floor( rect.height ) );
var dpr = window.devicePixelRatio || 1;
if ( canvas.width !== Math.floor( width * dpr ) || canvas.height !== Math.floor( height * dpr ) ) {
canvas.width = Math.floor( width * dpr );
canvas.height = Math.floor( height * dpr );
}
var ctx = canvas.getContext( '2d' );
if ( !ctx ) {
return;
}
ctx.setTransform( dpr, 0, 0, dpr, 0, 0 );
ctx.clearRect( 0, 0, width, height );
var colors = getColors( container );
var dur = getDuration();
var progress = dur > 0 ? Math.min( 1, Math.max( 0, ( audio.currentTime || 0 ) / dur ) ) : 0;
// Adapt bar count smoothly for narrow screens
var n = peaks.length;
var gap = width < 220 ? 1.5 : 2;
var barW = Math.max( 2, ( width - gap * ( n - 1 ) ) / n );
var step = width / n;
var midY = height * 0.56;
var maxUp = midY - 2;
var maxDown = height - midY - 2;
for ( var i = 0; i < n; i++ ) {
var norm = Math.max( 0.08, Math.min( 1, peaks[ i ] / 100 ) );
var upH = Math.max( 2, norm * maxUp );
var downH = Math.max( 1.5, norm * maxDown * 0.85 );
var x = i * step;
var ratio = ( i + 0.5 ) / n;
if ( ratio <= progress ) {
ctx.fillStyle = colors.played;
} else if ( hoverRatio >= 0 && ratio <= hoverRatio ) {
ctx.fillStyle = colors.hover;
ctx.globalAlpha = 0.55;
} else {
ctx.fillStyle = colors.unplayed;
}
var r = Math.min( barW / 2, 2 );
var y = midY - upH;
var totalH = upH + downH;
ctx.beginPath();
if ( ctx.roundRect ) {
ctx.roundRect( x, y, Math.max( 1.5, barW ), totalH, r );
} else {
ctx.rect( x, y, Math.max( 1.5, barW ), totalH );
}
ctx.fill();
ctx.globalAlpha = 1;
}
}
function tick() {
updateTime();
draw();
if ( !audio.paused && !audio.ended ) {
rafId = window.requestAnimationFrame( tick );
} else {
rafId = null;
}
}
function seekToClientX( clientX ) {
var rect = wrap.getBoundingClientRect();
var ratio = Math.min( 1, Math.max( 0, ( clientX - rect.left ) / Math.max( 1, rect.width ) ) );
var dur = getDuration();
if ( dur > 0 ) {
audio.currentTime = ratio * dur;
updateTime();
draw();
}
}
btn.addEventListener( 'click', function () {
if ( audio.paused ) {
if ( activeAudio && activeAudio !== audio ) {
activeAudio.pause();
}
activeAudio = audio;
var p = audio.play();
if ( p && p.catch ) {
p.catch( function () {} );
}
} else {
audio.pause();
}
} );
audio.addEventListener( 'play', function () {
container.classList.add( 'is-playing' );
btn.setAttribute( 'aria-label', 'Pause audio' );
if ( !rafId ) {
rafId = window.requestAnimationFrame( tick );
}
} );
audio.addEventListener( 'pause', function () {
container.classList.remove( 'is-playing' );
btn.setAttribute( 'aria-label', 'Play audio' );
if ( rafId ) {
window.cancelAnimationFrame( rafId );
rafId = null;
}
draw();
} );
audio.addEventListener( 'ended', function () {
container.classList.remove( 'is-playing' );
btn.setAttribute( 'aria-label', 'Play audio' );
audio.currentTime = 0;
updateTime();
draw();
} );
audio.addEventListener( 'loadedmetadata', function () {
updateTime();
draw();
} );
audio.addEventListener( 'timeupdate', function () {
if ( !rafId ) {
updateTime();
draw();
}
} );
// Pointer / touch scrubbing
wrap.addEventListener( 'pointerdown', function ( e ) {
isScrubbing = true;
if ( wrap.setPointerCapture ) {
try { wrap.setPointerCapture( e.pointerId ); } catch ( err ) {}
}
seekToClientX( e.clientX );
if ( audio.paused ) {
if ( activeAudio && activeAudio !== audio ) {
activeAudio.pause();
}
activeAudio = audio;
var p = audio.play();
if ( p && p.catch ) {
p.catch( function () {} );
}
}
} );
wrap.addEventListener( 'pointermove', function ( e ) {
var rect = wrap.getBoundingClientRect();
hoverRatio = Math.min( 1, Math.max( 0, ( e.clientX - rect.left ) / Math.max( 1, rect.width ) ) );
if ( isScrubbing ) {
seekToClientX( e.clientX );
} else {
draw();
}
} );
wrap.addEventListener( 'pointerup', function () {
isScrubbing = false;
} );
wrap.addEventListener( 'pointercancel', function () {
isScrubbing = false;
} );
wrap.addEventListener( 'pointerleave', function () {
hoverRatio = -1;
if ( !isScrubbing ) {
draw();
}
} );
wrap.addEventListener( 'keydown', function ( e ) {
var dur = getDuration();
if ( dur <= 0 ) {
return;
}
if ( e.key === 'ArrowRight' ) {
e.preventDefault();
audio.currentTime = Math.min( dur, audio.currentTime + 3 );
updateTime();
draw();
} else if ( e.key === 'ArrowLeft' ) {
e.preventDefault();
audio.currentTime = Math.max( 0, audio.currentTime - 3 );
updateTime();
draw();
} else if ( e.key === ' ' ) {
e.preventDefault();
btn.click();
}
} );
container.classList.add( 'bpm-wf-ready' );
updateTime();
draw();
var playerRef = { draw: draw, wrap: wrap };
allPlayers.push( playerRef );
if ( window.ResizeObserver ) {
var ro = new ResizeObserver( function () {
draw();
} );
ro.observe( wrap );
}
}
function initAll( $content ) {
var root = ( $content && $content[ 0 ] ) ? $content[ 0 ] : document;
var nodes = root.querySelectorAll( '.bpm-wf-player' );
for ( var i = 0; i < nodes.length; i++ ) {
initPlayer( nodes[ i ] );
}
}
// Re-draw all waveforms when Citizen switches light/dark theme
if ( window.MutationObserver ) {
var mo = new MutationObserver( function () {
for ( var i = 0; i < allPlayers.length; i++ ) {
allPlayers[ i ].draw();
}
} );
mo.observe( document.documentElement, { attributes: true, attributeFilter: [ 'class' ] } );
}
if ( window.mw && mw.hook ) {
mw.hook( 'wikipage.content' ).add( initAll );
} else if ( document.readyState === 'loading' ) {
document.addEventListener( 'DOMContentLoaded', function () { initAll( null ); } );
} else {
initAll( null );
}
}() );