-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
131 lines (127 loc) · 3.12 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const initData = () => ({
events: [],
keys: [],
codes: [],
})
const generateListeners = (data, listener) => {
function sugar(event) {
// Modifier keys
if (data.ctrl && event.ctrlKey === false) return
if (data.alt && event.altKey === false) return
if (data.shift && event.shiftKey === false) return
if (data.meta && event.metaKey === false) return
// Keys and codes
if (event.key !== undefined && data.keys.length) {
if (data.keys.indexOf(event.key) === -1) return
}
if (event.code !== undefined && data.codes.length) {
if (data.keys.indexOf(event.key) === -1) return
}
// Stop and prevent
if (data.prevent) event.preventDefault()
if (data.stop) event.stopPropagation()
// Trasform
let arg = event
if (data.value) arg = event.target.value
if (data.number) arg = +event.target.value
return listener.call(this, arg)
}
const listeners = {}
data.events.forEach(name => listeners[`on${name}${data.capture ? 'Capture' : ''}`] = sugar)
return listeners
}
const extend = func => {
function addEvent(event) {
return {
get() {
if (!this._data) return on(event)
this._data.events.push(event)
return this
},
}
}
function setFlag(flag) {
return {
get() {
this._data[flag] = true
return this
},
}
}
function addKey(...key) {
return {
get() {
this._data.keys.push(...key)
return this
},
}
}
function addCode(code) {
return {
get() {
this._data.codes.push(code)
return this
},
}
}
Object.defineProperties(func, {
// Events
click: addEvent('Click'),
keyup: addEvent('KeyUp'),
keydown: addEvent('KeyDown'),
keypress: addEvent('KeyPress'),
change: addEvent('Change'),
input: addEvent('Input'),
submit: addEvent('Submit'),
focus: addEvent('Focus'),
blur: addEvent('Blur'),
// Event flags
capture: setFlag('capture'),
prevent: setFlag('prevent'),
stop: setFlag('stop'),
// Modifier key flags
ctrl: setFlag('ctrl'),
alt: setFlag('alt'),
shift: setFlag('shift'),
meta: setFlag('meta'),
// Keys and codes
key: {
get() {
const that = this
return keyNamesWithSpace => {
that._data.keys.push(...keyNamesWithSpace.split(' '))
return that
}
},
},
code: {
get() {
const that = this
return (...codes) => {
that._data.codes.push(...codes)
return that
}
},
},
enter: addKey('Enter'),
tab: addKey('Tab'),
delete: addKey('Delete'),
backspace: addKey('Backspace'),
esc: addKey('Escape'),
space: addKey(' '),
up: addKey('ArrowUp', 'Up'),
down: addKey('ArrowDown', 'Down'),
left: addKey('ArrowLeft', 'Left'),
right: addKey('ArrowRight', 'Right'),
})
return func
}
const on = eventNamesWithSpace => {
const data = initData()
data.events.push(...eventNamesWithSpace.split(' '))
const func = generateListeners.bind(null, data)
func._data = data
return extend(func)
}
extend(on)
export default on