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;
// References to howler objects
var howlerSounds = [];
function ready(fn) {
@ -58,6 +61,13 @@ ready(function() {
reset.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
document.onkeydown = function(evt) {
evt = evt || window.event;
@ -68,12 +78,14 @@ ready(function() {
// ctrl+k and ctrl+c key binding
killAllAudio();
} else if (evt.keyCode == 88 && evt.ctrlKey){
// ctrl+x key binding
toggleLocalMode();
}
};
// 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");
for(i=0; i<all_a_s.length; i++) {
all_a_s[i].addEventListener('keypress', async function (e) {
@ -92,8 +104,7 @@ ready(function() {
}
});
}
});
}
function resetSearch() {
@ -108,6 +119,9 @@ function resetSearch() {
searchfield.focus();
}
/*
* Reads the stream url from input with id #streaming-url and forwards it to the server using AJAX.
*/
function playStream(){
var streamUrl = document.querySelector("#streaming-url").value;
ajaxRequest("/?video="+encodeURI(streamUrl));
@ -128,11 +142,15 @@ function ajaxRequest(url){
ajaxRequest.open("GET", url, true);
ajaxRequest.send(null);
}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;
}
}
/*
* 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(){
toggleButton = document.getElementById("local-mode-button").children[0];
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){
if(localModeEnabled){
// play local audio using howler.js
@ -167,6 +188,9 @@ function killAllHowlerAudio(){
}
}
/*
* Kills all local or remote audio, depending on localModeEnabled
*/
function killAllAudio(){
if (localModeEnabled) {
killAllHowlerAudio();