MediaWiki:Common.js: Difference between revisions

MediaWiki interface page
No edit summary
No edit summary
Tag: Manual revert
 
(80 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. */
mw.hook('wikipage.content').add(function($content) {
mw.hook('wikipage.content').add(function($content) {
var $bandcamp = $content.find( '.bandcamp:not(.loaded)' );
var $bandcamp = $content.find( '.bandcamp:not(.loaded)' );
Line 31: Line 29:
});
});
});
});
import lottieWeb from 'https://cdn.skypack.dev/lottie-web';


class AudioPlayer extends HTMLElement {
if(!players) {
    constructor() {
  var players = document.querySelectorAll(".player-wrapper");
        super();
        const template = document.querySelector('template');
        const templateContent = template.content;
        const shadow = this.attachShadow({mode: 'open'});
        shadow.appendChild(templateContent.cloneNode(true));
    }


    connectedCallback() {
players.forEach((player)=>{
        everything(this);
  let btn = player.querySelector(".btn");
    }
  let record = player.querySelector(".record");
}
  let toneArm = player.querySelector(".tone-arm");
  let song = player.querySelector(".my-song");
  let slider = player.querySelector(".slider");
  btn.addEventListener("click", () => {


const everything = function(element) {
  if (record.classList.contains("on")) {
   const shadow = element.shadowRoot;
    record.classList.remove("on");
    toneArm.classList.remove("play");
    song.pause();
  } else {
    record.classList.add("on");
    toneArm.classList.add("play");
    setTimeout(() => {
      song.play();
    }, 1000);
  }
});
  slider.addEventListener("input", (e) => {
   song.volume = Number(e.target.value);
});


    const audioPlayerContainer = shadow.getElementById('audio-player-container');
})
    const playIconContainer = shadow.getElementById('play-icon');
    const seekSlider = shadow.getElementById('seek-slider');
    const volumeSlider = shadow.getElementById('volume-slider');
    const muteIconContainer = shadow.getElementById('mute-icon');
    const audio = shadow.querySelector('audio');
    const durationContainer = shadow.getElementById('duration');
    const currentTimeContainer = shadow.getElementById('current-time');
    const outputContainer = shadow.getElementById('volume-output');
    let playState = 'play';
    let muteState = 'unmute';
    let raf = null;


    audio.src = element.getAttribute('data-src');
}


