housekeeping in main.js

adding some comments

restructured keylisteners
This commit is contained in:
Erhard 2017-08-04 14:48:19 +02:00
parent 6408ca40e4
commit 64102b9396

View File

@ -1,4 +1,7 @@
var localModeEnabled = false; var localModeEnabled = false;
// References to howler objects
var howlerSounds = []; var howlerSounds = [];
function ready(fn) { function ready(fn) {
@ -58,6 +61,13 @@ ready(function() {
reset.addEventListener("click", resetSearch, false); reset.addEventListener("click", resetSearch, false);
sounds_nav.addEventListener("click", resetSearch, false); sounds_nav.addEventListener("click", resetSearch, false);
addKeyListeners();
});
/*
* Adds all necessary KeyListeners to document
*/
function addKeyListeners() {
// keyboard listener for global key strokes/inputs // keyboard listener for global key strokes/inputs
document.onkeydown = function(evt) { document.onkeydown = function(evt) {
evt = evt || window.event; evt = evt || window.event;
@ -68,12 +78,14 @@ ready(function() {
// ctrl+k and ctrl+c key binding // ctrl+k and ctrl+c key binding
killAllAudio(); killAllAudio();
} else if (evt.keyCode == 88 && evt.ctrlKey){ } else if (evt.keyCode == 88 && evt.ctrlKey){
// ctrl+x key binding
toggleLocalMode(); toggleLocalMode();
} }
}; };
// TODO: make sure that only sound buttons get selected, not ALL a-tags. See d4377e0f49dc8dd5787f4466c9ab7f7e7cd37ec5#note_857 // TODO: make sure that only sound buttons get selected, not ALL a-tags. See d4377e0f49dc8dd5787f4466c9ab7f7e7cd37ec5#note_857
// keyboard listener for enter key // keyboard listener for playing sounds using enter key
var all_a_s = document.getElementsByTagName("a"); var all_a_s = document.getElementsByTagName("a");
for(i=0; i<all_a_s.length; i++) { for(i=0; i<all_a_s.length; i++) {
all_a_s[i].addEventListener('keypress', async function (e) { all_a_s[i].addEventListener('keypress', async function (e) {
@ -92,8 +104,7 @@ ready(function() {
} }
}); });
} }
}
});
function resetSearch() { function resetSearch() {
@ -108,6 +119,9 @@ function resetSearch() {
searchfield.focus(); searchfield.focus();
} }
/*
* Reads the stream url from input with id #streaming-url and forwards it to the server using AJAX.
*/
function playStream(){ function playStream(){
var streamUrl = document.querySelector("#streaming-url").value; var streamUrl = document.querySelector("#streaming-url").value;
ajaxRequest("/?video="+encodeURI(streamUrl)); ajaxRequest("/?video="+encodeURI(streamUrl));
@ -128,11 +142,15 @@ function ajaxRequest(url){
ajaxRequest.open("GET", url, true); ajaxRequest.open("GET", url, true);
ajaxRequest.send(null); ajaxRequest.send(null);
}catch (e){ }catch (e){
alert("Your browser broke!"); alert("Unfortunately we were unable to handle your request. Please try again later and contact the server administrator if the problem persists.");
return false; return false;
} }
} }
/*
* Switches between local and remote playback mode.
* Additionally changes the button text and its color so that the user has a visible feedback.
/*
function toggleLocalMode(){ function toggleLocalMode(){
toggleButton = document.getElementById("local-mode-button").children[0]; toggleButton = document.getElementById("local-mode-button").children[0];
if(localModeEnabled){ if(localModeEnabled){
@ -148,6 +166,9 @@ function toggleLocalMode(){
} }
} }
/*
* Either plays the given sound file locally by using howler.js or forwards the request to the remote server using AJAX.
*/
function playSound(filename){ function playSound(filename){
if(localModeEnabled){ if(localModeEnabled){
// play local audio using howler.js // play local audio using howler.js
@ -167,6 +188,9 @@ function killAllHowlerAudio(){
} }
} }
/*
* Kills all local or remote audio, depending on localModeEnabled
*/
function killAllAudio(){ function killAllAudio(){
if (localModeEnabled) { if (localModeEnabled) {
killAllHowlerAudio(); killAllHowlerAudio();