-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
200 lines (181 loc) · 3.57 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
var codes = require('keycode').codes
var Event = window.Event
/**
* Create a native DOM event
*
* event('mousemove')
* event('keydown', { key: 'a' })
* event('user-login')
*
* @param {String} type
* @param {Object} [options]
* @return {DomEvent}
*/
function event(type, options){
return (make[type] || custom)(type, options || {})
}
/*!
* TODO: switch functions to fallbacks here
* if we are in an old IE
*/
var make = {
// html
load: html,
unload: html,
abort: html,
error: html,
select: html,
change: html,
submit: html,
reset: html,
focus: html,
blur: html,
resize: html,
scroll: html,
input: html,
// mouse
click: function(name, o){
'clicks' in o || (o.clicks = 1)
return mouse('click', o)
},
dblclick: function(name, o){
'clicks' in o || (o.clicks = 2)
return mouse('dblclick', o)
},
mousedown: mouse,
mouseup: mouse,
mouseover: mouse,
mousemove: mouse,
mouseout: mouse,
contextmenu: function(name, o){
'button' in o || (o.button = 2)
return mouse('contextmenu', o)
},
// keyboard
keypress: keyboard,
keydown: keyboard,
keyup: keyboard
}
/**
* Create a native mouse event
*
* mouse('mousemove', {})
* mouse('mousemove', {clientX: 50, clientY: 50})
*
* @param {String} name
* @param {Object} o
* @return {MouseEvent}
* @api private
*/
function mouse (name, o){
var event = document.createEvent('MouseEvents')
event.initMouseEvent(
name,
o.bubbles !== false,
o.cancelable !== false,
window,
o.clicks,
o.screenX || 0,
o.screenY || 0,
o.clientX || 0,
o.clientY || 0,
o.ctrl === true,
o.alt === true,
o.shift === true,
o.meta === true,
o.button || 0,
o.relatedTarget
)
return event
}
/**
* Create a html document event
*
* event('blur', {})
* event('change', {bubbles: false})
*
* @param {String} name
* @param {Object} o
* @return {HTMLEvent}
* @api private
*/
function html (name, o){
var event = document.createEvent('HTMLEvents')
event.initEvent(name,
o.bubbles !== false,
o.cancelable !== false
)
return event
}
/**
* Create a keyboard event
*
* event('keypress', {
* key: 'enter'
* })
*
* @param {String} type
* @param {Object} o
* @return {KeyboardEvent}
* @api private
*/
function keyboard(type, o) {
var key = o.key || 'a'
var keycode = codes[key]
if (!keycode) throw new Error('invalid key: '+key)
var charCode = key.length === 1
? key.charCodeAt(0)
: 0
// Prefer custom events to avoid webkits bug
// https://bugs.webkit.org/show_bug.cgi?id=16735
if (Event) {
var e = custom(type, o)
e.keyCode = keycode
e.charCode = charCode
e.shift = o.shift === true
e.meta = o.meta === true
e.ctrl = o.ctrl === true
e.alt = o.alt === true
} else {
var e = document.createEvent('KeyboardEvent')
e[e.initKeyEvent ? 'initKeyEvent' : 'initKeyboardEvent'](
type,
o.bubbles !== false,
o.cancelable !== false,
window,
o.ctrl === true,
o.alt === true,
o.shift === true,
o.meta === true,
keycode,
charCode
)
}
return e
}
/**
* Create a custom event
*
* custom('select', {
* bubbles: false
* })
*
* @param {String} name
* @param {Object} o
* @return {Event}
* @api private
*/
function custom(name, o) {
return new Event(name, {
bubbles: o.bubbles !== false,
cancelable: o.cancelable !== false
})
}
/*!
* Export event factory and helpers
*/
module.exports = event
event.mouse = mouse
event.keyboard = keyboard
event.custom = custom
event.html = html