Commit 4422c4f3 authored by i❤computers's avatar i❤computers

Refactor code

parent 9d5fa494
var transcriptCuesTrack = [{ var cuesTrack = [{
cueTime: 0.33, cueTime: 0.33,
cuePositionName: "0" cuePositionName: "0"
}, { }, {
......
'use strict';
document.addEventListener('DOMContentLoaded', function initializePage() { document.addEventListener('DOMContentLoaded', function initializePage() {
'use strict';
var audio = document.getElementById('excerpt'); var audio = document.getElementById('excerpt');
var MAX_TRANSITION_TIME = 0.4; //make sure transition time matches in index.html's styling var MAX_TRANSITION_TIME = 0.4; //make sure transition time matches in index.html's styling
transcriptCuesTrack.forEach(function setTransitionTimes(cue, index) { cuesTrack.forEach(function setShortTransitionTimes(cue, index) {
if (index + 1 === transcriptCuesTrack.length) { if (index + 1 === cuesTrack.length) {
return; return;
} }
var cueDuration = transcriptCuesTrack[index + 1].cueTime - cue.cueTime; var cueDuration = cuesTrack[index + 1].cueTime - cue.cueTime;
if (cueDuration < MAX_TRANSITION_TIME) { if (cueDuration < MAX_TRANSITION_TIME) {
$(cue_position_selector(cue.cuePositionName)).forEach(function (cueElement) { $(cuePositionSelector(cue.cuePositionName)).forEach(function (cueElement) {
cueElement.style.transitionDuration = cueDuration + "s"; cueElement.style.transitionDuration = cueDuration + "s";
}); });
} }
...@@ -20,7 +20,7 @@ document.addEventListener('DOMContentLoaded', function initializePage() { ...@@ -20,7 +20,7 @@ document.addEventListener('DOMContentLoaded', function initializePage() {
var target = event.target; var target = event.target;
var cuePositionName = target.dataset.cuePosition; var cuePositionName = target.dataset.cuePosition;
if (cuePositionName) { if (cuePositionName) {
playAudio(transcriptCuesTrack.filter(function (cueTrack) { playAudio(cuesTrack.filter(function (cueTrack) {
return cueTrack.cuePositionName.localeCompare(cuePositionName) === 0; return cueTrack.cuePositionName.localeCompare(cuePositionName) === 0;
})[0].cueTime); })[0].cueTime);
} }
...@@ -41,7 +41,7 @@ document.addEventListener('DOMContentLoaded', function initializePage() { ...@@ -41,7 +41,7 @@ document.addEventListener('DOMContentLoaded', function initializePage() {
} }
} }
var currentTranscriptIndex = 0; var cuePosition = 0;
var HIGHLIGHTED_CLASS = "highlighted"; var HIGHLIGHTED_CLASS = "highlighted";
function updateHighlight() { function updateHighlight() {
...@@ -49,15 +49,19 @@ document.addEventListener('DOMContentLoaded', function initializePage() { ...@@ -49,15 +49,19 @@ document.addEventListener('DOMContentLoaded', function initializePage() {
return; return;
} }
while (transcriptCuesTrack[currentTranscriptIndex] && audio.currentTime >= transcriptCuesTrack[currentTranscriptIndex].cueTime) { while (cuesLeftToHighlight()) {
$(cue_position_selector(transcriptCuesTrack[currentTranscriptIndex].cuePositionName)).forEach(addHighlightClass); $(cuePositionSelector(cuesTrack[cuePosition].cuePositionName)).forEach(addHighlightClass);
currentTranscriptIndex++; cuePosition++;
} }
requestAnimationFrame(updateHighlight); requestAnimationFrame(updateHighlight);
function cuesLeftToHighlight() {
return cuesTrack[cuePosition] && audio.currentTime >= cuesTrack[cuePosition].cueTime;
}
} }
function cue_position_selector(id) { function cuePositionSelector(id) {
return "[data-cue-position='" + id + "']" return "[data-cue-position='" + id + "']"
} }
...@@ -67,7 +71,7 @@ document.addEventListener('DOMContentLoaded', function initializePage() { ...@@ -67,7 +71,7 @@ document.addEventListener('DOMContentLoaded', function initializePage() {
function resetPlayer() { function resetPlayer() {
audio.currentTime = 0; audio.currentTime = 0;
currentTranscriptIndex = 0; cuePosition = 0;
$("." + HIGHLIGHTED_CLASS).forEach(function removeHighlightClass(highlightedElement) { $("." + HIGHLIGHTED_CLASS).forEach(function removeHighlightClass(highlightedElement) {
highlightedElement.classList.remove(HIGHLIGHTED_CLASS); highlightedElement.classList.remove(HIGHLIGHTED_CLASS);
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment