This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (63 loc) · 1.48 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict'; // requires CustomEvent polyfill for IE
// https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent
var CustomEvent = require('custom-event');
var util = require('./util.js');
function onKeyDownOrUp(evt, el, keyEventType) {
if (!evt.shiftKey) {
var key = util.keyCodeToKeyMap[evt.keyCode];
switch (key) {
case 'Enter':
case 'Escape':
case 'Spacebar':
case 'PageUp':
case 'PageDown':
case 'End':
case 'Home':
case 'ArrowLeft':
case 'ArrowUp':
case 'ArrowRight':
case 'ArrowDown':
el.dispatchEvent(new CustomEvent(util.uncapitalizeFirstLetter("".concat(key, "Key").concat(keyEventType)), {
detail: evt,
bubbles: true
}));
break;
default:
return;
}
}
}
function onKeyDown(e) {
onKeyDownOrUp(e, this, 'Down');
}
function onKeyUp(e) {
onKeyDownOrUp(e, this, 'Up');
}
function addKeyDown(el) {
el.addEventListener('keydown', onKeyDown);
}
function addKeyUp(el) {
el.addEventListener('keyup', onKeyUp);
}
function removeKeyDown(el) {
el.removeEventListener('keydown', onKeyDown);
}
function removeKeyUp(el) {
el.removeEventListener('keyup', onKeyUp);
}
function add(el) {
addKeyDown(el);
addKeyUp(el);
}
function remove(el) {
removeKeyDown(el);
removeKeyUp(el);
}
module.exports = {
addKeyDown: addKeyDown,
addKeyUp: addKeyUp,
removeKeyDown: removeKeyDown,
removeKeyUp: removeKeyUp,
add: add,
remove: remove
};