Skip to content

Commit 37bdfc3

Browse files
author
Omri Schwarz
committed
Bugfixes to complete implemntation of $ref, patternProperties, and readOnly
1 parent 065bab5 commit 37bdfc3

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

src/js/brutusin-json-forms.js

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ if (typeof brutusin === "undefined") {
6060
var BrutusinForms = new Object();
6161
BrutusinForms.messages = {
6262
"required": "This field is **required**",
63-
"validationError": "Validation error",
63+
"validationError": "Validation error",
6464
"invalidValue": "Invalid field value",
6565
"addpropNameExistent": "This property is already present in the object",
6666
"addpropNameRequired": "A name is required",
@@ -134,11 +134,12 @@ if (typeof brutusin === "undefined") {
134134
var error;
135135
var initialValue;
136136
var inputCounter = 0;
137-
var root = schema;
137+
var root = schema;
138138
var formId = "BrutusinForms#" + BrutusinForms.instances.length;
139-
139+
140140
populateSchemaMap("$", schema);
141-
141+
console.log(schemaMap);
142+
142143
validateDepencyMapIsAcyclic();
143144

144145
var renderers = new Object();
@@ -164,13 +165,15 @@ if (typeof brutusin === "undefined") {
164165
if (s.type === "any") {
165166
input = document.createElement("textarea");
166167
if (value) {
167-
input.value = JSON.stringify(value, null, 4);
168+
input.value = JSON.stringify(value, null, 4);
169+
if (s.readOnly)
170+
input.disabled = true;
168171
}
169172
} else if (s.media) {
170173
input = document.createElement("input");
171174
input.type = "file";
172175
appendChild(input, option, s);
173-
// TODO, encode the SOB properly.
176+
// XXX TODO, encode the SOB properly.
174177
} else if (s.enum) {
175178
input = document.createElement("select");
176179
if (!s.required) {
@@ -187,11 +190,13 @@ if (typeof brutusin === "undefined") {
187190
option.value = s.enum[i];
188191
appendChild(option, textNode, s);
189192
appendChild(input, option, s);
190-
if (value && s.enum[i] === value) {
193+
if (value && s.enum[i] === value) {
191194
selectedIndex = i;
192195
if (!s.required) {
193196
selectedIndex++;
194197
}
198+
if (s.readOnly)
199+
input.disabled = true;
195200
}
196201
}
197202
if (s.enum.length == 1)
@@ -216,7 +221,11 @@ if (typeof brutusin === "undefined") {
216221
input.type = "text";
217222
}
218223
if (value !== null && typeof value !== "undefined") {
224+
// readOnly?
219225
input.value = value;
226+
if (s.readOnly)
227+
input.disabled = true;
228+
220229
}
221230
}
222231
input.schema = schemaId;
@@ -355,11 +364,8 @@ if (typeof brutusin === "undefined") {
355364
appendChild(input, option, s);
356365
if (value == undefined)
357366
continue;
358-
/*
359-
This is a hack used to pre-select elements that have
360-
a "type" string attribute. It is not standard,
361-
but it is tested and works, so feel free to uncomment.
362-
367+
if (s.readOnly)
368+
input.disabled = true;
363369
if (value.hasOwnProperty("type")) {
364370
if (ss.hasOwnProperty("properties") ){
365371
if (ss.properties.hasOwnProperty("type")){
@@ -369,7 +375,7 @@ but it is tested and works, so feel free to uncomment.
369375
render(null, display, id + "." + (input.selectedIndex-1), parentObject, propertyProvider, value) ; }
370376
}
371377
}
372-
}*/
378+
}
373379
}
374380
input.onchange=function(){
375381
render(null, display, id + "." + (input.selectedIndex-1), parentObject, propertyProvider, value) ;
@@ -391,9 +397,6 @@ but it is tested and works, so feel free to uncomment.
391397
}
392398

393399
function addAdditionalProperty(current, table, id, name, value,button) {
394-
// XXX needs a regex argument.
395-
// if defined, that needs to be the initial tet in the key field.
396-
// if (re !=== undefined ) set attribute placeholder = re.
397400
if (button != undefined) {
398401
var schemaId = getSchemaId(button.id);
399402
} else {
@@ -573,7 +576,7 @@ but it is tested and works, so feel free to uncomment.
573576
if (used_props.indexOf(p)!=-1){
574577
continue;
575578
}
576-
addAdditionalProperty(current, table, id + "[\"" + prop + "\"]", p, value[p]);
579+
addAdditionalProperty(current, table, id + "[" + pattern + "]", p, value[p]);
577580
used_props.push(p);
578581
}
579582
}
@@ -621,7 +624,7 @@ but it is tested and works, so feel free to uncomment.
621624
};
622625
// end of object renderer
623626
renderers["array"] = function (container, id, parentObject, propertyProvider, value) {
624-
function addItem(current, table, id, value) {
627+
function addItem(current, table, id, value,readOnly) {
625628
var schemaId = getSchemaId(id);
626629
var s = getSchema(schemaId);
627630
var tbody = document.createElement("tbody");
@@ -636,6 +639,8 @@ but it is tested and works, so feel free to uncomment.
636639
var removeButton = document.createElement("button");
637640
removeButton.setAttribute('type', 'button');
638641
removeButton.className = "remove";
642+
if (readOnly === true)
643+
removeButton.disabled=true;
639644
appendChild(removeButton, document.createTextNode("x"), s);
640645
var computRowCount = function () {
641646
for (var i = 0; i < table.rows.length; i++) {
@@ -685,6 +690,8 @@ but it is tested and works, so feel free to uncomment.
685690
appendChild(div, table, s);
686691
appendChild(container, div, s);
687692
var addButton = document.createElement("button");
693+
if (s.readOnly)
694+
addButton.disabled=true;
688695
addButton.setAttribute('type', 'button');
689696
addButton.getValidationError = function () {
690697
if (s.minItems && s.minItems > table.rows.length) {
@@ -705,7 +712,7 @@ but it is tested and works, so feel free to uncomment.
705712
appendChild(div, addButton, s);
706713
if (value && value instanceof Array) {
707714
for (var i = 0; i < value.length; i++) {
708-
addItem(current, table, id + "[" + i + "]", value[i]);
715+
addItem(current, table, id + "[" + i + "]", value[i],s.readOnly );
709716
}
710717
}
711718
appendChild(container, div, s);
@@ -875,6 +882,7 @@ but it is tested and works, so feel free to uncomment.
875882
}
876883
} else if (schema.hasOwnProperty("$ref")){
877884
var newSchema = getDefinition(schema["$ref"]);
885+
//console.log("$REF",name,newSchema);
878886
populateSchemaMap(name,newSchema);
879887

880888
} else if (schema.type === "object") {
@@ -893,7 +901,7 @@ but it is tested and works, so feel free to uncomment.
893901
pseudoSchema.patternProperties[pat] = patChildProp;
894902
var s =schema.patternProperties[pat];
895903

896-
if (s.hasOwnProperty("type")||
904+
if (s.hasOwnProperty("type")||s.hasOwnProperty("$ref")||
897905
s.hasOwnProperty("oneOf")) {
898906
populateSchemaMap(patChildProp, schema.patternProperties[pat]);
899907
} else {
@@ -1013,7 +1021,7 @@ but it is tested and works, so feel free to uncomment.
10131021
renderInfoMap[schemaId].value = value;
10141022
clear(titleContainer);
10151023
clear(container);
1016-
//console.log(s.type,id,s);
1024+
//console.log(id,s,value);
10171025
var r = renderers[s.type];
10181026
if (r && !s.dependsOn) {
10191027
if (s.title) {

0 commit comments

Comments
 (0)