Commit 6e7b33dc authored by Boris Kocherov's avatar Boris Kocherov

loadingJSONSchema only if necessary and stop using...

loadingJSONSchema only if necessary and stop using gadget_erp5_page_slap_load_schema.html for loading schema
parent da053068
......@@ -9,14 +9,11 @@
<link href="gadget_erp5_page_slap_parameter_form.css" rel="stylesheet" type="text/css"/>
<script src="rsvp.js" type="text/javascript"></script>
<script src="renderjs.js" type="text/javascript"></script>
<script src="jio.js" type="text/javascript"></script>
<script src="gadget_json_generated_form.js" type="text/javascript"></script>
</head>
<body>
<div data-json-path="/">
</div>
<div class="loadschema"
data-gadget-url="gadget_erp5_page_slap_load_schema.html"
data-gadget-scope="loadschema">
</div>
</body>
</html>
/*jslint nomen: true, maxlen: 200, indent: 2*/
/*global window, document, rJS, RSVP */
/*jslint nomen: true, maxlen: 200, indent: 2, maxerr: 100*/
/*global window, document, URL, rJS, RSVP, jIO */
(function (window, document, rJS, RSVP) {
(function (window, document, rJS, RSVP, jIO) {
"use strict";
var render_object;
......@@ -15,6 +15,62 @@
return _str.replace(/~/g, '~0').replace(/\//g, '~1');
}
function getBaseUrl(g, path) {
var base_url,
key,
map = g.props.schema_map,
max_len = 0;
if (!path) {
return;
}
for (key in map) {
if (map.hasOwnProperty(key) &&
path.startsWith(key) &&
key.length > max_len) {
base_url = map[key];
max_len = key.length;
}
}
return base_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) {
schema = schema[decodeJsonPointer(parts[i])];
}
return schema;
}
function loadJSONSchema(g, url, path) {
if (g.props.toplevel) {
return g.loadJSONSchema(url, path);
}
path = g.element.getAttribute('data-json-parent') +
encodeJsonPointer(g.element.getAttribute('data-json-property-name')) +
path;
return g.loadJSONSchemaParent(url, path);
}
function getDocumentType(doc) {
if (doc instanceof Array) {
return "array";
......@@ -215,7 +271,16 @@
if (json_field === undefined) {
json_field = getDocumentSchema(default_value);
} else if (json_field.type === undefined) {
}
if (json_field.$ref !== undefined) {
return loadJSONSchema(gadget, json_field.$ref, first_path)
.push(function (schema_part) {
return render_field(gadget, key, path, schema_part, default_value, root);
});
}
if (json_field.type === undefined && default_value) {
json_field.type = getDocumentType(default_value);
}
......@@ -503,7 +568,7 @@
} else {
additionalProperties = json_field.additionalProperties;
}
if (additionalProperties.type) {
if (getDocumentType(additionalProperties) === "object") {
addAdditional(additionalProperties);
}
......@@ -654,12 +719,6 @@
})
.declareAcquiredMethod("notifyValid", "notifyValid")
.declareAcquiredMethod("notifyInvalid", "notifyInvalid")
.declareMethod("loadJSONSchema", function (url) {
return this.getDeclaredGadget('loadschema')
.push(function (gadget) {
return gadget.loadJSONSchema(url);
});
})
.declareAcquiredMethod("renameChildrenParent", "renameChildren")
.allowPublicAcquisition("renameChildren", function (opt_arr, scope) {
......@@ -844,10 +903,60 @@
});
})
.declareMethod("loadJSONSchema", function (url, path) {
var g = this,
protocol,
download_url,
base_url,
hash;
// XXX need use $id
base_url = getBaseUrl(g, path);
if (!base_url) {
// allow relative link
base_url = window.location;
}
if (!path) {
path = "/";
}
url = new URL(url, base_url);
protocol = url.protocol;
if (protocol === "http:" || protocol === "https:") {
if (window.location.protocol !== protocol) {
throw new Error("You cannot mixed http and https calls");
}
}
download_url = url.origin + url.pathname;
hash = url.hash;
url = url.href;
return RSVP.Queue()
.push(function () {
if (g.props.schema_cache.hasOwnProperty(download_url)) {
return g.props.schema_cache[download_url];
}
return downloadJSON(download_url)
.push(function (json) {
g.props.schema_cache[download_url] = json;
return json;
});
})
.push(function (json) {
g.props.schema_map[path] = url;
return resolveLocalReference(json, hash);
});
})
.declareAcquiredMethod("loadJSONSchemaParent", "loadJSONSchemaTop")
.allowPublicAcquisition("loadJSONSchemaTop", function (arr) {
return loadJSONSchema(this, arr[0], arr[1]);
})
.declareMethod('render', function (options) {
var g = this,
queue;
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_map = {};
g.props.schema_cache = {};
g.options = options;
if (!options.value) {
options.value = {};
......@@ -858,7 +967,7 @@
return options.schema;
});
} else {
queue = g.loadJSONSchema(options.schema_url);
queue = loadJSONSchema(g, options.schema_url);
}
return queue
.push(function (schema) {
......@@ -917,4 +1026,4 @@
// });
});
}(window, document, rJS, RSVP));
\ No newline at end of file
}(window, document, rJS, RSVP, jIO));
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment