Commit 51fc22b2 authored by Boris Kocherov's avatar Boris Kocherov

tests: add test viewer based on demo

parent a718881c
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Demo UI for JSON Scheme form generator</title>
<link rel="stylesheet" href="../gadget_erp5_nojqm.css">
<script src="../rsvp.js"></script>
<script src="../renderjs.js"></script>
<script src="../jio.js"></script>
<script src="jsonform_test_view.js"></script>
</head>
<body>
<div data-role="page">
<div role="main" class="ui-content gadget-content">
<section class="ui-content-header-plain">
<h3 class="ui-content-title ui-body-c">
Demo UI for JSON Scheme form generator
</h3>
</section>
<div data-gadget-url="../jsonform.gadget.html"
data-gadget-scope="schema"
data-gadget-sandbox="public"></div>
<div data-gadget-url="../jsonform.gadget.html"
data-gadget-scope="document"
data-gadget-sandbox="public"></div>
<br>
<div class="ui-body-c">
<div class="field_container">
<div class="left">
<div class="ui-field-contain">
<div data-gadget-url="../jsonform.gadget.html"
data-gadget-scope="form_view"
data-gadget-sandbox="public">
</div>
</div>
</div>
<div class="right">
<div class="ui-field-contain">
<label for="json_document_content">Json Document</label>
<textarea name="json_document_content"
id="json_document_content"
title="Json Document"
spellcheck="false"
style="font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace;"
readonly
>{}</textarea>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
\ No newline at end of file
/*jslint nomen: true, maxlen: 200, indent: 2*/
/*global rJS, console, window, document, RSVP, jIO*/
(function (window, rJS, jIO) {
"use strict";
function downloadJSON(url) {
return RSVP.Queue()
.push(function () {
return jIO.util.ajax({
url: url,
dataType: "json"
});
})
.push(undefined, function (error) {
console.log(error);
throw error;
})
.push(function (evt) {
return evt.target.response;
});
}
function render_form(gadget, scope, schema) {
return gadget.getDeclaredGadget(scope)
.push(function (g) {
return g.render({
schema: schema
});
});
}
function render_document_selection_form(gadget, schema) {
var documents = gadget.props.documents[schema] || [""];
return render_form(gadget, "document", {
default: documents[0],
enum: documents
});
}
rJS(window)
.ready(function (g) {
var url_list = [];
g.props = {};
g.props.schemas = [];
g.props.documents = [];
g.props.test_data = {};
return RSVP.Queue()
.push(function () {
return downloadJSON("../test_index.json");
})
.push(function (list) {
var i,
url,
tasks = [];
for (i = 0; i < list.length; i += 1) {
url = '../' + list[i];
tasks.push(downloadJSON(url));
url_list.push(url);
}
return RSVP.all(tasks);
})
.push(function (list) {
var i,
k,
z,
m,
t;
for (i = 0; i < list.length; i += 1) {
for (k = 0; k < list[i].length; k += 1) {
m = list[i][k];
g.props.schemas.push(m.description);
g.props.documents[m.description] = [];
for (z = 0; z < m.tests.length; z += 1) {
t = m.tests[z];
g.props.documents[m.description].push(t.description);
g.props.test_data[m.description + ': ' + t.description] = {
schema: m.schema,
schema_url: url_list[i] + '#/' + k + '/schema',
data: t.data,
status: t.valid
};
}
}
}
})
.push(function () {
return RSVP.all([
render_form(g, "schema", {
type: "string",
default: g.props.schemas[0],
enum: g.props.schemas
})
]);
});
})
.allowPublicAcquisition("notifyValid", function () {
return;
})
.allowPublicAcquisition("notifyInvalid", function () {
return;
})
.allowPublicAcquisition("notifyChange", function (arr, scope) {
var gadget = this;
if (scope === "schema") {
return this.getDeclaredGadget("schema")
.push(function (g) {
return g.getContent();
})
.push(function (ret) {
if (ret !== gadget.props.schemas[0]) {
gadget.props.schema = ret;
} else {
gadget.props.schema = null;
}
return render_document_selection_form(gadget, ret);
});
}
if (scope === "document") {
return gadget.getDeclaredGadget("document")
.push(function (g) {
return g.getContent();
})
.push(function (ret) {
if (ret !== gadget.props.documents[0]) {
var test = gadget.props.schema + ': ' + ret;
test = gadget.props.test_data[test];
console.log(test.schema);
console.log(test.data);
return gadget.changeState({
schema_url: test.schema_url,
json_document: test.data
});
}
});
}
if (scope === "form_view") {
return this.getDeclaredGadget("form_view")
.push(function (g) {
return g.getContent();
})
.push(function (ret) {
if (ret === undefined) {
ret = {};
}
document.getElementById("json_document_content").textContent =
JSON.stringify(ret, null, " ");
});
}
})
.onStateChange(function () {
var g = this;
return g.getDeclaredGadget("form_view")
.push(function (form_view) {
return form_view.render({
value: g.state.json_document,
schema_url: g.state.schema_url
})
.push(function () {
return form_view.getContent();
})
.push(function (ret) {
if (ret === undefined) {
ret = {};
}
g.state.json_document = ret;
document.getElementById("json_document_content").textContent =
JSON.stringify(ret, null, 2);
});
});
});
}(window, rJS, jIO));
\ No newline at end of file
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