-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTextboxBuilder.test.js
191 lines (167 loc) · 5.82 KB
/
TextboxBuilder.test.js
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
const utils = {
createVirutalElement(tagName) {
if (typeof document === "undefined") {
const jsdom = require("jsdom").JSDOM;
const doc = new jsdom("<html><body></body></html>");
const document = doc.window.document;
return document.createElement(tagName);
} else {
if (tagName === "body") return document.body;
return document.createElement(tagName);
}
},
toCamelCase(name) {
const snake = name || "";
let nodes = snake.split(/[\s\-]/);
let nodesTail = nodes.slice(1);
const camel = nodes[0].concat(
nodesTail.map((i) => {
return i[0].toUpperCase() + i.slice(1);
})
);
return camel;
},
getClassName(name) {
const str = toCamelCase(name);
return str.slice(0, 1).toUpperCase() + str.slice(1);
},
};
/**
* @type {HTMLElement}
*/
const body = utils.createVirutalElement("body");
/**
* @class TextBoxBuilder
* @description
* This class is used to create a text box.
*/
class TextBoxBuilder {
render() {
const elements = this.prepareElement();
const rootElement = elements.root;
let parentNode = null;
const virtualRender = (root) => {
if (!root.tagName) {
return;
}
// create an element.
const elem = utils.createVirutalElement(root.tagName);
if (!parentNode) {
// parentNode = document.body;
parentNode = body;
}
parentNode.appendChild(elem);
// set attributes.
if (root.attributes) {
for (const attr in root.attributes) {
elem.setAttribute(attr, root.attributes[attr]);
}
}
// set class.
if (root.class) {
elem.className = root.class;
}
// set type.
if (root.type) {
elem.setAttribute("type", root.type);
}
// set id.
if (root.id) {
elem.setAttribute("id", root.id);
}
// set placeholder.
if (root.placeholder) {
elem.setAttribute("placeholder", root.placeholder);
}
// set value.
if (root.value) {
elem.setAttribute("value", root.value);
}
// set style.
if (root.style) {
// change snake to camel.
const propertyName = utils.toCamelCase(style);
for (const style in root.style) {
elem.style[propertyName] = root.style[style];
}
// cannot control media query.
}
// set injected style.
if (root.injectedStyle) {
elem.setAttribute("style", root.injectedStyle);
}
// set children.
if (root.children) {
// parent node를 이것으로 설정
parentNode = elem;
root.children.forEach((child) => {
virtualRender(child);
});
}
};
virtualRender(rootElement);
}
prepareElement() {
return {
root: {
class: "inputDialogContainer",
tagName: "table",
children: [
{
tagName: "tr",
children: [
{
tagName: "td",
class: "col",
children: [
{
tagName: "input",
type: "text",
id: "RS.InputDialog.Params.szTextBoxId",
class: "inputDialog",
placeholder:
"RS.InputDialog.Params.localText",
},
],
},
],
},
{
tagName: "tr",
class: "row",
attributes: {
valign: "bottom",
},
children: [
{
tagName: "td",
class: "col",
attributes: {
align: "right",
},
children: [
{
tagName: "input",
type: "button",
class: "defaultButton",
id: "inputDialog-OkBtn",
value: "RS.InputDialog.Params.okButtonName",
},
{
tagName: "input",
type: "button",
class: "defaultButton",
id: "inputDialog-CancelBtn",
value: "RS.InputDialog.Params.cancelButtonName",
},
],
},
],
},
],
},
};
}
}
const textBoxBuilder = new TextBoxBuilder();
textBoxBuilder.render();