Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function disabledAudio() {
- return null;
- }
- export default class SoundToggle {
- constructor(activator, sounds) {
- this.activator = activator;
- this.sounds = sounds;
- this.keys = Object.keys(sounds);
- this.backups = {};
- this.label = {
- disable: this.activator.dataset.labelDisable,
- enable: this.activator.dataset.labelEnable,
- };
- }
- init() {
- this._setState(this.activator.dataset.enabled);
- this.activator.addEventListener('click', this.toggle.bind(this));
- }
- toggle() {
- const newState = (this.activator.dataset.enabled === 'true') ? 'false' : 'true';
- this._setState(newState);
- if (Object.keys(this.backups).length === 0) {
- this._backupPlayMethods();
- }
- this.keys.forEach((sound) => {
- this.sounds[sound].play = (this.enabled) ? this.backups[sound] : disabledAudio;
- });
- }
- _backupPlayMethods() {
- this.keys.forEach((sound) => {
- this.backups[sound] = this.sounds[sound].play;
- });
- }
- _setState(state) {
- this.enabled = (state === 'true');
- this.activator.dataset.enabled = this.enabled;
- this.activator.textContent = (this.enabled) ? this.label.disable : this.label.enable;
- }
- }
Add Comment
Please, Sign In to add comment