Commit 38483df3 authored by Rafael Monnerat's avatar Rafael Monnerat

General fixes for json_editor

See merge request nexedi/erp5!1904
parents ef735f9b feba703c
...@@ -17,16 +17,22 @@ div.json-editor-container label { ...@@ -17,16 +17,22 @@ div.json-editor-container label {
div.json-editor-container label.visually-hidden { div.json-editor-container label.visually-hidden {
display: none; display: none;
} }
div.json-editor-container .alert-danger { div.json-editor-container .invalid-feedback {
padding: 1px 10px 2px 10px;
border-radius: 0.25rem;
border-color: #f5c2c7;
border: 1px solid #f5c2c7;
}
div.json-editor-container .alert-danger,
div.json-editor-container .invalid-feedback {
color: #842029; color: #842029;
background-color: #f8d7da; background-color: #f8d7da;
border-color: #f5c2c7;
} }
div.json-editor-container .alert { div.json-editor-container .alert {
position: relative; position: relative;
padding: 1rem 1rem; padding: 1rem 1rem;
margin-bottom: 1rem; margin-bottom: 1rem;
border: 1px solid transparent; border: 1px solid #f5c2c7;
border-radius: 0.25rem; border-radius: 0.25rem;
} }
div.json-editor-container .form-label { div.json-editor-container .form-label {
......
...@@ -43,6 +43,45 @@ ...@@ -43,6 +43,45 @@
} }
}; };
JSONEditor.defaults.editors.select.prototype.setValue = function (value, initial) {
/* Sanitize value before setting it */
var sanitized = this.typecast(value),
inEnum = (this.enum_options.length > 0 && this.enum_values.includes(sanitized)),
haveToUseDefaultValue = (!!this.jsoneditor.options.use_default_values || this.schema.default === undefined);
if (!this.hasPlaceholderOption && (!inEnum || (initial && !this.isRequired() && !haveToUseDefaultValue))) {
/* NXD: We forcefuly include the value even */
this.theme.setSelectOptions(this.input,
this.enum_options.concat([value]),
this.enum_display.concat([value]),
this.hasPlaceholderOption,
this.placeholderOptionText);
}
if (this.value === sanitized) {
return;
}
/* NXD: !this.hasPlaceholderOption seems to be a bug on upstream introduces by:
https://github.com/json-editor/json-editor/pull/1499/commits/2f9b1b3a30e64383b92dc4cd7494f55ba089ae66 */
if (inEnum && !this.hasPlaceholderOption) {
this.input.value = this.enum_options[this.enum_values.indexOf(sanitized)];
} else if (!inEnum && !this.hasPlaceholderOption) {
this.input.value = sanitized;
} else {
this.input.value = '_placeholder_';
}
this.value = sanitized;
if (!initial) {
this.is_dirty = true;
}
this.onChange();
this.change();
};
if (JSONEditor.defaults.editors.select.prototype.original_preBuild === undefined) { if (JSONEditor.defaults.editors.select.prototype.original_preBuild === undefined) {
JSONEditor.defaults.editors.select.prototype.original_preBuild = JSONEditor.defaults.editors.select.prototype.preBuild; JSONEditor.defaults.editors.select.prototype.original_preBuild = JSONEditor.defaults.editors.select.prototype.preBuild;
} }
...@@ -80,8 +119,19 @@ ...@@ -80,8 +119,19 @@
}; };
JSONEditor.defaults.editors.select.prototype.typecast = function (value) { JSONEditor.defaults.editors.select.prototype.typecast = function (value) {
if (this.schema.type === 'boolean') { /* The value should be modified but it must be preserve its integrity,
return value === 'undefined' || value === undefined ? undefined : !!value; it must not be modified even if the type is not the good one */
if (this.schema.type === 'boolean' && (value === 'undefined' || value === undefined)) {
return undefined;
}
if (this.schema.type === 'boolean' && (typeof value === "boolean")) {
return value;
}
if (this.schema.type === 'boolean' && (value === "true" || value === "1")) {
return true;
}
if (this.schema.type === 'boolean' && (value === "false" || value === "")) {
return false;
} }
if (this.schema.type === 'number' && value === "") { if (this.schema.type === 'number' && value === "") {
return undefined; return undefined;
...@@ -90,10 +140,12 @@ ...@@ -90,10 +140,12 @@
return undefined; return undefined;
} }
if (this.schema.type === 'number') { if (this.schema.type === 'number') {
return parseFloat(value) || 0; return parseFloat(value) || value;
} }
if (this.schema.type === 'integer') { if (this.schema.type === 'integer') {
return Math.floor(parseFloat(value) || 0); if (parseFloat(value)) {
return Math.floor(parseFloat(value));
}
} }
if (this.schema.enum && value === undefined) { if (this.schema.enum && value === undefined) {
return undefined; return undefined;
...@@ -101,7 +153,6 @@ ...@@ -101,7 +153,6 @@
if (value === undefined) { if (value === undefined) {
return undefined; return undefined;
} }
return value.toString(); return value.toString();
}; };
...@@ -248,8 +299,8 @@ ...@@ -248,8 +299,8 @@
// editor relies on async load function, so we must return the promise // editor relies on async load function, so we must return the promise
// to finish before continue, otherwise rendering errors wont throw Errors // to finish before continue, otherwise rendering errors wont throw Errors
// in the same stack as expected. // in the same stack as expected.
return editor.promise return editor.promise;
}) });
}) })
.declareMethod('getContent', function () { .declareMethod('getContent', function () {
var form_data = {}; var form_data = {};
......
...@@ -22,17 +22,23 @@ div.json-editor-container { ...@@ -22,17 +22,23 @@ div.json-editor-container {
display: none; display: none;
} }
& .alert-danger { & .invalid-feedback{
padding: 1px 10px 2px 10px;
border-radius: 0.25rem;
border-color: #f5c2c7;
border: 1px solid #f5c2c7;
}
& .alert-danger, .invalid-feedback{
color: #842029; color: #842029;
background-color: #f8d7da; background-color: #f8d7da;
border-color: #f5c2c7;
} }
& .alert { & .alert {
position: relative; position: relative;
padding: 1rem 1rem; padding: 1rem 1rem;
margin-bottom: 1rem; margin-bottom: 1rem;
border: 1px solid transparent; border: 1px solid #f5c2c7;
border-radius: 0.25rem; border-radius: 0.25rem;
} }
& .form-label { & .form-label {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment