From 316db0cfc599d66de9fb0d4d6c876b303091f5b1 Mon Sep 17 00:00:00 2001 From: Dan Seripap Date: Wed, 17 Feb 2016 21:23:20 -0500 Subject: [PATCH] Start of reorganization: Audio class --- package.json | 4 ---- src/js/audio.js | 21 +++++++++++++++++++++ src/js/main.js | 12 +++++++----- 3 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 src/js/audio.js diff --git a/package.json b/package.json index af5f7c4..97776b7 100644 --- a/package.json +++ b/package.json @@ -28,10 +28,6 @@ "compression": "^1.6.0", "gulp-nodemon": "^2.0.6" }, - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", - "jsbuild": "uglifyjs src/js/main.js --compress --mangle -o src/public/main.js" - }, "author": "Daniel Seripap", "license": "MIT" } diff --git a/src/js/audio.js b/src/js/audio.js new file mode 100644 index 0000000..486e5bb --- /dev/null +++ b/src/js/audio.js @@ -0,0 +1,21 @@ +export default class AudoHandler { + constructor() { + this.beep = new Audio('beep.mp3'); + this._soundEnabled = true; + } + + get soundEnabled() { + return this._soundEnabled; + } + + set soundEnabled(state) { + if (state) { + this._soundEnabled = state; + } + return this; + } + + play() { + this.beep.play(); + } +} diff --git a/src/js/main.js b/src/js/main.js index 6d14502..a13362d 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -1,3 +1,5 @@ +import AudioHandler from './audio'; + let fs = window.RequestFileSystem || window.webkitRequestFileSystem; window.favicon = new Favico({ @@ -6,12 +8,12 @@ window.favicon = new Favico({ }); $(function() { - let beep = new Audio('beep.mp3'); + const audio = new AudioHandler(); + let isActive = false; let newMessages = 0; let FADE_TIME = 150; // ms let TYPING_TIMER_LENGTH = 400; // ms - let soundEnabled = true; let COLORS = [ '#e21400', '#ffe400', '#ff8f00', @@ -437,8 +439,8 @@ $(function() { if (!isActive) { newMessages++; favicon.badge(newMessages); - if (soundEnabled && beep) { - beep.play(); + if (audio.soundEnabled && beep) { + audio.play(); } } @@ -567,7 +569,7 @@ $(function() { $('input.bs-switch').bootstrapSwitch(); $('input.bs-switch').on('switchChange.bootstrapSwitch', function(event, state) { - soundEnabled = state; + audio.setSoundEnabled(state); }); function convertStringToArrayBufferView(str) {