-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
custom-attribute.ts
210 lines (181 loc) · 8.15 KB
/
custom-attribute.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
import {
Class,
Constructable,
IContainer,
Immutable,
IResourceKind,
IResourceType,
IServiceLocator,
Omit,
PLATFORM,
Registration,
ResourceDescription,
Writable
} from '@aurelia/kernel';
import {
customAttributeKey,
customAttributeName,
IAttributeDefinition
} from '../definitions';
import { INode } from '../dom';
import { BindingMode, ensureValidStrategy, Hooks, LifecycleFlags } from '../flags';
import {
IComponent,
ILifecycleHooks,
IRenderable
} from '../lifecycle';
import { IChangeTracker } from '../observation';
import { Bindable } from '../templating/bindable';
import {
$attachAttribute,
$cacheAttribute,
$detachAttribute
} from '../templating/lifecycle-attach';
import {
$bindAttribute,
$patch,
$unbindAttribute
} from '../templating/lifecycle-bind';
import { $hydrateAttribute } from '../templating/lifecycle-render';
type CustomAttributeStaticProperties = Pick<Immutable<Required<IAttributeDefinition>>, 'bindables'>;
export type CustomAttributeConstructor = Constructable & CustomAttributeStaticProperties;
export interface ICustomAttributeType<T extends INode = INode, C extends Constructable = Constructable> extends
IResourceType<IAttributeDefinition, InstanceType<C> & ICustomAttribute<T>>,
CustomAttributeStaticProperties { }
export interface ICustomAttribute<T extends INode = INode> extends
Partial<IChangeTracker>,
ILifecycleHooks,
IComponent,
IRenderable<T> {
$hydrate(flags: LifecycleFlags, parentContext: IServiceLocator): void;
}
export interface ICustomAttributeResource<T extends INode = INode> extends
IResourceKind<IAttributeDefinition, ICustomAttribute<T>, Class<ICustomAttribute<T>> & CustomAttributeStaticProperties> {
}
/** @internal */
export function registerAttribute(this: ICustomAttributeType, container: IContainer): void {
const description = this.description;
const resourceKey = this.kind.keyFrom(description.name);
const aliases = description.aliases;
container.register(Registration.transient(resourceKey, this));
for (let i = 0, ii = aliases.length; i < ii; ++i) {
const aliasKey = this.kind.keyFrom(aliases[i]);
container.register(Registration.alias(resourceKey, aliasKey));
}
}
/**
* Decorator: Indicates that the decorated class is a custom attribute.
*/
export function customAttribute(definition: IAttributeDefinition): CustomAttributeDecorator;
export function customAttribute(name: string): CustomAttributeDecorator;
export function customAttribute(nameOrDefinition: string | IAttributeDefinition): CustomAttributeDecorator;
export function customAttribute(nameOrDefinition: string | IAttributeDefinition): CustomAttributeDecorator {
return target => CustomAttributeResource.define(nameOrDefinition, target);
}
/**
* Decorator: Applied to custom attributes. Indicates that whatever element the
* attribute is placed on should be converted into a template and that this
* attribute controls the instantiation of the template.
*/
export function templateController(definition: IAttributeDefinition): CustomAttributeDecorator;
export function templateController(name: string): CustomAttributeDecorator;
export function templateController(nameOrDefinition: string | Omit<IAttributeDefinition, 'isTemplateController'>): CustomAttributeDecorator;
export function templateController(nameOrDefinition: string | Omit<IAttributeDefinition, 'isTemplateController'>): CustomAttributeDecorator {
return target => CustomAttributeResource.define(
typeof nameOrDefinition === 'string'
? { isTemplateController: true , name: nameOrDefinition }
: { isTemplateController: true, ...nameOrDefinition },
target);
}
type HasDynamicOptions = Pick<IAttributeDefinition, 'hasDynamicOptions'>;
function dynamicOptionsDecorator<T extends Constructable>(target: T & HasDynamicOptions): T & Required<HasDynamicOptions> {
target.hasDynamicOptions = true;
return target as T & Required<HasDynamicOptions>;
}
/**
* Decorator: Indicates that the custom attributes has dynamic options.
*/
export function dynamicOptions(): typeof dynamicOptionsDecorator;
/**
* Decorator: Indicates that the custom attributes has dynamic options.
*/
export function dynamicOptions<T extends Constructable>(target: T & HasDynamicOptions): T & Required<HasDynamicOptions>;
export function dynamicOptions<T extends Constructable>(target?: T & HasDynamicOptions): T & Required<HasDynamicOptions> | typeof dynamicOptionsDecorator {
return target === undefined ? dynamicOptionsDecorator : dynamicOptionsDecorator<T>(target);
}
function isType<T>(this: ICustomAttributeResource, Type: T & Partial<ICustomAttributeType>): Type is T & ICustomAttributeType {
return Type.kind === this;
}
function define<N extends INode = INode, T extends Constructable = Constructable>(this: ICustomAttributeResource, definition: IAttributeDefinition, ctor: T): T & ICustomAttributeType<N, T>;
function define<N extends INode = INode, T extends Constructable = Constructable>(this: ICustomAttributeResource, name: string, ctor: T): T & ICustomAttributeType<N, T>;
function define<N extends INode = INode, T extends Constructable = Constructable>(this: ICustomAttributeResource, nameOrDefinition: string | IAttributeDefinition, ctor: T): T & ICustomAttributeType<N, T>;
function define<N extends INode = INode, T extends Constructable = Constructable>(this: ICustomAttributeResource, nameOrDefinition: string | IAttributeDefinition, ctor: T): T & ICustomAttributeType<N, T> {
const Type = ctor as T & ICustomAttributeType<N, T>;
const WritableType = Type as T & Writable<ICustomAttributeType<N, T>>;
const description = createCustomAttributeDescription(typeof nameOrDefinition === 'string' ? { name: nameOrDefinition } : nameOrDefinition, Type);
const proto: Writable<ICustomAttribute> = Type.prototype;
WritableType.kind = CustomAttributeResource as ICustomAttributeResource;
WritableType.description = description;
Type.register = registerAttribute;
proto.$hydrate = $hydrateAttribute;
proto.$bind = $bindAttribute;
proto.$patch = $patch;
proto.$attach = $attachAttribute;
proto.$detach = $detachAttribute;
proto.$unbind = $unbindAttribute;
proto.$cache = $cacheAttribute;
proto.$prevComponent = null;
proto.$nextComponent = null;
proto.$nextPatch = null;
proto.$nextUnbindAfterDetach = null;
proto.$scope = null;
proto.$hooks = 0;
proto.$state = 0;
if ('flush' in proto) {
proto.$nextFlush = null;
}
if ('binding' in proto) proto.$hooks |= Hooks.hasBinding;
if ('bound' in proto) {
proto.$hooks |= Hooks.hasBound;
proto.$nextBound = null;
}
if ('unbinding' in proto) proto.$hooks |= Hooks.hasUnbinding;
if ('unbound' in proto) {
proto.$hooks |= Hooks.hasUnbound;
proto.$nextUnbound = null;
}
if ('created' in proto) proto.$hooks |= Hooks.hasCreated;
if ('attaching' in proto) proto.$hooks |= Hooks.hasAttaching;
if ('attached' in proto) {
proto.$hooks |= Hooks.hasAttached;
proto.$nextAttached = null;
}
if ('detaching' in proto) proto.$hooks |= Hooks.hasDetaching;
if ('caching' in proto) proto.$hooks |= Hooks.hasCaching;
if ('detached' in proto) {
proto.$hooks |= Hooks.hasDetached;
proto.$nextDetached = null;
}
return Type;
}
export const CustomAttributeResource = {
name: customAttributeName,
keyFrom: customAttributeKey,
isType,
define
};
/** @internal */
export function createCustomAttributeDescription(def: IAttributeDefinition, Type: ICustomAttributeType): ResourceDescription<IAttributeDefinition> {
const aliases = def. aliases;
const defaultBindingMode = def.defaultBindingMode;
return {
name: def.name,
aliases: aliases === undefined || aliases === null ? PLATFORM.emptyArray : aliases,
defaultBindingMode: defaultBindingMode === undefined || defaultBindingMode === null ? BindingMode.toView : defaultBindingMode,
hasDynamicOptions: def.hasDynamicOptions === undefined ? false : def.hasDynamicOptions,
isTemplateController: def.isTemplateController === undefined ? false : def.isTemplateController,
bindables: { ...Bindable.for(Type as unknown as {}).get(), ...Bindable.for(def).get() },
strategy: ensureValidStrategy(def.strategy)
};
}
export type CustomAttributeDecorator = <T extends Constructable>(target: T) => T & ICustomAttributeType<T>;