    const playAnimation = lottieWeb.loadAnimation({
/**
        container: playIconContainer,
* Name:        DataTables.js
        path: 'https://maxst.icons8.com/vue-static/landings/animated-icons/icons/pause/pause.json',
* Author:     KockaAdmiralac <wikia@kocka.tech>
        renderer: 'svg',
* Description: Loads CSS and JavaScript from https://datatables.net and
        loop: false,
*              initializes all tables with the `datatable` class as data tables
        autoplay: false,
*/
        name: "Play Animation",
(function($, mw) {
    });
     'use strict';
         
     var initialized = false, queue = [];
     const muteAnimation = lottieWeb.loadAnimation({
     function process($content) {
        container: muteIconContainer,
         $content.find('.datatable:not(.datatable-loaded)').each(function() {
        path: 'https://maxst.icons8.com/vue-static/landings/animated-icons/icons/mute/mute.json',
            var $table = $(this).addClass('datatable-loaded'),
        renderer: 'svg',
                $tableHeader = $('<thead>');
        loop: false,
            $table.prepend($tableHeader);
        autoplay: false,
            $table.find('> tbody > tr').first().appendTo($tableHeader);
        name: "Mute Animation",
            $table.DataTable( {        
    });
    layout: {
         
        top1: 'searchPanes',
     playAnimation.goToAndStop(14, true);
    },
 
            searchPanes: {
     const whilePlaying = () => {
                cascadePanes: true,
         seekSlider.value = Math.floor(audio.currentTime);
                initCollapsed: true
        currentTimeContainer.textContent = calculateTime(seekSlider.value);
            },
        audioPlayerContainer.style.setProperty('--seek-before-width', `${seekSlider.value / seekSlider.max * 100}%`);
columnDefs: [
        raf = requestAnimationFrame(whilePlaying);
    {
    }
        targets: [0, 1, 2, 3, 4, 5],
 
        className: 'dt-nowrap'
    const showRangeProgress = (rangeInput) => {
    },
        if(rangeInput === seekSlider) audioPlayerContainer.style.setProperty('--seek-before-width', rangeInput.value / rangeInput.max * 100 + '%');
        {
        else audioPlayerContainer.style.setProperty('--volume-before-width', rangeInput.value / rangeInput.max * 100 + '%');
            searchPanes: {
    }
                show: true
 
            },
    const calculateTime = (secs) => {
            targets: [6]
        const minutes = Math.floor(secs / 60);
        }    
        const seconds = Math.floor(secs % 60);
  ]
        const returnedSeconds = seconds < 10 ? `0${seconds}` : `${seconds}`;
});
        return `${minutes}:${returnedSeconds}`;
    }
       
    const displayDuration = () => {
        durationContainer.textContent = calculateTime(audio.duration);
    }
       
    const setSliderMax = () => {
        seekSlider.max = Math.floor(audio.duration);
    }
       
    const displayBufferedAmount = () => {
        const bufferedAmount = Math.floor(audio.buffered.end(audio.buffered.length - 1));
        audioPlayerContainer.style.setProperty('--buffered-width', `${(bufferedAmount / seekSlider.max) * 100}%`);
    }
 
    if (audio.readyState > 0) {
        displayDuration();
        setSliderMax();
        displayBufferedAmount();
    } else {
        audio.addEventListener('loadedmetadata', () => {
            displayDuration();
            setSliderMax();
            displayBufferedAmount();
         });
         });
     }
     }
 
     function initialize($content) {
     playIconContainer.addEventListener('click', () => {
         if (initialized) {
         if(playState === 'play') {
             process($content);
             audio.play();
            playAnimation.playSegments([14, 27], true);
            requestAnimationFrame(whilePlaying);
            playState = 'pause';
         } else {
         } else {
             audio.pause();
             queue.push($content);
            playAnimation.playSegments([0, 14], true);
            cancelAnimationFrame(raf);
            playState = 'play';
        }
    });
       
    muteIconContainer.addEventListener('click', () => {
        if(muteState === 'unmute') {
            muteAnimation.playSegments([0, 15], true);
            audio.muted = true;
            muteState = 'mute';
        } else {
            muteAnimation.playSegments([15, 25], true);
            audio.muted = false;
            muteState = 'unmute';
         }
         }
    }
    mw.loader.load('https://cdn.datatables.net/v/dt/dt-2.0.0/r-3.0.0/sp-2.3.0/sl-2.0.0/datatables.min.css', 'text/css');
    mw.loader.getScript('https://cdn.datatables.net/v/dt/dt-2.0.0/r-3.0.0/sp-2.3.0/sl-2.0.0/datatables.min.js').then(function() {
        initialized = true;
        queue.forEach(process);
     });
     });
    mw.hook('wikipage.content').add(initialize);
    mw.hook('datatables.loaded').fire();
})(jQuery, mediaWiki);


