forked from leesue630/tree-to-sml-converter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
from-rose-script.js
199 lines (175 loc) · 5.31 KB
/
from-rose-script.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
192
193
194
195
196
197
198
199
function getRoseValue(indicesStr) {
return document.getElementById("rose_value" + indicesStr).value;
}
function getRoseChildCountValue(indicesStr) {
var count = document.getElementById("rose_child_count" + indicesStr).value;
if (isNaN(count) || count === "") {
return 0;
}
return Math.max(Math.min(count, 5), 0);
}
function getRoseChildRow(indicesStr) {
return document.getElementById("rose_children_row" + indicesStr);
}
function getRoseCell(indicesStr) {
return document.getElementById("cell" + indicesStr);
}
/**
* Clears the table of all its children.
*/
function createGenerateRoseChildrenFunction(rootIndicesStr) {
return function () {
generateRoseChildren(rootIndicesStr);
};
}
function generateRoseChildren(rootIndicesStr) {
// console.log("rootIndicesStr", rootIndicesStr);
var childCount = getRoseChildCountValue(rootIndicesStr);
document.getElementById("rose_root" + rootIndicesStr).colSpan =
childCount > 0 ? childCount : 1;
// console.log("childCount", childCount);
var childRow = getRoseChildRow(rootIndicesStr);
// console.log("childRow count", childRow.cells.length);
if (childCount < childRow.cells.length) {
// console.log("remove children");
while (childCount < childRow.cells.length) {
// console.log("removed a child");
childRow.removeChild(childRow.lastChild);
}
} else if (childCount > childRow.cells.length) {
// console.log("add children");
var i = childRow.cells.length;
for (; i < childCount; i++) {
// console.log("added a child");
var cell = childRow.insertCell();
setAttributes(cell, { colSpan: 1, align: "center" });
cell.appendChild(newRoseCell(rootIndicesStr + i));
document.getElementById(
"rose_child_count" + rootIndicesStr + i
).oninput = createGenerateRoseChildrenFunction(rootIndicesStr + i);
}
} else {
// console.log("correct");
}
}
function setAttributes(el, attrs) {
for (var key in attrs) {
el.setAttribute(key, attrs[key]);
}
}
function printHello(indicesStr) {
console.log("hello" + indicesStr);
}
function createPrintHello(indicesStr) {
return function () {
printHello(indicesStr);
};
}
function newRoseValueInput(indicesStr) {
var roseValue = document.createElement("input");
setAttributes(roseValue, {
type: "text",
size: "6",
id: "rose_value" + indicesStr,
placeholder: "value",
});
return roseValue;
}
function newRoseChildCountInput(indicesStr) {
var childCountValue = document.createElement("input");
setAttributes(childCountValue, {
type: "number",
size: 3,
min: 0,
max: 5,
id: "rose_child_count" + indicesStr,
value: 0,
placeholder: 0,
});
// childCountValue.oninput = createGenerateRoseChildrenFunction(indicesStr);
return childCountValue;
}
/**
* Creates and returns a table element with id "cellij".
* The div contains a Rose Value input element and a numeric input for children
* count/list length
* @param L The list of indices you traverse from the root to reach the cell
*/
function newRoseCell(indicesStr) {
// create table container
var table = document.createElement("table");
var row1 = table.insertRow();
var cell = row1.insertCell();
setAttributes(cell, {
colSpan: 1,
align: "center",
id: "rose_root" + indicesStr,
});
var div = document.createElement("div");
setAttributes(div, { id: "cell" + indicesStr, align: "left" });
div.classList.add("left-inner", "rose-div");
colorCell(div, "base");
div.innerHTML += "Rose: ";
var roseValueInput = newRoseValueInput(indicesStr);
div.appendChild(roseValueInput);
div.innerHTML += "<br/>";
var roseChildCountInput = newRoseChildCountInput(indicesStr);
div.appendChild(roseChildCountInput);
div.innerHTML += " children";
cell.appendChild(div);
var row2 = table.insertRow();
setAttributes(row2, {
id: "rose_children_row" + indicesStr,
valign: "top",
});
return table;
}
function generateRoseTable() {
// console.log("generateInputTable for rose");
clearTable();
var row = table.insertRow();
var cell = row.insertCell();
setAttributes(cell, { colSpan: 1, align: "center" });
cell.appendChild(newRoseCell("0"));
document.getElementById(
"rose_child_count0"
).oninput = createGenerateRoseChildrenFunction("0");
}
/**
* Returns the SML version of the rose rooted at cell indicesStr
* @param indicesStr The string of concatenated indices
*/
function roseTextHelper(indicesStr) {
var roseCellValue = getRoseValue(indicesStr);
colorCell(getRoseCell(indicesStr), "valid");
var text = "Rose(" + roseCellValue + ",[";
if (roseCellValue.includes("-"))
setNegativeWarningText("Warning: Negative sign used instead of ~");
if (roseCellValue === "")
setTableWarningText("Warning: Empty rose value");
for (var i = 0; i < getRoseChildCountValue(indicesStr); i++) {
text += roseTextHelper(indicesStr + i);
if (i < getRoseChildCountValue(indicesStr) - 1) {
text += ",";
}
}
text += "])";
return text;
}
/**
* Called when 'Generate SML Text' button clicked for "rose" tab
*/
function generateRoseText() {
// // reset error text
resetWarningText();
// compute SML text from tree/shrub
var text;
try {
text = roseTextHelper("0");
} catch (e) {
console.log(e);
text = "Something went wrong...";
}
// set SML output text
setSMLOutputText(text);
}