-
Notifications
You must be signed in to change notification settings - Fork 1
/
literatureForm.js
235 lines (214 loc) · 5.34 KB
/
literatureForm.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/** -- Operations -- **/
// Retrieve the data, and locate the div in the Twig
let data = API.getData("rowClicked").resurrect();
let divTwig = document.querySelector("#inputNext");
// Initialise the innerHTML of the regulation data input select
let inputRegulationIhtml = new String();
// Append options to the regulation input select innerHTML
["---", "decreased", "increased", "unknown"].forEach(e => {
let option = document.createElement("option");
option.innerHTML = e;
inputRegulationIhtml += option.outerHTML;
});
// Define the label elements
let label = {
smiles: makeNode({
node: "label",
attr: {
for: "inputSmiles",
innerHTML: "SMILES code",
disabled: true,
readonly: true
}
}),
name: makeNode({
node: "label",
attr: {
for: "inputName",
innerHTML: "Name(s)"
}
}),
disease: makeNode({
node: "label",
attr: {
for: "inputDisease",
innerHTML: "Disease status"
}
}),
doi: makeNode({
node: "label",
attr: {
for: "inputDoi",
innerHTML: "DOI"
}
}),
regulation: makeNode({
node: "label",
attr: {
for: "inputRegulation",
innerHTML: "Regulation"
}
})
};
// Define the input elements
let input = {
smiles: makeNode({
node: "input",
attr: {
id: "inputSmiles",
type: "text",
value: data.SMILES,
disabled: true,
readonly: true
}
}),
name: makeNode({
node: "input",
attr: {
id: "inputName",
type: "text",
value: (function() {
let n = data.main["Metabolite Name"];
if (typeof(n) === "string") return n;
return n.join(", ");
})(),
disabled: true,
readonly: true
}
}),
disease: makeNode({
node: "input",
attr: {
id: "inputDisease",
type: "text",
placeholder: "e.g. SARS-CoV-2",
required: true
}
}),
doi: makeNode({
node: "input",
attr: {
id: "inputDoi",
type: "text",
placeholder: "e.g. 10.1111/evj.13292", // Shameless self-promotion!
required: true
}
}),
regulation: makeNode({
node: "select",
attr: {
id: "inputRegulation",
innerHTML: inputRegulationIhtml
}
})
};
// Initialise the table and get the keys to iterate over
let table = divTable();
let keys = Object.keys(label);
// ^ ONLY because the keys are identical by design
// For each key, append the label and select elements
keys.forEach(k => {
table.append(tableRowLabelNode({label: label[k], node: input[k]}));
});
// Clear out the Twig template div and append the table
divTwig.innerHTML = '';
divTwig.append(table);
// Create the button to add a new entry
let button = makeNode({
node: "button",
attr: {
innerHTML: "ADD NEW LITERATURE ENTRY",
style: {
borderRadius: "0px",
fontFamily: "monospace",
fontWeight: "normal",
margin: "4px 6px"
}
}
});
// ^ Would like to centre this under the table.
// Perhaps wrap the table and button in a div? Use the main Twig div?
// Cannot assign functions using applyAttributes -- fix, if possible?
button.onclick = submitFormData;
// Append the button to the Twig template div
divTwig.append(button);
/* -- End of operations -- */
/** -- Function definitions -- **/
// Function for creating a table row containing a label and a DOM node
function tableRowLabelNode(data) {
let row = divTableRow();
row.append(applyAttributes(
divTableCell(),
{innerHTML: data.label.outerHTML}
));
row.append(applyAttributes(
divTableCell(),
{innerHTML: data.node.outerHTML}
));
return row;
};
// Helper function for creating a node using the provided spec
function makeNode(spec) {
return applyAttributes(document.createElement(spec.node), spec.attr);
};
// Helper function for applying a group of attributes to a DOM node
function applyAttributes(element, attributes) {
for (let attr in attributes) {
if (attr === "style") {
for (let s in attributes.style) element.style[s] = attributes.style[s];
} else if (attr === "innerHTML") {
element.innerHTML = attributes.innerHTML;
} else {
element.setAttribute(attr, attributes[attr]);
};
};
return element;
};
// Helper function for initialising a div table
function divTable() {
let attrs = {
class: "table",
style: {
display: "table"
}
};
return applyAttributes(document.createElement("div"), attrs);
};
// Helper function for initialising a div table row
function divTableRow() {
let attrs = {
class: "table-row",
style: {
display: "table-row"
}
};
return applyAttributes(document.createElement("div"), attrs);
};
// Helper function for initialising a div table cell
function divTableCell() {
let attrs = {
class: "table-cell",
style: {
display: "table-cell"
}
};
return applyAttributes(document.createElement("div"), attrs);
};
// Special-purpose function for submitting the HTML (Twig) form data provided
// for the new literature entry
function submitFormData() {
let output = new Object();
let keys = {
smiles: "SMILES",
name: "Name",
disease: "Disease Status",
doi: "DOI",
regulation: "Regulation"
};
for (let k in keys) {
let selector = `#input${k.replace(/^[a-z]/, l => l.toUpperCase())}`;
output[keys[k]] = document.querySelector(selector).value;
};
API.createData("newLiterature", output);
return;
};