/
blockEvent.js
127 lines (99 loc) · 3.38 KB
/
blockEvent.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
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/front/blob/master/LICENSE
*/
// blockEvent
// script of behaviours for a component which blocks event propagation
// this component can only block events added after the block was registered
Component.BlockEvent = function(type)
{
// not empty
if(Vari.isEmpty(this))
return null;
// handler
setHdlrs(this,'blockEvent:',{
isRegistered: function(type) {
return Integer.is(getBlock.call(this,type));
},
isUnblocked: function(type) {
return getBlock.call(this,type) === 0;
},
isBlocked: function(type) {
return getBlock.call(this,type) === 1;
},
getObj: function() {
return Pojo.copy(getBlockObj.call(this));
},
register: function(type) {
if(!trigHdlr(this,'blockEvent:isRegistered',type))
{
const blockObj = getBlockObj.call(this);
const handler = getFunc.call(this,type);
Pojo.setRef(type,0,blockObj);
ael(this,type,handler,'blockEvent-register-'+type);
}
},
unregister: function(type) {
if(trigHdlr(this,'blockEvent:isRegistered',type))
{
const blockObj = getBlockObj.call(this);
Pojo.unsetRef(type,blockObj);
rel(this,'blockEvent-register-'+type);
}
},
block: function(type) {
Str.typecheck(type,true);
const blockObj = getBlockObj.call(this);
if(Pojo.keyExists(type,blockObj))
Pojo.setRef(type,1,blockObj);
},
blockAll: function() {
const $this = this;
const blockObj = getBlockObj.call(this);
Pojo.each(blockObj,function(value,type) {
trigHdlr($this,'blockEvent:block',type);
});
},
unblock: function(type) {
Str.typecheck(type,true);
const blockObj = getBlockObj.call(this);
if(Pojo.keyExists(type,blockObj))
Pojo.setRef(type,0,blockObj);
},
unblockAll: function() {
const $this = this;
const blockObj = trigHdlr(this,'blockEvent:obj');
Pojo.each(blockObj,function(value,type) {
trigHdlr($this,'blockEvent:unblock',type);
});
}
});
// getFunc
const getFunc = function(type)
{
return function(event) {
const status = getBlock.call(this,type);
if(status === 1)
{
Evt.preventStop(event,true);
trigEvt(this,'blockEvent:'+type);
return false;
}
};
}
// getBlock
const getBlock = function(type)
{
return Pojo.get(type,getBlockObj.call(this));
}
// getBlockObj
const getBlockObj = function()
{
return Target.getOrSetData(this,'blockEvent-obj',{});
}
// register
if(Str.isNotEmpty(type))
trigHdlrs(this,'blockEvent:register',type);
return this;
}