datatable/src/keyboard.js
Faris Ansari 785cf8f466 Removed jQuery 💃
2017-11-05 21:31:20 +05:30

60 lines
1006 B
JavaScript

import $ from './dom';
const KEYCODES = {
13: 'enter',
91: 'meta',
16: 'shift',
17: 'ctrl',
18: 'alt',
37: 'left',
38: 'up',
39: 'right',
40: 'down',
9: 'tab',
27: 'esc',
67: 'c'
};
const handlers = {};
function bind() {
$.on(document, 'keydown', handler);
}
function handler(e) {
let key = KEYCODES[e.keyCode];
if (e.shiftKey && key !== 'shift') {
key = 'shift+' + key;
}
if ((e.ctrlKey && key !== 'ctrl') || (e.metaKey && key !== 'meta')) {
key = 'ctrl+' + key;
}
const _handlers = handlers[key];
if (_handlers && _handlers.length > 0) {
_handlers.map(handler => {
const preventBubbling = handler();
if (preventBubbling === undefined || preventBubbling === true) {
e.preventDefault();
}
});
}
}
bind();
export default {
on(key, handler) {
const keys = key.split(',').map(k => k.trim());
keys.map(key => {
handlers[key] = handlers[key] || [];
handlers[key].push(handler);
});
}
};