Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor MuteToggle#update #4058

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions src/js/control-bar/mute-toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,33 @@ class MuteToggle extends Button {
}

/**
* Update the state of volume.
* Update the `MuteToggle` button based on the state of `volume` and `muted`
* on the player.
*
* @param {EventTarget~Event} [event]
* The {@link Player#loadstart} event if this function was called through an
* event.
* The {@link Player#loadstart} event if this function was called
* through an event.
*
* @listens Player#loadstart
* @listens Player#volumechange
*/
update(event) {
this.updateIcon_();
this.updateControlText_();
}

/**
* Update the appearance of the `MuteToggle` icon.
*
* Possible states (given `level` variable below):
* - 0: crossed out
* - 1: zero bars of volume
* - 2: one bar of volume
* - 3: two bars of volume
*
* @private
*/
updateIcon_() {
const vol = this.player_.volume();
let level = 3;

Expand All @@ -85,22 +103,28 @@ class MuteToggle extends Button {
level = 2;
}

// Don't rewrite the button text if the actual text doesn't change.
// This causes unnecessary and confusing information for screen reader users.
// This check is needed because this function gets called every time the volume level is changed.
const toMute = this.player_.muted() ? 'Unmute' : 'Mute';

if (this.controlText() !== toMute) {
this.controlText(toMute);
}

// TODO improve muted icon classes
for (let i = 0; i < 4; i++) {
Dom.removeClass(this.el_, `vjs-vol-${i}`);
}
Dom.addClass(this.el_, `vjs-vol-${level}`);
}

/**
* If `muted` has changed on the player, update the control text
* (`title` attribute on `vjs-mute-control` element and content of
* `vjs-control-text` element).
*
* @private
*/
updateControlText_() {
const text = this.player_.muted() ? 'Unmute' : 'Mute';

if (this.controlText() !== text) {
this.controlText(text);
}
}

}

/**
Expand Down