Skip to content

Commit

Permalink
Merge pull request #191 from innogames/servershell_dismiss_by_esc
Browse files Browse the repository at this point in the history
Dismiss alerts by <ESC> keyboard shortcut
  • Loading branch information
kofrezo committed Nov 15, 2021
2 parents 04565ab + d55f3e2 commit ef5aa73
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
31 changes: 31 additions & 0 deletions serveradmin/common/static/js/plugins/esc_dismissible.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Dismiss by ESC keyboard shortcut, Copyright (c) 2021 InnoGames GmbH
*
* Allows to dismiss (delete) HTML elements that have the class dismissible
* by pressing escape.
*/
$(document).ready(function() {
$(document).keydown(function (event) {
// ESC
if (event.key === "Escape") {
let dismissible = $('.dismissible');

if (dismissible.length < 1) {
return;
}

// Pressing ESC on elements such as inputs makes them loose focus.
// To avoid this we remember the focused element to restore it.
let input = document.activeElement;

dismissible.remove();

if (input) {
input.focus();
}

event.preventDefault();
event.stopPropagation();
}
});
});
1 change: 1 addition & 0 deletions serveradmin/common/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<script src="{{ STATIC_URL }}js/js.cookie.min.js"></script>
<script src="{{ STATIC_URL }}js/serveradmin.js"></script>
<script src="{{ STATIC_URL }}js/plugins/terminal_keyboard.js"></script>
<script src="{{ STATIC_URL }}js/plugins/esc_dismissible.js"></script>
{% block additional_scripts %}{% endblock %}
{% endcompress %}
</body>
Expand Down
1 change: 1 addition & 0 deletions serveradmin/servershell/static/js/servershell.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ servershell.alert = function(text, level, auto_dismiss=5) {

let alert = template.clone();
alert.removeAttr('id');
alert.addClass('dismissible');
alert.addClass(`alert-${level}`);
alert.children('.alert-text').text(text);

Expand Down

0 comments on commit ef5aa73

Please sign in to comment.