diff --git a/dist/js/brutusin-json-forms.js b/dist/js/brutusin-json-forms.js index 507a588..23a4f11 100644 --- a/dist/js/brutusin-json-forms.js +++ b/dist/js/brutusin-json-forms.js @@ -318,9 +318,39 @@ if (typeof brutusin === "undefined") { renderers["boolean"] = function (container, id, parentObject, propertyProvider, value) { var schemaId = getSchemaId(id); var s = getSchema(schemaId); - var input = document.createElement("input"); - input.type = "checkbox"; - input.schema = schemaId; + var input; + if (s.required) { + input = document.createElement("input"); + input.type = "checkbox"; + if (value === true) { + input.checked = true; + } + } else { + input = document.createElement("select"); + var emptyOption = document.createElement("option"); + var textEmpty = document.createTextNode(""); + textEmpty.value = ""; + appendChild(emptyOption, textEmpty, s); + appendChild(input, emptyOption, s); + + var optionTrue = document.createElement("option"); + var textTrue = document.createTextNode("true"); + textTrue.value = "true"; + appendChild(optionTrue, textTrue, s); + appendChild(input, optionTrue, s); + + var optionFalse = document.createElement("option"); + var textFalse = document.createTextNode("false"); + textFalse.value = "false"; + appendChild(optionFalse, textFalse, s); + appendChild(input, optionFalse, s); + + if (value === true) { + input.selectedIndex = 1; + } else if (value === false) { + input.selectedIndex = 2; + } + } input.onchange = function () { if (parentObject) { parentObject[propertyProvider.getValue()] = getValue(s, input); @@ -329,9 +359,7 @@ if (typeof brutusin === "undefined") { } onDependencyChanged(schemaId, input); }; - if (value === true) { - input.checked = true; - } + input.schema = schemaId; input.id = getInputId(); inputCounter++; if (s.description) { @@ -772,30 +800,36 @@ if (typeof brutusin === "undefined") { }; obj.getData = function () { - function removeEmptiesAndNulls(object) { + function removeEmptiesAndNulls(object, schema) { if (object instanceof Array) { if (object.length === 0) { return null; } var clone = new Array(); for (var i = 0; i < object.length; i++) { - clone[i] = removeEmptiesAndNulls(object[i]); + clone[i] = removeEmptiesAndNulls(object[i], schema.items); } return clone; } else if (object === "") { return null; } else if (object instanceof Object) { var clone = new Object(); + var nonEmpty = false; for (var prop in object) { if (prop.startsWith("$") && prop.endsWith("$")) { continue; } - var value = removeEmptiesAndNulls(object[prop]); + var value = removeEmptiesAndNulls(object[prop], schema.properties[prop]); if (value !== null) { clone[prop] = value; + nonEmpty = true; } } - return clone; + if (nonEmpty || schema.required) { + return clone; + } else { + return null; + } } else { return object; } @@ -803,8 +837,7 @@ if (typeof brutusin === "undefined") { if (!container) { return null; } else { - return removeEmptiesAndNulls(data); - ; + return removeEmptiesAndNulls(data, schema); } }; @@ -1093,9 +1126,19 @@ if (typeof brutusin === "undefined") { value = null; } } else if (schema.type === "boolean") { - value = input.checked; - if (!value) { - value = false; + if (input.tagName.toLowerCase() === "input") { + value = input.checked; + if (!value) { + value = false; + } + } else if (input.tagName.toLowerCase() === "select") { + if (input.value === "true") { + value = true; + } else if (input.value === "false") { + value = false; + } else { + value = null; + } } } else if (schema.type === "any") { if (value) { diff --git a/dist/js/brutusin-json-forms.min.js b/dist/js/brutusin-json-forms.min.js index 0bb985f..07541ec 100644 --- a/dist/js/brutusin-json-forms.min.js +++ b/dist/js/brutusin-json-forms.min.js @@ -1 +1 @@ -if("undefined"==typeof brutusin)window.brutusin=new Object;else if("object"!=typeof brutusin)throw"brutusin global variable already exists";!function(){String.prototype.startsWith||(String.prototype.startsWith=function(e,t){return t=t||0,this.indexOf(e,t)===t}),String.prototype.endsWith||(String.prototype.endsWith=function(e,t){var r=this.toString();(void 0===t||t>r.length)&&(t=r.length),t-=e.length;var n=r.indexOf(e,t);return-1!==n&&n===t}),String.prototype.includes||(String.prototype.includes=function(){"use strict";return-1!==String.prototype.indexOf.apply(this,arguments)}),String.prototype.format||(String.prototype.format=function(){for(var e=this,t=0;t0)throw"Error parsing expression '"+e+"': Multiple tokens found inside a bracket";o+=u,i++}else r[r.length]=u;if(s===n.length-1&&a)throw"Error parsing expression '"+e+"': Unbalanced [ found"}this.exp=e,this.queue=r,this.visit=function(e,t){function r(e,n,a,i,o){if(null!=a){var s=n.shift();if("$"===s){e="$";var s=n.shift()}if(s)if(Array.isArray(a)){if(!s.startsWith("["))throw"Node '"+e+"' is of type array";var u=s.substring(1,s.length-1);if(u.equals("#"))for(var l=0;lp)throw"Element '"+u+"' of node '"+e+"' is lower than zero";var d=a[p];r(e+s,n.slice(0),d,a,p)}}else{if("object"!=typeof a)throw"boolean"==typeof a||"number"==typeof a||"string"==typeof a?"Node is leaf but still are tokens remaining: "+s:"Node type '"+typeof a+"' not supported for index field '"+e+"'";if("[*]"===s)for(var m in a){var d=a[m];r(e+s,n.slice(0),d,a,m),r(e+'["'+m+'"]',n.slice(0),d,a,m)}else{var d;if(s.startsWith("[")){var u=s.substring(1,s.length-1);if(!u.startsWith('"')&&!u.startsWith("'"))throw"Element '"+u+"' of node '"+e+"' must be a string expression or wilcard '*'";u=u.substring(1,u.length()-1),e+=s,d=a[u]}else e=e.length>0?e+"."+s:s,d=a[s];r(e,n,d,a,s)}}else t(a,i,o)}}r(this.exp,this.queue,e)}}var SCHEMA_ANY={type:"any"},obj=new Object,schemaMap=new Object,dependencyMap=new Object,renderInfoMap=new Object,container,data,error,initialValue,inputCounter=0,root=schema,formId="BrutusinForms#"+BrutusinForms.instances.length;populateSchemaMap("$",schema),validateDepencyMapIsAcyclic();var renderers=new Object;return renderers.integer=function(e,t,r,n,a){renderers.string(e,t,r,n,a)},renderers.number=function(e,t,r,n,a){renderers.string(e,t,r,n,a)},renderers.any=function(e,t,r,n,a){renderers.string(e,t,r,n,a)},renderers.string=function(e,t,r,n,a){var i,o=getSchemaId(t),s=getSchema(o);if("any"===s.type)i=document.createElement("textarea"),a&&(i.value=JSON.stringify(a,null,4),s.readOnly&&(i.disabled=!0));else if(s.media)i=document.createElement("input"),i.type="file",appendChild(i,u,s);else if(s["enum"]){if(i=document.createElement("select"),!s.required){var u=document.createElement("option"),l=document.createTextNode("");u.value="",appendChild(u,l,s),appendChild(i,u,s)}for(var d=0,p=0;pe.length))return BrutusinForms.messages.minLength.format(s.minLength);if(s.maxLength&&e&&s.maxLength=s.maximum)return BrutusinForms.messages.exclusiveMaximum.format(s.maximum);if(!s.exclusiveMaximum&&e>s.maximum)return BrutusinForms.messages.maximum.format(s.maximum)}if(s.hasOwnProperty("minimum")){if(s.exclusiveMinimum&&e<=s.minimum)return BrutusinForms.messages.exclusiveMinimum.format(s.minimum);if(!s.exclusiveMinimum&&eu.maxProperties?BrutusinForms.messages.maxProperties.format(u.maxProperties):void 0}),C.description&&(E.title=C.description),appendChild(E,document.createTextNode("Add "+w),u),appendChild(P,E,u),a)for(var I in a)if(!u.properties.hasOwnProperty(I)){var S=RegExp(w);-1!==I.search(S)&&-1===x.indexOf(I)&&(o(l,d,t+"["+w+"]",I,a[I]),x.push(I))}appendChild(O,P,u)}if(u.additionalProperties){var V=getSchema(u.additionalProperties),E=document.createElement("button");if(E.setAttribute("type","button"),E.onclick=function(){o(l,d,t+"[*]",void 0)},(u.maxProperties||u.minProperties)&&(E.getValidationError=function(){return u.minProperties&&m+d.rows.lengthu.maxProperties?BrutusinForms.messages.maxProperties.format(u.maxProperties):void 0}),V.description&&(E.title=V.description),appendChild(E,document.createTextNode("Add"),u),appendChild(O,E,u),a)for(var I in a)u.properties&&u.properties.hasOwnProperty(I)||-1===x.indexOf(I)&&o(l,d,t+'["'+c+'"]',I,a[I],void 0)}appendChild(e,O,u)}else appendChild(e,d,u)},renderers.array=function(e,t,r,n,a){function i(e,t,r,n,a){var i=getSchemaId(r),o=getSchema(i),s=document.createElement("tbody"),u=document.createElement("tr");u.className="item";var l=document.createElement("td");l.className="item-index";var d=document.createElement("td");d.className="item-action";var p=document.createElement("td");p.className="item-value";var m=document.createElement("button");m.setAttribute("type","button"),m.className="remove",a===!0&&(m.disabled=!0),appendChild(m,document.createTextNode("x"),o);var c=function(){for(var e=0;ep.rows.length)return BrutusinForms.messages.minItems.format(s.minItems);if(s.maxItems&&s.maxItemsr.length)&&(t=r.length),t-=e.length;var n=r.indexOf(e,t);return-1!==n&&n===t}),String.prototype.includes||(String.prototype.includes=function(){"use strict";return-1!==String.prototype.indexOf.apply(this,arguments)}),String.prototype.format||(String.prototype.format=function(){for(var e=this,t=0;t0)throw"Error parsing expression '"+e+"': Multiple tokens found inside a bracket";o+=u,i++}else r[r.length]=u;if(s===n.length-1&&a)throw"Error parsing expression '"+e+"': Unbalanced [ found"}this.exp=e,this.queue=r,this.visit=function(e,t){function r(e,n,a,i,o){if(null!=a){var s=n.shift();if("$"===s){e="$";var s=n.shift()}if(s)if(Array.isArray(a)){if(!s.startsWith("["))throw"Node '"+e+"' is of type array";var u=s.substring(1,s.length-1);if(u.equals("#"))for(var l=0;lp)throw"Element '"+u+"' of node '"+e+"' is lower than zero";var d=a[p];r(e+s,n.slice(0),d,a,p)}}else{if("object"!=typeof a)throw"boolean"==typeof a||"number"==typeof a||"string"==typeof a?"Node is leaf but still are tokens remaining: "+s:"Node type '"+typeof a+"' not supported for index field '"+e+"'";if("[*]"===s)for(var m in a){var d=a[m];r(e+s,n.slice(0),d,a,m),r(e+'["'+m+'"]',n.slice(0),d,a,m)}else{var d;if(s.startsWith("[")){var u=s.substring(1,s.length-1);if(!u.startsWith('"')&&!u.startsWith("'"))throw"Element '"+u+"' of node '"+e+"' must be a string expression or wilcard '*'";u=u.substring(1,u.length()-1),e+=s,d=a[u]}else e=e.length>0?e+"."+s:s,d=a[s];r(e,n,d,a,s)}}else t(a,i,o)}}r(this.exp,this.queue,e)}}var SCHEMA_ANY={type:"any"},obj=new Object,schemaMap=new Object,dependencyMap=new Object,renderInfoMap=new Object,container,data,error,initialValue,inputCounter=0,root=schema,formId="BrutusinForms#"+BrutusinForms.instances.length;populateSchemaMap("$",schema),validateDepencyMapIsAcyclic();var renderers=new Object;return renderers.integer=function(e,t,r,n,a){renderers.string(e,t,r,n,a)},renderers.number=function(e,t,r,n,a){renderers.string(e,t,r,n,a)},renderers.any=function(e,t,r,n,a){renderers.string(e,t,r,n,a)},renderers.string=function(e,t,r,n,a){var i,o=getSchemaId(t),s=getSchema(o);if("any"===s.type)i=document.createElement("textarea"),a&&(i.value=JSON.stringify(a,null,4),s.readOnly&&(i.disabled=!0));else if(s.media)i=document.createElement("input"),i.type="file",appendChild(i,u,s);else if(s["enum"]){if(i=document.createElement("select"),!s.required){var u=document.createElement("option"),l=document.createTextNode("");u.value="",appendChild(u,l,s),appendChild(i,u,s)}for(var d=0,p=0;pe.length))return BrutusinForms.messages.minLength.format(s.minLength);if(s.maxLength&&e&&s.maxLength=s.maximum)return BrutusinForms.messages.exclusiveMaximum.format(s.maximum);if(!s.exclusiveMaximum&&e>s.maximum)return BrutusinForms.messages.maximum.format(s.maximum)}if(s.hasOwnProperty("minimum")){if(s.exclusiveMinimum&&e<=s.minimum)return BrutusinForms.messages.exclusiveMinimum.format(s.minimum);if(!s.exclusiveMinimum&&eu.maxProperties?BrutusinForms.messages.maxProperties.format(u.maxProperties):void 0}),C.description&&(P.title=C.description),appendChild(P,document.createTextNode("Add "+O),u),appendChild(E,P,u),a)for(var I in a)if(!u.properties.hasOwnProperty(I)){var N=RegExp(O);-1!==I.search(N)&&-1===x.indexOf(I)&&(o(l,d,t+"["+O+"]",I,a[I]),x.push(I))}appendChild(w,E,u)}if(u.additionalProperties){var S=getSchema(u.additionalProperties),P=document.createElement("button");if(P.setAttribute("type","button"),P.onclick=function(){o(l,d,t+"[*]",void 0)},(u.maxProperties||u.minProperties)&&(P.getValidationError=function(){return u.minProperties&&m+d.rows.lengthu.maxProperties?BrutusinForms.messages.maxProperties.format(u.maxProperties):void 0}),S.description&&(P.title=S.description),appendChild(P,document.createTextNode("Add"),u),appendChild(w,P,u),a)for(var I in a)u.properties&&u.properties.hasOwnProperty(I)||-1===x.indexOf(I)&&o(l,d,t+'["'+c+'"]',I,a[I],void 0)}appendChild(e,w,u)}else appendChild(e,d,u)},renderers.array=function(e,t,r,n,a){function i(e,t,r,n,a){var i=getSchemaId(r),o=getSchema(i),s=document.createElement("tbody"),u=document.createElement("tr");u.className="item";var l=document.createElement("td");l.className="item-index";var d=document.createElement("td");d.className="item-action";var p=document.createElement("td");p.className="item-value";var m=document.createElement("button");m.setAttribute("type","button"),m.className="remove",a===!0&&(m.disabled=!0),appendChild(m,document.createTextNode("x"),o);var c=function(){for(var e=0;ep.rows.length)return BrutusinForms.messages.minItems.format(s.minItems);if(s.maxItems&&s.maxItems