Skip to content

Commit 797f906

Browse files
committed
fix pattern properties
1 parent 0e532b1 commit 797f906

File tree

6 files changed

+106
-56
lines changed

6 files changed

+106
-56
lines changed

dist/css/brutusin-json-forms.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*
1616
* @author Ignacio del Valle Alles idelvall@brutusin.org
1717
*/
18+
.add-pattern-div{
19+
margin-top: 6px;
20+
}
1821
.loading-layer{
1922
position: absolute;
2023
top:0px;

dist/css/brutusin-json-forms.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/brutusin-json-forms.js

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -443,13 +443,8 @@ if (typeof brutusin === "undefined") {
443443
return ret;
444444
}
445445

446-
function addAdditionalProperty(current, table, id, name, value, button) {
447-
var schemaId;
448-
if (button !== undefined) {
449-
schemaId = getSchemaId(button.id);
450-
} else {
451-
schemaId = getSchemaId(id);
452-
}
446+
function addAdditionalProperty(current, table, id, name, value, pattern) {
447+
var schemaId = getSchemaId(id);
453448
var s = getSchema(schemaId);
454449
var tbody = table.tBodies[0];
455450
var tr = document.createElement("tr");
@@ -464,6 +459,10 @@ if (typeof brutusin === "undefined") {
464459
td2.className = "prop-value";
465460
var nameInput = document.createElement("input");
466461
nameInput.type = "text";
462+
var regExp;
463+
if (pattern) {
464+
regExp = RegExp(pattern);
465+
}
467466
nameInput.getValidationError = function () {
468467
if (nameInput.previousValue !== nameInput.value) {
469468
if (current.hasOwnProperty(nameInput.value)) {
@@ -477,19 +476,24 @@ if (typeof brutusin === "undefined") {
477476
var pp = createPropertyProvider(
478477
function () {
479478
if (nameInput.value) {
480-
return nameInput.value;
481-
} else {
482-
return keyForBlank;
479+
if (regExp) {
480+
if (nameInput.value.search(regExp) !== -1) {
481+
return nameInput.value;
482+
}
483+
} else {
484+
return nameInput.value;
485+
}
483486
}
487+
return keyForBlank;
484488
},
485489
function (oldPropertyName) {
486490
if (pp.getValue() === oldPropertyName) {
487491
return;
488492
}
489-
if (!oldPropertyName) {
493+
if (!oldPropertyName || !current.hasOwnProperty(oldPropertyName)) {
490494
oldPropertyName = keyForBlank;
491495
}
492-
if (oldPropertyName && current.hasOwnProperty(oldPropertyName)) {
496+
if (current.hasOwnProperty(oldPropertyName) || regExp && pp.getValue().search(regExp) === -1) {
493497
current[pp.getValue()] = current[oldPropertyName];
494498
delete current[oldPropertyName];
495499
}
@@ -525,21 +529,17 @@ if (typeof brutusin === "undefined") {
525529
appendChild(innerTr, innerTd2, s);
526530
appendChild(innerTab, innerTr, s);
527531
appendChild(td1, innerTab, s);
528-
if (button !== undefined) {
529-
var secondTr = document.createElement("tr");
530-
var secondTd = document.createElement("td");
531-
appendChild(secondTd, document.createTextNode(button.pnp_pattern), s);
532-
appendChild(secondTr, secondTd, s);
533-
appendChild(innerTab, secondTr, s);
532+
533+
if (pattern !== undefined) {
534+
nameInput.placeholder = pattern;
534535
}
536+
535537
appendChild(tr, td1, s);
536538
appendChild(tr, td2, s);
537539
appendChild(tbody, tr, s);
538540
appendChild(table, tbody, s);
539-
if (button !== undefined)
540-
render(null, td2, button.id, current, pp, value);
541-
else
542-
render(null, td2, id, current, pp, value);
541+
render(null, td2, id, current, pp, value);
542+
543543
if (name) {
544544
nameInput.value = name;
545545
nameInput.onblur();
@@ -591,12 +591,13 @@ if (typeof brutusin === "undefined") {
591591
for (var pattern in s.patternProperties) {
592592
var patProps = s.patternProperties[pattern];
593593
var patdiv = document.createElement("div");
594+
patdiv.className = "add-pattern-div";
594595
var addButton = document.createElement("button");
595596
addButton.setAttribute('type', 'button');
596-
addButton.pnp_pattern = pattern.substr(0);
597+
addButton.pattern = pattern;
597598
addButton.id = id + "[" + pattern + "]";
598599
addButton.onclick = function () {
599-
addAdditionalProperty(current, table, id + "[" + pattern + "]", undefined, undefined, this);
600+
addAdditionalProperty(current, table, this.id, undefined, undefined, this.pattern);
600601
};
601602
if (s.maxProperties || s.minProperties) {
602603
addButton.getValidationError = function () {
@@ -625,7 +626,7 @@ if (typeof brutusin === "undefined") {
625626
if (usedProps.indexOf(p) !== -1) {
626627
continue;
627628
}
628-
addAdditionalProperty(current, table, id + "[" + pattern + "]", p, value[p]);
629+
addAdditionalProperty(current, table, id + "[" + pattern + "]", p, value[p], pattern);
629630
usedProps.push(p);
630631
}
631632
}
@@ -662,7 +663,7 @@ if (typeof brutusin === "undefined") {
662663
if (usedProps.indexOf(p) !== -1) {
663664
continue;
664665
}
665-
addAdditionalProperty(current, table, id + "[\"" + prop + "\"]", p, value[p], undefined);
666+
addAdditionalProperty(current, table, id + "[\"" + prop + "\"]", p, value[p]);
666667
}
667668
}
668669
}
@@ -822,6 +823,9 @@ if (typeof brutusin === "undefined") {
822823

823824
obj.getData = function () {
824825
function removeEmptiesAndNulls(object, s) {
826+
if (ss === null){
827+
ss = SCHEMA_ANY;
828+
}
825829
if (s.$ref) {
826830
s = getDefinition(s.$ref);
827831
}
@@ -843,7 +847,25 @@ if (typeof brutusin === "undefined") {
843847
if (prop.startsWith("$") && prop.endsWith("$")) {
844848
continue;
845849
}
846-
var value = removeEmptiesAndNulls(object[prop], s.properties[prop]);
850+
var ss = null;
851+
if (s.hasOwnProperty("properties") && s.properties.hasOwnProperty(prop)) {
852+
ss = s.properties[prop];
853+
}
854+
if (ss === null && s.hasOwnProperty("additionalProperties")) {
855+
if (typeof s.additionalProperties === 'object') {
856+
ss = s.additionalProperties;
857+
}
858+
}
859+
if (ss === null && s.hasOwnProperty("patternProperties")) {
860+
for (var p in s.patternProperties) {
861+
var r = RegExp(p);
862+
if (prop.search(r) !== -1) {
863+
ss = s.patternProperties[p];
864+
break;
865+
}
866+
}
867+
}
868+
var value = removeEmptiesAndNulls(object[prop], ss);
847869
if (value !== null) {
848870
clone[prop] = value;
849871
nonEmpty = true;

dist/js/brutusin-json-forms.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/css/brutusin-json-forms.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*
1616
* @author Ignacio del Valle Alles idelvall@brutusin.org
1717
*/
18+
.add-pattern-div{
19+
margin-top: 6px;
20+
}
1821
.loading-layer{
1922
position: absolute;
2023
top:0px;

src/js/brutusin-json-forms.js

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -443,13 +443,8 @@ if (typeof brutusin === "undefined") {
443443
return ret;
444444
}
445445

446-
function addAdditionalProperty(current, table, id, name, value, button) {
447-
var schemaId;
448-
if (button !== undefined) {
449-
schemaId = getSchemaId(button.id);
450-
} else {
451-
schemaId = getSchemaId(id);
452-
}
446+
function addAdditionalProperty(current, table, id, name, value, pattern) {
447+
var schemaId = getSchemaId(id);
453448
var s = getSchema(schemaId);
454449
var tbody = table.tBodies[0];
455450
var tr = document.createElement("tr");
@@ -464,6 +459,10 @@ if (typeof brutusin === "undefined") {
464459
td2.className = "prop-value";
465460
var nameInput = document.createElement("input");
466461
nameInput.type = "text";
462+
var regExp;
463+
if (pattern) {
464+
regExp = RegExp(pattern);
465+
}
467466
nameInput.getValidationError = function () {
468467
if (nameInput.previousValue !== nameInput.value) {
469468
if (current.hasOwnProperty(nameInput.value)) {
@@ -477,19 +476,24 @@ if (typeof brutusin === "undefined") {
477476
var pp = createPropertyProvider(
478477
function () {
479478
if (nameInput.value) {
480-
return nameInput.value;
481-
} else {
482-
return keyForBlank;
479+
if (regExp) {
480+
if (nameInput.value.search(regExp) !== -1) {
481+
return nameInput.value;
482+
}
483+
} else {
484+
return nameInput.value;
485+
}
483486
}
487+
return keyForBlank;
484488
},
485489
function (oldPropertyName) {
486490
if (pp.getValue() === oldPropertyName) {
487491
return;
488492
}
489-
if (!oldPropertyName) {
493+
if (!oldPropertyName || !current.hasOwnProperty(oldPropertyName)) {
490494
oldPropertyName = keyForBlank;
491495
}
492-
if (oldPropertyName && current.hasOwnProperty(oldPropertyName)) {
496+
if (current.hasOwnProperty(oldPropertyName) || regExp && pp.getValue().search(regExp) === -1) {
493497
current[pp.getValue()] = current[oldPropertyName];
494498
delete current[oldPropertyName];
495499
}
@@ -525,21 +529,17 @@ if (typeof brutusin === "undefined") {
525529
appendChild(innerTr, innerTd2, s);
526530
appendChild(innerTab, innerTr, s);
527531
appendChild(td1, innerTab, s);
528-
if (button !== undefined) {
529-
var secondTr = document.createElement("tr");
530-
var secondTd = document.createElement("td");
531-
appendChild(secondTd, document.createTextNode(button.pnp_pattern), s);
532-
appendChild(secondTr, secondTd, s);
533-
appendChild(innerTab, secondTr, s);
532+
533+
if (pattern !== undefined) {
534+
nameInput.placeholder = pattern;
534535
}
536+
535537
appendChild(tr, td1, s);
536538
appendChild(tr, td2, s);
537539
appendChild(tbody, tr, s);
538540
appendChild(table, tbody, s);
539-
if (button !== undefined)
540-
render(null, td2, button.id, current, pp, value);
541-
else
542-
render(null, td2, id, current, pp, value);
541+
render(null, td2, id, current, pp, value);
542+
543543
if (name) {
544544
nameInput.value = name;
545545
nameInput.onblur();
@@ -591,12 +591,13 @@ if (typeof brutusin === "undefined") {
591591
for (var pattern in s.patternProperties) {
592592
var patProps = s.patternProperties[pattern];
593593
var patdiv = document.createElement("div");
594+
patdiv.className = "add-pattern-div";
594595
var addButton = document.createElement("button");
595596
addButton.setAttribute('type', 'button');
596-
addButton.pnp_pattern = pattern.substr(0);
597+
addButton.pattern = pattern;
597598
addButton.id = id + "[" + pattern + "]";
598599
addButton.onclick = function () {
599-
addAdditionalProperty(current, table, id + "[" + pattern + "]", undefined, undefined, this);
600+
addAdditionalProperty(current, table, this.id, undefined, undefined, this.pattern);
600601
};
601602
if (s.maxProperties || s.minProperties) {
602603
addButton.getValidationError = function () {
@@ -625,7 +626,7 @@ if (typeof brutusin === "undefined") {
625626
if (usedProps.indexOf(p) !== -1) {
626627
continue;
627628
}
628-
addAdditionalProperty(current, table, id + "[" + pattern + "]", p, value[p]);
629+
addAdditionalProperty(current, table, id + "[" + pattern + "]", p, value[p], pattern);
629630
usedProps.push(p);
630631
}
631632
}
@@ -662,7 +663,7 @@ if (typeof brutusin === "undefined") {
662663
if (usedProps.indexOf(p) !== -1) {
663664
continue;
664665
}
665-
addAdditionalProperty(current, table, id + "[\"" + prop + "\"]", p, value[p], undefined);
666+
addAdditionalProperty(current, table, id + "[\"" + prop + "\"]", p, value[p]);
666667
}
667668
}
668669
}
@@ -822,6 +823,9 @@ if (typeof brutusin === "undefined") {
822823

823824
obj.getData = function () {
824825
function removeEmptiesAndNulls(object, s) {
826+
if (ss === null){
827+
ss = SCHEMA_ANY;
828+
}
825829
if (s.$ref) {
826830
s = getDefinition(s.$ref);
827831
}
@@ -843,7 +847,25 @@ if (typeof brutusin === "undefined") {
843847
if (prop.startsWith("$") && prop.endsWith("$")) {
844848
continue;
845849
}
846-
var value = removeEmptiesAndNulls(object[prop], s.properties[prop]);
850+
var ss = null;
851+
if (s.hasOwnProperty("properties") && s.properties.hasOwnProperty(prop)) {
852+
ss = s.properties[prop];
853+
}
854+
if (ss === null && s.hasOwnProperty("additionalProperties")) {
855+
if (typeof s.additionalProperties === 'object') {
856+
ss = s.additionalProperties;
857+
}
858+
}
859+
if (ss === null && s.hasOwnProperty("patternProperties")) {
860+
for (var p in s.patternProperties) {
861+
var r = RegExp(p);
862+
if (prop.search(r) !== -1) {
863+
ss = s.patternProperties[p];
864+
break;
865+
}
866+
}
867+
}
868+
var value = removeEmptiesAndNulls(object[prop], ss);
847869
if (value !== null) {
848870
clone[prop] = value;
849871
nonEmpty = true;

0 commit comments

Comments
 (0)