-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
168 lines (140 loc) · 5.63 KB
/
main.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
class Stats {
constructor() {
this.buttonCount = 0;
this.contentCount = 0;
this.imageCount = 0;
this.listCount = 0;
this.listItemCount = 0;
this.textCount = 0;
this.viewCount = 0;
}
getButtonCount() { return this.buttonCount; }
getContentCount() { return this.contentCount; }
getImageCount() { return this.imageCount; }
getListCount() { return this.listCount; }
getListItemCount() { return this.listItemCount; }
getTextCount() { return this.textCount; }
getViewCount() { return this.viewCount; }
addToButtonCount() { this.buttonCount += 1; }
addToContentCount() { this.contentCount += 1; }
addToImageCount() { this.imageCount += 1; }
addToListCount() { this.listCount += 1; }
addToListItemCount() { this.listItemCount += 1; }
addToTextCount() { this.textCount += 1; }
addToViewCount() { this.viewCount += 1; }
}
class Node {
constructor(nodeType, nodeId) {
this.nodeInnerContent = [];
this.nodeType = nodeType;
this.nodeId = nodeId;
}
addInnerNode(nodeType, nodeName, stats) {
let newInnerNode;
switch (nodeType) {
case 'view':
stats.addToViewCount();
if (nodeName) {
newInnerNode = new InnerNode('view', nodeName, new Node('view', nodeName));
} else {
newInnerNode = new InnerNode('view', stats.getViewCount(), new Node('view', stats.getViewCount()));
}
this.nodeInnerContent.push(newInnerNode);
break;
case 'content' :
stats.addToContentCount();
newInnerNode = new InnerNode('content', stats.getContentCount());
newInnerNode.contentValue = nodeName;
this.nodeInnerContent.push(newInnerNode);
break;
default:
}
}
dfs(nodeContent) {
console.log(this.nodeType + " " + this.nodeId + "Hello");
let nodeType = this.nodeType;
let openingTag, closingTag;
switch (nodeType) {
case 'view':
openingTag = '<ViewComponent>';
break;
case 'text':
openingTag = '<TextComponent>';
break;
default:
// console.log("Default");
}
nodeContent += openingTag;
for (let i = 0; i < this.nodeInnerContent.length; i++) {
if (this.nodeInnerContent[i].externalPointingNode) {
nodeContent = this.nodeInnerContent[i].externalPointingNode.dfs(nodeContent);
} else {
const element = this.nodeInnerContent[i];
if (element.innerNodeType === 'button') {
nodeContent += '<Button></Button>';
} else if (element.innerNodeType === 'image') {
nodeContent += '<Image></Image>';
} else if (element.innerNodeType === 'content') {
if(element.contentValue) {
nodeContent += '<Content inputContent={' + element.contentValue + '}></Content>';
} else {
nodeContent += '<Content></Content>';
}
}
console.log(element.innerNodeType + " " + element.innerNodeId);
}
}
switch (nodeType) {
case 'view':
closingTag = '</ViewComponent>';
break;
case 'text':
closingTag = '</TextComponent>';
break;
default:
// console.log("default");
}
nodeContent += closingTag;
return nodeContent;
}
}
class InnerNode {
constructor() {
if (arguments.length === 3) {
this.innerNodeType = arguments[0];
this.innerNodeId = arguments[1];
this.externalPointingNode = arguments[2];
} else if (arguments.length === 2) {
this.innerNodeType = arguments[0];
this.innerNodeId = arguments[1];
}
}
}
let parentLevelNode = new Node('view', 0);
let stats = new Stats();
// shift to level 1 node
let currentNode = parentLevelNode;
// construct level 1 node
stats.viewCount += 1;
currentNode.nodeInnerContent.push(new InnerNode('view', stats.viewCount, new Node('view', stats.viewCount)));
stats.viewCount += 1;
currentNode.nodeInnerContent.push(new InnerNode('view', stats.viewCount, new Node('view', stats.viewCount)));
stats.viewCount += 1;
currentNode.nodeInnerContent.push(new InnerNode('view', stats.viewCount, new Node('view', stats.viewCount)));
stats.buttonCount += 1;
currentNode.nodeInnerContent.push(new InnerNode('button', stats.buttonCount));
stats.textCount += 1;
currentNode.nodeInnerContent.push(new InnerNode('text', stats.textCount, new Node('text', stats.textCount)));
stats.textCount += 1;
currentNode.nodeInnerContent.push(new InnerNode('text', stats.textCount, new Node('text', stats.textCount)));
stats.contentCount += 1;
currentNode.nodeInnerContent.push(new InnerNode('content', stats.contentCount));
// 3rd level
currentNode = currentNode.nodeInnerContent[0].externalPointingNode;
stats.viewCount += 1;
currentNode.nodeInnerContent.push(new InnerNode('view', stats.viewCount, new Node('view', stats.viewCount)));
stats.viewCount += 1;
currentNode.nodeInnerContent.push(new InnerNode('view', stats.viewCount, new Node('view', stats.viewCount)));
// module.exports = { parentLevelNode: parentLevelNode, currentElements: nodeContent.__html };
module.exports = { parentLevelNode: parentLevelNode, stats: stats };
// console.log(parentLevelNode.dfs(""));