-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
131 lines (117 loc) · 3.14 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
'use strict';
function getListOrArray(fn) {
return function(itmList) {
return fn(itmList) ? itmList : new Array(itmList);
};
}
function isArrayLike(itmList) {
return itmList instanceof NodeList || Array.isArray(itmList);
}
var getArray = getListOrArray(Array.isArray);
var getArrayLike = getListOrArray(isArrayLike);
function addEventListenerEvent(eventType, itm, callbacks) {
itm &&
itm.addEventListener &&
itm.addEventListener(eventType, callbacks);
}
/**
* Generate the function to add event listiner
*
* @example
* var itmList = document.querySelectorAll('.btn');
* var callback = () => console.log('click');
* generateEventFn('click')(itmList, callback);
*
* @param String action
* @return Function
*/
function generateEventFn(eventType) {
return function(itmList, clbList) {
itmList = getArrayLike(itmList);
clbList = getArray(clbList);
for (var i = 0; i <= itmList.length - 1; i++) {
for (var a = 0; a <= clbList.length - 1; a++) {
addEventListenerEvent(eventType, itmList[i], clbList[a]);
}
}
};
}
/**
* Add event listener for focus to a Node
*
* @example
* var itm = document.querySelectorAll('.btn');
* var clb = [(event) => console.log('focus')];
* onFocus(itm, clb);
*
* @param NodeList | Node itmList
* @param Function[] | Function clbList
*/
var onFocus = generateEventFn('focus');
/**
* Add event listener for blur to a Node
*
* @example
* var itm = document.querySelectorAll('.btn');
* var clb = [(event) => console.log('blur')];
* onBlur(itm, clb);
*
* @param NodeList | Node itmList
* @param Function[] | Function clbList
*/
var onBlur = generateEventFn('blur');
/**
* Add event listener for click to a Node
*
* @example
* var itm = document.querySelectorAll('.btn');
* var clb = [(event) => console.log('click')];
* onClick(itm, clb);
*
* @param NodeList | Node itmList
* @param Function[] | Function clbList
*/
var onClick = generateEventFn('click');
/**
* Add event listener for keydown to a Node
*
* @example
* var itm = document.querySelectorAll('.btn');
* var clb = [(event) => console.log('key down')];
* onKeyDown(itm, clb);
*
* @param NodeList | Node itmList
* @param Function[] | Function clbList
*/
var onKeyDown = generateEventFn('keydown');
/**
* Add event listener for keyup to a Node
*
* @example
* var itm = document.querySelectorAll('.btn');
* var clb = [(event) => console.log('key up')];
* onKeyUp(itm, clb);
*
* @param NodeList | Node itmList
* @param Function[] | Function clbList
*/
var onKeyUp = generateEventFn('keyup');
/**
* Add event listener for change to a Node
*
* @example
* var itm = document.querySelectorAll('.select');
* var clb = [(event) => console.log('change')];
* onChange(itm, clb);
*
* @param NodeList | Node itmList
* @param Function[] | Function clbList
*/
var onChange = generateEventFn('change');
module.exports.generateEventFn = generateEventFn;
module.exports.onFocus = onFocus;
module.exports.onBlur = onBlur;
module.exports.onClick = onClick;
module.exports.onKeyDown = onKeyDown;
module.exports.onKeyUp = onKeyUp;
module.exports.onChange = onChange;