Commit 3773f8e2 authored by Boris Kocherov's avatar Boris Kocherov

add rendering const field

parent bb373146
......@@ -5,6 +5,31 @@
"use strict";
var render_object;
function deepEqual(x, y) {
if (x === y) {
return true;
}
if ((typeof x === "object" && x !== null) && (typeof y === "object" && y !== null)) {
if (Object.keys(x).length !== Object.keys(y).length) {
return false;
}
var prop;
for (prop in x) {
if (x.hasOwnProperty(prop)) {
if (y.hasOwnProperty(prop)) {
if (!deepEqual(x[prop], y[prop])) {
return false;
}
} else {
return false;
}
}
}
return true;
}
return false;
}
function decodeJsonPointer(_str) {
// https://tools.ietf.org/html/rfc6901#section-5
return _str.replace(/~1/g, '/').replace(/~0/g, '~');
......@@ -124,7 +149,7 @@
} else {
option.textContent = ser_value;
}
if (enum_arr[i] === json_document) {
if (deepEqual(enum_arr[i], json_document)) {
option.selected = true;
selected = true;
}
......@@ -170,6 +195,19 @@
return input;
}
function render_const(schema, json_document) {
var input = document.createElement("input"),
ser_const = JSON.stringify(schema.const);
input.setAttribute('readonly', true);
if (deepEqual(json_document, schema.const)) {
input.value = ser_const;
} else {
input.value = JSON.stringify(json_document) + '' + ser_const;
input.setAttribute('data-const-value', ser_const);
}
return input;
}
function render_textarea(json_document, data_format) {
var input = document.createElement("textarea");
if (json_document !== undefined) {
......@@ -730,13 +768,16 @@
div_input.setAttribute("id", gadget.element.getAttribute("data-gadget-scope") + first_path + '/');
div_input.setAttribute("class", "input");
if (json_field.enum !== undefined) {
if (json_field.const !== undefined) {
input = render_const(json_field, default_value);
type_changed = true;
} else if (json_field.enum !== undefined) {
input = render_enum(json_field, default_value);
// XXX take in account existing type with enum
type_changed = false;
}
if (type === "boolean") {
if (!input && type === "boolean") {
input = render_boolean(json_field, default_value);
}
......@@ -789,7 +830,7 @@
}
}
if (type === "array") {
if (!input && type === "array") {
queue = render_array(
gadget,
json_field,
......@@ -801,7 +842,7 @@
gadget.props.arrays[first_path + '/'] = div;
}
if (type === "object") {
if (!input && type === "object") {
queue
.push(function () {
return render_object(
......@@ -1608,6 +1649,9 @@
var link = evt.target.getAttribute("data-error-link"),
button_list = this.props.add_buttons,
field_list = this.props.inputs,
input,
changed = false,
i;
if (link) {
location.href = link;
......@@ -1619,6 +1663,21 @@
return button_list[i].event(evt);
}
}
for (i = 0; i < field_list.length; i = i + 1) {
if (evt.target === field_list[i]) {
input = evt.target;
if (input.hasAttribute('data-const-value')) {
input.value = input.getAttribute('data-const-value');
input.setAttribute('data-origin-value', input.value);
input.removeAttribute('data-const-value');
changed = true;
}
}
}
if (changed) {
return this.rootNotifyChange();
}
})
.onEvent('input', function (evt) {
......
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