2014-09-19 17:37:10 +02:00
|
|
|
var App = App || {};
|
|
|
|
|
2015-05-14 23:04:30 +02:00
|
|
|
App.Keyboard = function(jQuery, mousetrap, browsingSettings) {
|
2014-09-19 17:37:10 +02:00
|
|
|
|
2015-05-14 23:04:30 +02:00
|
|
|
var enabled = browsingSettings.getSettings().keyboardShortcuts;
|
2014-10-18 15:37:04 +02:00
|
|
|
var oldStopCallback = mousetrap.stopCallback;
|
|
|
|
mousetrap.stopCallback = function(e, element, combo, sequence) {
|
2014-10-31 23:27:41 +01:00
|
|
|
if (combo.indexOf('ctrl') === -1 && e.ctrlKey) {
|
2014-10-27 22:50:07 +01:00
|
|
|
return true;
|
|
|
|
}
|
2014-10-31 23:27:41 +01:00
|
|
|
if (combo.indexOf('alt') === -1 && e.altKey) {
|
2014-10-27 22:50:07 +01:00
|
|
|
return true;
|
|
|
|
}
|
2014-10-18 15:37:04 +02:00
|
|
|
if (combo.indexOf('ctrl') !== -1) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-26 22:51:48 +01:00
|
|
|
var $focused = jQuery(':focus').eq(0);
|
2015-03-19 23:02:53 +01:00
|
|
|
if ($focused.length) {
|
|
|
|
if ($focused.prop('tagName').match(/embed|object/i)) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-05-08 18:39:45 +02:00
|
|
|
if ($focused.prop('tagName').toLowerCase() === 'input' &&
|
|
|
|
$focused.attr('type').match(/checkbox|radio/i)) {
|
2015-03-19 23:02:53 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-10-26 22:51:48 +01:00
|
|
|
}
|
2014-10-18 15:37:04 +02:00
|
|
|
return oldStopCallback.apply(mousetrap, arguments);
|
2014-10-19 12:10:22 +02:00
|
|
|
};
|
2014-10-18 15:37:04 +02:00
|
|
|
|
2014-09-19 17:37:10 +02:00
|
|
|
function keyup(key, callback) {
|
2014-10-18 12:42:29 +02:00
|
|
|
unbind(key);
|
2015-05-14 23:04:30 +02:00
|
|
|
if (enabled) {
|
|
|
|
mousetrap.bind(key, callback, 'keyup');
|
|
|
|
}
|
2014-09-19 17:37:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function keydown(key, callback) {
|
2014-10-18 12:42:29 +02:00
|
|
|
unbind(key);
|
2015-05-14 23:04:30 +02:00
|
|
|
if (enabled) {
|
|
|
|
mousetrap.bind(key, callback);
|
|
|
|
}
|
2014-09-19 17:37:10 +02:00
|
|
|
}
|
|
|
|
|
2014-09-29 18:11:21 +02:00
|
|
|
function reset() {
|
|
|
|
mousetrap.reset();
|
|
|
|
}
|
|
|
|
|
2014-10-05 10:09:02 +02:00
|
|
|
function unbind(key) {
|
2014-10-18 12:42:29 +02:00
|
|
|
mousetrap.unbind(key, 'keyup');
|
2014-10-05 10:09:02 +02:00
|
|
|
mousetrap.unbind(key);
|
|
|
|
}
|
|
|
|
|
2014-09-19 17:37:10 +02:00
|
|
|
return {
|
|
|
|
keydown: keydown,
|
|
|
|
keyup: keyup,
|
2014-09-29 18:11:21 +02:00
|
|
|
reset: reset,
|
2014-10-05 10:09:02 +02:00
|
|
|
unbind: unbind,
|
2014-09-19 17:37:10 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2015-05-14 23:04:30 +02:00
|
|
|
App.DI.register('keyboard', ['jQuery', 'mousetrap', 'browsingSettings'], App.Keyboard);
|