Commit 273c120a authored by Rafael Monnerat's avatar Rafael Monnerat

erp5_json_editor: Fixup const and ensureArraySize

   * const is handled as enum with a single value automatically
   * patch ensureArraySize to not modify the user input, let the user safely slice by himself.
   * Reorganise patches so it is grouped by prototype
parent 244fb9fa
...@@ -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) {
......
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