-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
listener-expression.js
86 lines (79 loc) · 2.23 KB
/
listener-expression.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
export class ListenerExpression {
constructor(eventManager, targetEvent, sourceExpression, delegationStrategy, preventDefault, lookupFunctions) {
this.eventManager = eventManager;
this.targetEvent = targetEvent;
this.sourceExpression = sourceExpression;
this.delegationStrategy = delegationStrategy;
this.discrete = true;
this.preventDefault = preventDefault;
this.lookupFunctions = lookupFunctions;
}
createBinding(target) {
return new Listener(
this.eventManager,
this.targetEvent,
this.delegationStrategy,
this.sourceExpression,
target,
this.preventDefault,
this.lookupFunctions
);
}
}
export class Listener {
constructor(eventManager, targetEvent, delegationStrategy, sourceExpression, target, preventDefault, lookupFunctions) {
this.eventManager = eventManager;
this.targetEvent = targetEvent;
this.delegationStrategy = delegationStrategy;
this.sourceExpression = sourceExpression;
this.target = target;
this.preventDefault = preventDefault;
this.lookupFunctions = lookupFunctions;
}
callSource(event) {
let overrideContext = this.source.overrideContext;
overrideContext.$event = event;
let mustEvaluate = true;
let result = this.sourceExpression.evaluate(this.source, this.lookupFunctions, mustEvaluate);
delete overrideContext.$event;
if (result !== true && this.preventDefault) {
event.preventDefault();
}
return result;
}
handleEvent(event) {
this.callSource(event);
}
bind(source) {
if (this.isBound) {
if (this.source === source) {
return;
}
this.unbind();
}
this.isBound = true;
this.source = source;
if (this.sourceExpression.bind) {
this.sourceExpression.bind(this, source, this.lookupFunctions);
}
this._handler = this.eventManager.addEventListener(
this.target,
this.targetEvent,
this,
this.delegationStrategy,
true
);
}
unbind() {
if (!this.isBound) {
return;
}
this.isBound = false;
if (this.sourceExpression.unbind) {
this.sourceExpression.unbind(this, this.source);
}
this.source = null;
this._handler.dispose();
this._handler = null;
}
}