Guest User

Untitled

a guest
Oct 22nd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. function disabledAudio() {
  2. return null;
  3. }
  4.  
  5. export default class SoundToggle {
  6. constructor(activator, sounds) {
  7. this.activator = activator;
  8. this.sounds = sounds;
  9. this.keys = Object.keys(sounds);
  10. this.backups = {};
  11. this.label = {
  12. disable: this.activator.dataset.labelDisable,
  13. enable: this.activator.dataset.labelEnable,
  14. };
  15. }
  16.  
  17. init() {
  18. this._setState(this.activator.dataset.enabled);
  19. this.activator.addEventListener('click', this.toggle.bind(this));
  20. }
  21.  
  22. toggle() {
  23. const newState = (this.activator.dataset.enabled === 'true') ? 'false' : 'true';
  24. this._setState(newState);
  25.  
  26. if (Object.keys(this.backups).length === 0) {
  27. this._backupPlayMethods();
  28. }
  29.  
  30. this.keys.forEach((sound) => {
  31. this.sounds[sound].play = (this.enabled) ? this.backups[sound] : disabledAudio;
  32. });
  33. }
  34.  
  35. _backupPlayMethods() {
  36. this.keys.forEach((sound) => {
  37. this.backups[sound] = this.sounds[sound].play;
  38. });
  39. }
  40.  
  41. _setState(state) {
  42. this.enabled = (state === 'true');
  43. this.activator.dataset.enabled = this.enabled;
  44. this.activator.textContent = (this.enabled) ? this.label.disable : this.label.enable;
  45. }
  46. }
Add Comment
Please, Sign In to add comment