Skip to content

Commit 3e98752

Browse files
committed
update
1 parent be99520 commit 3e98752

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed

dist/js/brutusin-json-forms.js

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,39 @@ if (typeof brutusin === "undefined") {
318318
renderers["boolean"] = function (container, id, parentObject, propertyProvider, value) {
319319
var schemaId = getSchemaId(id);
320320
var s = getSchema(schemaId);
321-
var input = document.createElement("input");
322-
input.type = "checkbox";
323-
input.schema = schemaId;
321+
var input;
322+
if (s.required) {
323+
input = document.createElement("input");
324+
input.type = "checkbox";
325+
if (value === true) {
326+
input.checked = true;
327+
}
328+
} else {
329+
input = document.createElement("select");
330+
var emptyOption = document.createElement("option");
331+
var textEmpty = document.createTextNode("");
332+
textEmpty.value = "";
333+
appendChild(emptyOption, textEmpty, s);
334+
appendChild(input, emptyOption, s);
335+
336+
var optionTrue = document.createElement("option");
337+
var textTrue = document.createTextNode("true");
338+
textTrue.value = "true";
339+
appendChild(optionTrue, textTrue, s);
340+
appendChild(input, optionTrue, s);
341+
342+
var optionFalse = document.createElement("option");
343+
var textFalse = document.createTextNode("false");
344+
textFalse.value = "false";
345+
appendChild(optionFalse, textFalse, s);
346+
appendChild(input, optionFalse, s);
347+
348+
if (value === true) {
349+
input.selectedIndex = 1;
350+
} else if (value === false) {
351+
input.selectedIndex = 2;
352+
}
353+
}
324354
input.onchange = function () {
325355
if (parentObject) {
326356
parentObject[propertyProvider.getValue()] = getValue(s, input);
@@ -329,9 +359,7 @@ if (typeof brutusin === "undefined") {
329359
}
330360
onDependencyChanged(schemaId, input);
331361
};
332-
if (value === true) {
333-
input.checked = true;
334-
}
362+
input.schema = schemaId;
335363
input.id = getInputId();
336364
inputCounter++;
337365
if (s.description) {
@@ -772,39 +800,44 @@ if (typeof brutusin === "undefined") {
772800
};
773801

774802
obj.getData = function () {
775-
function removeEmptiesAndNulls(object) {
803+
function removeEmptiesAndNulls(object, schema) {
776804
if (object instanceof Array) {
777805
if (object.length === 0) {
778806
return null;
779807
}
780808
var clone = new Array();
781809
for (var i = 0; i < object.length; i++) {
782-
clone[i] = removeEmptiesAndNulls(object[i]);
810+
clone[i] = removeEmptiesAndNulls(object[i], schema.items);
783811
}
784812
return clone;
785813
} else if (object === "") {
786814
return null;
787815
} else if (object instanceof Object) {
788816
var clone = new Object();
817+
var nonEmpty = false;
789818
for (var prop in object) {
790819
if (prop.startsWith("$") && prop.endsWith("$")) {
791820
continue;
792821
}
793-
var value = removeEmptiesAndNulls(object[prop]);
822+
var value = removeEmptiesAndNulls(object[prop], schema.properties[prop]);
794823
if (value !== null) {
795824
clone[prop] = value;
825+
nonEmpty = true;
796826
}
797827
}
798-
return clone;
828+
if (nonEmpty || schema.required) {
829+
return clone;
830+
} else {
831+
return null;
832+
}
799833
} else {
800834
return object;
801835
}
802836
}
803837
if (!container) {
804838
return null;
805839
} else {
806-
return removeEmptiesAndNulls(data);
807-
;
840+
return removeEmptiesAndNulls(data, schema);
808841
}
809842
};
810843

@@ -1093,9 +1126,19 @@ if (typeof brutusin === "undefined") {
10931126
value = null;
10941127
}
10951128
} else if (schema.type === "boolean") {
1096-
value = input.checked;
1097-
if (!value) {
1098-
value = false;
1129+
if (input.tagName.toLowerCase() === "input") {
1130+
value = input.checked;
1131+
if (!value) {
1132+
value = false;
1133+
}
1134+
} else if (input.tagName.toLowerCase() === "select") {
1135+
if (input.value === "true") {
1136+
value = true;
1137+
} else if (input.value === "false") {
1138+
value = false;
1139+
} else {
1140+
value = null;
1141+
}
10991142
}
11001143
} else if (schema.type === "any") {
11011144
if (value) {

0 commit comments

Comments
 (0)