Commit f2318991 authored by Rafael Monnerat's avatar Rafael Monnerat

More fixes for json editor

See merge request !1919
parents ef20fc9b e17a41e5
Pipeline #34085 failed with stage
in 0 seconds
...@@ -61,13 +61,13 @@ div.json-editor-container .je-switcher { ...@@ -61,13 +61,13 @@ div.json-editor-container .je-switcher {
div.json-editor-container .btn { div.json-editor-container .btn {
vertical-align: middle; vertical-align: middle;
border: 1px solid transparent; border: 1px solid transparent;
padding: 0.25rem 0.5rem; padding: 2pt 5pt 3pt 5pt;
flex: 1 1 auto; flex: 1 1 auto;
color: #fff; color: #fff;
background-color: #6c757d; background-color: #6c757d;
border-color: #6c757d; border-color: #6c757d;
font-size: 0.875rem; font-size: 0.875rem;
border-radius: 0.2rem; border-radius: 0.325rem;
} }
div.json-editor-container .btn:hover { div.json-editor-container .btn:hover {
color: #fff; color: #fff;
......
...@@ -12,6 +12,13 @@ ...@@ -12,6 +12,13 @@
return undefined; return undefined;
}; };
JSONEditor.AbstractEditor.prototype.preBuild = function () {
if (this.jsoneditor.options.readonly) {
this.schema.readOnly = this.jsoneditor.options.readonly;
}
};
function isEmpty(obj) { function isEmpty(obj) {
return obj === undefined || obj === '' || return obj === undefined || obj === '' ||
( (
...@@ -37,10 +44,77 @@ ...@@ -37,10 +44,77 @@
return result; return result;
}; };
JSONEditor.AbstractEditor.prototype.preBuild = function () { if (JSONEditor.defaults.editors.object.prototype.original_getPropertySchema === undefined) {
if (this.jsoneditor.options.readonly) { JSONEditor.defaults.editors.object.prototype.original_getPropertySchema = JSONEditor.defaults.editors.object.prototype.getPropertySchema;
this.schema.readOnly = this.jsoneditor.options.readonly; }
JSONEditor.defaults.editors.object.prototype.getPropertySchema = function (key) {
var schema = this.original_getPropertySchema(key);
/* Strip forbidden properties, that aren't part of json schema spec.
They are removed because the UI must be complaint with other usages of
json schemas.
*/
delete schema.template;
delete schema.options;
if (schema.const !== undefined) {
schema.enum = [schema.const];
}
/* Display default value as part of description */
if (schema.default !== undefined && typeof schema.default !== "object") {
if (schema.description !== undefined) {
schema.description = schema.description + " (default: " + schema.default + ")";
} else {
schema.description = " (default: " + schema.default + ")";
}
} }
return schema;
};
/* The original code would remove the field if value is undefined */
JSONEditor.defaults.editors.object.prototype.setValue = function (value, initial) {
var object_editor = this;
value = value || {};
if (typeof value !== 'object' || Array.isArray(value)) {
value = {};
}
/* First, set the values for all of the defined properties */
// @ts-ignore
Object.entries(this.cached_editors).forEach(function (entry) {
var i = entry[0],
editor = entry[1];
/* Value explicitly set */
if (value[i] !== undefined) {
object_editor.addObjectProperty(i);
editor.setValue(value[i], initial);
editor.activate();
/* Otherwise if it is read only remove the field */
} else if (editor.schema.readOnly) {
object_editor.removeObjectProperty(i);
/* Otherwise, set the value to the default */
} else {
editor.setValue(editor.getDefault(), initial);
}
});
// @ts-ignore
Object.entries(value).forEach(function (entry) {
var i = entry[0],
val = entry[1];
if (!object_editor.cached_editors[i]) {
object_editor.addObjectProperty(i);
if (object_editor.editors[i]) {
object_editor.editors[i].setValue(val, initial, !!object_editor.editors[i].template);
}
}
});
object_editor.refreshValue();
object_editor.layoutEditors();
object_editor.onChange();
}; };
JSONEditor.defaults.editors.select.prototype.setValue = function (value, initial) { JSONEditor.defaults.editors.select.prototype.setValue = function (value, initial) {
...@@ -237,73 +311,16 @@ ...@@ -237,73 +311,16 @@
field.onChange(typeChanged); field.onChange(typeChanged);
}; };
if (JSONEditor.defaults.editors.object.prototype.original_getPropertySchema === undefined) { JSONEditor.defaults.editors.array.prototype.ensureArraySize = function (value) {
JSONEditor.defaults.editors.object.prototype.original_getPropertySchema = JSONEditor.defaults.editors.object.prototype.getPropertySchema; // Don't extend or slice data based on the input
} // Let the user handle it.
return value;
JSONEditor.defaults.editors.object.prototype.getPropertySchema = function (key) {
var schema = this.original_getPropertySchema(key);
/* Strip forbidden properties, that aren't part of json schema spec.
They are removed because the UI must be complaint with other usages of
json schemas.
*/
delete schema.template;
delete schema.options;
/* Display default value as part of description */
if (schema.default !== undefined && typeof schema.default !== "object") {
if (schema.description !== undefined) {
schema.description = schema.description + " (default: " + schema.default + ")";
} else {
schema.description = " (default: " + schema.default + ")";
}
}
return schema;
}; };
/* The original code would remove the field if value is undefined */ JSONEditor.defaults.editors.table.prototype.ensureArraySize = function (value) {
JSONEditor.defaults.editors.object.prototype.setValue = function (value, initial) { // Don't extend or slice data based on the input
var object_editor = this; // Let the user handle it.
value = value || {}; return value;
if (typeof value !== 'object' || Array.isArray(value)) {
value = {};
}
/* First, set the values for all of the defined properties */
// @ts-ignore
Object.entries(this.cached_editors).forEach(function (entry) {
var i = entry[0],
editor = entry[1];
/* Value explicitly set */
if (value[i] !== undefined) {
object_editor.addObjectProperty(i);
editor.setValue(value[i], initial);
editor.activate();
/* Otherwise if it is read only remove the field */
} else if (editor.schema.readOnly) {
object_editor.removeObjectProperty(i);
/* Otherwise, set the value to the default */
} else {
editor.setValue(editor.getDefault(), initial);
}
});
// @ts-ignore
Object.entries(value).forEach(function (entry) {
var i = entry[0],
val = entry[1];
if (!object_editor.cached_editors[i]) {
object_editor.addObjectProperty(i);
if (object_editor.editors[i]) {
object_editor.editors[i].setValue(val, initial, !!object_editor.editors[i].template);
}
}
});
object_editor.refreshValue();
object_editor.layoutEditors();
object_editor.onChange();
}; };
JSONEditor.defaults.editors.string.prototype.setValueToInputField = function (value) { JSONEditor.defaults.editors.string.prototype.setValueToInputField = function (value) {
......
...@@ -70,13 +70,13 @@ div.json-editor-container { ...@@ -70,13 +70,13 @@ div.json-editor-container {
& .btn { & .btn {
vertical-align: middle; vertical-align: middle;
border: 1px solid transparent; border: 1px solid transparent;
padding: 0.25rem 0.5rem; padding: 2pt 5pt 3pt 5pt;
flex: 1 1 auto; flex: 1 1 auto;
color: #fff; color: #fff;
background-color: #6c757d; background-color: #6c757d;
border-color: #6c757d; border-color: #6c757d;
font-size: .875rem; font-size: .875rem;
border-radius: 0.2rem; border-radius: 0.325rem;
} }
& .btn:hover { & .btn:hover {
......
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