ensl_gathers/lib/react/sound.jsx

274 lines
6 KiB
React
Raw Normal View History

2015-10-22 14:25:45 +00:00
function storageAvailable(type) {
try {
var storage = window[type],
x = '__storage_test__';
storage.setItem(x, x);
storage.removeItem(x);
return true;
}
catch(e) {
return false;
}
}
2015-10-04 13:01:59 +00:00
class SoundController {
constructor () {
if (Howl === undefined) {
throw new Error("Howl.js required to created sound controller");
}
this.MINIMUM_PLAY_INTERVAL = 120000;
this.playGatherMusic = _.throttle(() => {
this.gather.music.play();
}, this.MINIMUM_PLAY_INTERVAL);
this.isMuted = Howler._muted;
2015-10-22 15:28:20 +00:00
let gatherMusic;
2015-10-22 14:25:45 +00:00
if (storageAvailable("localStorage")) {
let volume = localStorage.getItem("gatherVolume");
if (volume !== undefined) Howler.volume(volume);
2015-10-22 15:28:20 +00:00
gatherMusic = localStorage.getItem("gatherMusic");
2015-10-22 14:25:45 +00:00
}
2015-10-04 13:01:59 +00:00
this.tunes = {
"classic": {
description: "Classic",
2015-10-22 15:28:20 +00:00
url: 'http://www.ensl.org/files/audio/gather-1.mp3'
},
"nights": {
description: "Nights",
url: 'http://www.ensl.org/files/audio/nights.mp3'
},
"america": {
description: "Infamous",
url: 'http://www.ensl.org/files/audio/america.mp3'
},
"skyice": {
description: "Skyice",
url: 'http://www.ensl.org/files/audio/skyice.mp3'
},
"justwannahavefun": {
description: "Gorges just want to have fun",
url: 'http://www.ensl.org/files/audio/justwannahavefun.mp3'
2015-10-04 13:01:59 +00:00
},
"eyeofthegorgie": {
description: "Eye of the Gorgie",
url: 'http://www.ensl.org/files/audio/eyeofthegorgie.mp3'
2015-10-22 15:28:20 +00:00
},
"boondock": {
description: "Boondock Marines",
url: 'http://www.ensl.org/files/audio/boondock.mp3'
},
2015-10-04 13:01:59 +00:00
}
2015-10-22 15:28:20 +00:00
this.setupGatherMusic(gatherMusic);
2015-10-04 13:01:59 +00:00
}
mute() {
this.isMuted = true;
return Howler.mute();
}
unMute() {
this.isMuted = false;
return Howler.unmute();
}
2015-10-22 14:25:45 +00:00
getVolume() {
return Howler.volume();
}
setVolume(val) {
if (val === undefined ||
typeof val !== 'number' ||
Math.abs(val) > 1) return;
if (storageAvailable("localStorage")) {
localStorage.setItem("gatherVolume", val);
}
return Howler.volume(val);
}
2015-10-04 13:01:59 +00:00
play(music) {
if (this.gather && this.gather.music) return this.gather.music.play();
}
stop(music) {
if (this.gather && this.gather.music) return this.gather.music.stop();
}
2015-10-22 15:28:20 +00:00
defaultGatherMusic() {
return "classic";
}
2015-10-04 13:01:59 +00:00
setupGatherMusic (musicName) {
let self = this;
2015-10-22 15:28:20 +00:00
let gatherMusic = self.tunes[musicName];
if (!gatherMusic) {
// Default to classic
musicName = this.defaultGatherMusic();
gatherMusic = self.tunes[musicName];
}
2015-10-04 13:01:59 +00:00
if (self.gather && self.gather.name === musicName) return;
// Stop if already playing
if (self.gather && self.gather.music) {
self.gather.music.stop();
}
let tune = self.tunes[musicName];
self.gather = {
name: musicName,
description: tune.description,
url: tune.url,
music: new Howl({
urls: [tune.url]
})
};
}
}
2015-10-22 15:28:20 +00:00
var MusicSelector = React.createClass({
getInitialState() {
return {
music: this.selectedMusic()
}
},
selectedMusic() {
if (storageAvailable("localStorage")) {
return localStorage.getItem("gatherMusic")
|| this.props.soundController.defaultGatherMusic();
} else {
return this.props.soundController.defaultGatherMusic();
}
},
setMusic(event) {
let name = event.target.value;
let soundController = this.props.soundController;
let selectedTune = soundController.tunes[name];
if (selectedTune === undefined) return;
this.setState({ music: name });
soundController.setupGatherMusic(name);
if (storageAvailable("localStorage")) {
localStorage.setItem("gatherMusic", name);
}
},
render() {
let soundController = this.props.soundController;
let tunes = [];
for (var attr in soundController.tunes) {
let o = soundController.tunes[attr];
o.id = attr;
tunes.push(o);
}
let options = tunes.map(tune => {
return <option key={tune.id} value={tune.id}>{tune.description}</option>;
});
return (
<div className="form-group music-select">
<label>Music</label>
<select
className="form-control"
defaultValue={this.state.music}
onChange={this.setMusic}
value={this.state.music}>
{options}
</select>
</div>
);
}
})
2015-10-04 13:01:59 +00:00
2015-08-27 16:54:34 +00:00
var SoundPanel = React.createClass({
2015-10-22 14:25:45 +00:00
componentDidMount() {
let soundController = this.props.soundController;
let scale = 10;
2015-10-22 15:28:20 +00:00
$('a#sound-dropdown').on('click', function (event) {
$(this).parent().toggleClass('open');
});
2015-10-22 14:25:45 +00:00
$("#volume-slide").slider({
min: 0,
max: scale,
step: 1
}).on("slideStop", ({value}) => {
soundController.setVolume(value / scale);
}).slider('setValue', soundController.getVolume() * scale);
},
2015-08-27 16:54:34 +00:00
mute() {
2015-10-02 14:53:11 +00:00
this.props.soundController.mute();
2015-08-27 16:54:34 +00:00
this.forceUpdate();
},
unMute() {
2015-10-02 14:53:11 +00:00
this.props.soundController.unMute();
2015-08-27 16:54:34 +00:00
this.forceUpdate();
},
2015-10-22 14:25:45 +00:00
play() {
this.props.soundController.play();
},
stop() {
this.props.soundController.stop();
},
2015-08-27 16:54:34 +00:00
render() {
2015-10-02 14:53:11 +00:00
let soundController = this.props.soundController;
2015-10-22 14:25:45 +00:00
let mutedIcon, mutedButton;
2015-08-27 16:54:34 +00:00
if (soundController.isMuted) {
2015-10-22 14:25:45 +00:00
mutedIcon = <i className="fa fa-volume-off fa-fw"></i>;
mutedButton = <li>
<a href="#" onClick={this.unMute}>
{mutedIcon}&nbsp;Muted
</a>
</li>;
2015-08-27 16:54:34 +00:00
} else {
2015-10-22 14:25:45 +00:00
mutedIcon = <i className="fa fa-volume-up fa-fw"></i>;
mutedButton = <li>
<a href="#" onClick={this.mute}>
{mutedIcon}&nbsp;Unmuted
</a>
</li>;
2015-08-27 16:54:34 +00:00
}
2015-10-22 14:25:45 +00:00
return <ul className="nav navbar-top-links navbar-right">
<li className="dropdown">
2015-10-22 15:28:20 +00:00
<a className="dropdown-toggle" href="#" id="sound-dropdown">
2015-10-22 14:25:45 +00:00
Sound &nbsp;{mutedIcon}&nbsp;<i className="fa fa-caret-down"></i>
</a>
2015-10-22 15:28:20 +00:00
<ul className="dropdown-menu" id="sound-dropdown">
2015-10-22 14:25:45 +00:00
{mutedButton}
<li>
<a href='#' onClick={this.play}>
<i className="fa fa-play"></i>&nbsp;Play
</a>
</li>
<li>
<a href='#' onClick={this.stop}>
<i className="fa fa-stop"></i>&nbsp;Stop
</a>
</li>
2015-10-22 15:28:20 +00:00
<hr />
2015-10-22 14:25:45 +00:00
<li>
<div className="volume-slide">
2015-10-22 15:28:20 +00:00
<label>Volume</label>
2015-10-22 14:25:45 +00:00
<div id="volume-slide"></div>
</div>
</li>
2015-10-22 15:28:20 +00:00
<li>
<MusicSelector soundController={soundController} />
</li>
2015-10-22 14:25:45 +00:00
</ul>
</li>
</ul>;
2015-08-27 16:54:34 +00:00
}
2015-10-02 14:53:11 +00:00
});