-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdom-bindings.d.ts
191 lines (167 loc) · 4.58 KB
/
dom-bindings.d.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
// Expressions
export enum ExpressionType {
ATTRIBUTE,
EVENT,
TEXT,
VALUE,
}
export interface BaseExpressionData<Scope = any> {
type: ExpressionType
evaluate(scope: Scope): any
}
export interface AttributeExpressionData<Scope = any>
extends BaseExpressionData<Scope> {
name: string
}
export interface EventExpressionData<Scope = any>
extends BaseExpressionData<Scope> {
name: string
}
export interface TextExpressionData<Scope = any>
extends BaseExpressionData<Scope> {
childNodeIndex: number
}
export interface ValueExpressionData<Scope = any>
extends BaseExpressionData<Scope> {}
export type ExpressionData<Scope = any> =
| AttributeExpressionData<Scope>
| EventExpressionData<Scope>
| TextExpressionData<Scope>
| ValueExpressionData<Scope>
export interface Expression<Scope = any> {
type: ExpressionType
node: HTMLElement
value: any
mount(scope: Scope): Expression<Scope>
update(scope: Scope): Expression<Scope>
unmount(scope: Scope): Expression<Scope>
}
// Bindings
export enum BindingType {
EACH,
IF,
SIMPLE,
TAG,
SLOT,
}
export interface BaseBindingData<Scope = any> {
selector?: string
redundantAttribute?: string
type?: BindingType
evaluate?(scope: Scope): any
}
export interface EachBindingData<
Scope = any,
ItemName extends string = string,
IndexName extends string = string,
ItemValue extends any = any,
ExtendedScope = Scope & { [Property in ItemName]: ItemValue } & {
[Property in IndexName]: number
},
> {
itemName: ItemName
indexName?: IndexName | null
template: TemplateChunk<ExtendedScope>
getKey?: ((scope: ExtendedScope) => any) | null
condition?: ((scope: ExtendedScope) => any) | null
evaluate(scope: Scope): ItemValue[]
selector?: string
redundantAttribute?: string
}
export interface IfBindingData<Scope = any> extends BaseBindingData<Scope> {
template: TemplateChunk<Scope>
}
export interface SimpleBindingData<Scope = any> extends BaseBindingData<Scope> {
expressions: ExpressionData<Scope>[]
}
export interface SlotBindingData<Scope = any> extends BaseBindingData<Scope> {
template?: TemplateChunk<Scope>
attributes: AttributeExpressionData<Scope>[]
name: string
}
export interface TagSlotData<Scope = any> {
id: string
html: string
bindings: BindingData<Scope>[]
}
export interface TagBindingData<Scope = any> extends BaseBindingData<Scope> {
getComponent(
name: string,
): ({
slots,
attributes,
}: {
slots: TagSlotData<Scope>[]
attributes: AttributeExpressionData<Scope>[]
}) => Pick<TemplateChunk<Scope>, 'mount' | 'update' | 'unmount'>
attributes: AttributeExpressionData<Scope>[]
slots: TagSlotData[]
}
export type BindingData<Scope = any> =
| EachBindingData<Scope>
| IfBindingData<Scope>
| SimpleBindingData<Scope>
| SlotBindingData<Scope>
| TagBindingData<Scope>
export interface Binding<Scope = any, ParentScope = any> {
mount(
el: HTMLElement,
scope: Scope,
parentScope?: ParentScope,
meta?: TemplateChunkMeta,
): Binding<Scope, ParentScope>
update(scope: Scope, parentScope?: ParentScope): Binding<Scope, ParentScope>
unmount(
scope: Scope,
parentScope?: ParentScope,
mustRemoveRoot?: boolean,
): Binding<Scope, ParentScope>
}
// Template Object
export interface TemplateChunkMeta {
fragment: DocumentFragment
children: HTMLCollection
avoidDOMInjection: boolean
}
export interface TemplateChunk<Scope = any, ParentScope = any> {
mount(
el: HTMLElement,
scope: Scope,
parentScope?: ParentScope,
meta?: TemplateChunkMeta,
): TemplateChunk
update(scope: Scope, parentScope?: ParentScope): TemplateChunk
unmount(
scope: Scope,
parentScope?: ParentScope,
mustRemoveRoot?: boolean,
): TemplateChunk
clone(): TemplateChunk
createDOM(el: HTMLElement): TemplateChunk
bindings?: Binding<Scope, ParentScope>[]
bindingsData?: BindingData<Scope>[]
html?: string | null
isTemplateTag?: boolean
fragment?: DocumentFragment
children?: HTMLCollection
dom?: HTMLElement
el?: HTMLElement
}
// API
export function template<Scope = any, ParentScope = any>(
template: string,
bindings?: BindingData<Scope>[],
): TemplateChunk<Scope, ParentScope>
export function createBinding<Scope = any>(
root: HTMLElement,
binding: BindingData<Scope>,
templateTagOffset?: number | null,
): Binding<Scope>
export function createExpression<Scope = any>(
node: HTMLElement,
expression: ExpressionData<Scope>,
): Expression<Scope>
export const bindingTypes: { [key in keyof typeof BindingType]: BindingType }
export const expressionTypes: {
[key in keyof typeof ExpressionType]: ExpressionType
}