Commit 76541c25 authored by Boris Kocherov's avatar Boris Kocherov

partial add ignore_incorrect option.

currently realized only for enum and object
parent ee725769
...@@ -995,7 +995,8 @@ ...@@ -995,7 +995,8 @@
.push(function (value) { .push(function (value) {
return gadget.rerender({ return gadget.rerender({
schema: opt.schema, schema: opt.schema,
value: value value: value,
ignore_incorrect: opt.ignore_incorrect
}) })
.push(function () { .push(function () {
return gadget.reValidate(value, opt.schema); return gadget.reValidate(value, opt.schema);
......
...@@ -125,20 +125,21 @@ ...@@ -125,20 +125,21 @@
return schema; return schema;
} }
function render_enum(schema, json_document) { function render_enum(g, schema, json_document) {
var input = document.createElement("select"), var input = document.createElement("select"),
option, option,
i, i,
ser_value, ser_value,
selected = false, selected = false,
empty_option,
enum_arr = schema['enum']; enum_arr = schema['enum'];
input.size = 1; input.size = 1;
option = document.createElement("option"); empty_option = document.createElement("option");
option.value = ""; empty_option.value = "";
if (json_document === undefined) { if (json_document === undefined) {
option.selected = true; empty_option.selected = true;
} }
input.appendChild(option); input.appendChild(empty_option);
for (i = 0; i < enum_arr.length; i += 1) { for (i = 0; i < enum_arr.length; i += 1) {
option = document.createElement("option"); option = document.createElement("option");
ser_value = JSON.stringify(enum_arr[i]); ser_value = JSON.stringify(enum_arr[i]);
...@@ -155,6 +156,10 @@ ...@@ -155,6 +156,10 @@
input.appendChild(option); input.appendChild(option);
} }
if (json_document !== undefined && !selected) { if (json_document !== undefined && !selected) {
if (g.props.ignore_incorrect) {
empty_option.selected = true;
g.props.changed = true;
} else {
// save original json_document even if it // save original json_document even if it
// not support with schema // not support with schema
// XXX element should be removed on first user interact // XXX element should be removed on first user interact
...@@ -169,11 +174,13 @@ ...@@ -169,11 +174,13 @@
option.selected = true; option.selected = true;
input.appendChild(option); input.appendChild(option);
} }
}
return input; return input;
} }
function render_enum_with_title(schema_arr, json_document, selected_schema) { function render_enum_with_title(g, schema_arr, json_document, selected_schema) {
var input = document.createElement("select"), var input = document.createElement("select"),
empty_option,
option, option,
i, i,
ser_value, ser_value,
...@@ -182,12 +189,12 @@ ...@@ -182,12 +189,12 @@
if (json_document === undefined && selected_schema !== undefined) { if (json_document === undefined && selected_schema !== undefined) {
json_document = selected_schema.schema.const; json_document = selected_schema.schema.const;
} }
option = document.createElement("option"); empty_option = document.createElement("option");
option.value = ""; empty_option.value = "";
if (json_document === undefined) { if (json_document === undefined) {
option.selected = true; empty_option.selected = true;
} }
input.appendChild(option); input.appendChild(empty_option);
for (i = 0; i < schema_arr.length; i += 1) { for (i = 0; i < schema_arr.length; i += 1) {
option = document.createElement("option"); option = document.createElement("option");
// XXX use number id for speedup // XXX use number id for speedup
...@@ -207,6 +214,10 @@ ...@@ -207,6 +214,10 @@
input.appendChild(option); input.appendChild(option);
} }
if (json_document !== undefined && !selected) { if (json_document !== undefined && !selected) {
if (g.props.ignore_incorrect) {
empty_option.selected = true;
g.props.changed = true;
} else {
// save original json_document even if it // save original json_document even if it
// not support with schema // not support with schema
// XXX element should be removed on first user interact // XXX element should be removed on first user interact
...@@ -221,10 +232,11 @@ ...@@ -221,10 +232,11 @@
option.selected = true; option.selected = true;
input.appendChild(option); input.appendChild(option);
} }
}
return input; return input;
} }
function render_boolean(json_document) { function render_boolean(g, json_document) {
var input, var input,
schema_for_selection = { schema_for_selection = {
type: "boolean", type: "boolean",
...@@ -237,7 +249,7 @@ ...@@ -237,7 +249,7 @@
if (json_document === "false") { if (json_document === "false") {
json_document = false; json_document = false;
} }
input = render_enum(schema_for_selection, json_document); input = render_enum(g, schema_for_selection, json_document);
input.setAttribute('data-json-type', "boolean"); input.setAttribute('data-json-type', "boolean");
return input; return input;
} }
...@@ -333,6 +345,7 @@ ...@@ -333,6 +345,7 @@
document: options.json_document, document: options.json_document,
display_label: options.parent_type !== "array", display_label: options.parent_type !== "array",
saveOrigValue: g.props.saveOrigValue, saveOrigValue: g.props.saveOrigValue,
ignore_incorrect: g.props.ignore_incorrect,
scope: scope scope: scope
}) })
.push(function () { .push(function () {
...@@ -994,13 +1007,13 @@ ...@@ -994,13 +1007,13 @@
// render input begin // render input begin
if (!input && schema_arr[0].is_arr_of_const && schema_arr.length > 1) { if (!input && schema_arr[0].is_arr_of_const && schema_arr.length > 1) {
input = render_enum_with_title(schema_arr, json_document, options.selected_schema); input = render_enum_with_title(gadget, schema_arr, json_document, options.selected_schema);
} }
if (!input && schema.const !== undefined) { if (!input && schema.const !== undefined) {
input = render_const(gadget, schema, json_document); input = render_const(gadget, schema, json_document);
} }
if (!input && schema.enum !== undefined) { if (!input && schema.enum !== undefined) {
input = render_enum(schema, json_document); input = render_enum(gadget, schema, json_document);
// XXX take in account existing type with enum // XXX take in account existing type with enum
type_changed = false; type_changed = false;
} }
...@@ -1010,7 +1023,7 @@ ...@@ -1010,7 +1023,7 @@
} }
if (!input && type === "boolean") { if (!input && type === "boolean") {
input = render_boolean(json_document); input = render_boolean(gadget, json_document);
} }
if (!input && ["string", "integer", "number", "null"].indexOf(type) >= 0) { if (!input && ["string", "integer", "number", "null"].indexOf(type) >= 0) {
...@@ -1560,6 +1573,10 @@ ...@@ -1560,6 +1573,10 @@
return queue; return queue;
}) })
.push(function () { .push(function () {
if (g.props.ignore_incorrect) {
g.props.changed = true;
return;
}
var key, var key,
queue = RSVP.Queue(); queue = RSVP.Queue();
for (key in json_document) { for (key in json_document) {
...@@ -2058,6 +2075,8 @@ ...@@ -2058,6 +2075,8 @@
} }
g.props.property_name = property_name; g.props.property_name = property_name;
g.props.schema_arr = options.schema_arr; g.props.schema_arr = options.schema_arr;
// XXX realized only for enum and object
g.props.ignore_incorrect = options.ignore_incorrect;
g.props.render_opt = { g.props.render_opt = {
type: options.type, type: options.type,
selected_schema: options.selected_schema, selected_schema: options.selected_schema,
...@@ -2072,6 +2091,9 @@ ...@@ -2072,6 +2091,9 @@
var g = this, var g = this,
for_delete, for_delete,
root = g.element.querySelector('[data-json-path="/"]'); root = g.element.querySelector('[data-json-path="/"]');
if (opt.ignore_incorrect !== undefined) {
g.props.ignore_incorrect = opt.ignore_incorrect;
}
g.props.changed = false; g.props.changed = false;
g.props.inputs = []; g.props.inputs = [];
g.props.add_buttons = []; g.props.add_buttons = [];
......
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