the-glorious-startpage/js/keybindings.js

114 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-06-09 04:02:03 +02:00
class KeyBinding {
constructor() {
this._searchBox = document.querySelector('#searchBox');
this._keysPressed = {};
this._documentAddKeyDownEvent = this._documentAddKeyDownEvent.bind(this);
this._documentAddKeyUpEvent = this._documentAddKeyUpEvent.bind(this);
2020-06-09 12:13:12 +02:00
this._init();
}
_init = () => {
2020-06-09 04:02:03 +02:00
this._registerDocumentAddKeyDownEvent();
this._registerDocumentAddKeyUpEvent();
}
_documentAddKeyDownEvent = e => {
const searchBoxVisibility = searchBoxShow.getSearchBoxVisibility();
this._keysPressed[e.key] = true;
if (this._keysPressed['Alt'] && e.key === 's') {
e.preventDefault();
dashboard.toggleDashboard();
2020-06-04 09:09:22 +02:00
return;
2020-06-09 04:02:03 +02:00
} else if (this._keysPressed['Alt'] && e.key === 'e') {
e.preventDefault();
webMenu.toggleWebMenu();
2020-06-04 09:09:22 +02:00
return;
2020-06-09 04:02:03 +02:00
} else if (this._keysPressed['Alt'] && e.key === 'x') {
e.preventDefault();
weatherScreen.toggleWeatherScreen();
2020-06-04 09:09:22 +02:00
return;
2020-06-09 04:02:03 +02:00
2020-06-04 09:09:22 +02:00
}
2020-06-04 09:18:32 +02:00
2020-06-09 04:02:03 +02:00
if (e.key === 'Escape') {
2020-06-05 04:22:20 +02:00
2020-06-09 04:02:03 +02:00
e.preventDefault();
2020-06-04 09:18:32 +02:00
// If any of this are open, close it
2020-06-09 04:02:03 +02:00
if (searchBoxVisibility) {
2020-06-05 04:22:20 +02:00
2020-06-04 09:18:32 +02:00
// Hide searchbox
2020-06-09 04:02:03 +02:00
searchBoxShow.toggleSearchBox();
this._searchBox.value = '';
2020-06-04 09:18:32 +02:00
return;
2020-06-05 04:22:20 +02:00
2020-06-09 04:02:03 +02:00
} else if (dashboard.getRightDashboardVisibility()) {
2020-06-05 04:22:20 +02:00
2020-06-09 04:02:03 +02:00
// Hide dashboard
dashboard.toggleDashboard();
return;
2020-06-05 04:22:20 +02:00
2020-06-09 04:02:03 +02:00
} else if (weatherScreen.getWeatherScreenVisiblity()) {
2020-06-05 04:22:20 +02:00
2020-06-09 04:02:03 +02:00
// Hide weather
weatherScreen.toggleWeatherScreen();
return;
}
2020-06-04 09:18:32 +02:00
// Show web menu
2020-06-09 04:02:03 +02:00
webMenu.toggleWebMenu();
2020-06-04 09:18:32 +02:00
return;
}
if (!searchBoxVisibility) {
2020-06-04 09:18:32 +02:00
// Don't show searchbox if web menu, dashboard
2020-06-04 09:18:32 +02:00
// and weather screen is open
2020-06-09 04:02:03 +02:00
if (dashboard.getRightDashboardVisibility() ||
webMenu.getwebMenuVisibility() ||
weatherScreen.getWeatherScreenVisiblity()) return;
2020-06-04 09:18:32 +02:00
2020-06-05 04:22:20 +02:00
// Only accept single charactered key and backspace to open search box
2020-06-09 04:02:03 +02:00
if ((e.key.length > 1) && (e.key !== 'Backspace')) return;
2020-06-04 09:18:32 +02:00
// Open searchbox
2020-06-09 04:02:03 +02:00
searchBoxShow.toggleSearchBox();
2020-06-04 09:18:32 +02:00
} else {
// Backspacing while there's no search query will hide searhbox
// Will also hide if you hit enter
2020-06-09 04:02:03 +02:00
if ((e.key === 'Backspace' || e.key === 'Enter') &&
this._searchBox.value < 1) { searchBoxShow.toggleSearchBox(); return; };
2020-06-04 09:18:32 +02:00
}
2020-06-09 04:02:03 +02:00
}
_documentAddKeyUpEvent = e => {
delete this._keysPressed[e.key];
}
_registerDocumentAddKeyDownEvent = () => {
document.addEventListener('keydown', this._documentAddKeyDownEvent);
2020-06-04 09:09:22 +02:00
}
2020-06-09 04:02:03 +02:00
_registerDocumentAddKeyUpEvent = () => {
document.addEventListener('keyup', this._documentAddKeyUpEvent);
2020-06-04 09:09:22 +02:00
}
2020-06-09 04:02:03 +02:00
}