Commit b0f0e605 authored by Boris Kocherov's avatar Boris Kocherov Committed by Romain Courteaud

WIP [erp5_json_form/erp5_web_renderjs_ui/erp5_only_office] BK XMLA work

Squash of: https://lab.nexedi.com/bk/erp5/commits/onlyoffice_gui_for_xmla

initial add from https://lab.nexedi.com/bk/rjs_json_form

erp5_web_renderjs_ui: use 'error' class for validation errors

i need it because use span for displaying icons in json form

erp5_web_renderjs_ui: add design for erp5_json_form

erp5_web_renderjs_ui: html5_select: event option for notifyChange added. needs for determine event.type on parent gadget

[erp5_only_office] add gui for xmla settings

[erp5_only_office] jslint fix

[erp5_only_office] fix cube functions: use xmla_request_retry always

[erp5_only_office] use url array in cube functions for failover mirrors

[erp5_only_office] use jio.getAttachment optional parameter format

[erp5_only_office] allow use object as jio_putAttachment argument
parent a9f36f6e
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Folder" module="OFS.Folder"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_objects</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>erp5_json_form</string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Folder" module="OFS.Folder"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_objects</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>json-schema</string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
{
"id": "http://json-schema.org/draft-04/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Core schema meta-schema",
"definitions": {
"schemaArray": {
"type": "array",
"minItems": 1,
"items": { "$ref": "#" }
},
"positiveInteger": {
"type": "integer",
"minimum": 0
},
"positiveIntegerDefault0": {
"allOf": [ { "$ref": "#/definitions/positiveInteger" }, { "default": 0 } ]
},
"simpleTypes": {
"enum": [ "array", "boolean", "integer", "null", "number", "object", "string" ]
},
"stringArray": {
"type": "array",
"items": { "type": "string" },
"minItems": 1,
"uniqueItems": true
}
},
"type": "object",
"properties": {
"id": {
"type": "string"
},
"$schema": {
"type": "string"
},
"title": {
"type": "string"
},
"description": {
"type": "string"
},
"default": {},
"multipleOf": {
"type": "number",
"minimum": 0,
"exclusiveMinimum": true
},
"maximum": {
"type": "number"
},
"exclusiveMaximum": {
"type": "boolean",
"default": false
},
"minimum": {
"type": "number"
},
"exclusiveMinimum": {
"type": "boolean",
"default": false
},
"maxLength": { "$ref": "#/definitions/positiveInteger" },
"minLength": { "$ref": "#/definitions/positiveIntegerDefault0" },
"pattern": {
"type": "string",
"format": "regex"
},
"additionalItems": {
"anyOf": [
{ "type": "boolean" },
{ "$ref": "#" }
],
"default": {}
},
"items": {
"anyOf": [
{ "$ref": "#" },
{ "$ref": "#/definitions/schemaArray" }
],
"default": {}
},
"maxItems": { "$ref": "#/definitions/positiveInteger" },
"minItems": { "$ref": "#/definitions/positiveIntegerDefault0" },
"uniqueItems": {
"type": "boolean",
"default": false
},
"maxProperties": { "$ref": "#/definitions/positiveInteger" },
"minProperties": { "$ref": "#/definitions/positiveIntegerDefault0" },
"required": { "$ref": "#/definitions/stringArray" },
"additionalProperties": {
"anyOf": [
{ "type": "boolean" },
{ "$ref": "#" }
],
"default": {}
},
"definitions": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"properties": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"patternProperties": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"dependencies": {
"type": "object",
"additionalProperties": {
"anyOf": [
{ "$ref": "#" },
{ "$ref": "#/definitions/stringArray" }
]
}
},
"enum": {
"type": "array",
"minItems": 1,
"uniqueItems": true
},
"type": {
"anyOf": [
{ "$ref": "#/definitions/simpleTypes" },
{
"type": "array",
"items": { "$ref": "#/definitions/simpleTypes" },
"minItems": 1,
"uniqueItems": true
}
]
},
"format": { "type": "string" },
"allOf": { "$ref": "#/definitions/schemaArray" },
"anyOf": { "$ref": "#/definitions/schemaArray" },
"oneOf": { "$ref": "#/definitions/schemaArray" },
"not": { "$ref": "#" }
},
"dependencies": {
"exclusiveMaximum": [ "maximum" ],
"exclusiveMinimum": [ "minimum" ]
},
"default": {}
}
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="File" module="OFS.Image"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>__name__</string> </key>
<value> <string>schema4.json</string> </value>
</item>
<item>
<key> <string>content_type</string> </key>
<value> <string>application/json</string> </value>
</item>
<item>
<key> <string>precondition</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>schema4.json</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
{
"$schema": "http://json-schema.org/draft-06/schema#",
"$id": "http://json-schema.org/draft-06/schema#",
"title": "Core schema meta-schema",
"definitions": {
"schemaArray": {
"type": "array",
"minItems": 1,
"items": { "$ref": "#" }
},
"nonNegativeInteger": {
"type": "integer",
"minimum": 0
},
"nonNegativeIntegerDefault0": {
"allOf": [
{ "$ref": "#/definitions/nonNegativeInteger" },
{ "default": 0 }
]
},
"simpleTypes": {
"enum": [
"array",
"boolean",
"integer",
"null",
"number",
"object",
"string"
]
},
"stringArray": {
"type": "array",
"items": { "type": "string" },
"uniqueItems": true,
"default": []
}
},
"type": ["object", "boolean"],
"properties": {
"$id": {
"type": "string",
"format": "uri-reference"
},
"$schema": {
"type": "string",
"format": "uri"
},
"$ref": {
"type": "string",
"format": "uri-reference"
},
"title": {
"type": "string"
},
"description": {
"type": "string"
},
"default": {},
"examples": {
"type": "array",
"items": {}
},
"multipleOf": {
"type": "number",
"exclusiveMinimum": 0
},
"maximum": {
"type": "number"
},
"exclusiveMaximum": {
"type": "number"
},
"minimum": {
"type": "number"
},
"exclusiveMinimum": {
"type": "number"
},
"maxLength": { "$ref": "#/definitions/nonNegativeInteger" },
"minLength": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
"pattern": {
"type": "string",
"format": "regex"
},
"additionalItems": { "$ref": "#" },
"items": {
"anyOf": [
{ "$ref": "#" },
{ "$ref": "#/definitions/schemaArray" }
],
"default": {}
},
"maxItems": { "$ref": "#/definitions/nonNegativeInteger" },
"minItems": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
"uniqueItems": {
"type": "boolean",
"default": false
},
"contains": { "$ref": "#" },
"maxProperties": { "$ref": "#/definitions/nonNegativeInteger" },
"minProperties": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
"required": { "$ref": "#/definitions/stringArray" },
"additionalProperties": { "$ref": "#" },
"definitions": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"properties": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"patternProperties": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"dependencies": {
"type": "object",
"additionalProperties": {
"anyOf": [
{ "$ref": "#" },
{ "$ref": "#/definitions/stringArray" }
]
}
},
"propertyNames": { "$ref": "#" },
"const": {},
"enum": {
"type": "array",
"minItems": 1,
"uniqueItems": true
},
"type": {
"anyOf": [
{ "$ref": "#/definitions/simpleTypes" },
{
"type": "array",
"items": { "$ref": "#/definitions/simpleTypes" },
"minItems": 1,
"uniqueItems": true
}
]
},
"format": { "type": "string" },
"allOf": { "$ref": "#/definitions/schemaArray" },
"anyOf": { "$ref": "#/definitions/schemaArray" },
"oneOf": { "$ref": "#/definitions/schemaArray" },
"not": { "$ref": "#" }
},
"default": {}
}
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="File" module="OFS.Image"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>__name__</string> </key>
<value> <string>schema6.json</string> </value>
</item>
<item>
<key> <string>content_type</string> </key>
<value> <string>application/json</string> </value>
</item>
<item>
<key> <string>precondition</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>schema6.json</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://json-schema.org/draft-07/schema#",
"title": "Core schema meta-schema",
"definitions": {
"schemaArray": {
"type": "array",
"minItems": 1,
"items": { "$ref": "#" }
},
"nonNegativeInteger": {
"type": "integer",
"minimum": 0
},
"nonNegativeIntegerDefault0": {
"allOf": [
{ "$ref": "#/definitions/nonNegativeInteger" },
{ "default": 0 }
]
},
"simpleTypes": {
"enum": [
"array",
"boolean",
"integer",
"null",
"number",
"object",
"string"
]
},
"stringArray": {
"type": "array",
"items": { "type": "string" },
"uniqueItems": true,
"default": []
}
},
"type": ["object", "boolean"],
"properties": {
"$id": {
"type": "string",
"format": "uri-reference"
},
"$schema": {
"type": "string",
"format": "uri"
},
"$ref": {
"type": "string",
"format": "uri-reference"
},
"$comment": {
"type": "string"
},
"title": {
"type": "string"
},
"description": {
"type": "string"
},
"default": true,
"readOnly": {
"type": "boolean",
"default": false
},
"examples": {
"type": "array",
"items": true
},
"multipleOf": {
"type": "number",
"exclusiveMinimum": 0
},
"maximum": {
"type": "number"
},
"exclusiveMaximum": {
"type": "number"
},
"minimum": {
"type": "number"
},
"exclusiveMinimum": {
"type": "number"
},
"maxLength": { "$ref": "#/definitions/nonNegativeInteger" },
"minLength": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
"pattern": {
"type": "string",
"format": "regex"
},
"additionalItems": { "$ref": "#" },
"items": {
"anyOf": [
{ "$ref": "#" },
{ "$ref": "#/definitions/schemaArray" }
],
"default": true
},
"maxItems": { "$ref": "#/definitions/nonNegativeInteger" },
"minItems": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
"uniqueItems": {
"type": "boolean",
"default": false
},
"contains": { "$ref": "#" },
"maxProperties": { "$ref": "#/definitions/nonNegativeInteger" },
"minProperties": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
"required": { "$ref": "#/definitions/stringArray" },
"additionalProperties": { "$ref": "#" },
"definitions": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"properties": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"patternProperties": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"propertyNames": { "format": "regex" },
"default": {}
},
"dependencies": {
"type": "object",
"additionalProperties": {
"anyOf": [
{ "$ref": "#" },
{ "$ref": "#/definitions/stringArray" }
]
}
},
"propertyNames": { "$ref": "#" },
"const": true,
"enum": {
"type": "array",
"items": true,
"minItems": 1,
"uniqueItems": true
},
"type": {
"anyOf": [
{ "$ref": "#/definitions/simpleTypes" },
{
"type": "array",
"items": { "$ref": "#/definitions/simpleTypes" },
"minItems": 1,
"uniqueItems": true
}
]
},
"format": { "type": "string" },
"contentMediaType": { "type": "string" },
"contentEncoding": { "type": "string" },
"if": {"$ref": "#"},
"then": {"$ref": "#"},
"else": {"$ref": "#"},
"allOf": { "$ref": "#/definitions/schemaArray" },
"anyOf": { "$ref": "#/definitions/schemaArray" },
"oneOf": { "$ref": "#/definitions/schemaArray" },
"not": { "$ref": "#" }
},
"default": true
}
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="File" module="OFS.Image"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>__name__</string> </key>
<value> <string>schema7.json</string> </value>
</item>
<item>
<key> <string>content_type</string> </key>
<value> <string>application/json</string> </value>
</item>
<item>
<key> <string>precondition</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>schema7.json</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
#shared
rsvp.js
renderjs.js
handlebars.js
gadget_erp5_nojqm.css
gadget_erp5_global.js
gadget_html5_select.html
gadget_html5_select.js
#jsonform
jio.js
gadget_html5_select.html
gadget_html5_select.js
json-schema/schema4.json
json-schema/schema6.json
json-schema/schema7.json
jsonform.gadget.html
jsonform.gadget.js
jsonform/gadget_json_generated_form_child.html
jsonform/gadget_json_generated_form_child.js
jsonform/tv4.js
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="File" module="OFS.Image"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>__name__</string> </key>
<value> <string>jsonform.gadget.appcache</string> </value>
</item>
<item>
<key> <string>content_type</string> </key>
<value> <string>text/cache-manifest</string> </value>
</item>
<item>
<key> <string>precondition</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>jsonform.gadget.appcache</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
<!DOCTYPE html>
<html manifest="jsonform.gadget.appcache">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ERP5</title>
<link rel="stylesheet" href="gadget_erp5_nojqm.css">
<script src="rsvp.js" type="text/javascript"></script>
<script src="jsonform/tv4.js" type="text/javascript"></script>
<script src="renderjs.js" type="text/javascript"></script>
<script src="jio.js" type="text/javascript"></script>
<script src="jsonform.gadget.js" type="text/javascript"></script>
</head>
<body>
</body>
</html>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="File" module="OFS.Image"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>__name__</string> </key>
<value> <string>jsonform.gadget.html</string> </value>
</item>
<item>
<key> <string>content_type</string> </key>
<value> <string>text/html</string> </value>
</item>
<item>
<key> <string>precondition</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>schema.gadget.html</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
/*jslint nomen: true, maxlen: 200, indent: 2, maxerr: 100*/
/*global window, document, URL, rJS, RSVP, jIO, tv4, Blob */
(function (window, document, Blob, rJS, RSVP, jIO, tv4) {
"use strict";
var expandSchema;
function arrayIntersect(x, y) {
return x.filter(function (value) {
return y.indexOf(value) >= 0;
});
}
function getUrlWithoutHash(url) {
if (typeof url !== "string") {
url = url.href;
}
var index = url.indexOf('#');
if (index >= 0) {
return url.substring(0, index);
}
return url;
}
function URLwithJio(url, base_url) {
var urn_prefix,
pathname,
fake_prefix = "https://jio_urn_prefix/";
// XXX urn: can any case
if (url.startsWith("urn:jio:reference?")) {
urn_prefix = url.indexOf("?") + 1;
urn_prefix = url.slice(0, urn_prefix);
url = fake_prefix + decodeURIComponent(url.replace(urn_prefix, ""));
}
if (typeof base_url === "string" &&
!(url.startsWith("http://") || url.startsWith("https://") || url.startsWith("//")) &&
base_url.startsWith("urn:jio:reference?")) {
if (!urn_prefix) {
urn_prefix = base_url.indexOf("?") + 1;
urn_prefix = base_url.slice(0, urn_prefix);
}
base_url = fake_prefix + decodeURIComponent(base_url.replace(urn_prefix, ""));
}
url = new URL(url, base_url);
if (urn_prefix) {
pathname = url.pathname.slice(1);
this.href = urn_prefix + encodeURIComponent(pathname + url.search + url.hash);
this.origin = urn_prefix;
this.pathname = encodeURIComponent(pathname);
this.hash = url.hash;
this.search = "";
return this;
}
return url;
}
function decodeJsonPointer(_str) {
// https://tools.ietf.org/html/rfc6901#section-5
return _str.replace(/~1/g, '/').replace(/~0/g, '~');
}
function encodeJsonPointer(_str) {
// https://tools.ietf.org/html/rfc6901#section-5
return _str.replace(/~/g, '~0').replace(/\//g, '~1');
}
function getMaxPathInDict(dict, path) {
var target,
key,
max_len = 0;
if (!path) {
return "";
}
for (key in dict) {
if (dict.hasOwnProperty(key) &&
path.startsWith(key) &&
key.length > max_len) {
target = key;
max_len = key.length;
}
}
return target;
}
function checkCircular(urls, path, url) {
var stack,
idx,
prev_field_path = getMaxPathInDict(urls, path);
stack = urls[prev_field_path] || [];
idx = stack.indexOf(url);
if (idx >= 0) {
if (path === prev_field_path && idx === 0) {
return;
}
return true;
}
// copy and add url as first element
urls[path] = [url].concat(stack);
}
function checkHardCircular(g, path, url) {
return checkCircular(g.props.schema_required_urls, path, url);
}
function checkAndMarkSoftCircular(g, schema_arr, path, url) {
var ret = true;
// if schema_arr.length > 1 selection rendered in any case
// so we not need checkCircular and have small optimisation
if (schema_arr.length === 1) {
ret = checkCircular(g.props.schema_urls, path, url);
schema_arr[0].circular = ret;
}
if (ret) {
// if schema_arr.length > 1 selection rendered and loop break
// if circular found selection rendered and loop break
// so we can begin from start
g.props.schema_urls[path] = [];
}
}
function convertToRealWorldSchemaPath(g, path) {
var url,
hash,
map = g.props.schema_map,
prev_downl_path,
max_len = 0;
if (!path) {
return "";
}
// previous downloaded path
prev_downl_path = getMaxPathInDict(map, path);
if (prev_downl_path === undefined) {
url = "";
max_len = 0;
} else {
url = map[prev_downl_path];
if (prev_downl_path === "/") {
max_len = 0;
} else {
max_len = prev_downl_path.length;
}
}
hash = path.substr(max_len);
if (hash) {
// XXX urlencode for hash
if (url.indexOf("#") >= 0) {
url = url + hash;
} else {
url = url + "#" + hash;
}
}
return url;
}
function convertUrlToAbsolute(g, path, url, base_url_failback) {
var // previous downloaded path
base_url = convertToRealWorldSchemaPath(g, path),
absolute_url;
if (base_url === "" || base_url.indexOf("#") === 0) {
absolute_url = new URLwithJio(url, base_url_failback);
} else {
absolute_url = new URLwithJio(url, base_url);
}
return absolute_url;
}
function downloadJSON(url) {
return RSVP.Queue()
.push(function () {
return jIO.util.ajax({
url: url,
dataType: "json"
});
})
.push(function (evt) {
return evt.target.response;
});
}
function resolveLocalReference(schema, ref) {
// 2 here is for #/
var i, ref_path = ref.substr(2, ref.length),
parts = ref_path.split("/");
if (parts.length === 1 && parts[0] === "") {
// It was uses #/ to reference the entire json so just return it.
return schema;
}
for (i = 0; i < parts.length; i += 1) {
if (schema === undefined) {
throw new Error("local ref `" + ref + "` does not exist in:");
}
schema = schema[decodeJsonPointer(parts[i])];
}
return schema;
}
function schemaPushSchemaPart(schema, schema_path, schema_part) {
var i,
k,
key_list;
if (schema_path === "/") {
schema_path = "";
}
key_list = schema_path.split("/");
for (i = 0; i < key_list.length; i += 1) {
k = decodeJsonPointer(key_list[i]);
if (i === key_list.length - 1) {
if (schema_part !== undefined) {
schema[k] = schema_part;
} else {
return schema[k];
}
} else {
if (!schema.hasOwnProperty(k)) {
schema[k] = {};
}
schema = schema[k];
}
}
}
function map_url(g, download_url) {
var hash = download_url.hash,
mapped_url = getUrlWithoutHash(download_url),
i,
schemas = g.props.schemas,
next_mapped_url;
// simple defence forever loop
for (i = 0; i < Object.keys(schemas).length; i += 1) {
next_mapped_url = schemas[mapped_url];
if (next_mapped_url === undefined) {
break;
}
mapped_url = new URL(next_mapped_url, g.__path);
if (hash[0] === '#') {
hash = hash.slice(1);
}
if (hash === '/') {
hash = '';
}
hash = mapped_url.hash || "#" + hash;
mapped_url = getUrlWithoutHash(mapped_url);
}
return new URL(mapped_url + hash);
}
function loadJSONSchema(g, $ref, path) {
var protocol,
abs_url,
url,
download_url,
hash,
queue;
// XXX need use `id` property
if (!path) {
path = "/";
}
abs_url = convertUrlToAbsolute(g, path, decodeURI($ref), window.location);
url = map_url(g, abs_url);
abs_url = abs_url.href;
protocol = url.protocol;
if (protocol === "http:") {
if (window.location.protocol !== protocol) {
// try change url protocol to https
url = new URL(url.toString().replace(protocol + "//", window.location.protocol + "//"));
// throw new Error("You cannot mixed http and https calls");
}
}
download_url = getUrlWithoutHash(url);
hash = url.hash;
url = url.href;
if (download_url.startsWith("urn:jio:")) {
queue = RSVP.Queue()
.push(function () {
return g.resolveExternalReference(download_url);
});
} else {
queue = RSVP.Queue()
.push(function () {
return downloadJSON(download_url);
});
}
return queue
.push(function (json) {
if (checkHardCircular(g, path, url)) {
throw new Error("Circular reference detected");
}
return resolveLocalReference(json, hash);
})
.push(undefined, function (err) {
// XXX it will be great to have ability convert json_pointers(hash)
// in line numbers for pointed to line in rich editors.
// we can use https://github.com/vtrushin/json-to-ast for it
var url_from_pointed = convertToRealWorldSchemaPath(g, path),
schema_a = document.createElement("a"),
pointed_a = document.createElement("a");
schema_a.setAttribute("href", download_url);
schema_a.text = (new URLwithJio(download_url)).pathname;
pointed_a.setAttribute("href", url_from_pointed);
pointed_a.text = (new URLwithJio(url_from_pointed)).pathname;
g.props.schema_resolve_errors[url_from_pointed] = {
schemaPath: path,
message: [
document.createTextNode("schema error: "),
document.createTextNode(err.message),
schema_a,
document.createTextNode(" pointed from schema: "),
pointed_a
]
};
return null; // schema part can't be null
})
.push(function (schema_part) {
// console.log(path);
if (schema_part === null) {
// if resolving schema part contain errors
// use {} as failback
schema_part = {};
} else {
// save map url only for correctly resolved schema
// otherwise we have issue in convertToRealWorldSchemaPath
if (!g.props.hasOwnProperty(path)) {
g.props.schema_map[path] = abs_url;
}
}
schemaPushSchemaPart(g.props.schema, path, JSON.parse(JSON.stringify(schema_part)));
// console.log(g.props.schema[""]);
return expandSchema(g, schema_part, path, $ref);
})
.push(function (schema_arr) {
checkAndMarkSoftCircular(g, schema_arr, path, url);
return schema_arr;
});
}
function mergeSchemas(x, y, doesntcopy) {
if (x === true && y === true) {
return true;
}
if (x === false || y === false) {
return false;
}
var key,
p;
if (x.hasOwnProperty("$ref") ||
y.hasOwnProperty("$ref")) {
if (doesntcopy) {
// we need reference resolve before merging
// so allOf schema returned and array item or object field
// run merging on next iteration.
return {
allOf: [
x,
y
]
};
}
throw new Error("all reference must be resolved before merge run on first recursion level");
}
if (x === true) {
x = {};
} else if (!doesntcopy) {
x = JSON.parse(JSON.stringify(x));
// cleanup already walked schema variations
if (x.anyOf) {
delete x.anyOf;
}
if (x.oneOf) {
delete x.oneOf;
}
if (x.allOf) {
delete x.allOf;
}
}
if (y === true) {
y = {};
}
for (key in y) {
if (y.hasOwnProperty(key)) {
if (x.hasOwnProperty(key)) {
switch (key) {
case "maxProperties":
case "maxLength":
case "maxItems":
case "maximum":
case "exclusiveMaximum":
if (y[key] < x[key]) {
x[key] = y[key];
}
break;
case "minProperties":
case "minItems":
case "minLength":
case "minimum":
case "exclusiveMinimum":
if (x[key] < y[key]) {
x[key] = y[key];
}
break;
case "additionalProperties":
case "additionalItems":
case "contains":
case "propertyNames":
x[key] = mergeSchemas(x[key], y[key], true);
break;
case "items":
// XXX items can be array
x[key] = mergeSchemas(x[key], y[key], true);
break;
case "contentEncoding":
case "contentMediaType":
if (x[key] !== y[key]) {
return false;
}
break;
case "multipleOf":
x[key] = x[key] * y[key];
break;
case "type":
if (typeof x.type === "string") {
if (typeof y.type === "string") {
if (x.type !== y.type) {
return false;
}
} else if (y.type.indexOf(x.type) === -1) {
return false;
}
} else {
if (typeof y.type === "string") {
if (x.type.indexOf(y.type) === -1) {
return false;
}
} else {
x.type = arrayIntersect(x.type, y.type);
if (x.type.length === 0) {
return false;
}
}
}
break;
case "properties":
case "patternProperties":
for (p in y[key]) {
if (y[key].hasOwnProperty(p)) {
if (x[key].hasOwnProperty(p)) {
x[key][p] = mergeSchemas(x[key][p], y[key][p], true);
} else {
x[key][p] = y[key][p];
}
}
}
break;
case "pattern":
// XXX regex string merge
case "dependencies":
// XXX find solution how merge
x[key] = y[key];
break;
case "required":
for (p = 0; p < y.required.length; p += 1) {
if (x.required.indexOf(y.required[p]) < 0) {
x.required.push(y.required[p]);
}
}
break;
case "uniqueItems":
x[key] = y[key];
break;
case "allOf":
case "anyOf":
case "oneOf":
case "$ref":
case "id":
case "$id":
// XXX
break;
default:
// XXX
x[key] = y[key];
}
} else {
switch (key) {
case "allOf":
case "anyOf":
case "oneOf":
case "$ref":
break;
default:
x[key] = y[key];
}
}
}
}
return x;
}
function allOf(g, schema_array, schema_path, base_schema) {
return RSVP.Queue()
.push(function () {
var i,
arr = [];
for (i = 0; i < schema_array.length; i += 1) {
arr.push(expandSchema(g, schema_array[i], schema_path + '/' + i.toString()));
}
return RSVP.all(arr);
})
.push(function (arr) {
var i,
x,
y,
next_schema,
schema,
summ_arr;
for (i = 0; i < arr.length - 1; i += 1) {
summ_arr = [];
for (x = 0; x < arr[i].length; x += 1) {
for (y = 0; y < arr[i + 1].length; y += 1) {
schema = arr[i][x].schema;
next_schema = arr[i + 1][y].schema;
schema = mergeSchemas(schema, next_schema);
summ_arr.push({
schema: schema,
// XXX we loss path arr[i + 1][y].schema_path
schema_path: arr[i][x].schema_path
});
}
}
arr[i + 1] = summ_arr;
}
for (x = 0; x < summ_arr.length; x += 1) {
summ_arr[x].schema = mergeSchemas(summ_arr[x].schema, base_schema);
}
return summ_arr;
});
}
function anyOf(g, schema_array, schema_path, base_schema) {
return RSVP.Queue()
.push(function () {
var i,
arr = [];
for (i = 0; i < schema_array.length; i += 1) {
arr.push(expandSchema(g, schema_array[i], schema_path + '/' + i.toString()));
}
return RSVP.all(arr);
})
.push(function (arr) {
var i,
z,
schema_arr = [];
for (i = 0; i < arr.length; i += 1) {
for (z = 0; z < arr[i].length; z += 1) {
if (arr[i][z].schema === true) {
// or(any, restricted, restricted, .. ) simplify to any
return [arr[i][z]];
}
if (base_schema.title) {
arr[i][z].title = base_schema.title;
}
if (base_schema.description) {
arr[i][z].description = base_schema.description;
}
arr[i][z].schema = mergeSchemas(base_schema, arr[i][z].schema);
schema_arr.push(arr[i][z]);
}
}
return schema_arr;
});
}
expandSchema = function (g, schema, schema_path, ref) {
// XXX `if then else` construction can be simplify to
// anyOf(allOf(if_schema, then_schema), else_schema)
// and realized by existed rails
if (schema === undefined ||
Object.keys(schema).length === 0) {
schema = true;
}
if (schema.anyOf !== undefined) {
return anyOf(g, schema.anyOf, schema_path + '/anyOf', schema);
}
if (schema.oneOf !== undefined) {
return anyOf(g, schema.oneOf, schema_path + '/oneOf', schema);
}
if (schema.allOf !== undefined) {
return allOf(g, schema.allOf, schema_path + '/allOf', schema);
}
if (schema.$ref) {
return loadJSONSchema(g, schema.$ref, schema_path);
}
if (schema.definitions) {
var key,
d,
url,
mapped_url;
for (key in schema.definitions) {
if (schema.definitions.hasOwnProperty(key)) {
d = schema.definitions[key];
url = d.$id || d.id;
if (url) {
mapped_url = convertUrlToAbsolute(g, schema_path, '#' + schema_path, window.location);
// XXX /?
mapped_url = mapped_url + 'definitions/' + key;
if (!g.props.schemas.hasOwnProperty(url)) {
g.props.schemas[url] = mapped_url;
}
}
}
}
}
return RSVP.Queue()
.push(function () {
return [{
title: schema.title,
ref: ref,
schema: schema,
schema_path: schema_path
}];
});
};
function schema_arr_marker(schema_arr) {
var i;
// XXX need cleanup false schema before
for (i = 0; i < schema_arr.length; i += 1) {
if (!schema_arr[i].schema.hasOwnProperty('const')) {
schema_arr[0].is_arr_of_const = false;
break;
}
if (i === schema_arr.length - 1) {
schema_arr[0].is_arr_of_const = true;
}
}
return schema_arr;
}
function expandSchemaForField(g, schema, schema_path, for_required) {
var required_stack,
prev_field_path;
if (for_required) {
prev_field_path = getMaxPathInDict(g.props.schema_required_urls, schema_path);
required_stack = g.props.schema_required_urls[prev_field_path];
} else {
required_stack = [];
}
g.props.schema_required_urls[schema_path] = required_stack;
return expandSchema(g, schema, schema_path)
.push(schema_arr_marker);
}
rJS(window)
.ready(function () {
var g = this;
g.props = {};
g.options = {};
})
.declareAcquiredMethod("resolveExternalReference", "resolveExternalReference")
.declareAcquiredMethod("notifyChange", "notifyChange")
.allowPublicAcquisition("rootNotifyChange", function () {
this.props.changed = true;
return this.notifyChange();
})
.declareAcquiredMethod("notifyValid", "notifyValid")
.declareAcquiredMethod("notifyInvalid", "notifyInvalid")
.allowPublicAcquisition("checkValidity", function (arr) {
return this.checkValidity(arr[0]);
})
.declareMethod('checkValidity', function (json_document) {
// XXX need use local schema and local json document
// in every subgadget to take into account user anyOf choice
// and so more precisely point to issue
var g = this.props.form_gadget,
gadget = this;
return RSVP.Queue()
.push(function () {
if (json_document === undefined) {
return g.getContent();
}
return json_document;
})
.push(function (json_d) {
gadget.state.value = JSON.stringify(json_d);
return tv4.validateMultiple(json_d, gadget.props.schema[""]);
})
.push(function (validation) {
var i,
error_id,
error,
span,
tasks = [],
errors = [],
schema_resolve_errors = gadget.props.schema_resolve_errors,
errors_block = g.element.querySelector("div.error-block");
if (errors_block) {
errors_block.parentNode.removeChild(errors_block);
}
g.element.querySelectorAll(".error").forEach(function (error_message) {
error_message.textContent = "";
error_message.removeAttribute("id");
error_message.hidden = true;
});
g.element.querySelectorAll("div.error-input").forEach(function (div) {
div.setAttribute("class", "");
});
for (i in schema_resolve_errors) {
if (schema_resolve_errors.hasOwnProperty(i)) {
errors.push(schema_resolve_errors[i]);
}
}
errors = errors.concat(validation.errors);
errors = errors.concat(validation.missing);
if (errors.length === 0) {
return gadget.notifyValid()
.push(function () {
return false;
});
}
span = document.createElement("span");
span.setAttribute("class", "error");
span.textContent = "errors: ";
errors_block = document.createElement("div");
errors_block.setAttribute("class", "subfield error-block");
errors_block.appendChild(span);
function print_error(error, errorUid, errorId) {
return function (element) {
var id = element.id,
error_message,
createTextNode = document.createTextNode.bind(document),
a = document.createElement("a");
a.setAttribute("href", "#" + errorUid);
a.text = errorId;
element.setAttribute("class", "error-input");
error_message = document.getElementById(id).querySelector(".error");
error_message.appendChild(a);
error_message.setAttribute("id", errorUid);
if (error.message instanceof Array) {
error.message.forEach(function (x) {
error_message.appendChild(x);
});
} else {
error_message.appendChild(createTextNode(error.message));
}
error_message.appendChild(document.createElement("br"));
error_message.hidden = false;
a = document.createElement("a");
a.text = errorId;
a.setAttribute("data-error-link", "#" + errorUid);
a.setAttribute("class", "error-link");
if (errorId !== "1") {
errors_block.appendChild(createTextNode(","));
}
errors_block.appendChild(a);
};
}
for (i = 0; i < errors.length; i += 1) {
error = errors[i];
error_id = (i + 1).toString();
tasks.push(
g.getElementByPath(error.dataPath || "/")
.push(print_error(error, "error" + error_id, error_id))
);
}
return RSVP.Queue()
.push(function () {
return RSVP.all(tasks);
})
.push(function () {
g.element.insertBefore(errors_block, g.element.firstChild);
})
.push(gadget.notifyInvalid.bind(gadget))
.push(function () {
return false;
});
});
})
.declareMethod('render', function (options) {
var z = {
saveOrigValue: options.saveOrigValue,
editable: options.editable === undefined ? true : options.editable
};
if (options.hasOwnProperty("key")) {
z.key = options.key;
}
if (options.hasOwnProperty("schema")) {
if (typeof options.schema === "string") {
z.schema = options.schema;
} else {
z.schema = JSON.stringify(options.schema);
}
}
if (options.hasOwnProperty("schema_url")) {
z.schema_url = (new URL(options.schema_url, window.location))
.toString();
}
if (options.value !== undefined) {
z.value = JSON.stringify(options.value);
}
return this.changeState(z);
})
.onStateChange(function () {
var g = this,
json_document = g.state.value,
schema;
if (json_document !== undefined) {
json_document = JSON.parse(json_document);
}
if (g.state.schema !== undefined) {
schema = JSON.parse(g.state.schema);
}
g.props.toplevel = true;
// contain map of current normalized schema
// json pointer and corresponding url
// it's need for schema uri computation
g.props.schema = {};
g.props.schema_map = {};
g.props.schemas = {
"http://json-schema.org/draft-04/schema": "json-schema/schema4.json",
"http://json-schema.org/draft-06/schema": "json-schema/schema6.json",
"http://json-schema.org/draft-07/schema": "json-schema/schema7.json",
"http://json-schema.org/schema": "json-schema/schema7.json"
};
// schema_urls[path] = [
// stack urls
// "url1",
// "url2"
// ]
// used for break soft circular relation of schemas
g.props.schema_urls = {};
// schema_required_urls[path] = [
// stack required urls, on every unrequired field stack begining from []
// "url1",
// "url2"
// ]
// used for break hard circular relation of schemas
g.props.schema_required_urls = {};
// schema_resolve_errors[schema_url] = {
// schemaPath: local_schema_path,
// message: error_message can be array containing dom elements
// }
g.props.schema_resolve_errors = {};
return RSVP.Queue()
.push(function () {
if (!g.props.form_gadget) {
return g.declareGadget('jsonform/gadget_json_generated_form_child.html',
{scope: "j" + Math.random().toString(36).substr(2, 9)})
.push(function (json_form_child) {
g.props.form_gadget = json_form_child;
g.element.appendChild(json_form_child.element);
});
}
})
.push(function () {
var schema_url,
queue;
if (schema !== undefined) {
schema_url = g.state.schema_url ||
schema.$id ||
schema.id ||
window.location.toString();
g.props.schema[""] = schema;
g.props.schema_map["/"] = schema_url;
g.props.schemas[schema_url] = URL
.createObjectURL(new Blob([g.state.schema], {type : 'application/json'}));
queue = expandSchemaForField(g, schema, "/", true);
} else {
schema_url = g.state.schema_url ||
(json_document && json_document.$schema);
if (schema_url) {
queue = loadJSONSchema(g, schema_url)
.push(schema_arr_marker);
}
}
if (queue) {
return queue;
}
return [{
schema: true,
schema_path: ""
}];
})
.push(function (schema_arr) {
return g.props.form_gadget.renderForm({
schema_arr: schema_arr,
document: json_document,
saveOrigValue: g.state.saveOrigValue,
required: true,
top: true
});
})
.push(function () {
return g.checkValidity();
})
.push(function () {
if (g.props.form_gadget.props.changed) {
g.notifyChange();
}
})
.push(function () {
return g;
})
.push(undefined, function (err) {
console.log(err);
});
})
.allowPublicAcquisition("expandSchema", function (arr) {
return expandSchemaForField(this, arr[0], arr[1], arr[2]);
})
.onLoop(function () {
var gadget = this;
if (this.props.changed) {
return this.checkValidity()
.push(function () {
gadget.props.changed = false;
});
}
}, 500)
.declareMethod('getContent', function () {
var g = this;
if (g.state.editable) {
return g.props.form_gadget.getContent()
.push(function (value) {
// Change the value state in place
// This will prevent the gadget to be changed if
// its parent call render with the same value
// (as ERP5 does in case of formulator error)
g.state.value = JSON.stringify(value);
if (g.state.key) {
var form_data = {};
value = JSON.stringify(value);
form_data[g.state.key] = value;
return form_data;
}
return value;
});
}
return {};
}, {mutex: 'changestate'});
}(window, document, Blob, rJS, RSVP, jIO, tv4));
\ No newline at end of file
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="File" module="OFS.Image"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>__name__</string> </key>
<value> <string>jsonform.gadget.js</string> </value>
</item>
<item>
<key> <string>content_type</string> </key>
<value> <string>application/javascript</string> </value>
</item>
<item>
<key> <string>precondition</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>jsonform.gadget.js</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Folder" module="OFS.Folder"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_objects</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>jsonform</string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ERP5</title>
<link rel="stylesheet" href="../gadget_erp5_nojqm.css">
<script src="../rsvp.js" type="text/javascript"></script>
<script src="tv4.js" type="text/javascript"></script>
<script src="../renderjs.js" type="text/javascript"></script>
<script src="gadget_json_generated_form_child.js" type="text/javascript"></script>
</head>
<body>
<div data-json-path="/">
</div>
</body>
</html>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="File" module="OFS.Image"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>__name__</string> </key>
<value> <string>gadget_json_generated_form_child.html</string> </value>
</item>
<item>
<key> <string>content_type</string> </key>
<value> <string>text/html</string> </value>
</item>
<item>
<key> <string>precondition</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>gadget_json_generated_form_child.html</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
/*jslint nomen: true, maxlen: 200, indent: 2, maxerr: 100*/
/*global window, document, URL, rJS, RSVP, jIO, tv4, location */
(function (window, document, location, rJS, RSVP, tv4) {
"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, '~');
}
function encodeJsonPointer(_str) {
// https://tools.ietf.org/html/rfc6901#section-5
return _str.replace(/~/g, '~0').replace(/\//g, '~1');
}
function getDocumentType(doc) {
if (doc === undefined) {
return;
}
if (doc === null) {
return "null";
}
if (doc instanceof Array) {
return "array";
}
return typeof doc;
}
function guessSchemaType(schema) {
var property_name;
for (property_name in schema) {
if (schema.hasOwnProperty(property_name)) {
switch (property_name) {
// case "allOf":
// case "anyOf":
// case "oneOf":
// return false;
case "required":
case "maxProperties":
case "minProperties":
case "additionalProperties":
case "properties":
case "patternProperties":
case "dependencies":
case "propertyNames":
return "object";
case "additionalItems":
case "items":
case "maxItems":
case "minItems":
case "uniqueItems":
case "contains":
return "array";
case "maxLength":
case "minLength":
case "pattern":
case "contentEncoding":
case "contentMediaType":
return "string";
case "multipleOf":
case "maximum":
case "exclusiveMaximum":
case "minimum":
case "exclusiveMinimum":
return "number";
}
}
}
}
function createElement(type, props) {
var element = document.createElement(type),
key;
for (key in props) {
if (props.hasOwnProperty(key)) {
element.setAttribute(key, props[key]);
}
}
return element;
}
function getDocumentSchema(doc) {
var type = getDocumentType(doc),
schema = {
type: type
};
if (type === "array") {
schema.maxItems = 0;
} else if (type === "object") {
schema.additionalProperties = false;
} else {
schema.readOnly = true;
}
return schema;
}
function render_enum(schema, json_document) {
var input = document.createElement("select"),
option,
i,
ser_value,
selected = false,
enum_arr = schema['enum'];
input.size = 1;
option = document.createElement("option");
option.value = "";
if (json_document === undefined) {
option.selected = true;
}
input.appendChild(option);
for (i = 0; i < enum_arr.length; i += 1) {
if (enum_arr.hasOwnProperty(i)) {
option = document.createElement("option");
// XXX use number id for speedup
ser_value = JSON.stringify(enum_arr[i]);
option.value = ser_value;
if (typeof enum_arr[i] === "string") {
option.textContent = enum_arr[i];
} else {
option.textContent = ser_value;
}
if (deepEqual(enum_arr[i], json_document)) {
option.selected = true;
selected = true;
}
input.appendChild(option);
}
}
if (json_document !== undefined && !selected) {
// save original json_document even if it
// not support with schema
// XXX element should be removed on first user interact
option = document.createElement("option");
ser_value = JSON.stringify(json_document);
option.value = ser_value;
if (typeof json_document === "string") {
option.textContent = json_document;
} else {
option.textContent = ser_value;
}
option.selected = true;
input.appendChild(option);
}
return input;
}
function render_enum_with_title(schema_arr, json_document, selected_schema) {
var input = document.createElement("select"),
option,
i,
ser_value,
selected = false;
input.size = 1;
if (json_document === undefined && selected_schema !== undefined) {
json_document = selected_schema.schema.const;
}
option = document.createElement("option");
option.value = "";
if (json_document === undefined) {
option.selected = true;
}
input.appendChild(option);
for (i = 0; i < schema_arr.length; i += 1) {
option = document.createElement("option");
// XXX use number id for speedup
ser_value = JSON.stringify(schema_arr[i].schema.const);
option.value = ser_value;
if (schema_arr[i].schema.title) {
option.textContent = schema_arr[i].schema.title;
} else if (typeof schema_arr[i].schema.const === "string") {
option.textContent = schema_arr[i].schema.const;
} else {
option.textContent = ser_value;
}
if (deepEqual(schema_arr[i].schema.const, json_document)) {
option.selected = true;
selected = true;
}
input.appendChild(option);
}
if (json_document !== undefined && !selected) {
// save original json_document even if it
// not support with schema
// XXX element should be removed on first user interact
option = document.createElement("option");
ser_value = JSON.stringify(json_document);
option.value = ser_value;
if (typeof json_document === "string") {
option.textContent = json_document;
} else {
option.textContent = ser_value;
}
option.selected = true;
input.appendChild(option);
}
return input;
}
function render_boolean(json_document) {
var input,
schema_for_selection = {
type: "boolean",
enum: [true, false]
};
// XXX change json_document on open is not correct @bk
if (json_document === "true") {
json_document = true;
}
if (json_document === "false") {
json_document = false;
}
input = render_enum(schema_for_selection, json_document);
input.setAttribute('data-json-type', "boolean");
return input;
}
function render_const(g, schema, json_document) {
var input = document.createElement("input"),
ser_doc = JSON.stringify(json_document),
ser_const = JSON.stringify(schema.const);
input.setAttribute('readonly', true);
if (json_document === undefined || deepEqual(json_document, schema.const)) {
if (json_document === undefined) {
g.props.changed = true;
}
input.setAttribute('data-origin-value', ser_const);
if (schema.title) {
input.value = schema.title;
} else {
input.value = ser_const;
}
} else {
input.value = ser_doc + '' + ser_const;
input.setAttribute('data-origin-value', ser_doc);
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) {
if (typeof json_document === "object") {
input.value = JSON.stringify(json_document, null, 2);
} else {
input.value = json_document;
}
}
input["data-format"] = data_format;
return input;
}
function addSubForm(options) {
var input_element = options.element,
g = options.gadget,
property_name,
parent_path,
scope;
scope = "j" + Math.random().toString(36).substr(2, 9);
if (options.parent_type !== "array") {
parent_path = options.path;
property_name = options.property_name;
if (!property_name) {
property_name = input_element.value;
}
if (!property_name) {
// XXX notify user
// you can't create property without property_name
return RSVP.Queue();
}
if (g.props.objects[parent_path].hasOwnProperty(property_name) && g.props.objects[parent_path][property_name] !== "") {
// XXX notify user
// you can't create property with existed property_name
return RSVP.Queue();
}
if (input_element) {
input_element.value = "";
}
}
return g.declareGadget('gadget_json_generated_form_child.html', {scope: scope})
.push(function (form_gadget) {
form_gadget.element.setAttribute("data-gadget-parent-scope",
g.element.getAttribute("data-gadget-scope"));
if (options.parent_type !== "array") {
g.props.objects[parent_path][property_name] = scope;
form_gadget.element.setAttribute("data-json-parent", parent_path);
form_gadget.element.setAttribute("data-json-property-name", property_name);
}
return form_gadget.renderForm({
type: options.type,
required: options.required,
delete_button: options.delete_button,
selected_schema: options.selected_schema,
schema_arr: options.schema_arr,
document: options.json_document,
display_label: options.parent_type !== "array",
saveOrigValue: g.props.saveOrigValue,
scope: scope
})
.push(function () {
if (form_gadget.props.changed) {
g.props.changed = true;
}
return form_gadget.element;
});
});
}
function expandItems(g, items, schema_path, minItems) {
if (!(items instanceof Array)) {
return g.expandSchema(items, schema_path, minItems !== 0);
}
var i,
tasks = [];
for (i = 0; i < items.length; i += 1) {
tasks.push(g.expandSchema(items[i], schema_path + '/' + i, i < minItems));
}
return RSVP.Queue()
.push(function () {
return RSVP.all(tasks);
});
}
function expandProperties(g, properties, schema_path, required) {
var ret_obj = {};
return RSVP.Queue()
.push(function () {
var property_name,
arr = [];
function addPropertyName(p_name) {
return function (schema_array) {
ret_obj[p_name] = schema_array;
};
}
for (property_name in properties) {
if (properties.hasOwnProperty(property_name)) {
arr.push(
g.expandSchema(
properties[property_name],
schema_path + encodeJsonPointer(property_name),
required.indexOf(property_name) >= 0
)
.push(addPropertyName(property_name))
);
}
}
return RSVP.all(arr);
})
.push(function () {
return ret_obj;
});
}
function checkSchemaArrOneChoise(schema_arr) {
if (schema_arr.length === 1) {
if (schema_arr[0].schema === true ||
!(schema_arr[0].schema.hasOwnProperty('type') ||
schema_arr[0].schema.hasOwnProperty('enum') ||
schema_arr[0].schema.hasOwnProperty('const')
)) {
return false;
}
if (schema_arr[0].schema.type instanceof Array) {
return schema_arr[0].schema.type.length <= 1;
}
return true;
}
if (schema_arr[0].is_arr_of_const) {
return true;
}
return false;
}
function checkSchemaSimpleType(schema_arr) {
// return true if rendering are not recursive
var schema = schema_arr[0].schema;
return schema_arr[0].is_arr_of_const ||
schema.hasOwnProperty('const') ||
[
'string',
'integer',
'number',
'boolean',
'null'
].indexOf(schema.type) >= 0;
}
function convertExpandedProperties2array(properties) {
var property_name,
arr = [],
i,
schema_array;
for (property_name in properties) {
if (properties.hasOwnProperty(property_name)) {
schema_array = properties[property_name];
for (i = 0; i < schema_array.length; i += 1) {
// add propertyName to title
if (schema_array[i].title && schema_array.length > 1) {
schema_array[i].title = property_name + ' /' + schema_array[i].title;
} else if (schema_array[i].ref && schema_array.length > 1) {
schema_array[i].title = property_name + ' /' + schema_array[i].ref;
} else {
schema_array[i].title = property_name;
}
// add propertyName to schemaItem
schema_array[i].property_name = property_name;
arr.push(schema_array[i]);
}
}
}
return arr;
}
function schemaArrFilteredByDocument(schema_arr, json_document) {
var x,
i,
errors,
error,
flag,
circular = schema_arr[0].circular,
ret_arr = [],
validation,
schema;
if (schema_arr.length === 1 ||
schema_arr[0].is_arr_of_const) {
return schema_arr;
}
if (json_document !== undefined) {
for (x = 0; x < schema_arr.length; x += 1) {
schema = schema_arr[x].schema;
if (schema === true) {
flag = true;
} else if (schema === false) {
flag = false;
} else {
flag = tv4.validate(json_document, schema);
}
if (flag) {
ret_arr.push(schema_arr[x]);
}
}
if (ret_arr.length === 0) {
// currently try to find
// more compatible schema for current document
// XXX it may be need be more smart in future
// (every error has weigh, weigh depend from level...),
// may be not.
for (x = 0; x < schema_arr.length; x += 1) {
schema = schema_arr[x].schema;
if (schema !== false) {
validation = tv4.validateMultiple(json_document, schema);
errors = validation.errors;
flag = true;
for (i = 0; i < errors.length; i += 1) {
error = errors[i];
if (error.code === 0 || // INVALID_TYPE
error.code === 13 || // NOT_PASSED
error.code === 14 // BOOLEAN_SCHEMA_FALSE
) {
if (error.dataPath.split('/').length === 1) {
flag = false;
break;
}
}
if (error.code === 15 // CONST_NOT_EQUAL
) {
// take in account errors only on fist level
if (error.dataPath.split('/').length <= 2) {
flag = false;
break;
}
}
}
if (flag) {
ret_arr = [schema_arr[x]];
break;
}
}
}
}
if (ret_arr.length === 0) {
return schema_arr;
}
ret_arr[0].circular = circular;
return ret_arr;
}
return schema_arr;
}
function render_schema_selector(gadget, title, schema_arr, event, rerender) {
return RSVP.Queue()
.push(function () {
var schema_alternatives = [],
schema_item,
description,
i,
z,
type;
function generateItemsForAny(property_name, schema_path) {
var desc,
types = [
"string",
"number",
"boolean",
"array",
"object",
"null"
],
ii;
if (property_name) {
desc = property_name + " # ";
} else {
desc = "";
}
for (ii = 0; ii < types.length; ii += 1) {
schema_alternatives.push({
title: desc + types[ii],
value: {
property_name: property_name,
schema: { type: types[ii] },
schema_path: schema_path
}
});
}
}
for (i = 0; i < schema_arr.length; i += 1) {
schema_item = schema_arr[i];
description = schema_item.title;
if (schema_item.schema === true ||
!(schema_item.schema.hasOwnProperty('type') ||
schema_item.schema.hasOwnProperty('enum') ||
schema_item.schema.hasOwnProperty('const'))) {
generateItemsForAny(schema_item.property_name, schema_item.schema_path);
} else if (getDocumentType(schema_item.schema.type) === "array") {
description = description || schema_item.schema.description;
for (z = 0; z < schema_item.schema.type.length; z += 1) {
type = schema_item.schema.type[z];
schema_alternatives.push({
title: description + ' # ' + type,
value: {
type: type,
property_name: schema_item.property_name,
schema_path: schema_item.schema_path,
schema: schema_item.schema
}
});
}
} else {
description = description ||
schema_item.schema.type ||
schema_item.schema.description;
schema_alternatives.push({
title: description,
value: {
property_name: schema_item.property_name,
schema_path: schema_item.schema_path,
schema: schema_item.schema
}
});
}
}
return schema_alternatives;
})
.push(function (schema_alternatives) {
var scope = 's' + Math.random().toString(36).substr(2, 9);
if (schema_alternatives.length > 1) {
return gadget.declareGadget("../gadget_html5_select.html", {scope: scope})
.push(function (g) {
return RSVP.Queue()
.push(function () {
var x,
item_list = [[title, title]],
item;
if (rerender) {
return rerender(g, schema_alternatives);
}
for (x = 0; x < schema_alternatives.length; x += 1) {
item = schema_alternatives[x];
item_list.push([item.title, x]);
}
return {
name: scope,
editable: true,
hidden: item_list.length === 0,
value: item_list[0][1],
item_list: item_list
};
})
.push(function (render_options) {
gadget.props.add_custom_data[scope] = {
element: g.element,
event: function () {
return g.getContent()
.push(function (value) {
return event(schema_alternatives[value[scope]].value);
})
.push(function () {
if (rerender) {
return rerender(g, schema_alternatives);
}
return render_options;
})
.push(function (render_options) {
return g.render(render_options);
})
.push(function () {
return gadget.rootNotifyChange();
});
},
rerender: function () {
return RSVP.Queue()
.push(function () {
if (rerender) {
return rerender(g, schema_alternatives);
}
return render_options;
})
.push(function (render_options) {
return g.render(render_options);
});
}
};
return g.render(render_options);
})
//not need if gadget_html5_select.render return element
.push(function () {
return g.element;
});
});
}
if (schema_alternatives.length === 1) {
return RSVP.Queue()
.push(function () {
if (rerender) {
return rerender(undefined, schema_alternatives);
}
return true;
})
.push(function (ret) {
var input = document.createElement("button");
input.setAttribute("class", "ui-btn-icon-notext ui-icon-plus");
input.type = "button";
input.title = title;
if (!ret) {
input.setAttribute("style", "display: none;");
}
gadget.props.add_buttons.push({
element: input,
event: function () {
return event(schema_alternatives[0].value)
.push(function () {
if (rerender) {
return rerender(undefined, schema_alternatives);
}
return true;
})
.push(function (r) {
if (!r) {
input.setAttribute("style", "display: none;");
} else {
input.removeAttribute("style");
}
return gadget.rootNotifyChange();
});
},
rerender: function () {
return RSVP.Queue()
.push(function () {
if (rerender) {
return rerender(undefined, schema_alternatives);
}
return true;
})
.push(function (r) {
if (!r) {
input.setAttribute("style", "display: none;");
} else {
input.removeAttribute("style");
}
});
}
});
return input;
});
}
return RSVP.Queue()
.push(function () {
return document.createElement("div");
});
});
}
function render_array(gadget, schema, json_document, div_input, path, schema_path) {
var input,
is_items_arr = schema.items instanceof Array,
minItems = schema.minItems || 0;
if (json_document instanceof Array &&
json_document.length === 0) {
div_input.setAttribute("data-json-empty-array", "true");
}
function element_append(child) {
if (child) {
input.parentNode.insertBefore(child, input);
div_input.removeAttribute("data-json-empty-array");
}
}
function div_append(child) {
if (child) {
div_input.appendChild(child);
div_input.removeAttribute("data-json-empty-array");
}
}
// XXX add failback rendering if json_document not array
// input = render_textarea(schema, default_value, "array");
return RSVP.Queue()
.push(function () {
return RSVP.all([
expandItems(gadget, schema.items, schema_path + '/items', minItems),
gadget.expandSchema(schema.additionalItems, schema_path + '/additionalItems', false)
]);
})
.push(function (arr) {
var queue = RSVP.Queue(),
i,
schema_arr_arr = arr[0],
additionalItems = arr[1],
schema_arr = schema_arr_arr,
len = 0;
// XXX rewrite loading document for anyOf schema
if (json_document) {
for (i = 0; i < json_document.length; i = i + 1) {
if (is_items_arr) {
if (i < schema_arr_arr.length) {
schema_arr = schema_arr_arr[i];
} else {
schema_arr = additionalItems;
}
}
queue
.push(
addSubForm.bind(gadget, {
gadget: gadget,
parent_type: 'array',
schema_arr: schema_arr,
json_document: json_document[i],
required: i < minItems
})
)
.push(div_append);
}
len = json_document.length;
}
if (is_items_arr) {
if (minItems > len) {
for (i; i < (minItems - len); i += 1) {
if (i < schema_arr_arr.length) {
schema_arr = schema_arr_arr[i];
} else {
schema_arr = additionalItems;
}
if (!checkSchemaArrOneChoise(schema_arr)) {
break;
}
queue
.push(
addSubForm.bind(gadget, {
gadget: gadget,
parent_type: 'array',
schema_arr: schema_arr,
required: true
})
)
.push(div_append);
}
}
if (i < schema_arr_arr.length) {
schema_arr = schema_arr_arr[i];
} else {
schema_arr = additionalItems;
}
// XXX rerender on next item in schema.items
queue.push(render_schema_selector.bind(gadget,
gadget, "add item to array",
schema_arr, function (value) {
return addSubForm({
gadget: gadget,
parent_type: 'array',
type: value.type,
selected_schema: value,
schema_arr: schema_arr
})
.push(element_append);
}));
} else {
if (minItems > len && checkSchemaArrOneChoise(schema_arr)) {
for (i = 0; i < (minItems - len); i += 1) {
queue
.push(
addSubForm.bind(gadget, {
gadget: gadget,
parent_type: 'array',
schema_arr: schema_arr,
required: true
})
)
.push(div_append);
}
}
queue.push(render_schema_selector.bind(gadget,
gadget, "add item to array",
schema_arr, function (value) {
return addSubForm({
gadget: gadget,
parent_type: 'array',
type: value.type,
selected_schema: value,
schema_arr: schema_arr
})
.push(element_append);
}));
}
return queue;
})
.push(function (element) {
// var maxItems = schema.maxItems;
input = element;
// XXX update on every add/delete item
// input.hidden = maxItems !== undefined && json_document.length >= maxItems;
div_input.appendChild(input);
gadget.props.arrays[path] = div_input;
});
}
function render_field(gadget, property_name, path, schema_arr, json_document, root, options) {
var type,
div,
delete_button,
label,
label_text,
div_input,
span_info,
error_message,
input,
schema,
schema_path,
schema_ob,
first_path,
type_changed,
queue = RSVP.Queue();
if (options.selected_schema) {
schema_ob = options.selected_schema;
} else {
// XXX if (ret_arr.length > 1) notify user
schema_ob = schemaArrFilteredByDocument(schema_arr, json_document)[0];
}
schema = schema_ob.schema;
schema_path = schema_ob.schema_path;
if (schema_path === '/') {
schema_path = '';
}
options = options || {};
type = options.type;
if (path && property_name) {
first_path = path + encodeJsonPointer(property_name);
} else {
first_path = "";
}
if (schema === undefined) {
schema = getDocumentSchema(json_document);
}
if (getDocumentType(schema.type) === "string") {
type = schema.type;
} else if (type === undefined &&
json_document === undefined &&
getDocumentType(schema.type) === "array") {
type = schema.type[0];
}
if (["object", "array"].indexOf(type) >= 0 &&
!(path !== "" && json_document === undefined) &&
getDocumentType(json_document) !== type) {
if (gadget.props.saveOrigValue) {
// XXX is not useful for user
// only for tests
schema = {
const: json_document
};
} else {
gadget.props.changed = true;
}
}
if (type === undefined && json_document !== undefined) {
type = getDocumentType(json_document);
}
if (typeof type === "string") {
// it's only for simple types so we not use
// complex type detection
type_changed = json_document !== undefined &&
typeof json_document !== type;
}
// render input begin
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);
}
if (!input && schema.const !== undefined) {
input = render_const(gadget, schema, json_document);
}
if (!input && schema.enum !== undefined) {
input = render_enum(schema, json_document);
// XXX take in account existing type with enum
type_changed = false;
}
if (!input && type === "null") {
input = render_const(gadget, {const: null}, json_document);
}
if (!input && type === "boolean") {
input = render_boolean(json_document);
}
if (!input && ["string", "integer", "number", "null"].indexOf(type) >= 0) {
if (schema.contentMediaType === "text/plain") {
input = render_textarea(json_document, "string");
} else {
input = document.createElement("input");
if (json_document !== undefined) {
if (typeof json_document === "object") {
input.value = JSON.stringify(json_document);
} else {
input.value = json_document;
}
}
if (type === "integer" || type === "number") {
input.setAttribute("data-json-type", type);
if (json_document === undefined || json_document === null ||
typeof json_document === "number") {
input.type = "number";
}
if (type === "integer") {
input.setAttribute("step", "1");
if (typeof json_document === "number" &&
parseInt(json_document, 10) !== json_document) {
// original json_document contain float schema
// limit integer we can save original document
type_changed = true;
}
}
if (type === "number") {
input.setAttribute("step", "any");
}
if (schema.multipleOf && schema.multipleOf >= 0) {
input.step = schema.multipleOf;
}
if (schema.minimum &&
// step work from min value so we can't
// use min if min not multipleOf step
!(schema.multipleOf &&
(schema.minimum % schema.multipleOf) !== 0)) {
input.min = schema.minimum;
}
if (schema.maximum) {
input.max = schema.maximum;
}
} else {
input.type = "text";
if (schema.pattern) {
input.pattern = schema.pattern;
} else if (schema.minLength) {
// minLength absent in html5 so
// use pattern for this task
input.pattern = ".{" + schema.minLength + ",}";
}
if (schema.maxLength) {
input.maxLength = schema.maxLength;
}
if (schema.format === 'uri') {
input.type = "url";
input.spellcheck = false;
}
}
}
}
// render input end
// html layout render begin
div = document.createElement("div");
div.setAttribute("class", "jsonformfield ui-field-contain");
if (schema.description) {
div.title = schema.description;
}
// if (property_name && !first_path) {
if (options.delete_button === true) {
delete_button = createElement("span",
{"class": "ui-btn-icon-top ui-icon-trash-o"}
);
gadget.props.delete_button = delete_button;
div.appendChild(delete_button);
} else if (options.top !== true) {
if (options.required) {
delete_button = createElement("span",
{"class": "ui-btn-icon-top ui-icon-circle"}
);
div.appendChild(delete_button);
} else {
delete_button = createElement("span");
delete_button.innerHTML = "&emsp;";
div.appendChild(delete_button);
}
}
label_text = [property_name, schema_ob.title]
.filter(function (v) { return v; })
.join(" ")
// use non-breaking hyphen
.replace(/-/g, "");
if (property_name || options.top) {
if (options.top) {
label = document.createElement("span");
label.textContent = label_text;
root.appendChild(label);
} else {
label = document.createElement("label");
label.textContent = label_text;
div.appendChild(label);
}
}
div_input = document.createElement("div");
div_input.setAttribute("id", gadget.element.getAttribute("data-gadget-scope") + first_path + '/');
div_input.setAttribute("class", "input");
if (!input && type === "array") {
queue = render_array(
gadget,
schema,
json_document,
div_input,
first_path + '/',
schema_path
);
}
if (!input && type === "object") {
queue
.push(function () {
return render_object(
gadget,
schema,
json_document,
div_input,
first_path + '/',
schema_path
);
});
}
if (input) {
// object and array excluded from
// gadget.props.inputs not contain values
gadget.props.inputs.push(input);
input.name = first_path;
input.required = options.required;
if (type_changed) {
input.setAttribute('data-origin-value', JSON.stringify(json_document));
}
// XXX for gui
//input.setAttribute("class", "slapos-parameter");
div_input.appendChild(input);
} else {
div.setAttribute("data-parent-scope", gadget.element.getAttribute("data-gadget-scope"));
div.setAttribute("data-json-path", first_path + '/');
div.setAttribute("data-json-type", type);
if (options.required) {
div.setAttribute("data-json-required", "true");
}
}
if (schema.info !== undefined) {
span_info = document.createElement("span");
span_info.textContent = schema.info;
div_input.appendChild(span_info);
}
error_message = document.createElement("span");
error_message.setAttribute("class", "error");
error_message.hidden = true;
div_input.appendChild(error_message);
div.appendChild(div_input);
// html layout render end
return queue
.push(function () {
root.appendChild(div);
return div;
});
}
function render_object_additionalProperty(g, title, json_document, path, schema, schema_path, used, element_append) {
var div,
div_input,
input;
div = document.createElement("div");
div.setAttribute("class", "jsonformfield");
// div.title = schema.description;
div_input = document.createElement("div");
div_input.setAttribute("class", "input");
input = document.createElement("input");
input.type = "text";
input.placeholder = "name of " + title;
div_input.appendChild(input);
return g.expandSchema(schema, schema_path)
.push(function (schema_arr) {
var queue = RSVP.Queue(),
property_name;
if (checkSchemaArrOneChoise(schema_arr) && schema_arr[0].schema.title) {
title = schema_arr[0].schema.title;
// XXX change placeholder again
input.placeholder = "name of " + title;
}
for (property_name in json_document) {
if (json_document.hasOwnProperty(property_name) && !used.hasOwnProperty(property_name)) {
used[property_name] = "";
queue
.push(
addSubForm.bind(g, {
gadget: g,
property_name: property_name,
path: path,
schema_arr: schema_arr,
json_document: json_document[property_name]
})
)
.push(element_append);
}
}
queue.push(function () {
return render_schema_selector(g, "add " + title, schema_arr, function (value) {
return addSubForm({
gadget: g,
element: input,
path: path,
type: value.type,
schema_arr: [value]
})
.push(element_append);
});
});
return queue;
})
.push(function (input) {
div_input.appendChild(input);
div.appendChild(div_input);
return div;
});
}
function checkSchemaIsMetaSchema(schema) {
if (!schema) {
return false;
}
if (schema instanceof Array) {
var i,
sch;
for (i = 0; i < schema.length; i += 1) {
sch = schema[i].schema;
if (sch.hasOwnProperty("properties") &&
sch.properties.hasOwnProperty("$schema")) {
return true;
}
}
return false;
}
return schema.hasOwnProperty("properties") &&
schema.properties.hasOwnProperty("$schema");
}
function checkSchemaType(type, check) {
if (type instanceof Array) {
return type.indexOf(check) >= 0;
}
return type === check;
}
// filter property for schema editor mode
function filterPropery(property_name, current_document) {
if (current_document.hasOwnProperty("type")) {
switch (property_name) {
case "allOf":
case "anyOf":
case "oneOf":
return false;
case "additionalItems":
case "items":
case "maxItems":
case "minItems":
case "uniqueItems":
case "contains":
if (!checkSchemaType(current_document.type, "array")) {
return false;
}
break;
case "required":
case "maxProperties":
case "minProperties":
case "additionalProperties":
case "properties":
case "patternProperties":
case "dependencies":
case "propertyNames":
if (!checkSchemaType(current_document.type, "object")) {
return false;
}
break;
case "maxLength":
case "minLength":
case "pattern":
case "contentEncoding":
case "contentMediaType":
if (!checkSchemaType(current_document.type, "string")) {
return false;
}
break;
case "multipleOf":
case "maximum":
case "exclusiveMaximum":
case "minimum":
case "exclusiveMinimum":
if (!(checkSchemaType(current_document.type, "number") ||
checkSchemaType(current_document.type, "integer"))) {
return false;
}
break;
}
} else {
if (current_document.hasOwnProperty("allOf") ||
current_document.hasOwnProperty("anyOf") ||
current_document.hasOwnProperty("oneOf")) {
switch (property_name) {
case "type":
case "allOf":
case "anyOf":
case "oneOf":
return false;
}
}
switch (property_name) {
case "additionalItems":
case "items":
case "maxItems":
case "minItems":
case "uniqueItems":
case "required":
case "maxProperties":
case "minProperties":
case "additionalProperties":
case "properties":
case "patternProperties":
case "propertyNames":
case "maxLength":
case "minLength":
case "pattern":
case "multipleOf":
case "maximum":
case "exclusiveMaximum":
case "minimum":
case "exclusiveMinimum":
case "contains":
case "dependencies":
case "contentEncoding":
case "contentMediaType":
return false;
}
}
return true;
}
render_object = function (g, schema, json_document, root, path, schema_path) {
var required = schema.required || [],
schema_editor = checkSchemaIsMetaSchema(schema),
used_properties = {},
properties,
selector = {};
g.props.objects[path] = used_properties;
function element_append(child) {
if (child) {
// insert additionalProperty before selector
selector.element.parentNode.insertBefore(child, selector.element);
}
}
function root_append(child) {
root.appendChild(child);
}
if (json_document === undefined) {
json_document = {};
}
return expandProperties(g, schema.properties, schema_path + '/properties/', required)
.push(function (ret) {
var schema_arr,
q = RSVP.Queue(),
filtered_schema_arr,
key;
properties = ret;
for (key in properties) {
if (properties.hasOwnProperty(key)) {
schema_arr = properties[key];
filtered_schema_arr = schemaArrFilteredByDocument(schema_arr, json_document[key]);
// XXX need schema merge with patternProperties passed key
if (checkSchemaArrOneChoise(schema_arr)) {
if (required.indexOf(key) >= 0) {
used_properties[key] = false;
q.push(render_field.bind(g, g, key, path,
filtered_schema_arr, json_document[key], root, {required: true})
);
}
if (!used_properties.hasOwnProperty(key) &&
!schema_editor &&
(checkSchemaSimpleType(filtered_schema_arr) || !filtered_schema_arr[0].circular)
) {
used_properties[key] = false;
q.push(render_field.bind(g, g, key, path,
filtered_schema_arr, json_document[key], root, {
required: false,
delete_button: false
}));
}
}
if (!used_properties.hasOwnProperty(key) &&
json_document.hasOwnProperty(key)) {
used_properties[key] = "";
q.push(
addSubForm.bind(g, {
gadget: g,
property_name: key,
path: path,
schema_arr: filtered_schema_arr,
json_document: json_document[key]
})
)
.push(root_append);
}
}
}
return q;
})
.push(function () {
var schema_arr = convertExpandedProperties2array(properties);
return render_schema_selector(g, "add property", schema_arr, function (value) {
used_properties[value.property_name] = "";
return addSubForm({
gadget: g,
property_name: value.property_name,
path: path,
type: value.type,
schema_arr: [value]
})
.push(function (element) {
var s_e = selector.element;
if (s_e) {
s_e.parentNode.insertBefore(element, s_e);
}
});
},
function (gadget_s, schema_alternatives) {
var x,
item_list = [["add property", "add property"]],
item,
current_document = g.props.current_document;
if (schema_alternatives) {
for (x = 0; x < schema_alternatives.length; x += 1) {
item = schema_alternatives[x];
if (!used_properties.hasOwnProperty(item.value.property_name) &&
!(schema_editor && current_document &&
!filterPropery(item.value.property_name, current_document))) {
item_list.push([item.title, x]);
}
}
if (gadget_s) {
return {
name: gadget_s.element.getAttribute('data-gadget-scope'),
editable: true,
hidden: item_list.length === 1,
value: item_list[0][1],
item_list: item_list
};
}
return item_list.length > 1;
}
});
})
.push(function (element) {
selector.element = element;
return root_append(element);
})
.push(function () {
var queue = RSVP.Queue(),
key,
additionalProperties;
// XXX for pattern properties needs schemas merge for
// all passed patterns
if (schema.patternProperties !== undefined) {
for (key in schema.patternProperties) {
if (schema.patternProperties.hasOwnProperty(key)) {
if (key === ".*" ||
key === "^.*$" ||
key === ".*$" ||
key === "^.*"
) {
// additionalProperties nether used in this case
additionalProperties = false;
}
queue
.push(render_object_additionalProperty.bind(g,
g,
key + " property",
json_document,
path,
schema.patternProperties[key],
schema_path + '/patternProperties/' + key,
used_properties,
element_append
))
.push(root_append);
}
}
}
if (additionalProperties === undefined) {
if (schema.additionalProperties === undefined) {
additionalProperties = true;
} else {
additionalProperties = schema.additionalProperties;
}
}
if (additionalProperties !== false) {
queue
.push(render_object_additionalProperty.bind(g,
g,
"additional property",
json_document,
path,
additionalProperties,
schema_path + '/additionalProperties',
used_properties,
element_append
))
.push(root_append);
}
return queue;
})
.push(function () {
var key,
queue = RSVP.Queue();
for (key in json_document) {
if (json_document.hasOwnProperty(key)) {
if (!used_properties.hasOwnProperty(key)) {
queue
.push(
addSubForm.bind(g, {
gadget: g,
property_name: key,
path: path,
schema_arr: [{
schema: undefined,
schema_path: ""
}],
json_document: json_document[key]
})
)
.push(root_append);
}
}
}
return queue;
});
};
function getFormValuesAsJSONDict(g) {
var multi_level_dict = {"": {}},
is_empty = true,
scope,
options = g.props,
array,
path,
key,
i,
len,
json_dict = {},
queue = RSVP.Queue();
function convertOnMultiLevel(d, key, value) {
var ii,
kk,
key_list = key.split("/");
for (ii = 0; ii < key_list.length; ii += 1) {
kk = decodeJsonPointer(key_list[ii]);
if (ii === key_list.length - 1) {
if (value !== undefined) {
d[kk] = value;
is_empty = false;
} else {
return d[kk];
}
} else {
if (!d.hasOwnProperty(kk)) {
if (value !== undefined) {
d[kk] = {};
} else {
return;
}
}
d = d[kk];
}
}
}
function check_parent_path_not_empty(path) {
var key_list = path.split("/"),
parent_path = key_list.slice(0, key_list.length - 1).join("/");
return convertOnMultiLevel(multi_level_dict, parent_path) !== undefined;
}
function recursiveGetContent(scope, path) {
queue
.push(function () {
return g.getDeclaredGadget(scope);
})
.push(function (gadget) {
return gadget.getContent();
})
.push(function (jdict) {
if (jdict === undefined) {
return;
}
convertOnMultiLevel(multi_level_dict, path, jdict);
});
}
function getContentAndPushArray(scope, parent_path) {
queue
.push(function () {
return g.getDeclaredGadget(scope);
})
.push(function (gadget) {
return gadget.getContent();
})
.push(function (jdict) {
if (jdict === undefined) {
return;
}
var arr = convertOnMultiLevel(multi_level_dict, parent_path);
if (!(arr instanceof Array)) {
arr = [];
convertOnMultiLevel(multi_level_dict, parent_path, arr);
}
arr.push(jdict);
});
}
g.props.inputs.forEach(function (input) {
if (input.hasAttribute('data-origin-value')) {
json_dict[input.name] = JSON.parse(input.getAttribute('data-origin-value'));
} else {
if (input.value !== "") {
var type = input.getAttribute('data-json-type');
if (input.tagName === "SELECT" && input.value) {
// selection used for enums
json_dict[input.name] = JSON.parse(input.value);
} else if (type === 'number') {
json_dict[input.name] = parseFloat(input.value);
} else if (type === "integer") {
json_dict[input.name] = parseInt(input.value, 10);
} else if (type === "boolean") {
if (input.value === "true") {
json_dict[input.name] = true;
} else if (input.value === "false") {
json_dict[input.name] = false;
}
} else if (input.tagName === "TEXTAREA") {
if (input["data-format"] === "string") {
json_dict[input.name] = input.value;
} else {
json_dict[input.name] = input.value.split('\n');
}
} else {
json_dict[input.name] = input.value;
}
}
}
});
for (path in json_dict) {
if (json_dict.hasOwnProperty(path)) {
convertOnMultiLevel(multi_level_dict, path, json_dict[path]);
}
}
for (path in options.arrays) {
if (options.arrays.hasOwnProperty(path)) {
array = options.arrays[path]
.querySelectorAll("div[data-gadget-parent-scope='" + g.element.getAttribute("data-gadget-scope") + "']");
len = array.length;
if (len === 0 &&
options.arrays[path].hasAttribute('data-json-empty-array')) {
convertOnMultiLevel(multi_level_dict, path.slice(0, -1), []);
}
for (i = 0; i < len; i = i + 1) {
getContentAndPushArray(
array[i].getAttribute('data-gadget-scope'),
// slice remove concluding '/'
path.slice(0, -1)
);
}
}
}
for (path in options.objects) {
if (options.objects.hasOwnProperty(path)) {
for (key in options.objects[path]) {
if (options.objects[path].hasOwnProperty(key)) {
scope = options.objects[path][key];
if (scope) {
recursiveGetContent(scope, path + encodeJsonPointer(key));
}
}
}
}
}
return queue
.push(function () {
// set empty object/array for required properties/items
// if parent object/array existed
array = g.element
.querySelectorAll("div[data-parent-scope='" +
g.element.getAttribute("data-gadget-scope") + "']");
for (i = 0; i < array.length; i += 1) {
path = array[i].getAttribute("data-json-path").slice(0, -1);
if (check_parent_path_not_empty(path) &&
convertOnMultiLevel(multi_level_dict, path) === undefined) {
if (array[i].hasAttribute("data-json-required")) {
if (array[i].getAttribute("data-json-type") === "object") {
convertOnMultiLevel(multi_level_dict, path, {});
} else {
convertOnMultiLevel(multi_level_dict, path, []);
}
}
}
}
if (is_empty) {
switch (g.props.type) {
case "string":
return "";
case "number":
return null;
case "boolean":
return null;
case "array":
return [];
case "object":
return {};
default:
return;
}
}
return multi_level_dict[""];
});
}
function getSubGadgetElement(g, scope) {
return g.element.querySelector("div[data-gadget-scope='" + scope + "']");
}
rJS(window)
.ready(function () {
var g = this;
g.props = {};
g.options = {};
})
.declareAcquiredMethod("rootNotifyChange", "rootNotifyChange")
.declareAcquiredMethod("selfRemove", "deleteChildren")
.allowPublicAcquisition("deleteChildren", function (arr, scope) {
var g = this,
key,
i,
button_list = this.props.add_buttons,
objects = this.props.objects,
element = getSubGadgetElement(g, scope),
parent = element.getAttribute("data-json-parent"),
tasks = [];
if (objects.hasOwnProperty(parent)) {
parent = objects[parent];
for (key in parent) {
if (parent.hasOwnProperty(key) && parent[key] === scope) {
delete parent[key];
}
}
}
element.parentNode.removeChild(element);
for (key in g.props.add_custom_data) {
if (g.props.add_custom_data.hasOwnProperty(key)) {
tasks.push(g.props.add_custom_data[key].rerender());
}
}
for (i = 0; i < button_list.length; i = i + 1) {
tasks.push(button_list[i].rerender());
}
tasks.push(g.rootNotifyChange());
return RSVP.Queue()
.push(function () {
return RSVP.all(tasks);
});
})
.declareMethod('getElementByPath', function (data_path) {
var g = this,
array,
path,
scope,
key,
next_data_path,
slash_count = 0,
slash_count_next,
bingo,
idx,
options = g.props;
if (data_path !== "/") {
for (path in options.arrays) {
if (options.arrays.hasOwnProperty(path) && data_path.startsWith(path)) {
slash_count_next = path.split("/").length - 1;
if (slash_count_next > slash_count) {
bingo = path;
slash_count = slash_count_next;
}
}
}
if (bingo) {
array = options.arrays[bingo]
.querySelectorAll("div[data-gadget-parent-scope='" + g.element.getAttribute("data-gadget-scope") + "']");
next_data_path = data_path.slice(bingo.length).split("/");
idx = next_data_path[0];
next_data_path = "/" + next_data_path.slice(1).join("/");
return g.getDeclaredGadget(array[idx].getAttribute('data-gadget-scope'))
.push(function (gadget) {
return gadget.getElementByPath(next_data_path);
});
}
slash_count = 0;
for (path in options.objects) {
if (options.objects.hasOwnProperty(path) && data_path.startsWith(path)) {
slash_count_next = path.split("/").length - 1;
if (slash_count_next > slash_count) {
bingo = path;
slash_count = slash_count_next;
}
}
}
if (bingo) {
path = options.objects[bingo];
key = decodeJsonPointer(data_path.slice(bingo.length).split('/')[0]);
if (path.hasOwnProperty(key)) {
next_data_path = data_path.slice(bingo.length + encodeJsonPointer(key).length);
if (!next_data_path) {
next_data_path = "/";
}
scope = path[key];
}
}
if (scope === false) {
// gadget for this element absent
// so find element in current gadget
return document.getElementById(
g.element.getAttribute("data-gadget-scope") + bingo + key + '/'
);
}
if (scope) {
// get gadget by scope and use relative path for find element in gadget
return g.getDeclaredGadget(scope)
.push(function (gadget) {
return gadget.getElementByPath(next_data_path);
});
}
}
return RSVP.Queue()
.push(function () {
return document.getElementById(
g.element.getAttribute("data-gadget-scope") + data_path
);
});
})
.declareAcquiredMethod("notifyValid", "notifyValid")
.declareAcquiredMethod("notifyInvalid", "notifyInvalid")
.declareAcquiredMethod("checkValidity", "checkValidity")
.allowPublicAcquisition("notifyValid", function () {
return true;
})
.allowPublicAcquisition("notifyChange", function (arr, sub_scope) {
var g = this,
opt = arr[0],
event_object;
event_object = g.props.add_custom_data[sub_scope];
if (event_object && opt.type === "change") {
return event_object.event();
}
return g.rootNotifyChange();
})
.declareMethod('renderForm', function (options) {
var g = this,
property_name = g.element.getAttribute('data-json-property-name'),
schema = options.schema_arr !== undefined && options.schema_arr[0].schema,
root;
g.props.changed = false;
g.props.saveOrigValue = options.saveOrigValue;
g.props.inputs = [];
g.props.add_buttons = [];
g.props.add_custom_data = {};
g.props.arrays = {};
g.props.objects = {};
g.props.path = options.path; // self gadget scope
if (!property_name || !options.display_label) {
property_name = "";
}
root = g.element.querySelector('[data-json-path="/"]');
if (!root) {
root = g.element;
}
if (options.delete_button === undefined) {
if (options.top) {
options.delete_button = false;
} else {
options.delete_button = !options.required;
}
}
if (!options.type && schema && !schema.type) {
options.type = guessSchemaType(schema);
}
// used for empty document generation
g.props.type = (schema && typeof schema.type === "string" && schema.type) ||
options.type || getDocumentType(options.document);
while (root.firstChild) {
root.removeChild(root.firstChild);
}
if (checkSchemaIsMetaSchema(schema)) {
g.props.updatePropertySelectors = true;
g.props.current_document = options.document;
}
return render_field(g, property_name, "", options.schema_arr,
options.document, root,
{
type: options.type,
selected_schema: options.selected_schema,
required: options.required,
delete_button: options.delete_button,
top: options.top
})
.push(function () {
return g.element;
});
})
.declareAcquiredMethod("expandSchema", "expandSchema")
.onEvent('click', function (evt) {
if (evt.target === this.props.delete_button) {
return this.selfRemove(evt);
}
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;
return;
}
for (i = 0; i < button_list.length; i = i + 1) {
if (evt.target === button_list[i].element) {
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) {
var gadget = this,
field_list = this.props.inputs,
i,
input,
changed = false;
// on form data field
for (i = 0; i < field_list.length; i = i + 1) {
if (evt.target === field_list[i]) {
input = evt.target;
if (input.hasAttribute('data-origin-value')) {
input.removeAttribute('data-origin-value');
}
if (!input.hasAttribute("type")) {
if (["integer", "number"]
.indexOf(input.getAttribute('data-json-type')) >= 0) {
input.type = "number";
}
}
changed = true;
}
}
if (changed) {
return gadget.rootNotifyChange();
}
})
.declareMethod('getContent', function () {
var g = this;
return getFormValuesAsJSONDict(g)
.push(function (data) {
if (g.props.updatePropertySelectors) {
g.props.current_document = data;
var key,
tasks = [];
for (key in g.props.add_custom_data) {
if (g.props.add_custom_data.hasOwnProperty(key)) {
tasks.push(g.props.add_custom_data[key].rerender());
}
}
if (tasks.length > 0) {
return RSVP.Queue()
.push(function () {
return RSVP.all(tasks);
})
.push(function () {
return data;
});
}
}
return data;
});
});
}(window, document, location, rJS, RSVP, tv4));
\ No newline at end of file
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="File" module="OFS.Image"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>__name__</string> </key>
<value> <string>gadget_json_generated_form_child.js</string> </value>
</item>
<item>
<key> <string>content_type</string> </key>
<value> <string>application/javascript</string> </value>
</item>
<item>
<key> <string>precondition</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>gadget_json_generated_form_child.js</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
/*
Author: Geraint Luff and others
Year: 2013
This code is released into the "public domain" by its author(s). Anybody may use, alter and distribute the code without restriction. The author makes no guarantees, and takes no liability of any kind for use of this code.
If you find a bug or make an improvement, it would be courteous to let the author know, but it is not compulsory.
*/
/*global module, define*/
/*jslint indent: 2, white: true*/
/*jshint -W014: true, -W089: true, -W084: true, -W069: true*/
(function (global, factory) {
"use strict";
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module !== 'undefined' && module.exports){
// CommonJS. Define export.
module.exports = factory();
} else {
// Browser globals
global.tv4 = factory();
}
}(this, function () {
"use strict";
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FObject%2Fkeys
if (!Object.keys) {
Object.keys = (function () {
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;
return function (obj) {
if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null) {
throw new TypeError('Object.keys called on non-object');
}
var result = [];
for (var prop in obj) {
if (hasOwnProperty.call(obj, prop)) {
result.push(prop);
}
}
if (hasDontEnumBug) {
for (var i=0; i < dontEnumsLength; i++) {
if (hasOwnProperty.call(obj, dontEnums[i])) {
result.push(dontEnums[i]);
}
}
}
return result;
};
})();
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create
if (!Object.create) {
Object.create = (function(){
function F(){}
return function(o){
if (arguments.length !== 1) {
throw new Error('Object.create implementation only accepts one parameter.');
}
F.prototype = o;
return new F();
};
})();
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FArray%2FisArray
if(!Array.isArray) {
Array.isArray = function (vArg) {
return Object.prototype.toString.call(vArg) === "[object Array]";
};
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FArray%2FindexOf
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
/*jslint bitwise: true */
if (this === null) {
throw new TypeError();
}
var t = Object(this);
var len = t.length >>> 0;
if (len === 0) {
return -1;
}
var n = 0;
if (arguments.length > 1) {
n = Number(arguments[1]);
if (n !== n) { // shortcut for verifying if it's NaN
n = 0;
} else if (n !== 0 && n !== Infinity && n !== -Infinity) {
n = (n > 0 || -1) * Math.floor(Math.abs(n));
}
}
if (n >= len) {
return -1;
}
var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
for (; k < len; k++) {
if (k in t && t[k] === searchElement) {
return k;
}
}
return -1;
};
}
// Grungey Object.isFrozen hack
if (!Object.isFrozen) {
Object.isFrozen = function (obj) {
var key = "tv4_test_frozen_key";
while (obj.hasOwnProperty(key)) {
key += Math.random();
}
try {
obj[key] = true;
delete obj[key];
return false;
} catch (e) {
return true;
}
};
}
// Based on: https://github.com/geraintluff/uri-templates, but with all the de-substitution stuff removed
var uriTemplateGlobalModifiers = {
"+": true,
"#": true,
".": true,
"/": true,
";": true,
"?": true,
"&": true
};
var uriTemplateSuffices = {
"*": true
};
function notReallyPercentEncode(string) {
return encodeURI(string).replace(/%25[0-9][0-9]/g, function (doubleEncoded) {
return "%" + doubleEncoded.substring(3);
});
}
function uriTemplateSubstitution(spec) {
var modifier = "";
if (uriTemplateGlobalModifiers[spec.charAt(0)]) {
modifier = spec.charAt(0);
spec = spec.substring(1);
}
var separator = "";
var prefix = "";
var shouldEscape = true;
var showVariables = false;
var trimEmptyString = false;
if (modifier === '+') {
shouldEscape = false;
} else if (modifier === ".") {
prefix = ".";
separator = ".";
} else if (modifier === "/") {
prefix = "/";
separator = "/";
} else if (modifier === '#') {
prefix = "#";
shouldEscape = false;
} else if (modifier === ';') {
prefix = ";";
separator = ";";
showVariables = true;
trimEmptyString = true;
} else if (modifier === '?') {
prefix = "?";
separator = "&";
showVariables = true;
} else if (modifier === '&') {
prefix = "&";
separator = "&";
showVariables = true;
}
var varNames = [];
var varList = spec.split(",");
var varSpecs = [];
var varSpecMap = {};
for (var i = 0; i < varList.length; i++) {
var varName = varList[i];
var truncate = null;
if (varName.indexOf(":") !== -1) {
var parts = varName.split(":");
varName = parts[0];
truncate = parseInt(parts[1], 10);
}
var suffices = {};
while (uriTemplateSuffices[varName.charAt(varName.length - 1)]) {
suffices[varName.charAt(varName.length - 1)] = true;
varName = varName.substring(0, varName.length - 1);
}
var varSpec = {
truncate: truncate,
name: varName,
suffices: suffices
};
varSpecs.push(varSpec);
varSpecMap[varName] = varSpec;
varNames.push(varName);
}
var subFunction = function (valueFunction) {
var result = "";
var startIndex = 0;
for (var i = 0; i < varSpecs.length; i++) {
var varSpec = varSpecs[i];
var value = valueFunction(varSpec.name);
if (value === null || value === undefined || (Array.isArray(value) && value.length === 0) || (typeof value === 'object' && Object.keys(value).length === 0)) {
startIndex++;
continue;
}
if (i === startIndex) {
result += prefix;
} else {
result += (separator || ",");
}
if (Array.isArray(value)) {
if (showVariables) {
result += varSpec.name + "=";
}
for (var j = 0; j < value.length; j++) {
if (j > 0) {
result += varSpec.suffices['*'] ? (separator || ",") : ",";
if (varSpec.suffices['*'] && showVariables) {
result += varSpec.name + "=";
}
}
result += shouldEscape ? encodeURIComponent(value[j]).replace(/!/g, "%21") : notReallyPercentEncode(value[j]);
}
} else if (typeof value === "object") {
if (showVariables && !varSpec.suffices['*']) {
result += varSpec.name + "=";
}
var first = true;
for (var key in value) {
if (!first) {
result += varSpec.suffices['*'] ? (separator || ",") : ",";
}
first = false;
result += shouldEscape ? encodeURIComponent(key).replace(/!/g, "%21") : notReallyPercentEncode(key);
result += varSpec.suffices['*'] ? '=' : ",";
result += shouldEscape ? encodeURIComponent(value[key]).replace(/!/g, "%21") : notReallyPercentEncode(value[key]);
}
} else {
if (showVariables) {
result += varSpec.name;
if (!trimEmptyString || value !== "") {
result += "=";
}
}
if (varSpec.truncate !== null) {
value = value.substring(0, varSpec.truncate);
}
result += shouldEscape ? encodeURIComponent(value).replace(/!/g, "%21"): notReallyPercentEncode(value);
}
}
return result;
};
subFunction.varNames = varNames;
return {
prefix: prefix,
substitution: subFunction
};
}
function UriTemplate(template) {
if (!(this instanceof UriTemplate)) {
return new UriTemplate(template);
}
var parts = template.split("{");
var textParts = [parts.shift()];
var prefixes = [];
var substitutions = [];
var varNames = [];
while (parts.length > 0) {
var part = parts.shift();
var spec = part.split("}")[0];
var remainder = part.substring(spec.length + 1);
var funcs = uriTemplateSubstitution(spec);
substitutions.push(funcs.substitution);
prefixes.push(funcs.prefix);
textParts.push(remainder);
varNames = varNames.concat(funcs.substitution.varNames);
}
this.fill = function (valueFunction) {
var result = textParts[0];
for (var i = 0; i < substitutions.length; i++) {
var substitution = substitutions[i];
result += substitution(valueFunction);
result += textParts[i + 1];
}
return result;
};
this.varNames = varNames;
this.template = template;
}
UriTemplate.prototype = {
toString: function () {
return this.template;
},
fillFromObject: function (obj) {
return this.fill(function (varName) {
return obj[varName];
});
}
};
var ValidatorContext = function ValidatorContext(parent, collectMultiple, errorReporter, checkRecursive, trackUnknownProperties) {
this.missing = [];
this.missingMap = {};
this.formatValidators = parent ? Object.create(parent.formatValidators) : {};
this.schemas = parent ? Object.create(parent.schemas) : {};
this.collectMultiple = collectMultiple;
this.errors = [];
this.handleError = collectMultiple ? this.collectError : this.returnError;
if (checkRecursive) {
this.checkRecursive = true;
this.scanned = [];
this.scannedFrozen = [];
this.scannedFrozenSchemas = [];
this.scannedFrozenValidationErrors = [];
this.validatedSchemasKey = 'tv4_validation_id';
this.validationErrorsKey = 'tv4_validation_errors_id';
}
if (trackUnknownProperties) {
this.trackUnknownProperties = true;
this.knownPropertyPaths = {};
this.unknownPropertyPaths = {};
}
this.errorReporter = errorReporter || defaultErrorReporter('en');
if (typeof this.errorReporter === 'string') {
throw new Error('debug');
}
this.definedKeywords = {};
if (parent) {
for (var key in parent.definedKeywords) {
this.definedKeywords[key] = parent.definedKeywords[key].slice(0);
}
}
};
ValidatorContext.prototype.defineKeyword = function (keyword, keywordFunction) {
this.definedKeywords[keyword] = this.definedKeywords[keyword] || [];
this.definedKeywords[keyword].push(keywordFunction);
};
ValidatorContext.prototype.createError = function (code, messageParams, dataPath, schemaPath, subErrors, data, schema) {
var error = new ValidationError(code, messageParams, dataPath, schemaPath, subErrors);
error.message = this.errorReporter(error, data, schema);
return error;
};
ValidatorContext.prototype.returnError = function (error) {
return error;
};
ValidatorContext.prototype.collectError = function (error) {
if (error) {
this.errors.push(error);
}
return null;
};
ValidatorContext.prototype.prefixErrors = function (startIndex, dataPath, schemaPath) {
for (var i = startIndex; i < this.errors.length; i++) {
this.errors[i] = this.errors[i].prefixWith(dataPath, schemaPath);
}
return this;
};
ValidatorContext.prototype.banUnknownProperties = function (data, schema) {
for (var unknownPath in this.unknownPropertyPaths) {
var error = this.createError(ErrorCodes.UNKNOWN_PROPERTY, {path: unknownPath}, unknownPath, "", null, data, schema);
var result = this.handleError(error);
if (result) {
return result;
}
}
return null;
};
ValidatorContext.prototype.addFormat = function (format, validator) {
if (typeof format === 'object') {
for (var key in format) {
this.addFormat(key, format[key]);
}
return this;
}
this.formatValidators[format] = validator;
};
ValidatorContext.prototype.resolveRefs = function (schema, urlHistory) {
if (schema['$ref'] !== undefined) {
urlHistory = urlHistory || {};
if (urlHistory[schema['$ref']]) {
return this.createError(ErrorCodes.CIRCULAR_REFERENCE, {urls: Object.keys(urlHistory).join(', ')}, '', '', null, undefined, schema);
}
urlHistory[schema['$ref']] = true;
schema = this.getSchema(schema['$ref'], urlHistory);
}
return schema;
};
ValidatorContext.prototype.getSchema = function (url, urlHistory) {
var schema;
if (this.schemas[url] !== undefined) {
schema = this.schemas[url];
return this.resolveRefs(schema, urlHistory);
}
var baseUrl = url;
var fragment = "";
if (url.indexOf('#') !== -1) {
fragment = url.substring(url.indexOf("#") + 1);
baseUrl = url.substring(0, url.indexOf("#"));
}
if (typeof this.schemas[baseUrl] === 'object') {
schema = this.schemas[baseUrl];
var pointerPath = decodeURIComponent(fragment);
if (pointerPath === "") {
return this.resolveRefs(schema, urlHistory);
} else if (pointerPath.charAt(0) !== "/") {
return undefined;
}
var parts = pointerPath.split("/").slice(1);
for (var i = 0; i < parts.length; i++) {
var component = parts[i].replace(/~1/g, "/").replace(/~0/g, "~");
if (schema[component] === undefined) {
schema = undefined;
break;
}
schema = schema[component];
}
if (schema !== undefined) {
return this.resolveRefs(schema, urlHistory);
}
}
if (this.missing[baseUrl] === undefined) {
this.missing.push(baseUrl);
this.missing[baseUrl] = baseUrl;
this.missingMap[baseUrl] = baseUrl;
}
};
ValidatorContext.prototype.searchSchemas = function (schema, url) {
if (Array.isArray(schema)) {
for (var i = 0; i < schema.length; i++) {
this.searchSchemas(schema[i], url);
}
} else if (schema && typeof schema === "object") {
if (typeof schema.id === "string") {
if (isTrustedUrl(url, schema.id)) {
if (this.schemas[schema.id] === undefined) {
this.schemas[schema.id] = schema;
}
}
}
for (var key in schema) {
if (key !== "enum") {
if (typeof schema[key] === "object") {
this.searchSchemas(schema[key], url);
} else if (key === "$ref") {
var uri = getDocumentUri(schema[key]);
if (uri && this.schemas[uri] === undefined && this.missingMap[uri] === undefined) {
this.missingMap[uri] = uri;
}
}
}
}
}
};
ValidatorContext.prototype.addSchema = function (url, schema) {
//overload
if (typeof url !== 'string' || typeof schema === 'undefined') {
if (typeof url === 'object' && typeof url.id === 'string') {
schema = url;
url = schema.id;
}
else {
return;
}
}
if (url === getDocumentUri(url) + "#") {
// Remove empty fragment
url = getDocumentUri(url);
}
this.schemas[url] = schema;
delete this.missingMap[url];
// schema normalisation and downloading are disabled because already done by us.
// and current realisation not support CIRCULAR_REFERENCE
// normSchema(schema, url);
// this.searchSchemas(schema, url);
};
ValidatorContext.prototype.getSchemaMap = function () {
var map = {};
for (var key in this.schemas) {
map[key] = this.schemas[key];
}
return map;
};
ValidatorContext.prototype.getSchemaUris = function (filterRegExp) {
var list = [];
for (var key in this.schemas) {
if (!filterRegExp || filterRegExp.test(key)) {
list.push(key);
}
}
return list;
};
ValidatorContext.prototype.getMissingUris = function (filterRegExp) {
var list = [];
for (var key in this.missingMap) {
if (!filterRegExp || filterRegExp.test(key)) {
list.push(key);
}
}
return list;
};
ValidatorContext.prototype.dropSchemas = function () {
this.schemas = {};
this.reset();
};
ValidatorContext.prototype.reset = function () {
this.missing = [];
this.missingMap = {};
this.errors = [];
};
ValidatorContext.prototype.validateAllValidators = function validateAllValidators(data, schema, dataPointerPath) {
return this.validateBasic(data, schema, dataPointerPath)
|| this.validateNumeric(data, schema, dataPointerPath)
|| this.validateString(data, schema, dataPointerPath)
|| this.validateArray(data, schema, dataPointerPath)
|| this.validateObject(data, schema, dataPointerPath)
|| this.validateCombinations(data, schema, dataPointerPath)
|| this.validateHypermedia(data, schema, dataPointerPath)
|| this.validateFormat(data, schema, dataPointerPath)
|| this.validateDefinedKeywords(data, schema, dataPointerPath)
|| null;
};
ValidatorContext.prototype.validateAll = function (data, schema, dataPathParts, schemaPathParts, dataPointerPath) {
var topLevel;
if (schema === undefined || schema === true) {
return null;
} else if (schema instanceof ValidationError) {
this.errors.push(schema);
return schema;
}
var startErrorCount = this.errors.length;
var frozenIndex, scannedFrozenSchemaIndex = null, scannedSchemasIndex = null;
if (this.checkRecursive && data && typeof data === 'object') {
topLevel = !this.scanned.length;
if (data[this.validatedSchemasKey]) {
var schemaIndex = data[this.validatedSchemasKey].indexOf(schema);
if (schemaIndex !== -1) {
this.errors = this.errors.concat(data[this.validationErrorsKey][schemaIndex]);
return null;
}
}
if (Object.isFrozen(data)) {
frozenIndex = this.scannedFrozen.indexOf(data);
if (frozenIndex !== -1) {
var frozenSchemaIndex = this.scannedFrozenSchemas[frozenIndex].indexOf(schema);
if (frozenSchemaIndex !== -1) {
this.errors = this.errors.concat(this.scannedFrozenValidationErrors[frozenIndex][frozenSchemaIndex]);
return null;
}
}
}
this.scanned.push(data);
if (Object.isFrozen(data)) {
if (frozenIndex === -1) {
frozenIndex = this.scannedFrozen.length;
this.scannedFrozen.push(data);
this.scannedFrozenSchemas.push([]);
}
scannedFrozenSchemaIndex = this.scannedFrozenSchemas[frozenIndex].length;
this.scannedFrozenSchemas[frozenIndex][scannedFrozenSchemaIndex] = schema;
this.scannedFrozenValidationErrors[frozenIndex][scannedFrozenSchemaIndex] = [];
} else {
if (!data[this.validatedSchemasKey]) {
try {
Object.defineProperty(data, this.validatedSchemasKey, {
value: [],
configurable: true
});
Object.defineProperty(data, this.validationErrorsKey, {
value: [],
configurable: true
});
} catch (e) {
//IE 7/8 workaround
data[this.validatedSchemasKey] = [];
data[this.validationErrorsKey] = [];
}
}
scannedSchemasIndex = data[this.validatedSchemasKey].length;
data[this.validatedSchemasKey][scannedSchemasIndex] = schema;
data[this.validationErrorsKey][scannedSchemasIndex] = [];
}
}
var errorCount = this.errors.length;
var error = this.validateAllValidators(data, schema, dataPointerPath);
if (topLevel) {
while (this.scanned.length) {
var item = this.scanned.pop();
delete item[this.validatedSchemasKey];
}
this.scannedFrozen = [];
this.scannedFrozenSchemas = [];
}
if (error || errorCount !== this.errors.length) {
while ((dataPathParts && dataPathParts.length) || (schemaPathParts && schemaPathParts.length)) {
var dataPart = (dataPathParts && dataPathParts.length) ? "" + dataPathParts.pop() : null;
var schemaPart = (schemaPathParts && schemaPathParts.length) ? "" + schemaPathParts.pop() : null;
if (error) {
error = error.prefixWith(dataPart, schemaPart);
}
this.prefixErrors(errorCount, dataPart, schemaPart);
}
}
if (scannedFrozenSchemaIndex !== null) {
this.scannedFrozenValidationErrors[frozenIndex][scannedFrozenSchemaIndex] = this.errors.slice(startErrorCount);
} else if (scannedSchemasIndex !== null) {
data[this.validationErrorsKey][scannedSchemasIndex] = this.errors.slice(startErrorCount);
}
return this.handleError(error);
};
ValidatorContext.prototype.validateFormat = function (data, schema) {
if (typeof schema.format !== 'string' || !this.formatValidators[schema.format]) {
return null;
}
var errorMessage = this.formatValidators[schema.format].call(null, data, schema);
if (typeof errorMessage === 'string' || typeof errorMessage === 'number') {
return this.createError(ErrorCodes.FORMAT_CUSTOM, {message: errorMessage}, '', '/format', null, data, schema);
} else if (errorMessage && typeof errorMessage === 'object') {
return this.createError(ErrorCodes.FORMAT_CUSTOM, {message: errorMessage.message || "?"}, errorMessage.dataPath || '', errorMessage.schemaPath || "/format", null, data, schema);
}
return null;
};
ValidatorContext.prototype.validateDefinedKeywords = function (data, schema, dataPointerPath) {
for (var key in this.definedKeywords) {
if (typeof schema[key] === 'undefined') {
continue;
}
var validationFunctions = this.definedKeywords[key];
for (var i = 0; i < validationFunctions.length; i++) {
var func = validationFunctions[i];
var result = func(data, schema[key], schema, dataPointerPath);
if (typeof result === 'string' || typeof result === 'number') {
return this.createError(ErrorCodes.KEYWORD_CUSTOM, {key: key, message: result}, '', '', null, data, schema).prefixWith(null, key);
} else if (result && typeof result === 'object') {
var code = result.code;
if (typeof code === 'string') {
if (!ErrorCodes[code]) {
throw new Error('Undefined error code (use defineError): ' + code);
}
code = ErrorCodes[code];
} else if (typeof code !== 'number') {
code = ErrorCodes.KEYWORD_CUSTOM;
}
var messageParams = (typeof result.message === 'object') ? result.message : {key: key, message: result.message || "?"};
var schemaPath = result.schemaPath || ("/" + key.replace(/~/g, '~0').replace(/\//g, '~1'));
return this.createError(code, messageParams, result.dataPath || null, schemaPath, null, data, schema);
}
}
}
return null;
};
function recursiveCompare(A, B) {
if (A === B) {
return true;
}
if (A && B && typeof A === "object" && typeof B === "object") {
if (Array.isArray(A) !== Array.isArray(B)) {
return false;
} else if (Array.isArray(A)) {
if (A.length !== B.length) {
return false;
}
for (var i = 0; i < A.length; i++) {
if (!recursiveCompare(A[i], B[i])) {
return false;
}
}
} else {
var key;
for (key in A) {
if (B[key] === undefined && A[key] !== undefined) {
return false;
}
}
for (key in B) {
if (A[key] === undefined && B[key] !== undefined) {
return false;
}
}
for (key in A) {
if (!recursiveCompare(A[key], B[key])) {
return false;
}
}
}
return true;
}
return false;
}
ValidatorContext.prototype.validateBasic = function validateBasic(data, schema, dataPointerPath) {
if (schema === false && data !== undefined) {
return this.createError(ErrorCodes.BOOLEAN_SCHEMA_FALSE, {}, '', '', null, data, schema);
}
var error;
if (error = this.validateType(data, schema, dataPointerPath)) {
return error.prefixWith(null, "type");
}
if (error = this.validateConst(data, schema, dataPointerPath)) {
return error.prefixWith(null, "const");
}
if (error = this.validateEnum(data, schema, dataPointerPath)) {
return error.prefixWith(null, "type");
}
return null;
};
ValidatorContext.prototype.validateType = function validateType(data, schema) {
if (schema.type === undefined) {
return null;
}
var dataType = typeof data;
if (data === null) {
dataType = "null";
} else if (Array.isArray(data)) {
dataType = "array";
}
var allowedTypes = schema.type;
if (!Array.isArray(allowedTypes)) {
allowedTypes = [allowedTypes];
}
for (var i = 0; i < allowedTypes.length; i++) {
var type = allowedTypes[i];
if (type === dataType || (type === "integer" && dataType === "number" && (data % 1 === 0))) {
return null;
}
}
return this.createError(ErrorCodes.INVALID_TYPE, {type: dataType, expected: allowedTypes.join("/")}, '', '', null, data, schema);
};
ValidatorContext.prototype.validateConst = function validateConst(data, schema) {
if (schema.const === undefined ||
recursiveCompare(data, schema.const)) {
return null;
}
return this.createError(ErrorCodes.CONST_NOT_EQUAL, {}, '', '', null, data, schema);
};
ValidatorContext.prototype.validateEnum = function validateEnum(data, schema) {
if (schema["enum"] === undefined) {
return null;
}
for (var i = 0; i < schema["enum"].length; i++) {
var enumVal = schema["enum"][i];
if (recursiveCompare(data, enumVal)) {
return null;
}
}
return this.createError(ErrorCodes.ENUM_MISMATCH, {value: (typeof JSON !== 'undefined') ? JSON.stringify(data) : data}, '', '', null, data, schema);
};
ValidatorContext.prototype.validateNumeric = function validateNumeric(data, schema, dataPointerPath) {
return this.validateMultipleOf(data, schema, dataPointerPath)
|| this.validateMinMax(data, schema, dataPointerPath)
|| this.validateNaN(data, schema, dataPointerPath)
|| null;
};
var CLOSE_ENOUGH_LOW = Math.pow(2, -51);
var CLOSE_ENOUGH_HIGH = 1 - CLOSE_ENOUGH_LOW;
ValidatorContext.prototype.validateMultipleOf = function validateMultipleOf(data, schema) {
var multipleOf = schema.multipleOf || schema.divisibleBy;
if (multipleOf === undefined) {
return null;
}
if (typeof data === "number") {
var remainder = (data/multipleOf)%1;
if (remainder >= CLOSE_ENOUGH_LOW && remainder < CLOSE_ENOUGH_HIGH) {
return this.createError(ErrorCodes.NUMBER_MULTIPLE_OF, {value: data, multipleOf: multipleOf}, '', '', null, data, schema);
}
}
return null;
};
ValidatorContext.prototype.validateMinMax = function validateMinMax(data, schema) {
if (typeof data !== "number") {
return null;
}
if (schema.minimum !== undefined) {
if (data < schema.minimum) {
return this.createError(ErrorCodes.NUMBER_MINIMUM, {value: data, minimum: schema.minimum}, '', '/minimum', null, data, schema);
}
if (schema.exclusiveMinimum === true && data === schema.minimum) {
return this.createError(ErrorCodes.NUMBER_MINIMUM_EXCLUSIVE, {value: data, minimum: schema.minimum}, '', '/exclusiveMinimum', null, data, schema);
}
}
if ((typeof schema.exclusiveMinimum === "number") && data <= schema.exclusiveMinimum) {
return this.createError(ErrorCodes.NUMBER_MINIMUM_EXCLUSIVE, {value: data, minimum: schema.exclusiveMinimum}, '', '/exclusiveMinimum', null, data, schema);
}
if (schema.maximum !== undefined) {
if (data > schema.maximum) {
return this.createError(ErrorCodes.NUMBER_MAXIMUM, {value: data, maximum: schema.maximum}, '', '/maximum', null, data, schema);
}
if (schema.exclusiveMaximum === true && data === schema.maximum) {
return this.createError(ErrorCodes.NUMBER_MAXIMUM_EXCLUSIVE, {value: data, maximum: schema.maximum}, '', '/exclusiveMaximum', null, data, schema);
}
}
if ((typeof schema.exclusiveMaximum === "number") && data >= schema.exclusiveMaximum) {
return this.createError(ErrorCodes.NUMBER_MAXIMUM_EXCLUSIVE, {value: data, maximum: schema.exclusiveMaximum}, '', '/exclusiveMaximum', null, data, schema);
}
return null;
};
ValidatorContext.prototype.validateNaN = function validateNaN(data, schema) {
if (typeof data !== "number") {
return null;
}
if (isNaN(data) === true || data === Infinity || data === -Infinity) {
return this.createError(ErrorCodes.NUMBER_NOT_A_NUMBER, {value: data}, '', '/type', null, data, schema);
}
return null;
};
ValidatorContext.prototype.validateString = function validateString(data, schema, dataPointerPath) {
return this.validateStringLength(data, schema, dataPointerPath)
|| this.validateStringPattern(data, schema, dataPointerPath)
|| null;
};
ValidatorContext.prototype.validateStringLength = function validateStringLength(data, schema) {
if (typeof data !== "string") {
return null;
}
if (schema.minLength !== undefined) {
if (data.length < schema.minLength) {
return this.createError(ErrorCodes.STRING_LENGTH_SHORT, {length: data.length, minimum: schema.minLength}, '', '/minLength', null, data, schema);
}
}
if (schema.maxLength !== undefined) {
if (data.length > schema.maxLength) {
return this.createError(ErrorCodes.STRING_LENGTH_LONG, {length: data.length, maximum: schema.maxLength}, '', '/maxLength', null, data, schema);
}
}
return null;
};
ValidatorContext.prototype.validateStringPattern = function validateStringPattern(data, schema) {
if (typeof data !== "string" || (typeof schema.pattern !== "string" && !(schema.pattern instanceof RegExp))) {
return null;
}
var regexp;
if (schema.pattern instanceof RegExp) {
regexp = schema.pattern;
}
else {
var body, flags = '';
// Check for regular expression literals
// @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.5
var literal = schema.pattern.match(/^\/(.+)\/([img]*)$/);
if (literal) {
body = literal[1];
flags = literal[2];
}
else {
body = schema.pattern;
}
regexp = new RegExp(body, flags);
}
if (!regexp.test(data)) {
return this.createError(ErrorCodes.STRING_PATTERN, {pattern: schema.pattern}, '', '/pattern', null, data, schema);
}
return null;
};
ValidatorContext.prototype.validateArray = function validateArray(data, schema, dataPointerPath) {
if (!Array.isArray(data)) {
return null;
}
return this.validateArrayLength(data, schema, dataPointerPath)
|| this.validateArrayUniqueItems(data, schema, dataPointerPath)
|| this.validateArrayContains(data, schema, dataPointerPath)
|| this.validateArrayItems(data, schema, dataPointerPath)
|| null;
};
ValidatorContext.prototype.validateArrayLength = function validateArrayLength(data, schema) {
var error;
if (schema.minItems !== undefined) {
if (data.length < schema.minItems) {
error = this.createError(ErrorCodes.ARRAY_LENGTH_SHORT, {length: data.length, minimum: schema.minItems}, '', '/minItems', null, data, schema);
if (this.handleError(error)) {
return error;
}
}
}
if (schema.maxItems !== undefined) {
if (data.length > schema.maxItems) {
error = this.createError(ErrorCodes.ARRAY_LENGTH_LONG, {length: data.length, maximum: schema.maxItems}, '', '/maxItems', null, data, schema);
if (this.handleError(error)) {
return error;
}
}
}
return null;
};
ValidatorContext.prototype.validateArrayContains = function validateArrayContains(data, schema, dataPointerPath) {
if ((schema.contains === true && data.length > 0) || schema.contains === undefined) {
return null;
}
var error;
if (data.length === 0 || schema.contains === false) {
error = true;
} else {
for (var i = 0; i < data.length; i++) {
error = this.validateAllValidators(data[i], schema.contains, dataPointerPath + "/" + i);
if (!error) {
return null;
}
}
error = true;
}
if (error) {
return this.createError(ErrorCodes.ARRAY_CONTAINS, {}, '', '/contains', null, data, schema);
}
return null;
};
ValidatorContext.prototype.validateArrayUniqueItems = function validateArrayUniqueItems(data, schema) {
if (schema.uniqueItems) {
for (var i = 0; i < data.length; i++) {
for (var j = i + 1; j < data.length; j++) {
if (recursiveCompare(data[i], data[j])) {
var error = this.createError(ErrorCodes.ARRAY_UNIQUE, {match1: i, match2: j}, '', '/uniqueItems', null, data, schema);
if (this.handleError(error)) {
return error;
}
}
}
}
}
return null;
};
ValidatorContext.prototype.validateArrayItems = function validateArrayItems(data, schema, dataPointerPath) {
if (schema.items === undefined) {
return null;
}
var error, i;
if (Array.isArray(schema.items)) {
for (i = 0; i < data.length; i++) {
if (i < schema.items.length) {
if (error = this.validateAll(data[i], schema.items[i], [i], ["items", i], dataPointerPath + "/" + i)) {
return error;
}
} else if (schema.additionalItems !== undefined) {
if (typeof schema.additionalItems === "boolean") {
if (!schema.additionalItems) {
error = (this.createError(ErrorCodes.ARRAY_ADDITIONAL_ITEMS, {}, '/' + i, '/additionalItems', null, data, schema));
if (this.handleError(error)) {
return error;
}
}
} else if (error = this.validateAll(data[i], schema.additionalItems, [i], ["additionalItems"], dataPointerPath + "/" + i)) {
return error;
}
}
}
} else {
for (i = 0; i < data.length; i++) {
if (error = this.validateAll(data[i], schema.items, [i], ["items"], dataPointerPath + "/" + i)) {
return error;
}
}
}
return null;
};
ValidatorContext.prototype.validateObject = function validateObject(data, schema, dataPointerPath) {
if (typeof data !== "object" || data === null || Array.isArray(data)) {
return null;
}
return this.validateObjectMinMaxProperties(data, schema, dataPointerPath)
|| this.validateObjectRequiredProperties(data, schema, dataPointerPath)
|| this.validateObjectProperties(data, schema, dataPointerPath)
|| this.validateObjectDependencies(data, schema, dataPointerPath)
|| null;
};
ValidatorContext.prototype.validateObjectMinMaxProperties = function validateObjectMinMaxProperties(data, schema) {
var keys = Object.keys(data);
var error;
if (schema.minProperties !== undefined) {
if (keys.length < schema.minProperties) {
error = this.createError(ErrorCodes.OBJECT_PROPERTIES_MINIMUM, {propertyCount: keys.length, minimum: schema.minProperties}, '', '/minProperties', null, data, schema);
if (this.handleError(error)) {
return error;
}
}
}
if (schema.maxProperties !== undefined) {
if (keys.length > schema.maxProperties) {
error = this.createError(ErrorCodes.OBJECT_PROPERTIES_MAXIMUM, {propertyCount: keys.length, maximum: schema.maxProperties}, '', '/maxProperties', null, data, schema);
if (this.handleError(error)) {
return error;
}
}
}
return null;
};
ValidatorContext.prototype.validateObjectRequiredProperties = function validateObjectRequiredProperties(data, schema) {
if (schema.required !== undefined) {
for (var i = 0; i < schema.required.length; i++) {
var key = schema.required[i];
if (data[key] === undefined) {
var error = this.createError(ErrorCodes.OBJECT_REQUIRED, {key: key}, '', '/required/' + i, null, data, schema);
if (this.handleError(error)) {
return error;
}
}
}
}
return null;
};
ValidatorContext.prototype.validateObjectProperties = function validateObjectProperties(data, schema, dataPointerPath) {
var error;
for (var key in data) {
var keyPointerPath = dataPointerPath + "/" + key.replace(/~/g, '~0').replace(/\//g, '~1');
var foundMatch = false;
if (schema.propertyNames !== undefined && schema.propertyNames !== true) {
if (error = this.validateAllValidators(key, schema.propertyNames, keyPointerPath)) {
return this.createError(ErrorCodes.OBJECT_PROPERTY_NAMES, {key: key, error: error.message}, '', '/propertyNames', null, data, schema).prefixWith(key, null);
}
}
if (schema.properties !== undefined && schema.properties[key] !== undefined) {
foundMatch = true;
if (error = this.validateAll(data[key], schema.properties[key], [key], ["properties", key], keyPointerPath)) {
return error;
}
}
if (schema.patternProperties !== undefined) {
for (var patternKey in schema.patternProperties) {
var regexp = new RegExp(patternKey);
if (regexp.test(key)) {
foundMatch = true;
if (error = this.validateAll(data[key], schema.patternProperties[patternKey], [key], ["patternProperties", patternKey], keyPointerPath)) {
return error;
}
}
}
}
if (!foundMatch) {
if (schema.additionalProperties !== undefined) {
if (this.trackUnknownProperties) {
this.knownPropertyPaths[keyPointerPath] = true;
delete this.unknownPropertyPaths[keyPointerPath];
}
if (typeof schema.additionalProperties === "boolean") {
if (!schema.additionalProperties) {
error = this.createError(ErrorCodes.OBJECT_ADDITIONAL_PROPERTIES, {key: key}, '', '/additionalProperties', null, data, schema).prefixWith(key, null);
if (this.handleError(error)) {
return error;
}
}
} else {
if (error = this.validateAll(data[key], schema.additionalProperties, [key], ["additionalProperties"], keyPointerPath)) {
return error;
}
}
} else if (this.trackUnknownProperties && !this.knownPropertyPaths[keyPointerPath]) {
this.unknownPropertyPaths[keyPointerPath] = true;
}
} else if (this.trackUnknownProperties) {
this.knownPropertyPaths[keyPointerPath] = true;
delete this.unknownPropertyPaths[keyPointerPath];
}
}
return null;
};
ValidatorContext.prototype.validateObjectDependencies = function validateObjectDependencies(data, schema, dataPointerPath) {
var error;
if (schema.dependencies !== undefined) {
for (var depKey in schema.dependencies) {
if (data[depKey] !== undefined) {
var dep = schema.dependencies[depKey];
if (typeof dep === "string") {
if (data[dep] === undefined) {
error = this.createError(ErrorCodes.OBJECT_DEPENDENCY_KEY, {key: depKey, missing: dep}, '', '', null, data, schema).prefixWith(null, depKey).prefixWith(null, "dependencies");
if (this.handleError(error)) {
return error;
}
}
} else if (Array.isArray(dep)) {
for (var i = 0; i < dep.length; i++) {
var requiredKey = dep[i];
if (data[requiredKey] === undefined) {
error = this.createError(ErrorCodes.OBJECT_DEPENDENCY_KEY, {key: depKey, missing: requiredKey}, '', '/' + i, null, data, schema).prefixWith(null, depKey).prefixWith(null, "dependencies");
if (this.handleError(error)) {
return error;
}
}
}
} else {
if (error = this.validateAll(data, dep, [], ["dependencies", depKey], dataPointerPath)) {
return error;
}
}
}
}
}
return null;
};
ValidatorContext.prototype.validateCombinations = function validateCombinations(data, schema, dataPointerPath) {
return this.validateAllOf(data, schema, dataPointerPath)
|| this.validateAnyOf(data, schema, dataPointerPath)
|| this.validateOneOf(data, schema, dataPointerPath)
|| this.validateNot(data, schema, dataPointerPath)
|| null;
};
ValidatorContext.prototype.validateAllOf = function validateAllOf(data, schema, dataPointerPath) {
if (schema.allOf === undefined) {
return null;
}
var error;
for (var i = 0; i < schema.allOf.length; i++) {
var subSchema = schema.allOf[i];
if (error = this.validateAll(data, subSchema, [], ["allOf", i], dataPointerPath)) {
return error;
}
}
return null;
};
ValidatorContext.prototype.validateAnyOf = function validateAnyOf(data, schema, dataPointerPath) {
if (schema.anyOf === undefined) {
return null;
}
var errors = [];
var startErrorCount = this.errors.length;
var oldUnknownPropertyPaths, oldKnownPropertyPaths;
if (this.trackUnknownProperties) {
oldUnknownPropertyPaths = this.unknownPropertyPaths;
oldKnownPropertyPaths = this.knownPropertyPaths;
}
var errorAtEnd = true;
for (var i = 0; i < schema.anyOf.length; i++) {
if (this.trackUnknownProperties) {
this.unknownPropertyPaths = {};
this.knownPropertyPaths = {};
}
var subSchema = schema.anyOf[i];
var errorCount = this.errors.length;
var error = this.validateAll(data, subSchema, [], ["anyOf", i], dataPointerPath);
if (error === null && errorCount === this.errors.length) {
this.errors = this.errors.slice(0, startErrorCount);
if (this.trackUnknownProperties) {
for (var knownKey in this.knownPropertyPaths) {
oldKnownPropertyPaths[knownKey] = true;
delete oldUnknownPropertyPaths[knownKey];
}
for (var unknownKey in this.unknownPropertyPaths) {
if (!oldKnownPropertyPaths[unknownKey]) {
oldUnknownPropertyPaths[unknownKey] = true;
}
}
// We need to continue looping so we catch all the property definitions, but we don't want to return an error
errorAtEnd = false;
continue;
}
return null;
}
if (error) {
errors.push(error.prefixWith(null, "" + i).prefixWith(null, "anyOf"));
}
}
if (this.trackUnknownProperties) {
this.unknownPropertyPaths = oldUnknownPropertyPaths;
this.knownPropertyPaths = oldKnownPropertyPaths;
}
if (errorAtEnd) {
errors = errors.concat(this.errors.slice(startErrorCount));
this.errors = this.errors.slice(0, startErrorCount);
return this.createError(ErrorCodes.ANY_OF_MISSING, {}, "", "/anyOf", errors, data, schema);
}
};
ValidatorContext.prototype.validateOneOf = function validateOneOf(data, schema, dataPointerPath) {
if (schema.oneOf === undefined) {
return null;
}
var validIndex = null;
var errors = [];
var startErrorCount = this.errors.length;
var oldUnknownPropertyPaths, oldKnownPropertyPaths;
if (this.trackUnknownProperties) {
oldUnknownPropertyPaths = this.unknownPropertyPaths;
oldKnownPropertyPaths = this.knownPropertyPaths;
}
for (var i = 0; i < schema.oneOf.length; i++) {
if (this.trackUnknownProperties) {
this.unknownPropertyPaths = {};
this.knownPropertyPaths = {};
}
var subSchema = schema.oneOf[i];
var errorCount = this.errors.length;
var error = this.validateAll(data, subSchema, [], ["oneOf", i], dataPointerPath);
if (error === null && errorCount === this.errors.length) {
if (validIndex === null) {
validIndex = i;
} else {
this.errors = this.errors.slice(0, startErrorCount);
return this.createError(ErrorCodes.ONE_OF_MULTIPLE, {index1: validIndex, index2: i}, "", "/oneOf", null, data, schema);
}
if (this.trackUnknownProperties) {
for (var knownKey in this.knownPropertyPaths) {
oldKnownPropertyPaths[knownKey] = true;
delete oldUnknownPropertyPaths[knownKey];
}
for (var unknownKey in this.unknownPropertyPaths) {
if (!oldKnownPropertyPaths[unknownKey]) {
oldUnknownPropertyPaths[unknownKey] = true;
}
}
}
} else if (error) {
errors.push(error);
}
}
if (this.trackUnknownProperties) {
this.unknownPropertyPaths = oldUnknownPropertyPaths;
this.knownPropertyPaths = oldKnownPropertyPaths;
}
if (validIndex === null) {
errors = errors.concat(this.errors.slice(startErrorCount));
this.errors = this.errors.slice(0, startErrorCount);
return this.createError(ErrorCodes.ONE_OF_MISSING, {}, "", "/oneOf", errors, data, schema);
} else {
this.errors = this.errors.slice(0, startErrorCount);
}
return null;
};
ValidatorContext.prototype.validateNot = function validateNot(data, schema, dataPointerPath) {
if (schema.not === undefined) {
return null;
}
var oldErrorCount = this.errors.length;
var oldUnknownPropertyPaths, oldKnownPropertyPaths;
if (this.trackUnknownProperties) {
oldUnknownPropertyPaths = this.unknownPropertyPaths;
oldKnownPropertyPaths = this.knownPropertyPaths;
this.unknownPropertyPaths = {};
this.knownPropertyPaths = {};
}
var error = this.validateAll(data, schema.not, null, null, dataPointerPath);
var notErrors = this.errors.slice(oldErrorCount);
this.errors = this.errors.slice(0, oldErrorCount);
if (this.trackUnknownProperties) {
this.unknownPropertyPaths = oldUnknownPropertyPaths;
this.knownPropertyPaths = oldKnownPropertyPaths;
}
if (error === null && notErrors.length === 0) {
return this.createError(ErrorCodes.NOT_PASSED, {}, "", "/not", null, data, schema);
}
return null;
};
ValidatorContext.prototype.validateHypermedia = function validateCombinations(data, schema, dataPointerPath) {
if (!schema.links) {
return null;
}
var error;
for (var i = 0; i < schema.links.length; i++) {
var ldo = schema.links[i];
if (ldo.rel === "describedby") {
var template = new UriTemplate(ldo.href);
var allPresent = true;
for (var j = 0; j < template.varNames.length; j++) {
if (!(template.varNames[j] in data)) {
allPresent = false;
break;
}
}
if (allPresent) {
var schemaUrl = template.fillFromObject(data);
var subSchema = {"$ref": schemaUrl};
if (error = this.validateAll(data, subSchema, [], ["links", i], dataPointerPath)) {
return error;
}
}
}
}
};
// parseURI() and resolveUrl() are from https://gist.github.com/1088850
// - released as public domain by author ("Yaffle") - see comments on gist
function parseURI(url) {
var m = String(url).replace(/^\s+|\s+$/g, '').match(/^([^:\/?#]+:)?(\/\/(?:[^:@]*(?::[^:@]*)?@)?(([^:\/?#]*)(?::(\d*))?))?([^?#]*)(\?[^#]*)?(#[\s\S]*)?/);
// authority = '//' + user + ':' + pass '@' + hostname + ':' port
return (m ? {
href : m[0] || '',
protocol : m[1] || '',
authority: m[2] || '',
host : m[3] || '',
hostname : m[4] || '',
port : m[5] || '',
pathname : m[6] || '',
search : m[7] || '',
hash : m[8] || ''
} : null);
}
function resolveUrl(base, href) {// RFC 3986
function removeDotSegments(input) {
var output = [];
input.replace(/^(\.\.?(\/|$))+/, '')
.replace(/\/(\.(\/|$))+/g, '/')
.replace(/\/\.\.$/, '/../')
.replace(/\/?[^\/]*/g, function (p) {
if (p === '/..') {
output.pop();
} else {
output.push(p);
}
});
return output.join('').replace(/^\//, input.charAt(0) === '/' ? '/' : '');
}
href = parseURI(href || '');
base = parseURI(base || '');
return !href || !base ? null : (href.protocol || base.protocol) +
(href.protocol || href.authority ? href.authority : base.authority) +
removeDotSegments(href.protocol || href.authority || href.pathname.charAt(0) === '/' ? href.pathname : (href.pathname ? ((base.authority && !base.pathname ? '/' : '') + base.pathname.slice(0, base.pathname.lastIndexOf('/') + 1) + href.pathname) : base.pathname)) +
(href.protocol || href.authority || href.pathname ? href.search : (href.search || base.search)) +
href.hash;
}
function getDocumentUri(uri) {
return uri.split('#')[0];
}
function normSchema(schema, baseUri) {
if (schema && typeof schema === "object") {
if (baseUri === undefined) {
baseUri = schema.id;
} else if (typeof schema.id === "string") {
baseUri = resolveUrl(baseUri, schema.id);
schema.id = baseUri;
}
if (Array.isArray(schema)) {
for (var i = 0; i < schema.length; i++) {
normSchema(schema[i], baseUri);
}
} else {
if (typeof schema['$ref'] === "string") {
schema['$ref'] = resolveUrl(baseUri, schema['$ref']);
}
for (var key in schema) {
if (key !== "enum") {
normSchema(schema[key], baseUri);
}
}
}
}
}
function defaultErrorReporter(language) {
language = language || 'en';
var errorMessages = languages[language];
return function (error) {
var messageTemplate = errorMessages[error.code] || ErrorMessagesDefault[error.code];
if (typeof messageTemplate !== 'string') {
return "Unknown error code " + error.code + ": " + JSON.stringify(error.messageParams);
}
var messageParams = error.params;
// Adapted from Crockford's supplant()
return messageTemplate.replace(/\{([^{}]*)\}/g, function (whole, varName) {
var subValue = messageParams[varName];
return typeof subValue === 'string' || typeof subValue === 'number' ? subValue : whole;
});
};
}
var ErrorCodes = {
INVALID_TYPE: 0,
ENUM_MISMATCH: 1,
ANY_OF_MISSING: 10,
ONE_OF_MISSING: 11,
ONE_OF_MULTIPLE: 12,
NOT_PASSED: 13,
BOOLEAN_SCHEMA_FALSE: 14,
CONST_NOT_EQUAL: 15,
// Numeric errors
NUMBER_MULTIPLE_OF: 100,
NUMBER_MINIMUM: 101,
NUMBER_MINIMUM_EXCLUSIVE: 102,
NUMBER_MAXIMUM: 103,
NUMBER_MAXIMUM_EXCLUSIVE: 104,
NUMBER_NOT_A_NUMBER: 105,
// String errors
STRING_LENGTH_SHORT: 200,
STRING_LENGTH_LONG: 201,
STRING_PATTERN: 202,
// Object errors
OBJECT_PROPERTIES_MINIMUM: 300,
OBJECT_PROPERTIES_MAXIMUM: 301,
OBJECT_REQUIRED: 302,
OBJECT_ADDITIONAL_PROPERTIES: 303,
OBJECT_DEPENDENCY_KEY: 304,
OBJECT_PROPERTY_NAMES: 305,
// Array errors
ARRAY_LENGTH_SHORT: 400,
ARRAY_LENGTH_LONG: 401,
ARRAY_UNIQUE: 402,
ARRAY_ADDITIONAL_ITEMS: 403,
ARRAY_CONTAINS: 404,
// Custom/user-defined errors
FORMAT_CUSTOM: 500,
KEYWORD_CUSTOM: 501,
// Schema structure
CIRCULAR_REFERENCE: 600,
// Non-standard validation options
UNKNOWN_PROPERTY: 1000
};
var ErrorCodeLookup = {};
for (var key in ErrorCodes) {
ErrorCodeLookup[ErrorCodes[key]] = key;
}
var ErrorMessagesDefault = {
INVALID_TYPE: "Invalid type: {type} (expected {expected})",
ENUM_MISMATCH: "No enum match for: {value}",
ANY_OF_MISSING: "Data does not match any schemas from \"anyOf\"",
ONE_OF_MISSING: "Data does not match any schemas from \"oneOf\"",
ONE_OF_MULTIPLE: "Data is valid against more than one schema from \"oneOf\": indices {index1} and {index2}",
NOT_PASSED: "Data matches schema from \"not\"",
BOOLEAN_SCHEMA_FALSE: "Schema does not allow any data",
CONST_NOT_EQUAL: "Data does not match schema.const",
// Numeric errors
NUMBER_MULTIPLE_OF: "Value {value} is not a multiple of {multipleOf}",
NUMBER_MINIMUM: "Value {value} is less than minimum {minimum}",
NUMBER_MINIMUM_EXCLUSIVE: "Value {value} is equal to exclusive minimum {minimum}",
NUMBER_MAXIMUM: "Value {value} is greater than maximum {maximum}",
NUMBER_MAXIMUM_EXCLUSIVE: "Value {value} is equal to exclusive maximum {maximum}",
NUMBER_NOT_A_NUMBER: "Value {value} is not a valid number",
// String errors
STRING_LENGTH_SHORT: "String is too short ({length} chars), minimum {minimum}",
STRING_LENGTH_LONG: "String is too long ({length} chars), maximum {maximum}",
STRING_PATTERN: "String does not match pattern: {pattern}",
// Object errors
OBJECT_PROPERTIES_MINIMUM: "Too few properties defined ({propertyCount}), minimum {minimum}",
OBJECT_PROPERTIES_MAXIMUM: "Too many properties defined ({propertyCount}), maximum {maximum}",
OBJECT_REQUIRED: "Missing required property: {key}",
OBJECT_ADDITIONAL_PROPERTIES: "Additional properties not allowed",
OBJECT_DEPENDENCY_KEY: "Dependency failed - key must exist: {missing} (due to key: {key})",
OBJECT_PROPERTY_NAMES: "Property name \"{key}\" does not match schema with error: {error}",
// Array errors
ARRAY_LENGTH_SHORT: "Array is too short ({length}), minimum {minimum}",
ARRAY_LENGTH_LONG: "Array is too long ({length}), maximum {maximum}",
ARRAY_UNIQUE: "Array items are not unique (indices {match1} and {match2})",
ARRAY_ADDITIONAL_ITEMS: "Additional items not allowed",
ARRAY_CONTAINS: "Array are not contain item matching schema.contains",
// Format errors
FORMAT_CUSTOM: "Format validation failed ({message})",
KEYWORD_CUSTOM: "Keyword failed: {key} ({message})",
// Schema structure
CIRCULAR_REFERENCE: "Circular $refs: {urls}",
// Non-standard validation options
UNKNOWN_PROPERTY: "Unknown property (not in schema)"
};
function ValidationError(code, params, dataPath, schemaPath, subErrors) {
Error.call(this);
if (code === undefined) {
throw new Error ("No error code supplied: " + schemaPath);
}
this.message = '';
this.params = params;
this.code = code;
this.dataPath = dataPath || "";
this.schemaPath = schemaPath || "";
this.subErrors = subErrors || null;
var err = new Error(this.message);
this.stack = err.stack || err.stacktrace;
if (!this.stack) {
/*jshint -W002: true*/
try {
throw err;
}
catch(err) {
this.stack = err.stack || err.stacktrace;
}
}
}
ValidationError.prototype = Object.create(Error.prototype);
ValidationError.prototype.constructor = ValidationError;
ValidationError.prototype.name = 'ValidationError';
ValidationError.prototype.prefixWith = function (dataPrefix, schemaPrefix) {
if (dataPrefix !== null) {
dataPrefix = dataPrefix.replace(/~/g, "~0").replace(/\//g, "~1");
this.dataPath = "/" + dataPrefix + this.dataPath;
}
if (schemaPrefix !== null) {
schemaPrefix = schemaPrefix.replace(/~/g, "~0").replace(/\//g, "~1");
this.schemaPath = "/" + schemaPrefix + this.schemaPath;
}
if (this.subErrors !== null) {
for (var i = 0; i < this.subErrors.length; i++) {
this.subErrors[i].prefixWith(dataPrefix, schemaPrefix);
}
}
return this;
};
function isTrustedUrl(baseUrl, testUrl) {
if(testUrl.substring(0, baseUrl.length) === baseUrl){
var remainder = testUrl.substring(baseUrl.length);
if ((testUrl.length > 0 && testUrl.charAt(baseUrl.length - 1) === "/")
|| remainder.charAt(0) === "#"
|| remainder.charAt(0) === "?") {
return true;
}
}
return false;
}
var languages = {};
function createApi(language) {
var globalContext = new ValidatorContext();
var currentLanguage;
var customErrorReporter;
var api = {
setErrorReporter: function (reporter) {
if (typeof reporter === 'string') {
return this.language(reporter);
}
customErrorReporter = reporter;
return true;
},
addFormat: function () {
globalContext.addFormat.apply(globalContext, arguments);
},
language: function (code) {
if (!code) {
return currentLanguage;
}
if (!languages[code]) {
code = code.split('-')[0]; // fall back to base language
}
if (languages[code]) {
currentLanguage = code;
return code; // so you can tell if fall-back has happened
}
return false;
},
addLanguage: function (code, messageMap) {
var key;
for (key in ErrorCodes) {
if (messageMap[key] && !messageMap[ErrorCodes[key]]) {
messageMap[ErrorCodes[key]] = messageMap[key];
}
}
var rootCode = code.split('-')[0];
if (!languages[rootCode]) { // use for base language if not yet defined
languages[code] = messageMap;
languages[rootCode] = messageMap;
} else {
languages[code] = Object.create(languages[rootCode]);
for (key in messageMap) {
if (typeof languages[rootCode][key] === 'undefined') {
languages[rootCode][key] = messageMap[key];
}
languages[code][key] = messageMap[key];
}
}
return this;
},
freshApi: function (language) {
var result = createApi();
if (language) {
result.language(language);
}
return result;
},
validate: function (data, schema, checkRecursive, banUnknownProperties) {
var def = defaultErrorReporter(currentLanguage);
var errorReporter = customErrorReporter ? function (error, data, schema) {
return customErrorReporter(error, data, schema) || def(error, data, schema);
} : def;
var context = new ValidatorContext(globalContext, false, errorReporter, checkRecursive, banUnknownProperties);
if (typeof schema === "string") {
schema = {"$ref": schema};
}
context.addSchema("", schema);
var error = context.validateAll(data, schema, null, null, "");
if (!error && banUnknownProperties) {
error = context.banUnknownProperties(data, schema);
}
this.error = error;
this.missing = context.missing;
this.valid = (error === null);
this.toString = function () {
if (this.error) {
return this.error.message;
} else {
return 'Object passed schema validation';
}
};
return this.valid;
},
validateResult: function () {
var result = {};
this.validate.apply(result, arguments);
return result;
},
validateMultiple: function (data, schema, checkRecursive, banUnknownProperties) {
var def = defaultErrorReporter(currentLanguage);
var errorReporter = customErrorReporter ? function (error, data, schema) {
return customErrorReporter(error, data, schema) || def(error, data, schema);
} : def;
var context = new ValidatorContext(globalContext, true, errorReporter, checkRecursive, banUnknownProperties);
if (typeof schema === "string") {
schema = {"$ref": schema};
}
context.addSchema("", schema);
context.validateAll(data, schema, null, null, "");
if (banUnknownProperties) {
context.banUnknownProperties(data, schema);
}
var result = {};
result.errors = context.errors;
result.missing = context.missing;
result.valid = (result.errors.length === 0);
return result;
},
addSchema: function () {
return globalContext.addSchema.apply(globalContext, arguments);
},
getSchema: function () {
return globalContext.getSchema.apply(globalContext, arguments);
},
getSchemaMap: function () {
return globalContext.getSchemaMap.apply(globalContext, arguments);
},
getSchemaUris: function () {
return globalContext.getSchemaUris.apply(globalContext, arguments);
},
getMissingUris: function () {
return globalContext.getMissingUris.apply(globalContext, arguments);
},
dropSchemas: function () {
globalContext.dropSchemas.apply(globalContext, arguments);
},
defineKeyword: function () {
globalContext.defineKeyword.apply(globalContext, arguments);
},
defineError: function (codeName, codeNumber, defaultMessage) {
if (typeof codeName !== 'string' || !/^[A-Z]+(_[A-Z]+)*$/.test(codeName)) {
throw new Error('Code name must be a string in UPPER_CASE_WITH_UNDERSCORES');
}
if (typeof codeNumber !== 'number' || codeNumber%1 !== 0 || codeNumber < 10000) {
throw new Error('Code number must be an integer > 10000');
}
if (typeof ErrorCodes[codeName] !== 'undefined') {
throw new Error('Error already defined: ' + codeName + ' as ' + ErrorCodes[codeName]);
}
if (typeof ErrorCodeLookup[codeNumber] !== 'undefined') {
throw new Error('Error code already used: ' + ErrorCodeLookup[codeNumber] + ' as ' + codeNumber);
}
ErrorCodes[codeName] = codeNumber;
ErrorCodeLookup[codeNumber] = codeName;
ErrorMessagesDefault[codeName] = ErrorMessagesDefault[codeNumber] = defaultMessage;
for (var langCode in languages) {
var language = languages[langCode];
if (language[codeName]) {
language[codeNumber] = language[codeNumber] || language[codeName];
}
}
},
reset: function () {
globalContext.reset();
this.error = null;
this.missing = [];
this.valid = true;
},
missing: [],
error: null,
valid: true,
normSchema: normSchema,
resolveUrl: resolveUrl,
getDocumentUri: getDocumentUri,
errorCodes: ErrorCodes
};
api.language(language || 'en');
return api;
}
var tv4 = createApi();
tv4.addLanguage('en-gb', ErrorMessagesDefault);
//legacy property
tv4.tv4 = tv4;
return tv4; // used by _header.js to globalise.
}));
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="File" module="OFS.Image"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>__name__</string> </key>
<value> <string>tv4.js</string> </value>
</item>
<item>
<key> <string>content_type</string> </key>
<value> <string>application/javascript</string> </value>
</item>
<item>
<key> <string>precondition</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>tv4.js</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
jsonform is available at:
https://lab.nexedi.com/bk/rjs_json_form
\ No newline at end of file
erp5_view_style
\ No newline at end of file
Form generation based on http://json-schema.org/
\ No newline at end of file
bk
\ No newline at end of file
erp5_json_form
\ No newline at end of file
erp5_json_form
\ No newline at end of file
5.4.7
\ No newline at end of file
...@@ -234,6 +234,16 @@ gadget_officejs_jio_spreadsheet_view.js\n ...@@ -234,6 +234,16 @@ gadget_officejs_jio_spreadsheet_view.js\n
gadget_ooffice_spreadsheet.json\n gadget_ooffice_spreadsheet.json\n
500px-Gnome-x-office-spreadsheet.svg.png\n 500px-Gnome-x-office-spreadsheet.svg.png\n
\n \n
#jsonform\n
json-schema/schema4.json\n
json-schema/schema6.json\n
json-schema/schema7.json\n
jsonform.gadget.html\n
jsonform.gadget.js\n
jsonform/gadget_json_generated_form_child.html\n
jsonform/gadget_json_generated_form_child.js\n
jsonform/tv4.js\n
\n
#onlyoffice editor\n #onlyoffice editor\n
jio.js\n jio.js\n
zipfilestorage-with-jszip.js\n zipfilestorage-with-jszip.js\n
...@@ -241,6 +251,8 @@ onlyoffice.gadget.html\n ...@@ -241,6 +251,8 @@ onlyoffice.gadget.html\n
onlyoffice.gadget.js\n onlyoffice.gadget.js\n
onlyoffice.gadget.appcache\n onlyoffice.gadget.appcache\n
#autogenerated for: erp5_onlyoffice_webapps\n #autogenerated for: erp5_onlyoffice_webapps\n
onlyoffice/remote_settings.json\n
onlyoffice/xmla_connection.json\n
onlyoffice/web-apps/apps/common/Analytics.js\n onlyoffice/web-apps/apps/common/Analytics.js\n
onlyoffice/web-apps/apps/common/Gateway.js\n onlyoffice/web-apps/apps/common/Gateway.js\n
onlyoffice/web-apps/apps/common/IrregularStack.js\n onlyoffice/web-apps/apps/common/IrregularStack.js\n
...@@ -339,6 +351,7 @@ onlyoffice/web-apps/apps/common/main/lib/view_folder/Plugins.js\n ...@@ -339,6 +351,7 @@ onlyoffice/web-apps/apps/common/main/lib/view_folder/Plugins.js\n
onlyoffice/web-apps/apps/common/main/lib/view_folder/RenameDialog.js\n onlyoffice/web-apps/apps/common/main/lib/view_folder/RenameDialog.js\n
onlyoffice/web-apps/apps/common/main/lib/view_folder/ReviewChanges.js\n onlyoffice/web-apps/apps/common/main/lib/view_folder/ReviewChanges.js\n
onlyoffice/web-apps/apps/common/main/lib/view_folder/SearchDialog.js\n onlyoffice/web-apps/apps/common/main/lib/view_folder/SearchDialog.js\n
onlyoffice/web-apps/apps/common/main/lib/view_folder/RenderJSDialog.js\n
onlyoffice/web-apps/apps/css.js\n onlyoffice/web-apps/apps/css.js\n
onlyoffice/web-apps/apps/documenteditor/main/app.js\n onlyoffice/web-apps/apps/documenteditor/main/app.js\n
onlyoffice/web-apps/apps/documenteditor/main/app/collection/EquationGroups.js\n onlyoffice/web-apps/apps/documenteditor/main/app/collection/EquationGroups.js\n
......
...@@ -4,7 +4,24 @@ rsvp.js ...@@ -4,7 +4,24 @@ rsvp.js
renderjs.js renderjs.js
jio.js jio.js
jiodev.js jiodev.js
#jsonform
gadget_erp5_global.js
gadget_html5_select.html
gadget_html5_select.js
json-schema/schema4.json
json-schema/schema6.json
json-schema/schema7.json
jsonform.gadget.html
jsonform.gadget.js
jsonform/gadget_json_generated_form_child.html
jsonform/gadget_json_generated_form_child.js
jsonform/tv4.js
#autogenerated for: erp5_onlyoffice_webapps #autogenerated for: erp5_onlyoffice_webapps
onlyoffice/remote_settings.json
onlyoffice/xmla_connection.json
onlyoffice/web-apps/apps/common/Analytics.js onlyoffice/web-apps/apps/common/Analytics.js
onlyoffice/web-apps/apps/common/Gateway.js onlyoffice/web-apps/apps/common/Gateway.js
onlyoffice/web-apps/apps/common/IrregularStack.js onlyoffice/web-apps/apps/common/IrregularStack.js
...@@ -103,6 +120,7 @@ onlyoffice/web-apps/apps/common/main/lib/view_folder/Plugins.js ...@@ -103,6 +120,7 @@ onlyoffice/web-apps/apps/common/main/lib/view_folder/Plugins.js
onlyoffice/web-apps/apps/common/main/lib/view_folder/RenameDialog.js onlyoffice/web-apps/apps/common/main/lib/view_folder/RenameDialog.js
onlyoffice/web-apps/apps/common/main/lib/view_folder/ReviewChanges.js onlyoffice/web-apps/apps/common/main/lib/view_folder/ReviewChanges.js
onlyoffice/web-apps/apps/common/main/lib/view_folder/SearchDialog.js onlyoffice/web-apps/apps/common/main/lib/view_folder/SearchDialog.js
onlyoffice/web-apps/apps/common/main/lib/view_folder/RenderJSDialog.js
onlyoffice/web-apps/apps/css.js onlyoffice/web-apps/apps/css.js
onlyoffice/web-apps/apps/documenteditor/main/app.js onlyoffice/web-apps/apps/documenteditor/main/app.js
onlyoffice/web-apps/apps/documenteditor/main/app/collection/EquationGroups.js onlyoffice/web-apps/apps/documenteditor/main/app/collection/EquationGroups.js
......
/*global window, rJS, RSVP, DocsAPI, console, document, /*global window, rJS, RSVP, DocsAPI, console, document,
Common, require, jIO, URL, FileReader, atob, ArrayBuffer, Common, require, jIO, URL, FileReader, atob, ArrayBuffer,
Uint8Array, XMLHttpRequest, Blob, Rusha, define, Uint8Array, XMLHttpRequest, Blob, Rusha, define,
Uint8ClampedArray,
TextDecoder, DesktopOfflineAppDocumentEndSave*/ TextDecoder, DesktopOfflineAppDocumentEndSave*/
/*jslint nomen: true, maxlen:80, indent:2*/ /*jslint nomen: true, maxlen:80, indent:2*/
"use strict"; "use strict";
...@@ -69,11 +70,13 @@ DocsAPI.DocEditor.version = function () { ...@@ -69,11 +70,13 @@ DocsAPI.DocEditor.version = function () {
.declareAcquiredMethod("triggerSubmit", "triggerSubmit") .declareAcquiredMethod("triggerSubmit", "triggerSubmit")
.declareAcquiredMethod("triggerMaximize", "triggerMaximize") .declareAcquiredMethod("triggerMaximize", "triggerMaximize")
.declareAcquiredMethod('getSetting', 'getSetting') .declareAcquiredMethod('getSetting', 'getSetting')
.declareAcquiredMethod('notifyChange', 'notifyChange')
.declareMethod("jio_getAttachment", function (docId, attId, opt) { .declareMethod("jio_getAttachment", function (docId, attId, opt) {
var g = this, var g = this,
queue; convert;
opt = opt || {};
if (attId === 'body.txt' || attId === 'Editor.bin') { if (attId === 'body.txt' || attId === 'Editor.bin') {
opt = 'asBinArray'; opt = { 'format': 'bin_array' };
if (!docId) { if (!docId) {
docId = '/'; docId = '/';
} }
...@@ -82,49 +85,44 @@ DocsAPI.DocEditor.version = function () { ...@@ -82,49 +85,44 @@ DocsAPI.DocEditor.version = function () {
docId = '/media/'; docId = '/media/';
} }
} }
queue = g.props.value_zip_storage.getAttachment(docId, attId) if (opt.format === "blob_url") {
convert = opt.format;
delete opt.format;
}
if (opt.format === "bin_array") {
convert = opt.format;
opt.format = 'array_buffer';
}
return g.props.value_zip_storage.getAttachment(docId, attId, opt)
.push(function (blob) { .push(function (blob) {
var data; if (convert === "bin_array") {
if (opt === "asText") { return new Uint8ClampedArray(blob);
data = jIO.util.readBlobAsText(blob)
.then(function (evt) {
return evt.target.result;
});
} else if (opt === "asBinArray") {
data = jIO.util.readBlobAsArrayBuffer(blob)
.then(function (evt) {
return new Uint8ClampedArray(evt.target.result);
});
} else if (opt === "asBlobURL") {
data = URL.createObjectURL(blob);
} else if (opt === "asDataURL") {
data = new RSVP.Promise(function (resolve, reject) {
var reader = new FileReader();
reader.addEventListener('load', function () {
resolve(reader.result);
});
reader.addEventListener('error', reject);
reader.readAsDataURL(blob);
});
} else {
data = blob;
} }
return data; if (convert === "blob_url") {
return URL.createObjectURL(blob);
}
return blob;
}); });
return queue;
}) })
.declareMethod("jio_putAttachment", function (docId, atId, data) { .declareMethod("jio_putAttachment", function (docId, atId, data) {
var g = this, var g = this,
zip = g.props.value_zip_storage, zip = g.props.value_zip_storage,
queue, queue,
content_type, content_type,
start = data.slice(0, 5); start;
if (!docId) { if (!docId) {
docId = '/media/'; docId = '/media/';
} }
if (typeof data === 'string' && start === "data:") { if (typeof data === 'string') {
start = data.slice(0, 5);
if (start === "data:") {
data = dataURLtoBlob(data); data = dataURLtoBlob(data);
} }
}
if (typeof data === 'object' &&
!(data instanceof Blob)) {
data = new Blob([JSON.stringify(data, undefined, 2)]);
}
if (atId) { if (atId) {
queue = zip.putAttachment(docId, atId, data) queue = zip.putAttachment(docId, atId, data)
.push(undefined, function (error) { .push(undefined, function (error) {
...@@ -168,6 +166,25 @@ DocsAPI.DocEditor.version = function () { ...@@ -168,6 +166,25 @@ DocsAPI.DocEditor.version = function () {
} }
return queue; return queue;
}) })
.allowPublicAcquisition("notifyChange", function (arr, scope) {
var gadget = this;
if (scope === "remote_settings") {
return this.getDeclaredGadget(scope)
.push(function (g) {
return g.getContent();
})
.push(function (data) {
return gadget
.jio_putAttachment('/', 'remote_settings.json', data);
})
.push(function () {
return gadget.notifyChange();
})
.push(undefined, function (e) {
console.error(e);
});
}
})
// methods emulating Gateway used for connection with ooffice begin. // methods emulating Gateway used for connection with ooffice begin.
.declareMethod('appReady', function () { .declareMethod('appReady', function () {
......
{
"$schema": "http://json-schema.org/draft-07/schema",
"title": "remote XMLA connections",
"additionalProperties": {
"$ref": "xmla_connection.json"
}
}
\ No newline at end of file
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="File" module="OFS.Image"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_Cacheable__manager_id</string> </key>
<value> <string>http_cache</string> </value>
</item>
<item>
<key> <string>__name__</string> </key>
<value> <string>remote_settings.json</string> </value>
</item>
<item>
<key> <string>content_type</string> </key>
<value> <string>application/json</string> </value>
</item>
<item>
<key> <string>precondition</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
/*global AscCommonExcel, RSVP, Xmla, console*/
/* jshint -W040 */ /* jshint -W040 */
/* /*
* Copyright (c) 2017 Nexedi SA and Contributors. All Rights Reserved. * Copyright (c) 2017 Nexedi SA and Contributors. All Rights Reserved.
...@@ -38,9 +39,8 @@ ...@@ -38,9 +39,8 @@
* @param {Object} RSVP * @param {Object} RSVP
* @param {Xmla} Xmla * @param {Xmla} Xmla
* @param {console} console * @param {console} console
* @param {undefined} undefined
*/ */
function (window, RSVP, Xmla, console, undefined) { function (window, RSVP, Xmla, console) {
var cBaseFunction = AscCommonExcel.cBaseFunction; var cBaseFunction = AscCommonExcel.cBaseFunction;
var cFormulaFunctionGroup = AscCommonExcel.cFormulaFunctionGroup, var cFormulaFunctionGroup = AscCommonExcel.cFormulaFunctionGroup,
cElementType = AscCommonExcel.cElementType, cElementType = AscCommonExcel.cElementType,
...@@ -66,22 +66,6 @@ ...@@ -66,22 +66,6 @@
cFormulaFunctionGroup.NotRealised.push(cCUBEKPIMEMBER, cCUBEMEMBERPROPERTY, cCUBERANKEDMEMBER, cFormulaFunctionGroup.NotRealised.push(cCUBEKPIMEMBER, cCUBEMEMBERPROPERTY, cCUBERANKEDMEMBER,
cCUBESET, cCUBESETCOUNT); cCUBESET, cCUBESETCOUNT);
var xmla = new Xmla({
// listeners: {
// events: Xmla.EVENT_ERROR,
// handler: function (eventName, eventData, xmla) {
// console.log(eventData.exception);
// // alert(
// // "Snap, an error occurred: " + eventData.exception.message + " (" + eventData.exception.code + ")" +
// // (eventData.exception.code === Xmla.Exception.HTTP_ERROR_CDE
// // ? "\nstatus: " + eventData.exception.data.status + "; statusText: " + eventData.exception.data.statusText
// // : "")
// // );
// }
// },
async: true
});
function xmla_request(func, prop) { function xmla_request(func, prop) {
var xmla = new Xmla({async: true}); var xmla = new Xmla({async: true});
// return function () { // return function () {
...@@ -99,8 +83,15 @@ ...@@ -99,8 +83,15 @@
}); });
} }
function xmla_request_retry(func, prop) { function xmla_request_retry(func, settings) {
return xmla_request(func, prop) var queue,
urls = settings.urls,
i;
function make_request(url) {
return function (error) {
settings.prop.url = url;
return xmla_request(func, settings.prop)
.push(undefined, function (response) { .push(undefined, function (response) {
// fix mondrian Internal and Sql errors // fix mondrian Internal and Sql errors
if (response) { if (response) {
...@@ -108,23 +99,33 @@ ...@@ -108,23 +99,33 @@
case "SOAP-ENV:Server.00HSBE02": case "SOAP-ENV:Server.00HSBE02":
case "SOAP-ENV:00UE001.Internal Error": case "SOAP-ENV:00UE001.Internal Error":
// rarely server error, so try again // rarely server error, so try again
return xmla_request(func, prop); return xmla_request(func, settings.prop);
} }
} }
throw response; throw response;
}); });
};
}
queue = make_request(urls[0])();
for (i = 1; i < settings.urls.length; i += 1) {
queue.push(undefined, make_request(urls[i]));
}
return queue;
} }
function discover_hierarchies(connection) { function discover_hierarchies(connection) {
var settings = getProperties(connection), return getProperties(connection)
prop = settings.prop; .push(function (settings) {
var prop = settings.prop;
prop.restrictions = { prop.restrictions = {
// 'CATALOG_NAME': 'FoodMart', // 'CATALOG_NAME': 'FoodMart',
// 'HIERARCHY_NAME': hierarchy_name, // 'HIERARCHY_NAME': hierarchy_name,
// 'HIERARCHY_UNIQUE_NAME': hierarchy_name, // 'HIERARCHY_UNIQUE_NAME': hierarchy_name,
'CUBE_NAME': settings["cube"] 'CUBE_NAME': settings["cube"]
}; };
return xmla_request_retry("discoverMDHierarchies", prop) return xmla_request_retry("discoverMDHierarchies", settings);
})
.push(function (response) { .push(function (response) {
var hierarchies = {}, var hierarchies = {},
hierarchy, hierarchy,
...@@ -169,34 +170,33 @@ ...@@ -169,34 +170,33 @@
} }
function getProperties(connection) { function getProperties(connection) {
var connections = { return Common.Gateway.jio_getAttachment('/', 'remote_settings.json', {format: 'json'})
"xmla": { .push(undefined, function (e) {
"prop": { if (e.status_code === 404) {
"url": "https://d1.erp5.ru/saiku/xmla", return {};
"properties": {
"DataSourceInfo": "FoodMart",
"Catalog": "FoodMart"
} }
}, throw e;
"cube": "Sales" })
}, .push(function (value) {
"olapy": { var c;
"prop": { if (!value.hasOwnProperty(connection)) {
"url": "https://d1.erp5.ru/olapy/xmla", throw "connection not exist";
"properties": {
"DataSourceInfo": "-",
"Catalog": "sales"
} }
}, c = value[connection];
"cube": "Sales" if (!c.hasOwnProperty('properties')) {
c.properties = {};
} }
}; return {
connection = connections[connection]; urls: c.urls,
if (!connection) { prop: {
throw "connection not exist"; properties: {
DataSourceInfo: c.properties.DataSourceInfo,
Catalog: c.properties.Catalog
} }
connection = JSON.parse(JSON.stringify(connection)); },
return connection; cube: c.properties.Cube
};
});
} }
function getScheme(connection) { function getScheme(connection) {
...@@ -324,16 +324,22 @@ ...@@ -324,16 +324,22 @@
scheme; scheme;
if (!execution_scheme.execute) { if (!execution_scheme.execute) {
execution_scheme.execute = RSVP.defer(); execution_scheme.execute = RSVP.defer();
return getScheme(connection) return RSVP.Queue()
.push(function (s) { .push(function () {
var settings = getProperties(connection), return RSVP.all([
getScheme(connection),
getProperties(connection)
]);
})
.push(function (arr) {
var settings = arr[1],
prop = settings.prop, prop = settings.prop,
hierarchies = execution_scheme.hierarchies, hierarchies = execution_scheme.hierarchies,
hierarchy, hierarchy,
mdx = [], mdx = [],
tuple_str, tuple_str,
all_member; all_member;
scheme = s; scheme = arr[0];
for (hierarchy in hierarchies) { for (hierarchy in hierarchies) {
tuple_str = hierarchies[hierarchy].join(","); tuple_str = hierarchies[hierarchy].join(",");
all_member = scheme.hierarchies[hierarchy]["all_member"]; all_member = scheme.hierarchies[hierarchy]["all_member"];
...@@ -344,7 +350,7 @@ ...@@ -344,7 +350,7 @@
} }
prop.statement = "SELECT " + mdx.join("*") + prop.statement = "SELECT " + mdx.join("*") +
" ON 0 FROM [" + settings["cube"] + "]"; " ON 0 FROM [" + settings["cube"] + "]";
return xmla_request("execute", prop); return xmla_request_retry("execute", settings);
}) })
.push(function (dataset) { .push(function (dataset) {
var cellset = dataset.getCellset(), var cellset = dataset.getCellset(),
...@@ -360,7 +366,7 @@ ...@@ -360,7 +366,7 @@
}; };
for (axis_id = 0; axis_id < axis_count; axis_id++) { for (axis_id = 0; axis_id < axis_count; axis_id += 1) {
axis_array.push(dataset.getAxis(axis_id)); axis_array.push(dataset.getAxis(axis_id));
} }
...@@ -410,10 +416,9 @@ ...@@ -410,10 +416,9 @@
} }
function discover_members(connection, opt) { function discover_members(connection, opt) {
return new RSVP.Queue() return getProperties(connection)
.push(function () { .push(function (settings) {
var settings = getProperties(connection), var prop = settings.prop,
prop = settings.prop,
cached_member, cached_member,
scheme = getExecutionScheme(connection); scheme = getExecutionScheme(connection);
prop.restrictions = { prop.restrictions = {
...@@ -433,30 +438,30 @@ ...@@ -433,30 +438,30 @@
if (cached_member) { if (cached_member) {
return [cached_member]; return [cached_member];
} else { } else {
return xmla_request_retry("discoverMDMembers", prop) return xmla_request_retry("discoverMDMembers", settings)
.push(function (r) { .push(function (r) {
var ret = [], var ret = [],
uname, uname,
level, level,
cached_member; cached_member1;
while (r.hasMoreRows()) { while (r.hasMoreRows()) {
uname = r["getMemberUniqueName"](); uname = r["getMemberUniqueName"]();
level = r["getLevelUniqueName"](); level = r["getLevelUniqueName"]();
// we can check cache twice because fist check // we can check cache twice because fist check
// only if discover by member_uname // only if discover by member_uname
if (!scheme.members.hasOwnProperty(uname)) { if (!scheme.members.hasOwnProperty(uname)) {
cached_member = { cached_member1 = {
uname: uname, uname: uname,
h: r["getHierarchyUniqueName"](), h: r["getHierarchyUniqueName"](),
level: r["getLevelUniqueName"](), level: r["getLevelUniqueName"](),
caption: r["getMemberCaption"](), caption: r["getMemberCaption"](),
type: r["getMemberType"]() type: r["getMemberType"]()
}; };
scheme.members[uname] = cached_member; scheme.members[uname] = cached_member1;
} else { } else {
cached_member = scheme.members[uname]; cached_member1 = scheme.members[uname];
} }
ret.push(cached_member); ret.push(cached_member1);
r.nextRow(); r.nextRow();
if (!scheme.levels.hasOwnProperty(level)) { if (!scheme.levels.hasOwnProperty(level)) {
scheme.levels[level] = discover_level(connection, scheme, level); scheme.levels[level] = discover_level(connection, scheme, level);
...@@ -476,15 +481,17 @@ ...@@ -476,15 +481,17 @@
var i; var i;
function compare(a, b) { function compare(a, b) {
if (a.uname < b.uname) if (a.uname < b.uname) {
return -1; return -1;
if (a.uname > b.uname) }
if (a.uname > b.uname) {
return 1; return 1;
}
return 0; return 0;
} }
members.sort(compare); members.sort(compare);
for (i = 0; i < members.length; i++) { for (i = 0; i < members.length; i += 1) {
members[i].level_index = i; members[i].level_index = i;
} }
scheme.levels[level] = members; scheme.levels[level] = members;
...@@ -498,9 +505,8 @@ ...@@ -498,9 +505,8 @@
function check_interseption(hierarchy) { function check_interseption(hierarchy) {
if (hierarchies.hasOwnProperty(hierarchy)) { if (hierarchies.hasOwnProperty(hierarchy)) {
throw "The tuple is invalid because there is no intersection for the specified values."; throw "The tuple is invalid because there is no intersection for the specified values.";
} else {
hierarchies[hierarchy] = 1;
} }
hierarchies[hierarchy] = 1;
} }
members.forEach(function (member) { members.forEach(function (member) {
...@@ -511,11 +517,11 @@ ...@@ -511,11 +517,11 @@
member_uname: member member_uname: member
}) })
.push(function (members) { .push(function (members) {
var member; var m;
if (members.length > 0) { if (members.length > 0) {
member = members[0]; m = members[0];
check_interseption(member.h); check_interseption(m.h);
return member; return m;
} else { } else {
throw "member not found"; throw "member not found";
} }
...@@ -605,7 +611,7 @@ ...@@ -605,7 +611,7 @@
member, member,
new_member, new_member,
level; level;
for (i = 0; i < arg.length; i++) { for (i = 0; i < arg.length; i += 1) {
elem = arg[i]; elem = arg[i];
if (cElementType.string === elem.type) { if (cElementType.string === elem.type) {
member = scheme.members[elem.value]; member = scheme.members[elem.value];
...@@ -732,25 +738,25 @@ ...@@ -732,25 +738,25 @@
ret; ret;
function getHierarchyByMember(member_path) { function getHierarchyByMember(member_path) {
var h; var hierarchy;
h = cube.members[member_path]; hierarchy = cube.members[member_path];
if (h === undefined) { if (hierarchy === undefined) {
throw "query result not contain data for member:" + throw "query result not contain data for member:" +
member_path; member_path;
} }
h = h.hierarchy; hierarchy = hierarchy.hierarchy;
h = cube.hierarchies[h]; hierarchy = cube.hierarchies[hierarchy];
return h; return hierarchy;
} }
for (i = 0; i < cube.hierarchies.length; i++) { for (i = 0; i < cube.hierarchies.length; i += 1) {
h = cube.hierarchies[i]; h = cube.hierarchies[i];
if (!coordinate[h.axis_id]) { if (!coordinate[h.axis_id]) {
coordinate[h.axis_id] = []; coordinate[h.axis_id] = [];
} }
coordinate[h.axis_id][h.tuple_id] = null; coordinate[h.axis_id][h.tuple_id] = null;
} }
for (i = 0; i < members.length; i++) { for (i = 0; i < members.length; i += 1) {
member_path = members[i]; member_path = members[i];
h = getHierarchyByMember(members[i]); h = getHierarchyByMember(members[i]);
coordinate[h.axis_id][h.tuple_id] = member_path; coordinate[h.axis_id][h.tuple_id] = member_path;
......
...@@ -509,7 +509,7 @@ ...@@ -509,7 +509,7 @@
queue, queue,
oThis = this; oThis = this;
if (0 === start.indexOf('jio:')) { if (0 === start.indexOf('jio:')) {
queue = Common.Gateway.jio_getAttachment(this.Api.documentId, image_id.slice(4), 'asBlobURL') queue = Common.Gateway.jio_getAttachment(this.Api.documentId, image_id.slice(4), {format:'blob_url'})
.push(undefined, function (error) { .push(undefined, function (error) {
console.error(error); console.error(error);
return ""; return "";
......
define([
'common/main/lib/component/Window',
'common/main/lib/component/LoadMask'
], function () {
'use strict';
Common.Views.RenderJSDialog = Common.UI.Window.extend(_.extend({
initialize : function(options) {
var _options = {};
_.extend(_options, {
title: this.textTitle,
width: 1024,
height: 621,
header: true
}, options);
this.template = [
'<div id="<%= scope %>"></div>'
].join('');
_options.tpl = _.template(this.template)(_options);
Common.UI.Window.prototype.initialize.call(this, _options);
},
render: function() {
var q,
me = this,
element;
Common.UI.Window.prototype.render.call(this);
element = document.getElementById(this.options.scope);
this.$window.find('> .body').css({height: 'auto', overflow: 'hidden'});
// this.loadMask = new Common.UI.LoadMask({owner: $(element)});
// this.loadMask.setTitle(this.textLoading);
// this.loadMask.show();
if (!me.gadget) {
q = Common.Gateway.declareGadget(this.options.gadget_url, {
scope: this.options.scope,
element: element,
sandbox: "iframe"
})
.push(function (sub_gadget) {
me.gadget = sub_gadget;
var iframe = sub_gadget.element.querySelector("iframe");
iframe.width = '100%';
iframe.height = "585px";
iframe.setAttribute("frameBorder","0");
// iframe.style = "border: none;";
// iframe.align = "top";
// iframe.scrolling = "no";
// this._eventfunc = function(msg) {
// me._onWindowMessage(msg);
// };
// this._bindWindowEvents.call(this);
//
// this.on('close', function(obj){
// me._unbindWindowEvents();
// });
});
} else {
q = RSVP.Queue();
}
return q.push(function () {
return me.gadget.render(me.options.gadget_render_opt);
})
.push(undefined, function () {
// XXX
return;
})
.push(function () {
me._onLoad();
});
},
_bindWindowEvents: function() {
if (window.addEventListener) {
window.addEventListener("message", this._eventfunc, false);
} else if (window.attachEvent) {
window.attachEvent("onmessage", this._eventfunc);
}
},
_unbindWindowEvents: function() {
if (window.removeEventListener) {
window.removeEventListener("message", this._eventfunc);
} else if (window.detachEvent) {
window.detachEvent("onmessage", this._eventfunc);
}
},
_onWindowMessage: function(msg) {
// TODO: check message origin
if (msg && window.JSON) {
try {
this._onMessage.call(this, window.JSON.parse(msg.data));
} catch(e) {}
}
},
_onMessage: function(msg) {
if (msg && msg.file !== undefined) {
Common.NotificationCenter.trigger('window:close', this);
var me = this;
setTimeout(function() {
if ( !_.isEmpty(msg.file) ) {
me.trigger('mailmergerecepients', me, msg.file);
}
}, 50);
}
},
_onLoad: function() {
if (this.loadMask)
this.loadMask.hide();
},
textTitle : 'Select Data Source',
textLoading : 'Loading'
}, Common.Views.RenderJSDialog || {}));
});
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="File" module="OFS.Image"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_Cacheable__manager_id</string> </key>
<value> <string>http_cache</string> </value>
</item>
<item>
<key> <string>__name__</string> </key>
<value> <string>RenderJSDialog.js</string> </value>
</item>
<item>
<key> <string>content_type</string> </key>
<value> <string>text/javascript</string> </value>
</item>
<item>
<key> <string>precondition</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
...@@ -34,7 +34,8 @@ define([ ...@@ -34,7 +34,8 @@ define([
'core', 'core',
'common/main/lib/util/Shortcuts', 'common/main/lib/util/Shortcuts',
'spreadsheeteditor/main/app/view/LeftMenu', 'spreadsheeteditor/main/app/view/LeftMenu',
'spreadsheeteditor/main/app/view/FileMenu' 'spreadsheeteditor/main/app/view/FileMenu',
'common/main/lib/view/RenderJSDialog'
], function () { ], function () {
'use strict'; 'use strict';
...@@ -88,6 +89,7 @@ define([ ...@@ -88,6 +89,7 @@ define([
onLaunch: function() { onLaunch: function() {
this.leftMenu = this.createView('LeftMenu').render(); this.leftMenu = this.createView('LeftMenu').render();
this.leftMenu.btnSearch.on('toggle', _.bind(this.onMenuSearch, this)); this.leftMenu.btnSearch.on('toggle', _.bind(this.onMenuSearch, this));
this.leftMenu.btnRemote.on('toggle', _.bind(this.onRemote, this));
Common.util.Shortcuts.delegateShortcuts({ Common.util.Shortcuts.delegateShortcuts({
shortcuts: { shortcuts: {
...@@ -476,6 +478,51 @@ define([ ...@@ -476,6 +478,51 @@ define([
this.showSearchDlg(show); this.showSearchDlg(show);
}, },
onRemote: function(obj, show) {
var queue,
me = this;
if ( !this.dlgRemote ) {
queue = Common.Gateway.jio_getAttachment('/', 'remote_settings.json', {format: 'json'})
.push(undefined, function (e) {
if (e.status_code === 404) {
return {};
}
throw e;
})
.push(function (value) {
me.dlgRemote = new Common.Views.RenderJSDialog({
toolclose: "hide",
gadget_url: "jsonform.gadget.html",
scope: "remote_settings",
gadget_render_opt: {
schema_url: "onlyoffice/remote_settings.json",
value: value
}
});
me.dlgRemote.on("hide", _.bind(function () {
this.leftMenu.btnRemote.toggle(false, true);
}, me));
});
} else {
queue = RSVP.Queue();
}
queue.push(function () {
if (show) {
if (me.dlgRemote.isVisible()) {
me.dlgRemote.focus();
} else {
me.dlgRemote.show();
}
me.api.asc_closeCellEditor();
}
})
.push(undefined, function (e) {
console.error(e);
});
},
onSearchDlgHide: function() { onSearchDlgHide: function() {
this.leftMenu.btnSearch.toggle(false, true); this.leftMenu.btnSearch.toggle(false, true);
$(this.leftMenu.btnSearch.el).blur(); $(this.leftMenu.btnSearch.el).blur();
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
<button id="left-btn-chat" class="btn btn-category" content-target="left-panel-chat"><span class="btn-icon img-toolbarmenu btn-menu-chat">&nbsp;</span></button> <button id="left-btn-chat" class="btn btn-category" content-target="left-panel-chat"><span class="btn-icon img-toolbarmenu btn-menu-chat">&nbsp;</span></button>
<!-- /** coauthoring end **/ --> <!-- /** coauthoring end **/ -->
<button id="left-btn-plugins" class="btn btn-category" content-target=""><span class="btn-icon img-toolbarmenu btn-menu-plugin">&nbsp;</span></button> <button id="left-btn-plugins" class="btn btn-category" content-target=""><span class="btn-icon img-toolbarmenu btn-menu-plugin">&nbsp;</span></button>
<button id="left-btn-remote" class="btn btn-category" content-target=""><span class="btn-icon img-toolbarmenu btn-settings">&nbsp;</span></button>
<button id="left-btn-support" class="btn btn-category" content-target=""><span class="btn-icon img-toolbarmenu btn-menu-support">&nbsp;</span></button> <button id="left-btn-support" class="btn btn-category" content-target=""><span class="btn-icon img-toolbarmenu btn-menu-support">&nbsp;</span></button>
<button id="left-btn-about" class="btn btn-category" content-target=""><span class="btn-icon img-toolbarmenu btn-menu-about">&nbsp;</span></button> <button id="left-btn-about" class="btn btn-category" content-target=""><span class="btn-icon img-toolbarmenu btn-menu-about">&nbsp;</span></button>
</div> </div>
......
...@@ -107,6 +107,14 @@ define([ ...@@ -107,6 +107,14 @@ define([
toggleGroup: 'leftMenuGroup' toggleGroup: 'leftMenuGroup'
}); });
this.btnRemote = new Common.UI.Button({
action: 'remote',
el: $('#left-btn-remote', this.el),
hint: this.tipRemote,
enableToggle: true,
disabled: true
});
this.btnSupport = new Common.UI.Button({ this.btnSupport = new Common.UI.Button({
action: 'support', action: 'support',
el: $('#left-btn-support', this.el), el: $('#left-btn-support', this.el),
...@@ -150,6 +158,7 @@ define([ ...@@ -150,6 +158,7 @@ define([
this.btnPlugins.on('click', _.bind(this.onBtnMenuClick, this)); this.btnPlugins.on('click', _.bind(this.onBtnMenuClick, this));
this.btnSearch.on('click', _.bind(this.onBtnMenuClick, this)); this.btnSearch.on('click', _.bind(this.onBtnMenuClick, this));
this.btnRemote.on('toggle', _.bind(this.onBtnMenuClick, this));
this.btnAbout.on('toggle', _.bind(this.onBtnMenuToggle, this)); this.btnAbout.on('toggle', _.bind(this.onBtnMenuToggle, this));
this.btnFile.on('toggle', _.bind(this.onBtnMenuToggle, this)); this.btnFile.on('toggle', _.bind(this.onBtnMenuToggle, this));
...@@ -187,7 +196,8 @@ define([ ...@@ -187,7 +196,8 @@ define([
this.btnFile.toggle(false); this.btnFile.toggle(false);
this.btnAbout.toggle(false); this.btnAbout.toggle(false);
if (btn.options.action == 'search') { if (btn.options.action == 'search' ||
btn.options.action == 'remote') {
} else { } else {
if (btn.pressed) { if (btn.pressed) {
if (!(this.$el.width() > SCALE_MIN)) { if (!(this.$el.width() > SCALE_MIN)) {
...@@ -289,6 +299,7 @@ define([ ...@@ -289,6 +299,7 @@ define([
disableMenu: function(menu, disable) { disableMenu: function(menu, disable) {
this.btnFile.setDisabled(false); this.btnFile.setDisabled(false);
this.btnAbout.setDisabled(false); this.btnAbout.setDisabled(false);
this.btnRemote.setDisabled(false);
this.btnSupport.setDisabled(false); this.btnSupport.setDisabled(false);
this.btnSearch.setDisabled(false); this.btnSearch.setDisabled(false);
/** coauthoring begin **/ /** coauthoring begin **/
...@@ -369,6 +380,7 @@ define([ ...@@ -369,6 +380,7 @@ define([
tipChat : 'Chat', tipChat : 'Chat',
/** coauthoring end **/ /** coauthoring end **/
tipAbout : 'About', tipAbout : 'About',
tipRemote : 'Remote Connections Settings',
tipSupport : 'Feedback & Support', tipSupport : 'Feedback & Support',
tipFile : 'File', tipFile : 'File',
tipSearch : 'Search', tipSearch : 'Search',
......
{
"$schema": "http://json-schema.org/draft-07/schema",
"title": "XMLA connection",
"type": "object",
"properties": {
"urls": {
"uniqueItems": true,
"items": {
"format": "uri",
"minLength": 1,
"type": "string"
},
"minItems": 1,
"type": "array"
},
"properties": {
"type": "object",
"additionalProperties": false,
"required": ["Cube"],
"properties": {
"DataSourceInfo": {"type": "string"},
"Catalog": {"type": "string"},
"Cube": {"type": "string"}
}
},
"username": {
"type": "string"
},
"password": {
"type": "string"
}
},
"required": [
"urls"
],
"additionalProperties": false
}
\ No newline at end of file
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="File" module="OFS.Image"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_Cacheable__manager_id</string> </key>
<value> <string>http_cache</string> </value>
</item>
<item>
<key> <string>__name__</string> </key>
<value> <string>xmla_connection.json</string> </value>
</item>
<item>
<key> <string>content_type</string> </key>
<value> <string>application/json</string> </value>
</item>
<item>
<key> <string>precondition</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
...@@ -141,6 +141,7 @@ ...@@ -141,6 +141,7 @@
if (this.state.error_text) { if (this.state.error_text) {
if (span === null) { if (span === null) {
span = document.createElement('span'); span = document.createElement('span');
span.setAttribute('class', 'error');
span.textContent = this.state.error_text; span.textContent = this.state.error_text;
this.props.container_element.appendChild(span); this.props.container_element.appendChild(span);
} else { } else {
......
...@@ -1190,11 +1190,11 @@ div[data-gadget-scope='header'] .ui-header ul { ...@@ -1190,11 +1190,11 @@ div[data-gadget-scope='header'] .ui-header ul {
.gadget-content form .ui-field-contain { .gadget-content form .ui-field-contain {
position: relative; position: relative;
} }
.gadget-content form .ui-field-contain > span { .gadget-content form .ui-field-contain > span.error {
animation: fadein 0.2s ease-out; animation: fadein 0.2s ease-out;
} }
@media not screen and (max-width: 85em), only screen and (min-width: 45em) and (max-width: 85em) { @media not screen and (max-width: 85em), only screen and (min-width: 45em) and (max-width: 85em) {
.gadget-content form .ui-field-contain > span { .gadget-content form .ui-field-contain > span.error {
background-color: #FF6600; background-color: #FF6600;
color: #f8fff3; color: #f8fff3;
left: 25%; left: 25%;
...@@ -1206,7 +1206,7 @@ div[data-gadget-scope='header'] .ui-header ul { ...@@ -1206,7 +1206,7 @@ div[data-gadget-scope='header'] .ui-header ul {
width: auto; width: auto;
z-index: 1001; z-index: 1001;
} }
.gadget-content form .ui-field-contain > span:before { .gadget-content form .ui-field-contain > span.error:before {
position: absolute; position: absolute;
top: 100%; top: 100%;
left: 2em; left: 2em;
...@@ -1218,7 +1218,7 @@ div[data-gadget-scope='header'] .ui-header ul { ...@@ -1218,7 +1218,7 @@ div[data-gadget-scope='header'] .ui-header ul {
} }
} }
@media not screen and (min-width: 45em) { @media not screen and (min-width: 45em) {
.gadget-content form .ui-field-contain > span { .gadget-content form .ui-field-contain > span.error {
margin-left: 6pt; margin-left: 6pt;
color: #FF6600; color: #FF6600;
} }
...@@ -1807,6 +1807,39 @@ div[data-gadget-url$="gadget_erp5_page_front.html"] > ul > li li { ...@@ -1807,6 +1807,39 @@ div[data-gadget-url$="gadget_erp5_page_front.html"] > ul > li li {
padding: 3pt 6pt; padding: 3pt 6pt;
} }
/********************************************** /**********************************************
* JSONForm
**********************************************/
fieldset > .jsonformfield {
padding-left: 0;
}
.jsonformfield {
display: flex;
/*padding-left: 20px;*/
padding-top: 3px;
/*margin-left: 15px;*/
}
.jsonformfield button {
display: block;
vertical-align: top;
}
.jsonformfield .ui-btn-icon-top::before {
vertical-align: top;
}
.jsonformfield label {
word-break: keep-all;
hyphens: none;
}
.jsonformfield span {
color: #5e7f8d;
}
.jsonformfield .error {
color: #E82525;
font-weight: 600;
}
.jsonformfield input[type=number] {
text-align: right;
}
/**********************************************
* Icons * Icons
**********************************************/ **********************************************/
.ui-btn-icon-top::before, .ui-btn-icon-top::before,
......
...@@ -125,16 +125,16 @@ ...@@ -125,16 +125,16 @@
}) })
.declareAcquiredMethod("notifyChange", "notifyChange") .declareAcquiredMethod("notifyChange", "notifyChange")
.onEvent('change', function change() { .onEvent('change', function change(e) {
return RSVP.all([ return RSVP.all([
this.checkValidity(), this.checkValidity(),
this.notifyChange() this.notifyChange(e)
]); ]);
}, false, false) }, false, false)
.onEvent('input', function input() { .onEvent('input', function input(e) {
return RSVP.all([ return RSVP.all([
this.checkValidity(), this.checkValidity(),
this.notifyChange() this.notifyChange(e)
]); ]);
}, false, false) }, false, false)
......
...@@ -1403,7 +1403,7 @@ div[data-gadget-scope='header'] .ui-header { ...@@ -1403,7 +1403,7 @@ div[data-gadget-scope='header'] .ui-header {
// relative for displaying popups with errors // relative for displaying popups with errors
position: relative; position: relative;
& > span { & > span.error {
animation: fadein @transition-timing; animation: fadein @transition-timing;
@media @desktop, @tablet { @media @desktop, @tablet {
...@@ -2050,6 +2050,42 @@ div[data-gadget-url$="gadget_erp5_page_front.html"] { ...@@ -2050,6 +2050,42 @@ div[data-gadget-url$="gadget_erp5_page_front.html"] {
} }
} }
/**********************************************
* JSONForm
**********************************************/
fieldset > .jsonformfield {
padding-left: 0
}
.jsonformfield {
display: flex;
/*padding-left: 20px;*/
padding-top: 3px;
/*margin-left: 15px;*/
button {
display: block;
vertical-align: top;
}
.ui-btn-icon-top::before {
vertical-align: top;
}
label {
word-break: keep-all;
hyphens: none;
}
span {
color: rgb(94, 127, 141)
}
.error {
color: #E82525;
font-weight: 600;
}
input[type=number] {
text-align:right;
}
}
/********************************************** /**********************************************
* Icons * Icons
**********************************************/ **********************************************/
......
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