-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
listener.ts
149 lines (123 loc) · 4.03 KB
/
listener.ts
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
import { IDisposable, IIndexable, IServiceLocator, Tracer } from '@aurelia/kernel';
import {
DelegationStrategy,
hasBind,
hasUnbind,
IBinding,
IConnectableBinding,
IDOM,
IsBindingBehavior,
IScope,
LifecycleFlags,
State
} from '@aurelia/runtime';
import { IEventManager } from '../observation/event-manager';
const slice = Array.prototype.slice;
export interface Listener extends IConnectableBinding {}
/**
* Listener binding. Handle event binding between view and view model
*/
export class Listener implements IBinding {
public dom: IDOM;
public $state: State;
public $scope!: IScope;
public part?: string;
public delegationStrategy: DelegationStrategy;
public locator: IServiceLocator;
public preventDefault: boolean;
public sourceExpression: IsBindingBehavior;
public target: Node;
public targetEvent: string;
private readonly eventManager: IEventManager;
private handler!: IDisposable;
// tslint:disable-next-line:parameters-max-number
constructor(
dom: IDOM,
targetEvent: string,
delegationStrategy: DelegationStrategy,
sourceExpression: IsBindingBehavior,
target: Node,
preventDefault: boolean,
eventManager: IEventManager,
locator: IServiceLocator
) {
this.dom = dom;
this.$state = State.none;
this.delegationStrategy = delegationStrategy;
this.locator = locator;
this.preventDefault = preventDefault;
this.sourceExpression = sourceExpression;
this.target = target;
this.targetEvent = targetEvent;
this.eventManager = eventManager;
}
public callSource(event: Event): ReturnType<IsBindingBehavior['evaluate']> {
if (Tracer.enabled) { Tracer.enter('Listener', 'callSource', slice.call(arguments)); }
const overrideContext = this.$scope.overrideContext;
overrideContext.$event = event;
const result = this.sourceExpression.evaluate(LifecycleFlags.mustEvaluate, this.$scope, this.locator, this.part);
Reflect.deleteProperty(overrideContext, '$event');
if (result !== true && this.preventDefault) {
event.preventDefault();
}
if (Tracer.enabled) { Tracer.leave(); }
return result;
}
public handleEvent(event: Event): void {
this.callSource(event);
}
public $bind(flags: LifecycleFlags, scope: IScope, part?: string): void {
if (Tracer.enabled) { Tracer.enter('Listener', '$bind', slice.call(arguments)); }
if (this.$state & State.isBound) {
if (this.$scope === scope) {
if (Tracer.enabled) { Tracer.leave(); }
return;
}
this.$unbind(flags | LifecycleFlags.fromBind);
}
// add isBinding flag
this.$state |= State.isBinding;
this.$scope = scope;
this.part = part;
const sourceExpression = this.sourceExpression;
if (hasBind(sourceExpression)) {
sourceExpression.bind(flags, scope, this);
}
this.handler = this.eventManager.addEventListener(
this.dom,
this.target,
this.targetEvent,
this,
this.delegationStrategy
);
// add isBound flag and remove isBinding flag
this.$state |= State.isBound;
this.$state &= ~State.isBinding;
if (Tracer.enabled) { Tracer.leave(); }
}
public $unbind(flags: LifecycleFlags): void {
if (Tracer.enabled) { Tracer.enter('Listener', '$unbind', slice.call(arguments)); }
if (!(this.$state & State.isBound)) {
if (Tracer.enabled) { Tracer.leave(); }
return;
}
// add isUnbinding flag
this.$state |= State.isUnbinding;
const sourceExpression = this.sourceExpression;
if (hasUnbind(sourceExpression)) {
sourceExpression.unbind(flags, this.$scope, this);
}
this.$scope = null!;
this.handler.dispose();
this.handler = null!;
// remove isBound and isUnbinding flags
this.$state &= ~(State.isBound | State.isUnbinding);
if (Tracer.enabled) { Tracer.leave(); }
}
public observeProperty(flags: LifecycleFlags, obj: IIndexable, propertyName: string): void {
return;
}
public handleChange(newValue: unknown, previousValue: unknown, flags: LifecycleFlags): void {
return;
}
}