/
XNode.ts
186 lines (164 loc) · 5.45 KB
/
XNode.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
import Bind from "./Bind";
import { IClassOf } from "./types";
export interface IAttributes {
[key: string]: string | number | null | any;
}
export class RootObject {
public get vsProps(): {
[k in keyof this]?: this[k] | Bind
} | { [k: string]: any } | {} {
return undefined;
}
}
// tslint:disable-next-line: no-namespace
declare global {
namespace JSX {
// tslint:disable-next-line: interface-name
interface ElementAttributesProperty {
vsProps;
}
type HtmlPartial<T> = {
[k in keyof T]?: {
[tx in keyof T[k]]?: T[k][tx] | any;
} | {
eventClick?: any;
eventBlur?: any;
eventFocus?: any;
text?: string | any;
[key: string]: any
}
};
type IAllHtmlElements = HtmlPartial<HTMLElementTagNameMap>;
// tslint:disable-next-line
interface IntrinsicElements extends IAllHtmlElements {
}
}
}
export type IMergedControl<T, T1> =
{
[P in keyof (T & T1)]?: (T & T1)[P];
} & {
vsProps: {
[P in keyof (T & T1)]?: (T & T1)[P];
}
};
export type NodeFactory = (a?: any, ... nodes: XNode[]) => XNode;
export type AttachedNode = (n: any) => { [key: string]: any};
export default class XNode {
public static attach<T, T1 extends HTMLElementTagNameMap, K extends keyof T1>(
n: IClassOf<T>,
tag: K): new (... a: any[]) => IMergedControl<T, T1[K]> ;
public static attach<T, T1>(
n: IClassOf<T>,
tag: (a?: Partial<T1>, ... nodes: XNode[]) => XNode): new (... a: any[]) => IMergedControl<T, T1> ;
public static attach(
n: any,
tag: any): any {
return {
factory: (attributes: any, ... nodes: XNode[] ) => new XNode(n, attributes
? { ... attributes, for: tag }
: { for: tag}, nodes)
};
}
public static prepare<T>(
n: any,
isProperty?: boolean,
isTemplate?: boolean): ((attributes: Partial<T>, ... nodes: XNode[]) => XNode) {
return {
factory(a: any, ... nodes: any[]) {
return new XNode(n, a, nodes, isProperty , isTemplate);
},
toString() {
return n;
}
} as any;
}
public static template(): NodeFactory {
return {
factory: true,
isTemplate: true,
} as any;
}
/**
* This is only for intellisense...
*/
public static attached(): AttachedNode {
return {
attached: true
} as any;
}
public static property(): NodeFactory {
return {
factory: true
} as any;
}
/**
* Declares Root Namespace and Assembly. You can use return function to
* to declare the type
* @param ns Root Namespace
*/
public static namespace(ns: string, assemblyName: string) {
return (type: string, isTemplate?: boolean) => {
return (c) => {
for (const key in c) {
if (c.hasOwnProperty(key)) {
const element = c[key];
if (element) {
const n = ns + "." + type + ":" + key + ";" + assemblyName;
const af: any = (a) => {
const r = {
[n]: a
};
Object.defineProperty(r, "toString", {
value: () => n,
enumerable: false,
configurable: false
});
return r;
};
af.factory = (a?: any, ... nodes: any[]) =>
new XNode(n, a, nodes, true, element.isTemplate );
af.toString = () => n;
c[key] = af;
}
}
}
const tn = ns + "." + type + ";" + assemblyName;
c.factory = (a?: any, ... nodes: XNode[]) => {
return new XNode(tn, a, nodes, false, isTemplate);
};
c.toString = () => tn;
};
};
}
public static create(
// tslint:disable-next-line: ban-types
name: string | Function,
attributes: IAttributes,
... children: Array<XNode | XNode[] | any>): XNode {
if ((name as any).factory) {
return ((name as any).factory)(attributes, ... children);
}
if ((name as any).isControl) {
return new XNode(name as any, attributes, children);
}
if (typeof name === "object") {
name = (name as any).toString();
}
return new XNode(name as any, attributes, children);
}
constructor(
// tslint:disable-next-line: ban-types
public name: string | Function,
public attributes: IAttributes,
public children: XNode[] | XNode[][] | any[],
public isProperty?: boolean,
public isTemplate?: boolean) {
}
public toString(): string {
if (typeof this.name === "string") {
return `name is of type string and value is ${this.name}`;
}
return `name is of type ${typeof this.name}`;
}
}