Skip to content

Commit df09c7d

Browse files
Merge pull request #12 from DSACMS/nat/objs
Components: Added Data Grid component & Updated CDNs
2 parents 69ddbf4 + 3a8da32 commit df09c7d

File tree

4 files changed

+56
-28
lines changed

4 files changed

+56
-28
lines changed

form.html

+9-13
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,13 @@
1414
<!-- <script src="https://cdn.form.io/js/formio.embed.js"></script> -->
1515

1616
<!-- Uses bootstrap for now -->
17-
<link
18-
rel="stylesheet"
19-
href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css"
20-
/>
21-
<link
22-
rel="stylesheet"
23-
href="https://cdn.jsdelivr.net/npm/bootstrap/dist/css/bootstrap.min.css"
24-
/>
17+
<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'>
2518

2619
<!-- Working Form.io CDN -->
27-
<script src="https://unpkg.com/[email protected]/dist/formio.full.min.js"></script>
28-
</head>
29-
<body>
30-
<div id="form-header"></div>
31-
<div id="formio"></div>
20+
<script src="https://unpkg.com/[email protected]/dist/formio.full.min.js"></script>
21+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/formio.full.min.css" rel="stylesheet">
22+
23+
<!-- Render the form -->
3224
<script src="js/generateFormComponents.js"></script>
3325
<script src="js/formDataToJson.js"></script>
3426
<script type="text/javascript">
@@ -55,5 +47,9 @@
5547
console.error("Error creating components:", error);
5648
});
5749
</script>
50+
</head>
51+
<body>
52+
<div id="form-header"></div>
53+
<div id="formio"></div>
5854
</body>
5955
</html>

js/generateFormComponents.js

+39-8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ function determineType(field) {
2626
if (field.type === "object") {
2727
return "container";
2828
} else if (field.type === "array") {
29+
// Array of objects
30+
if (field.items.type === "object") {
31+
return "datagrid"
32+
}
2933
// Multi-select
3034
if (field.items.hasOwnProperty("enum")) {
3135
return "selectboxes";
@@ -37,9 +41,9 @@ function determineType(field) {
3741
return "radio";
3842
} else if (field.type === "number") {
3943
return "number";
40-
} else if (field.type === "boolean"){
44+
} else if (field.type === "boolean") {
4145
return "select-boolean";
42-
} else if (field.type === "string") {
46+
} else if (field.type === "string" || field.type.includes("string")) {
4347
if (field.format == "date-time") {
4448
return "datetime";
4549
}
@@ -100,7 +104,8 @@ function createComponent(fieldName, fieldObject) {
100104
key: fieldName,
101105
type: "radio",
102106
input: true,
103-
description: fieldObject["description"]
107+
description: fieldObject["description"],
108+
tooltip: fieldObject["description"]
104109
};
105110
case "selectboxes":
106111
var options = transformArrayToOptions(fieldObject.items.enum);
@@ -184,43 +189,69 @@ function createComponent(fieldName, fieldObject) {
184189
input: true,
185190
components: []
186191
};
192+
case "datagrid":
193+
return {
194+
label: fieldName,
195+
reorder: false,
196+
addAnotherPosition: "bottom",
197+
layoutFixed: false,
198+
enableRowGroups: false,
199+
initEmpty: false,
200+
tableView: false,
201+
defaultValue: [
202+
{}
203+
],
204+
validateWhenHidden: false,
205+
key: fieldName,
206+
type: "datagrid",
207+
input: true,
208+
components: []
209+
};
187210
default:
188211
break;
189212
}
190213
}
191214

215+
// Adds heading containing schema information
192216
function createFormHeading(title, description) {
193217
const container = document.getElementById('form-header');
194218
container.innerHTML = `<h1>${title}</h1>\n<h2>${description}</h2>`;
195219
}
196220

221+
// Iterates through each json field and creates component array for Form.io
197222
function createAllComponents(schema, prefix = ""){
198223
let components = [];
199224

200-
console.log("checking schema", schema);
201-
202225
if (schema.type === "object" && schema.properties) {
203-
for (const [key, value] of Object.entries(schema.properties)) {
226+
227+
let items = schema.properties.hasOwnProperty("items") ? schema.properties.items : schema.properties
228+
229+
for (const [key, value] of Object.entries(items)) {
204230

231+
console.log("key at play:", key);
205232
const fullKey = prefix ? `${prefix}.${key}` : key;
206233

207234
var fieldComponent = createComponent(key, value);
208235

209236
if (fieldComponent.type === "container") {
210237
fieldComponent.components = createAllComponents(value, fullKey);
238+
}
239+
else if (fieldComponent.type === "datagrid") {
240+
fieldComponent.components = createAllComponents(value.items, fullKey);
211241
}
242+
212243
components.push(fieldComponent);
213244
}
214245
}
215246

216247
return components;
217248
}
218249

219-
// Iterates through each json field and creates component array for Form.io
250+
// Creates complete form based on input json schema
220251
async function createFormComponents() {
221252
let components = [];
222253

223-
const filePath = "schemas/schema.json";
254+
const filePath = "schemas/schema-0.0.0.json";
224255
let jsonData = await retrieveFile(filePath);
225256
console.log("JSON Data:", jsonData);
226257

schemas/schema-0.0.0.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@
4242
"items": {
4343
"type": "object",
4444
"properties": {
45-
"URL": {
46-
"type": "string",
47-
"format": "uri",
48-
"description": "The URL of the release license"
49-
},
5045
"name": {
5146
"type": "string",
5247
"enum": [
@@ -66,11 +61,16 @@
6661
"None"
6762
],
6863
"description": "An abbreviation for the name of the license"
64+
},
65+
"URL": {
66+
"type": "string",
67+
"format": "uri",
68+
"description": "The URL of the release license"
6969
}
7070
},
7171
"required": [
72-
"url",
73-
"name"
72+
"name",
73+
"URL"
7474
]
7575
}
7676
},

schemas/template-code.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"description": "",
44
"longDescription": "",
55
"status": "",
6+
"permissions": "",
67
"organization": "",
78
"repositoryURL": "",
89
"vcs": "",

0 commit comments

Comments
 (0)