    audio.addEventListener('progress', displayBufferedAmount);
var sliders = document.querySelectorAll(".cards");
sliders.forEach((slider)=>{
let isDown = false;
let startX;
let scrollLeft;


    seekSlider.addEventListener('input', (e) => {
slider.addEventListener("pointerdown", (e) => {
        showRangeProgress(e.target);
isDown = true;
        currentTimeContainer.textContent = calculateTime(seekSlider.value);
slider.classList.add("active");
        if(!audio.paused) {
startX = e.pageX - slider.offsetLeft;
            cancelAnimationFrame(raf);
scrollLeft = slider.scrollLeft;
        }
});
    });
slider.addEventListener("pointerleave", () => {
 
isDown = false;
    seekSlider.addEventListener('change', () => {
slider.classList.remove("active");
        audio.currentTime = seekSlider.value;
});
        if(!audio.paused) {
slider.addEventListener("pointerup", () => {
            requestAnimationFrame(whilePlaying);
isDown = false;
        }
slider.classList.remove("active");
    });
});
 
slider.addEventListener("pointermove", (e) => {
    volumeSlider.addEventListener('input', (e) => {
if (!isDown) return;
        const value = e.target.value;
e.preventDefault();
        showRangeProgress(e.target);
const x = e.pageX - slider.offsetLeft;
        outputContainer.textContent = value;
const walk = (x - startX) * 3; //scroll-fast
        audio.volume = value / 100;
slider.scrollLeft = scrollLeft - walk;
    });
});
 
});
    if('mediaSession' in navigator) {
        navigator.mediaSession.metadata = new MediaMetadata({
            title: 'Komorebi',
            artist: 'Anitek',
            album: 'MainStay',
            artwork: [
                { src: 'https://assets.codepen.io/4358584/1.300.jpg', sizes: '96x96', type: 'image/png' },
                { src: 'https://assets.codepen.io/4358584/1.300.jpg', sizes: '128x128', type: 'image/png' },
                { src: 'https://assets.codepen.io/4358584/1.300.jpg', sizes: '192x192', type: 'image/png' },
                { src: 'https://assets.codepen.io/4358584/1.300.jpg', sizes: '256x256', type: 'image/png' },
                { src: 'https://assets.codepen.io/4358584/1.300.jpg', sizes: '384x384', type: 'image/png' },
                { src: 'https://assets.codepen.io/4358584/1.300.jpg', sizes: '512x512', type: 'image/png' }
            ]
        });
        navigator.mediaSession.setActionHandler('play', () => {
            if(playState === 'play') {
                audio.play();
                playAnimation.playSegments([14, 27], true);
                requestAnimationFrame(whilePlaying);
                playState = 'pause';
            } else {
                audio.pause();
                playAnimation.playSegments([0, 14], true);
                cancelAnimationFrame(raf);
                playState = 'play';
            }
        });
        navigator.mediaSession.setActionHandler('pause', () => {
            if(playState === 'play') {
                audio.play();
                playAnimation.playSegments([14, 27], true);
                requestAnimationFrame(whilePlaying);
                playState = 'pause';
            } else {
                audio.pause();
                playAnimation.playSegments([0, 14], true);
                cancelAnimationFrame(raf);
                playState = 'play';
            }
        });
        navigator.mediaSession.setActionHandler('seekbackward', (details) => {
            audio.currentTime = audio.currentTime - (details.seekOffset || 10);
        });
        navigator.mediaSession.setActionHandler('seekforward', (details) => {
            audio.currentTime = audio.currentTime + (details.seekOffset || 10);
        });
        navigator.mediaSession.setActionHandler('seekto', (details) => {
            if (details.fastSeek && 'fastSeek' in audio) {
              audio.fastSeek(details.seekTime);
              return;
            }
            audio.currentTime = details.seekTime;
        });
        navigator.mediaSession.setActionHandler('stop', () => {
            audio.currentTime = 0;
            seekSlider.value = 0;
            audioPlayerContainer.style.setProperty('--seek-before-width', '0%');
            currentTimeContainer.textContent = '0:00';
            if(playState === 'pause') {
                playAnimation.playSegments([0, 14], true);
                cancelAnimationFrame(raf);
                playState = 'play';
            }
        });
    }
}
 
customElements.define('audio-player', AudioPlayer);
/* GreenAudioPlayer.init({*/
/*    selector: '.player', // inits Green Audio Player on each audio container that has class "player"*/
/*    stopOthersOnPlay: true*/
/* });*/

Latest revision as of 15:43, 14 April 2024

