Skip to content

Commit 065bab5

Browse files
author
Omri Schwarz
committed
Add handler for patternProperties. Now there's a button for each
pattern and a button for non-pattern-defeined peroperties.
1 parent 2b71de5 commit 065bab5

File tree

1 file changed

+126
-41
lines changed

1 file changed

+126
-41
lines changed

src/js/brutusin-json-forms.js

Lines changed: 126 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ if (typeof brutusin === "undefined") {
136136
var inputCounter = 0;
137137
var root = schema;
138138
var formId = "BrutusinForms#" + BrutusinForms.instances.length;
139-
//console.log(schema);
139+
140140
populateSchemaMap("$", schema);
141141

142142
validateDepencyMapIsAcyclic();
@@ -348,14 +348,19 @@ if (typeof brutusin === "undefined") {
348348
var option = document.createElement("option");
349349
var propId = schemaId + "."+i;
350350
var ss = getSchema(propId);
351-
//console.log(s,i,id,ss,schemaMap,propId);
351+
352352
var textNode = document.createTextNode(ss.title);
353353
option.value = s.oneOf[i];
354354
appendChild(option, textNode, s);
355355
appendChild(input, option, s);
356356
if (value == undefined)
357357
continue;
358-
/* if (value.hasOwnProperty("type")) {
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+
363+
if (value.hasOwnProperty("type")) {
359364
if (ss.hasOwnProperty("properties") ){
360365
if (ss.properties.hasOwnProperty("type")){
361366
var tryit = getSchema(ss.properties.type);
@@ -364,7 +369,7 @@ if (typeof brutusin === "undefined") {
364369
render(null, display, id + "." + (input.selectedIndex-1), parentObject, propertyProvider, value) ; }
365370
}
366371
}
367-
}*/
372+
}*/
368373
}
369374
input.onchange=function(){
370375
render(null, display, id + "." + (input.selectedIndex-1), parentObject, propertyProvider, value) ;
@@ -385,8 +390,15 @@ if (typeof brutusin === "undefined") {
385390
return ret;
386391
}
387392

388-
function addAdditionalProperty(current, table, id, name, value) {
389-
var schemaId = getSchemaId(id);
393+
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.
397+
if (button != undefined) {
398+
var schemaId = getSchemaId(button.id);
399+
} else {
400+
var schemaId = getSchemaId(id);
401+
}
390402
var s = getSchema(schemaId);
391403
var tbody = table.tBodies[0];
392404
var tr = document.createElement("tr");
@@ -456,18 +468,27 @@ if (typeof brutusin === "undefined") {
456468
nameInput.value = null;
457469
pp.onchange(nameInput.previousValue);
458470
};
459-
460471
appendChild(innerTd1, nameInput, s);
461472
appendChild(innerTd2, removeButton, s);
462473
appendChild(innerTr, innerTd1, s);
463474
appendChild(innerTr, innerTd2, s);
464475
appendChild(innerTab, innerTr, s);
465476
appendChild(td1, innerTab, s);
477+
if (button !== undefined) {
478+
var secondTr = document.createElement("tr");
479+
var secondTd = document.createElement("td");
480+
appendChild(secondTd, document.createTextNode(button.pnp_pattern), s);
481+
appendChild(secondTr,secondTd, s);
482+
appendChild(innerTab,secondTr, s);
483+
}
466484
appendChild(tr, td1, s);
467485
appendChild(tr, td2, s);
468486
appendChild(tbody, tr, s);
469487
appendChild(table, tbody, s);
470-
render(null, td2, id, current, pp, value);
488+
if (button !== undefined)
489+
render(null, td2, button.id, current, pp, value);
490+
else
491+
render(null, td2, id, current, pp, value);
471492
if (name) {
472493
nameInput.value = name;
473494
nameInput.onblur();
@@ -509,39 +530,90 @@ if (typeof brutusin === "undefined") {
509530
render(td1, td2, propId, current, pp, propInitialValue);
510531
}
511532
}
512-
// TODO: handle complex additionalProperties type definitions.
513-
if (s.additionalProperties) {
514-
var addPropS = getSchema(s.additionalProperties);
533+
var used_props = [];
534+
if (s.patternProperties || s.additionalProperties) {
515535
var div = document.createElement("div");
516536
appendChild(div, table, s);
517-
var addButton = document.createElement("button");
518-
addButton.setAttribute('type', 'button');
519-
addButton.onclick = function () {
520-
addAdditionalProperty(current, table, id + "[*]");
521-
};
522-
if (s.maxProperties || s.minProperties) {
523-
addButton.getValidationError = function () {
524-
if (s.minProperties && propNum + table.rows.length < s.minProperties) {
525-
return BrutusinForms.messages["minProperties"].format(s.minProperties);
526-
}
527-
if (s.maxProperties && propNum + table.rows.length > s.maxProperties) {
528-
return BrutusinForms.messages["maxProperties"].format(s.maxProperties);
529-
}
537+
if (s.patternProperties) {
538+
for (pattern in s.patternProperties) {
539+
var PatProps = s.patternProperties[pattern];
540+
var PatPropSchema = getSchema(PatProps);
541+
var patdiv = document.createElement("div");
542+
var addButton = document.createElement("button");
543+
addButton.setAttribute('type', 'button');
544+
addButton.pnp_pattern= pattern.substr(0);
545+
addButton.id = id+ "["+pattern+"]";
546+
addButton.onclick = function () {
547+
addAdditionalProperty(current, table, id + "["+pattern+"]", undefined,undefined,this);
548+
};
549+
if (s.maxProperties || s.minProperties) {
550+
addButton.getValidationError = function () {
551+
if (s.minProperties && propNum + table.rows.length < s.minProperties) {
552+
return BrutusinForms.messages["minProperties"].format(s.minProperties);
553+
}
554+
if (s.maxProperties && propNum + table.rows.length > s.maxProperties) {
555+
return BrutusinForms.messages["maxProperties"].format(s.maxProperties);
556+
}
557+
};
558+
}
559+
if (PatProps.description) {
560+
addButton.title = PatProps.description;
561+
}
562+
appendChild(addButton, document.createTextNode("Add "+pattern), s);
563+
appendChild(patdiv, addButton, s);
564+
if (value) {
565+
for (var p in value) {
566+
if (s.properties.hasOwnProperty(p)) {
567+
continue;
568+
}
569+
var r = RegExp(pattern);
570+
if (p.search(r)==-1 ){
571+
continue;
572+
}
573+
if (used_props.indexOf(p)!=-1){
574+
continue;
575+
}
576+
addAdditionalProperty(current, table, id + "[\"" + prop + "\"]", p, value[p]);
577+
used_props.push(p);
578+
}
579+
}
580+
appendChild(div, patdiv, s);
581+
}
582+
}
583+
if (s.additionalProperties) {
584+
var addPropS = getSchema(s.additionalProperties);
585+
var addButton = document.createElement("button");
586+
addButton.setAttribute('type', 'button');
587+
addButton.onclick = function () {
588+
addAdditionalProperty(current, table, id + "[*]", undefined);
530589
};
531-
}
532-
if (addPropS.description) {
533-
addButton.title = addPropS.description;
534-
}
535-
appendChild(addButton, document.createTextNode("Add"), s);
536-
appendChild(div, addButton, s);
537-
if (value) {
538-
for (var p in value) {
539-
if (s.properties.hasOwnProperty(p)) {
540-
continue;
541-
}
542-
addAdditionalProperty(current, table, id + "[\"" + prop + "\"]", p, value[p]);
590+
if (s.maxProperties || s.minProperties) {
591+
addButton.getValidationError = function () {
592+
if (s.minProperties && propNum + table.rows.length < s.minProperties) {
593+
return BrutusinForms.messages["minProperties"].format(s.minProperties);
594+
}
595+
if (s.maxProperties && propNum + table.rows.length > s.maxProperties) {
596+
return BrutusinForms.messages["maxProperties"].format(s.maxProperties);
597+
}
598+
};
543599
}
544-
}
600+
if (addPropS.description) {
601+
addButton.title = addPropS.description;
602+
}
603+
appendChild(addButton, document.createTextNode("Add"), s);
604+
appendChild(div, addButton, s);
605+
if (value) {
606+
for (var p in value) {
607+
if (s.properties.hasOwnProperty(p)) {
608+
continue;
609+
}
610+
if (used_props.indexOf(p)!=-1){
611+
continue;
612+
}
613+
addAdditionalProperty(current, table, id + "[\"" + prop + "\"]", p, value[p],undefined);
614+
}
615+
}
616+
}
545617
appendChild(container, div, s);
546618
} else {
547619
appendChild(container, table, s);
@@ -797,16 +869,14 @@ if (typeof brutusin === "undefined") {
797869
pseudoSchema.oneOf = new Array();
798870
pseudoSchema.type = "oneOf";
799871
for (var i in schema.oneOf) {
800-
console.log("ONEOF",name,schema.oneOf[i]);
801872
var childProp = name + "." + i;
802873
pseudoSchema.oneOf[i] = childProp;
803874
populateSchemaMap(childProp, schema.oneOf[i]);
804875
}
805-
} else if (schema.hasOwnProperty("$ref")){
806-
console.log("REFERENTIAL");
876+
} else if (schema.hasOwnProperty("$ref")){
807877
var newSchema = getDefinition(schema["$ref"]);
808-
console.log(name,newSchema,schemaMap);
809878
populateSchemaMap(name,newSchema);
879+
810880
} else if (schema.type === "object") {
811881
if (schema.properties) {
812882
pseudoSchema.properties = new Object();
@@ -816,6 +886,21 @@ if (typeof brutusin === "undefined") {
816886
populateSchemaMap(childProp, schema.properties[prop]);
817887
}
818888
}
889+
if (schema.patternProperties) {
890+
pseudoSchema.patternProperties = new Object();
891+
for (pat in schema.patternProperties){
892+
var patChildProp = name + "["+pat+"]";
893+
pseudoSchema.patternProperties[pat] = patChildProp;
894+
var s =schema.patternProperties[pat];
895+
896+
if (s.hasOwnProperty("type")||
897+
s.hasOwnProperty("oneOf")) {
898+
populateSchemaMap(patChildProp, schema.patternProperties[pat]);
899+
} else {
900+
populateSchemaMap(patChildProp, SCHEMA_ANY);
901+
}
902+
}
903+
}
819904
if (schema.additionalProperties) {
820905
var childProp = name + "[*]";
821906
pseudoSchema.additionalProperties = childProp;

0 commit comments

Comments
 (0)