Skip to content

Commit

Permalink
Format media controls JS with prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
ferjm committed Jul 22, 2019
1 parent ef5ded9 commit 590ac0b
Showing 1 changed file with 58 additions and 37 deletions.
95 changes: 58 additions & 37 deletions resources/media-controls.js
Expand Up @@ -28,26 +28,26 @@
// State transitions. // State transitions.
const TRANSITIONS = { const TRANSITIONS = {
buffer: { buffer: {
"paused": BUFFERING, paused: BUFFERING
}, },
end: { end: {
"playing": ENDED, playing: ENDED,
"paused": ENDED, paused: ENDED
}, },
error: { error: {
"buffering": ERRORED, buffering: ERRORED,
"playing": ERRORED, playing: ERRORED,
"paused": ERRORED, paused: ERRORED
}, },
pause: { pause: {
"buffering": PAUSED, buffering: PAUSED,
"playing": PAUSED, playing: PAUSED
}, },
play: { play: {
"buffering": PLAYING, buffering: PLAYING,
"ended": PLAYING, ended: PLAYING,
"paused": PLAYING, paused: PLAYING
}, }
}; };


function camelCase(str) { function camelCase(str) {
Expand All @@ -62,14 +62,15 @@
time = Math.round(time / 1000); time = Math.round(time / 1000);


const hours = Math.floor(time / 3600); const hours = Math.floor(time / 3600);
const mins = Math.floor((time % 3600) / 60); const mins = Math.floor((time % 3600) / 60);
const secs = Math.floor(time % 60); const secs = Math.floor(time % 60);


const formattedHours = hours || showHours ? const formattedHours =
`${hours.toString().padStart(2, "0")}:` : hours || showHours ? `${hours.toString().padStart(2, "0")}:` : "";
"";


return `${formattedHours}${mins.toString().padStart(2, "0")}:${secs.toString().padStart(2, "0")}`; return `${formattedHours}${mins
.toString()
.padStart(2, "0")}:${secs.toString().padStart(2, "0")}`;
} }


class MediaControls { class MediaControls {
Expand Down Expand Up @@ -119,25 +120,25 @@


Object.defineProperties(this.elements.positionDurationBox, { Object.defineProperties(this.elements.positionDurationBox, {
durationSpan: { durationSpan: {
value: durationSpan, value: durationSpan
}, },
position: { position: {
get: () => { get: () => {
return positionTextNode.textContent; return positionTextNode.textContent;
}, },
set: (v) => { set: v => {
positionTextNode.textContent = positionFormat.replace("#1", v); positionTextNode.textContent = positionFormat.replace("#1", v);
}, }
}, },
duration: { duration: {
get: () => { get: () => {
return durationSpan.textContent; return durationSpan.textContent;
}, },
set: (v) => { set: v => {
durationSpan.textContent = v ? durationFormat.replace("#2", v) : ""; durationSpan.textContent = v ? durationFormat.replace("#2", v) : "";
}, }
}, },
show:{ show: {
value: (currentTime, duration) => { value: (currentTime, duration) => {
const self = this.elements.positionDurationBox; const self = this.elements.positionDurationBox;
if (self.position != currentTime) { if (self.position != currentTime) {
Expand All @@ -147,24 +148,39 @@
self.duration = duration; self.duration = duration;
} }
self.classList.remove("hidden"); self.classList.remove("hidden");
}, }
}, }
}); });


// Add event listeners. // Add event listeners.
this.mediaEvents = [ this.mediaEvents = [
"play", "pause", "ended", "volumechange", "loadeddata", "play",
"loadstart", "timeupdate", "progress", "playing", "pause",
"waiting", "canplay", "canplaythrough", "seeking", "ended",
"seeked", "emptied", "loadedmetadata", "error", "suspend"]; "volumechange",
"loadeddata",
"loadstart",
"timeupdate",
"progress",
"playing",
"waiting",
"canplay",
"canplaythrough",
"seeking",
"seeked",
"emptied",
"loadedmetadata",
"error",
"suspend"
];
this.mediaEvents.forEach(event => { this.mediaEvents.forEach(event => {
this.media.addEventListener(event, this); this.media.addEventListener(event, this);
}); });


this.controlEvents = [ this.controlEvents = [
{ el: this.elements.playPauseButton, type: "click"}, { el: this.elements.playPauseButton, type: "click" },
{ el: this.elements.volumeSwitch, type: "click" }, { el: this.elements.volumeSwitch, type: "click" },
{ el: this.elements.volumeLevel, type: "input" }, { el: this.elements.volumeLevel, type: "input" }
]; ];
this.controlEvents.forEach(({ el, type }) => { this.controlEvents.forEach(({ el, type }) => {
el.addEventListener(type, this); el.addEventListener(type, this);
Expand Down Expand Up @@ -245,7 +261,8 @@
} }


// Progress. // Progress.
const positionPercent = this.media.currentTime / this.media.duration * 100; const positionPercent =
(this.media.currentTime / this.media.duration) * 100;
if (Number.isFinite(positionPercent)) { if (Number.isFinite(positionPercent)) {
this.elements.progress.value = positionPercent; this.elements.progress.value = positionPercent;
} else { } else {
Expand All @@ -262,8 +279,11 @@
this.elements.positionDurationBox.show(currentTime, duration); this.elements.positionDurationBox.show(currentTime, duration);


// Volume. // Volume.
this.elements.volumeSwitch.className = this.media.muted || !this.media.volume ? "muted" : "volumeup"; this.elements.volumeSwitch.className =
const volumeLevelValue = this.media.muted ? 0 : Math.round(this.media.volume * 100); this.media.muted || !this.media.volume ? "muted" : "volumeup";
const volumeLevelValue = this.media.muted
? 0
: Math.round(this.media.volume * 100);
if (this.elements.volumeLevel.value != volumeLevelValue) { if (this.elements.volumeLevel.value != volumeLevelValue) {
this.elements.volumeLevel.value = volumeLevelValue; this.elements.volumeLevel.value = volumeLevelValue;
} }
Expand Down Expand Up @@ -301,7 +321,7 @@
break; break;
} }
break; break;
break; break;
throw new Error(`Unknown event ${event.type}`); throw new Error(`Unknown event ${event.type}`);
} }
} }
Expand Down Expand Up @@ -330,7 +350,7 @@
/* Media actions */ /* Media actions */


playOrPause() { playOrPause() {
switch(this.state) { switch (this.state) {
case PLAYING: case PLAYING:
this.media.pause(); this.media.pause();
break; break;
Expand Down Expand Up @@ -358,3 +378,4 @@


new MediaControls(); new MediaControls();
})(); })();

0 comments on commit 590ac0b

Please sign in to comment.