-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
binding-command.ts
223 lines (186 loc) · 8.45 KB
/
binding-command.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
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
import {
Class,
Constructable,
IContainer,
IRegistry,
IResourceDefinition,
IResourceKind,
IResourceType,
PLATFORM,
Registration,
Writable
} from '@aurelia/kernel';
import {
AttributeInstruction,
BindingMode,
BindingType,
CallBindingInstruction,
ForOfStatement,
FromViewBindingInstruction,
IsBindingBehavior,
ITargetedInstruction,
IteratorBindingInstruction,
OneTimeBindingInstruction,
ToViewBindingInstruction,
TwoWayBindingInstruction
} from '@aurelia/runtime';
import { BindingSymbol, PlainAttributeSymbol, SymbolFlags } from './semantic-model';
export interface IBindingCommand {
bindingType: BindingType;
compile(binding: PlainAttributeSymbol | BindingSymbol): ITargetedInstruction;
}
export interface IBindingCommandDefinition extends IResourceDefinition { }
export interface IBindingCommandType extends IResourceType<IBindingCommandDefinition, IBindingCommand, Class<IBindingCommand>> { }
export interface IBindingCommandResource extends
IResourceKind<IBindingCommandDefinition, IBindingCommand, Class<IBindingCommand>> { }
type BindingCommandDecorator = <TProto, TClass>(target: Class<TProto, TClass> & Partial<IBindingCommandType>) => Class<TProto, TClass> & IBindingCommandType;
function register(this: IBindingCommandType, container: IContainer): void {
const resourceKey = BindingCommandResource.keyFrom(this.description.name);
container.register(Registration.singleton(resourceKey, this));
}
export function bindingCommand(name: string): BindingCommandDecorator;
export function bindingCommand(definition: IBindingCommandDefinition): BindingCommandDecorator;
export function bindingCommand(nameOrDefinition: string | IBindingCommandDefinition): BindingCommandDecorator {
return target => BindingCommandResource.define(nameOrDefinition, target);
}
function keyFrom(this: IBindingCommandResource, name: string): string {
return `${this.name}:${name}`;
}
function isType<T>(this: IBindingCommandResource, Type: T & Partial<IBindingCommandType>): Type is T & IBindingCommandType {
return Type.kind === this;
}
function define<T extends Constructable>(this: IBindingCommandResource, name: string, ctor: T): T & IBindingCommandType;
function define<T extends Constructable>(this: IBindingCommandResource, definition: IBindingCommandDefinition, ctor: T): T & IBindingCommandType;
function define<T extends Constructable>(this: IBindingCommandResource, nameOrDefinition: string | IBindingCommandDefinition, ctor: T): T & IBindingCommandType {
const Type = ctor as T & IBindingCommandType;
const WritableType = Type as T & Writable<IBindingCommandType>;
const description = typeof nameOrDefinition === 'string' ? { name: nameOrDefinition, target: null } : nameOrDefinition;
WritableType.kind = BindingCommandResource as IBindingCommandResource;
WritableType.description = description;
Type.register = register;
return Type;
}
export const BindingCommandResource: IBindingCommandResource = {
name: 'binding-command',
keyFrom,
isType,
define
};
export function getTarget(binding: PlainAttributeSymbol | BindingSymbol, camelCase: boolean): string {
if (binding.flags & SymbolFlags.isBinding) {
return (binding as BindingSymbol).bindable.propName;
} else if (camelCase) {
return PLATFORM.camelCase((binding as PlainAttributeSymbol).syntax.target);
} else {
return (binding as PlainAttributeSymbol).syntax.target;
}
}
export function getMode(binding: PlainAttributeSymbol | BindingSymbol): BindingMode {
if (binding.flags & SymbolFlags.isBinding) {
return (binding as BindingSymbol).bindable.mode;
} else {
return commandToMode[(binding as PlainAttributeSymbol).syntax.command];
}
}
export interface OneTimeBindingCommand extends IBindingCommand {}
export class OneTimeBindingCommand implements IBindingCommand {
public static readonly register: IRegistry['register'];
public readonly bindingType: BindingType.OneTimeCommand;
constructor() {
this.bindingType = BindingType.OneTimeCommand;
}
public compile(binding: PlainAttributeSymbol | BindingSymbol): AttributeInstruction {
return new OneTimeBindingInstruction(binding.expression as IsBindingBehavior, getTarget(binding, false));
}
}
BindingCommandResource.define('one-time', OneTimeBindingCommand);
export interface ToViewBindingCommand extends IBindingCommand {}
export class ToViewBindingCommand implements IBindingCommand {
public static readonly register: IRegistry['register'];
public readonly bindingType: BindingType.ToViewCommand;
constructor() {
this.bindingType = BindingType.ToViewCommand;
}
public compile(binding: PlainAttributeSymbol | BindingSymbol): AttributeInstruction {
return new ToViewBindingInstruction(binding.expression as IsBindingBehavior, getTarget(binding, false));
}
}
BindingCommandResource.define('to-view', ToViewBindingCommand);
export interface FromViewBindingCommand extends IBindingCommand {}
export class FromViewBindingCommand implements IBindingCommand {
public static readonly register: IRegistry['register'];
public readonly bindingType: BindingType.FromViewCommand;
constructor() {
this.bindingType = BindingType.FromViewCommand;
}
public compile(binding: PlainAttributeSymbol | BindingSymbol): AttributeInstruction {
return new FromViewBindingInstruction(binding.expression as IsBindingBehavior, getTarget(binding, false));
}
}
BindingCommandResource.define('from-view', FromViewBindingCommand);
export interface TwoWayBindingCommand extends IBindingCommand {}
export class TwoWayBindingCommand implements IBindingCommand {
public static readonly register: IRegistry['register'];
public readonly bindingType: BindingType.TwoWayCommand;
constructor() {
this.bindingType = BindingType.TwoWayCommand;
}
public compile(binding: PlainAttributeSymbol | BindingSymbol): AttributeInstruction {
return new TwoWayBindingInstruction(binding.expression as IsBindingBehavior, getTarget(binding, false));
}
}
BindingCommandResource.define('two-way', TwoWayBindingCommand);
// Not bothering to throw on non-existing modes, should never happen anyway.
// Keeping all array elements of the same type for better optimizeability.
const modeToProperty = ['', '$1', '$2', '', '$4', '', '$6'];
const commandToMode = {
'bind': BindingMode.toView,
'one-time': BindingMode.oneTime,
'to-view': BindingMode.toView,
'from-view': BindingMode.fromView,
'two-way': BindingMode.twoWay,
};
export interface DefaultBindingCommand extends IBindingCommand {}
export class DefaultBindingCommand implements IBindingCommand {
public static readonly register: IRegistry['register'];
public readonly bindingType: BindingType.BindCommand;
public readonly $1: typeof OneTimeBindingCommand.prototype.compile;
public readonly $2: typeof ToViewBindingCommand.prototype.compile;
public readonly $4: typeof FromViewBindingCommand.prototype.compile;
public readonly $6: typeof TwoWayBindingCommand.prototype.compile;
constructor() {
this.bindingType = BindingType.BindCommand;
this.$1 = OneTimeBindingCommand.prototype.compile;
this.$2 = ToViewBindingCommand.prototype.compile;
this.$4 = FromViewBindingCommand.prototype.compile;
this.$6 = TwoWayBindingCommand.prototype.compile;
}
public compile(binding: PlainAttributeSymbol | BindingSymbol): AttributeInstruction {
return this[modeToProperty[getMode(binding)]](binding);
}
}
BindingCommandResource.define('bind', DefaultBindingCommand);
export interface CallBindingCommand extends IBindingCommand {}
export class CallBindingCommand implements IBindingCommand {
public static readonly register: IRegistry['register'];
public readonly bindingType: BindingType.CallCommand;
constructor() {
this.bindingType = BindingType.CallCommand;
}
public compile(binding: PlainAttributeSymbol | BindingSymbol): AttributeInstruction {
return new CallBindingInstruction(binding.expression as IsBindingBehavior, getTarget(binding, true));
}
}
BindingCommandResource.define('call', CallBindingCommand);
export interface ForBindingCommand extends IBindingCommand {}
export class ForBindingCommand implements IBindingCommand {
public static readonly register: IRegistry['register'];
public readonly bindingType: BindingType.ForCommand;
constructor() {
this.bindingType = BindingType.ForCommand;
}
public compile(binding: PlainAttributeSymbol | BindingSymbol): AttributeInstruction {
return new IteratorBindingInstruction(binding.expression as ForOfStatement, getTarget(binding, false));
}
}
BindingCommandResource.define('for', ForBindingCommand);