-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (92 loc) · 2.69 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
/**
* @file mofron-event-key/index.js
* @brief key event for mofron
* ## event function parameter
* - component: event target component object
* - string: event target key
* - mixed: user specified parameter
* @feature it is possible to implement key-event to target component by setting type that is "keydown", "keypress" or "keyup" (default type is "keydown")
* it is possible to filter the target event key by setting the 'key' method.
* the target key is all if user not set 'key' method.
* @attention set event to mofron.window or mofron.document if you apply key-event to the whole page.
* @license MIT
*/
const evCom = require('mofron-event-common');
module.exports = class extends evCom {
/**
* initialize event
*
* @param (mixed) listener parameter
* key-value: event config
* @param (string) key parameter
* @short listener,key
* @type private
*/
constructor (p1,p2) {
try {
super();
this.modname("Key");
this.shortForm("listener", "key");
/* init config */
this.confmng().add("type", { type: "string", select: ["keydown", "keypress", "keyup"], init: "keydown" });
this.confmng().add("key", { type: "string" });
/* set config */
if (0 < arguments.length) {
this.config(p1, p2);
}
} catch (e) {
console.error(e.stack);
throw e;
}
}
/**
* event listener function setter/getter
*
* @param (function) event function
* @param (mixed) event parameter
* @return (array) event object ([0]:function, [1]:parameter)
* @type parameter
*/
listener (fnc, prm) {
try {
if (undefined === fnc) {
/* getter */
return this.confmng("listener");
}
/* setter */
let evt_obj = this;
let set_fnc = (p1,p2,p3) => {
try {
if ((evt_obj.key() === p2.key) || (null === evt_obj.key())) {
if ("function" === typeof p3[0]) {
p3[0](evt_obj.component(), p2.key, p3[1]);
}
}
} catch (e) {
console.error(e.stack);
throw e;
}
}
return this.confmng("listener", [set_fnc,[fnc,prm]]);
} catch (e) {
console.error(e.stack);
throw e;
}
}
/**
* event target key
*
* @param (string) event target key
* @return (string) event target key
* @type parameter
*/
key (prm) {
try {
return this.confmng("key", prm);
} catch (e) {
console.error("invalid parameter");
throw e;
}
}
}
/* end of file */