Difference between revisions of "MediaWiki:Common.js"

From Conceptual Reconstructionism Project
Line 11: Line 11:


TimeSegment.prototype.isDisabled = function() {
TimeSegment.prototype.isDisabled = function() {
return this.element.hasClass('active');
return !this.element.hasClass('active');
};
};


Line 35: Line 35:
this.segments = [];
this.segments = [];
this.htmlAudios = [];
this.htmlAudios = [];
this.closestPlayableSegment = 0;
this.closestPlayableSegIndex = 0;
this.playingAudioIndex = null;
this.playingAudioIndex = null;
};
};
Line 71: Line 71:
playbackData.autostopTime = null;
playbackData.autostopTime = null;
playbackData.playingAudioIndex = null;
playbackData.playingAudioIndex = null;
playbackData.closestPlayableSegment = 0;
playbackData.closestPlayableSegIndex = 0;
}
}
function onSeeked(event) {
function onSeeked(event) {
playbackData.closestPlayableSegment = 0;
playbackData.closestPlayableSegIndex = 0;
playbackData.segments.forEach(function(s) { s.disable(); });
playbackData.segments.forEach(function(s) { s.disable(); });
}
}
Line 139: Line 139:
var previouSegmentActive = false;
var previouSegmentActive = false;
for(var segIndex = playbackData.closestPlayableSegment;
for(var segIndex = playbackData.closestPlayableSegIndex;
segIndex < playbackData.segments.length;
segIndex < playbackData.segments.length;
segIndex++) {
segIndex++) {
Line 153: Line 153:


if (!previouSegmentActive) {
if (!previouSegmentActive) {
playbackData.closestPlayableSegment = segIndex;
playbackData.closestPlayableSegIndex = segIndex;
}
}
previouSegmentActive = true;
previouSegmentActive = true;

Revision as of 21:41, 19 December 2021

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


function TimeSegment(trackIndex, startTime, endTime, element) {
	this.trackIndex = trackIndex;
	this.startTime = startTime;
	this.endTime = endTime;
	this.element = element;
}


TimeSegment.prototype.isDisabled = function() {
	return !this.element.hasClass('active');
};


TimeSegment.prototype.disable = function() {
	this.element.removeClass('active');
};


TimeSegment.prototype.update = function(trackIndex, currentTime) {
	var percent = 100.0 * Math.max(currentTime - this.startTime, 0) / (this.endTime - this.startTime)
	if (trackIndex === this.trackIndex && percent > 0.0 && percent < 100.0) {
		this.element.get(0).style.setProperty('--gauge-fill',  percent + "%");
		this.element.addClass('active');
	} else {
		this.disable();
	}
};


function PlaybackData() {
	this.autostopTime = null;
	this.segments = [];
	this.htmlAudios = [];
	this.closestPlayableSegIndex = 0;
	this.playingAudioIndex = null;
};


var playbackData = new PlaybackData();


$(document).ready(function() {


    // Create one audio player per declared track

    $('*[data-track]').each(function(trackNumber) {
        var audio = $('<audio></audio>')
			.prop('controls', true)
			.attr('preload', 'auto')
			.attr('src', $(this).attr('data-track'));
        
		$('<figure></figure>')
			.append($('<figcaption></figcaption>')
				.text($(this).attr('data-track-title')))
			.appendTo($(this))
			.append(audio);

		function onPlay(event) {
			if (playbackData.playingAudioIndex !== null
				&& playbackData.playingAudioIndex !== trackNumber) {
				playbackData.htmlAudios[playbackData.playingAudioIndex].pause();
			}
			playbackData.playingAudioIndex = trackNumber;
		}
		
		function onPause(event) {
			playbackData.autostopTime = null;
			playbackData.playingAudioIndex = null;
			playbackData.closestPlayableSegIndex = 0;
		}
		
		function onSeeked(event) {
			playbackData.closestPlayableSegIndex = 0;
			playbackData.segments.forEach(function(s) { s.disable(); });
		}
		
		function onEnded(event) {
			var nextPlayingAudioIndex = playbackData.playingAudioIndex + 1;
			var hasAutoStop = playbackData.autostopTime !== null;
			onPause(event);
			onSeeked(event);
			if (nextPlayingAudioIndex < playbackData.htmlAudios.length && !hasAutoStop) {
				playbackData.htmlAudios[nextPlayingAudioIndex].play();
			}
		}
		
        audio.on('play', onPlay);
        audio.on('pause', onPause);
		audio.on('ended', onEnded);
		audio.on('seeked', onSeeked);
		
        playbackData.htmlAudios.push(audio.get(0));
    });


    // Create hyperlinks out of reconstruction markup

    $('*[data-def]').click(function() {
        var url = location.href;
        location.href = "#" + $(this).attr('data-def');
        history.replaceState(null, null, url);
    });


    // Update timing data

    $('*[data-segment]').each(function() {

        var m = /(?<track>\d+\/)?((?<minuteStart>\d+):)?(?<secondStart>\d+(.\d*)?)-((?<minuteEnd>\d+):)?(?<secondEnd>\d+(.\d*)?)/.exec($(this).attr('data-segment'));

		if (m) {
			playbackData.segments.push(new TimeSegment(
				m.groups.track? parseInt(m.groups.track) : 0,
				(m.groups.minuteStart? parseInt(m.groups.minuteStart) : 0) * 60 + parseFloat(m.groups.secondStart),
				(m.groups.minuteEnd? parseInt(m.groups.minuteEnd) : 0) * 60 + parseFloat(m.groups.secondEnd),
				$(this).find('.meta-progress') ));
		}
		else {
			$(this).append('Wrong time segment');
		}
	});

	playbackData.segments.sort(function(a, b) { return a.startTime - b.startTime; });

	
	// Playing tick
	
	window.setInterval(function() {
		if (playbackData.playingAudioIndex === null) {
			return;
		}
		var playingAudio = playbackData.htmlAudios[playbackData.playingAudioIndex];
		var currentTime = playingAudio.currentTime;
		
		var previouSegmentActive = false;
		
		for(var segIndex = playbackData.closestPlayableSegIndex;
			segIndex < playbackData.segments.length;
			segIndex++) {
			var segment = playbackData.segments[segIndex];
			segment.update(playbackData.playingAudioIndex, currentTime);
			
			if (segment.isDisabled()) {
				if (segment.startTime > currentTime) {
					break;
				}
				continue;
			}

			if (!previouSegmentActive) {
				playbackData.closestPlayableSegIndex = segIndex;
			}
			previouSegmentActive = true;
		}
		
		if (playbackData.autostopTime !== null && currentTime >= playbackData.autostopTime) {
			playingAudio.pause();
			return;
		}

	}, 50);

});