-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmt-interaction.helper.js
253 lines (209 loc) · 6.03 KB
/
kmt-interaction.helper.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*!
* KMT-Interaction Helper (https://github.com/MrGracu/js-kmt-interaction_helper)
* Description: Key, Mouse, Touch Interaction Helper - Easier event handling
* Copyright by Gracjan Mika (https://gmika.pl/)
* Licensed under MIT (https://github.com/MrGracu/js-kmt-interaction_helper/blob/main/LICENSE)
*/
/* ARRAY CONTAINING FUNCTIONS FOR KEYS */
var _keyFn = [];
/* ARRAY CONTAINING FUNCTIONS FOR MOUSE */
var _mouseFn = [];
/* ARRAY CONTAINING FUNCTIONS FOR TOUCH */
var _touchFn = [];
/* ARRAY CONTAINING FUNCTIONS FOR PAGE LOAD */
var _onloadFn = [];
/**
* Propagate event to functions.
*
* @param args - object of arguments passed to functions
* @param e - (optional) event if not in args
*
* @return null
*/
function _eventPropagation(args, e = null) {
let fnArray = [];
if(args.io == "key") fnArray = _keyFn;
else if(args.io == "mouse") fnArray = _mouseFn;
else if(args.io == "touch") fnArray = _touchFn;
for(let i = 0; i < fnArray.length; i++) {
if(typeof fnArray[i] === "function") fnArray[i](args);
else {
if(typeof fnArray[i] === "object") {
let isOk = true;
if(fnArray[i].target !== undefined && fnArray[i].target != args.target) {
isOk = false;
}
if(fnArray[i].type !== undefined) {
if(typeof fnArray[i].type === "string" || typeof fnArray[i].type === "object") {
if(typeof fnArray[i].type === "string") {
if(fnArray[i].type != args.type) isOk = false;
} else {
if(typeof fnArray[i].type === "object") {
if(!fnArray[i].type.includes(args.type)) isOk = false;
}
}
}
else console.error("Object in " + args.io + " array have bad key type. Position: " + i);
}
if(isOk) {
if(fnArray[i].fn !== undefined && typeof fnArray[i].fn === "function") {
fnArray[i].fn(args);
if(fnArray[i].propagate !== undefined && !fnArray[i].propagate) {
if(e !== null) e.stopPropagation();
else if(args.ev != null) args.ev.stopPropagation();
}
if(fnArray[i].preventDefault !== undefined && fnArray[i].preventDefault) {
if(e !== null) e.preventDefault();
else if(args.ev != null) args.ev.preventDefault();
}
}
else console.error("Object in " + args.io + " array have not get function. Position: " + i);
}
}
else console.error("Unrecognized type in " + args.io + " array. Position: " + i + ", Type: '" + typeof fnArray[i] + "'");
}
}
}
/**
* Propagate key events.
*
* @param type - event type (up, down, press)
* @param code - key code
* @param name - key name
* @param target - event target
*
* @return null
*/
function _getKey(type, code, name, target = null, e = null) {
let args = {
io: "key",
type,
code,
name,
target
};
_eventPropagation(args, e);
}
/**
* Propagate mouse events.
*
* @param type - event type (click, context, dbclick, down, up, enter, leave, move, out, over, scroll)
* @param e - event
*
* @return null
*/
function _getMouse(type, e = null) {
let args = {
io: "mouse",
type,
target: e.target,
ev: e
};
_eventPropagation(args);
}
/**
* Propagate touch events.
*
* @param type - event type (cancel, end, move, start)
* @param e - event
*
* @return null
*/
function _getTouch(type, e = null) {
let args = {
io: "touch",
type,
target: e.target,
ev: e
};
_eventPropagation(args);
}
/*************************************************************/
/* ON KEY DOWN EVENT */
window.onkeydown = function(e) {
let code = e.which || e.keyCode;
let name = e.key;
_getKey("down", code, name, e.target, e);
};
/* ON KEY PRESS EVENT */
window.onkeypress = function(e) {
let code = e.which || e.keyCode;
let name = e.key;
_getKey("press", code, name, e.target, e);
};
/* ON KEY UP EVENT */
window.onkeyup = function(e) {
let code = e.which || e.keyCode;
let name = e.key;
_getKey("up", code, name, e.target, e);
};
/*************************************************************/
/* ON WINDOW CLICK EVENT */
window.onclick = function(e) {
_getMouse("click", e);
};
/* ON WINDOW CONTEXT MENU EVENT */
window.oncontextmenu = function(e) {
_getMouse("context", e);
};
/* ON WINDOW DOUBLE-CLICK EVENT */
window.ondblclick = function(e) {
_getMouse("dbclick", e);
};
/* ON WINDOW MOUSE DOWN EVENT */
window.onmousedown = function(e) {
_getMouse("down", e);
};
/* ON WINDOW MOUSE UP EVENT */
window.onmouseup = function(e) {
_getMouse("up", e);
};
/* ON WINDOW MOUSE ENTER EVENT */
window.onmouseenter = function(e) {
_getMouse("enter", e);
};
/* ON WINDOW MOUSE LEAVE EVENT */
window.onmouseleave = function(e) {
_getMouse("leave", e);
};
/* ON WINDOW MOUSE MOVE EVENT */
window.onmousemove = function(e) {
_getMouse("move", e);
};
/* ON WINDOW MOUSE OUT EVENT */
window.onmouseout = function(e) {
_getMouse("out", e);
};
/* ON WINDOW MOUSE OVER EVENT */
window.onmouseover = function(e) {
_getMouse("over", e);
};
/* ON WINDOW MOUSE SCROLL EVENT */
window.onscroll = function(e) {
_getMouse("scroll", e);
};
/*************************************************************/
/* ON WINDOW TOUCH CANCEL EVENT */
window.addEventListener("touchcancel", function(e) {
_getTouch("cancel", e);
});
/* ON WINDOW TOUCH END EVENT */
window.addEventListener("touchend", function(e) {
_getTouch("end", e);
});
/* ON WINDOW TOUCH MOVE EVENT */
window.addEventListener("touchmove", function(e) {
_getTouch("move", e);
});
/* ON WINDOW TOUCH START EVENT */
window.addEventListener("touchstart", function(e) {
_getTouch("start", e);
});
/*************************************************************/
/* ON WINDOW LOADED EVENT */
window.onload = function() {
for(let i = 0; i < _onloadFn.length; i++) {
if(_onloadFn[i].length > 1 && typeof _onloadFn[i][0] === "function") _onloadFn[i][0](_onloadFn[i][1]);
else if(typeof _onloadFn[i] === "function") _onloadFn[i]();
}
};