mw.hook('wikipage.content').add(function($content) {
	var $bandcamp = $content.find( '.bandcamp:not(.loaded)' );
    if ( !$bandcamp.length ) return;
 
	$bandcamp.each( function() {
		var elem = $( this );
 
		var width = elem.attr( 'data-width' ),
			height = elem.attr( 'data-height' ),
			data_src = elem.attr( 'data-src' );
 
		if ( !/^https?:\/\/bandcamp\.com\//.test( data_src ) ) return;
		elem.empty();
		var is_px = [ true, true ]; // width, height
 
		if ( /%/.test( width ) || !/\d+/.test( width ) ) is_px[ 0 ] = false;
		if ( /%/.test( height ) ) is_px[ 1 ] = false;

		var frame_width = parseFloat( width, 10 ) || 100;
			frame_height = height ? ( parseFloat( height, 10 ) || 'auto' ) : '';
 
		$( '<iframe />', {
			style: 'border: 0',
			width: frame_width + ( is_px[ 0 ] ? 'px' : '%' ),
			height: frame_height + ( frame_height ? ( is_px[ 1 ] ? 'px': '%' ) : '' ),
			src: data_src
		}).appendTo( elem );
            elem.addClass( 'loaded' );
	});
});

if(!players) {
  var players = document.querySelectorAll(".player-wrapper");

players.forEach((player)=>{
  let btn = player.querySelector(".btn");
  let record = player.querySelector(".record");
  let toneArm = player.querySelector(".tone-arm");
  let song = player.querySelector(".my-song");
  let slider = player.querySelector(".slider");
  btn.addEventListener("click", () => {

  if (record.classList.contains("on")) {
    record.classList.remove("on");
    toneArm.classList.remove("play");
    song.pause();
  } else {
    record.classList.add("on");
    toneArm.classList.add("play");
    setTimeout(() => {
      song.play();
    }, 1000);
  }
});
  slider.addEventListener("input", (e) => {
  song.volume = Number(e.target.value);
});

})

}

/**
 * Name:        DataTables.js
 * Author:      KockaAdmiralac <wikia@kocka.tech>
 * Description: Loads CSS and JavaScript from https://datatables.net and
 *              initializes all tables with the `datatable` class as data tables
 */
(function($, mw) {
    'use strict';
    var initialized = false, queue = [];
    function process($content) {
        $content.find('.datatable:not(.datatable-loaded)').each(function() {
            var $table = $(this).addClass('datatable-loaded'),
                $tableHeader = $('<thead>');
            $table.prepend($tableHeader);
            $table.find('> tbody > tr').first().appendTo($tableHeader);
            $table.DataTable( {         
			    layout: {
			        top1: 'searchPanes',
			    },
            	searchPanes: {
                	cascadePanes: true,
                	initCollapsed: true
            	},
				columnDefs: [
				    {
				        targets: [0, 1, 2, 3, 4, 5],
				        className: 'dt-nowrap'
				    },
			        {
			            searchPanes: {
			                show: true
			            },
			            targets: [6]
			        }				    
				  ]
});
        });
    }
    function initialize($content) {
        if (initialized) {
            process($content);
        } else {
            queue.push($content);
        }
    }
    mw.loader.load('https://cdn.datatables.net/v/dt/dt-2.0.0/r-3.0.0/sp-2.3.0/sl-2.0.0/datatables.min.css', 'text/css');
    mw.loader.getScript('https://cdn.datatables.net/v/dt/dt-2.0.0/r-3.0.0/sp-2.3.0/sl-2.0.0/datatables.min.js').then(function() {
        initialized = true;
        queue.forEach(process);
    });
    mw.hook('wikipage.content').add(initialize);
    mw.hook('datatables.loaded').fire();
})(jQuery, mediaWiki);

var sliders = document.querySelectorAll(".cards");
sliders.forEach((slider)=>{
	let isDown = false;
	let startX;
	let scrollLeft;

slider.addEventListener("pointerdown", (e) => {
	isDown = true;
	slider.classList.add("active");
	startX = e.pageX - slider.offsetLeft;
	scrollLeft = slider.scrollLeft;
});
slider.addEventListener("pointerleave", () => {
	isDown = false;
	slider.classList.remove("active");
});
slider.addEventListener("pointerup", () => {
	isDown = false;
	slider.classList.remove("active");
});
slider.addEventListener("pointermove", (e) => {
	if (!isDown) return;
	e.preventDefault();
	const x = e.pageX - slider.offsetLeft;
	const walk = (x - startX) * 3; //scroll-fast
	slider.scrollLeft = scrollLeft - walk;
});
});