forked from server/soundboard
housekeeping in main.js
adding some comments restructured keylisteners
This commit is contained in:
parent
6408ca40e4
commit
64102b9396
@ -1,4 +1,7 @@
|
||||
|
||||
var localModeEnabled = false;
|
||||
|
||||
// References to howler objects
|
||||
var howlerSounds = [];
|
||||
|
||||
function ready(fn) {
|
||||
@ -58,43 +61,51 @@ ready(function() {
|
||||
reset.addEventListener("click", resetSearch, false);
|
||||
sounds_nav.addEventListener("click", resetSearch, false);
|
||||
|
||||
// keyboard listener for global key strokes/inputs
|
||||
document.onkeydown = function(evt) {
|
||||
evt = evt || window.event;
|
||||
if (evt.keyCode == 27) {
|
||||
// esc key
|
||||
resetSearch();
|
||||
} else if ((evt.keyCode == 75 || evt.keyCode == 67) && evt.ctrlKey) {
|
||||
// ctrl+k and ctrl+c key binding
|
||||
killAllAudio();
|
||||
} else if (evt.keyCode == 88 && evt.ctrlKey){
|
||||
toggleLocalMode();
|
||||
}
|
||||
};
|
||||
|
||||
// TODO: make sure that only sound buttons get selected, not ALL a-tags. See d4377e0f49dc8dd5787f4466c9ab7f7e7cd37ec5#note_857
|
||||
// keyboard listener for 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) {
|
||||
var key = e.keyCode;
|
||||
var source = e.target;
|
||||
// keylistener for enter or ctrl+enter (which is k10 on chrome)
|
||||
if (key === 13 || (key === 10 && e.ctrlKey)) {
|
||||
if(e.ctrlKey){
|
||||
killAllAudio();
|
||||
await sleep(100);
|
||||
}
|
||||
source.classList.add("sound-pressed");
|
||||
source.onclick();
|
||||
await sleep(300);
|
||||
source.classList.remove("sound-pressed");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
addKeyListeners();
|
||||
});
|
||||
|
||||
/*
|
||||
* Adds all necessary KeyListeners to document
|
||||
*/
|
||||
function addKeyListeners() {
|
||||
// keyboard listener for global key strokes/inputs
|
||||
document.onkeydown = function(evt) {
|
||||
evt = evt || window.event;
|
||||
if (evt.keyCode == 27) {
|
||||
// esc key
|
||||
resetSearch();
|
||||
} else if ((evt.keyCode == 75 || evt.keyCode == 67) && evt.ctrlKey) {
|
||||
// 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 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) {
|
||||
var key = e.keyCode;
|
||||
var source = e.target;
|
||||
// keylistener for enter or ctrl+enter (which is k10 on chrome)
|
||||
if (key === 13 || (key === 10 && e.ctrlKey)) {
|
||||
if(e.ctrlKey){
|
||||
killAllAudio();
|
||||
await sleep(100);
|
||||
}
|
||||
source.classList.add("sound-pressed");
|
||||
source.onclick();
|
||||
await sleep(300);
|
||||
source.classList.remove("sound-pressed");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function resetSearch() {
|
||||
var searchfield = document.querySelector("#search");
|
||||
@ -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();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user