MediaWiki:Gadget-AudioPlayer.js

From Official Temtem Wiki
Jump to navigation Jump to search

In other languages: EspañolFrançais


CSS and Javascript changes must comply with the wiki design rules.


Note: After saving, 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)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
"use strict";

var i18n = {
    playTitle : "Click to play",
    pauseTitle : "Click to pause",
    stopTitle : "Click to stop"
};

/* Fired whenever wiki content is added. (#mw-content-text, live preview, load page, etc.) */
mw.hook( "wikipage.content" ).add( function( $wikipageContent ) {
    var $players = $wikipageContent.find( ".audio-player" );
    $players.click( function() {
        var audio = $( this ).find( "audio" )[0];
        if (audio) { // Audio exists?
            // Toggle play state
            if (audio.paused) {
                audio.play();
            } else {
                audio.pause();
            }
        }
    });

    var $audios = $players.find( "audio" );
    $audios.on( "play", function () {
        var $player = $( this ).closest(".audio-player");
        // Before playing, stop any other audio players
        var $otherAudios = $audios.not( this );
        $otherAudios.trigger( "pause" );
        $otherAudios.closest( ".audio-player" ).attr( "title", i18n.playTitle );

        $player.toggleClass( "playing" , true ).attr( "title", $player.hasClass("pausable") ? i18n.pauseTitle : i18n.stopTitle );
    });

    $audios.on( "pause", function () {
        var $player = $( this ).closest(".audio-player");
        $player.toggleClass( "playing" , false );
        
        if (!$player.hasClass("pausable"))
            this.currentTime = 0;
    });
});