Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$ref and fetching tree node from external source #720

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 54 additions & 14 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,22 @@ JSONEditor.prototype = {
}
}
};

if(schema.$ref && typeof schema.$ref !== "object" && schema.$ref.substr(0,1) !== "#" && !this.refs[schema.$ref]) {
refs[schema.$ref] = true;

var ref = schema.$ref;
if (ref)
{
// strip # part of the url, if found
if (typeof ref == "string")
{
var hash = ref.indexOf("#");
if (hash > 0) {
ref = ref.substr(0, hash);
}
}

if(typeof ref !== "object" && ref.substr(0,1) !== "#" && !this.refs[ref]) {
refs[ref] = true;
}
}

for(var i in schema) {
Expand Down Expand Up @@ -411,17 +424,44 @@ JSONEditor.prototype = {
}
},
expandRefs: function(schema) {
schema = $extend({},schema);

while (schema.$ref) {
var ref = schema.$ref;
delete schema.$ref;

if(!this.refs[ref]) ref = decodeURIComponent(ref);

schema = this.extendSchemas(schema,this.refs[ref]);
}
return schema;
schema = $extend({},schema);

while (schema.$ref) {
var ref = schema.$ref;
delete schema.$ref;

if(!this.refs[ref]) ref = decodeURIComponent(ref);
var ref_object = this.refs[ref];

if (!ref_object) {
// cannot find the object by ref, try to find the one that matches
for (var i in this.refs) {
if (ref.indexOf(i) === 0) {
var path = ref.substr(i.length);
if (path[0] == '#')
path = path.substr(1);
var keys = path.split('/');
var new_ref_object = this.refs[i];
for (var key in keys) {
var key_name = keys[key];
if (!key_name)
continue;
new_ref_object = new_ref_object[key_name];
if (!new_ref_object)
break;
}
if (new_ref_object) {
ref_object = new_ref_object;
}

break;
}
}
}

schema = this.extendSchemas(schema,ref_object);
}
return schema;
},
expandSchema: function(schema) {
var self = this;
Expand Down
11 changes: 11 additions & 0 deletions src/editors/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ JSONEditor.defaults.editors.select = JSONEditor.AbstractEditor.extend({
if(this.enumSource[i].filter) {
this.enumSource[i].filter = this.jsoneditor.compileTemplate(this.enumSource[i].filter, this.template_engine);
}
// if the source yet is an object, then try to pull the $ref
if(this.enumSource[i].source && typeof this.enumSource[i].source == "object") {
var src = this.jsoneditor.expandRefs(this.enumSource[i].source);
this.enumSource[i].source = [];
var j;
var keys = Object.keys(src).sort();
for (j=0; j<keys.length; j++)
{
this.enumSource[i].source.push(src[j]);
}
}
}
}
// Other, not supported
Expand Down
11 changes: 11 additions & 0 deletions src/editors/selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ JSONEditor.defaults.editors.selectize = JSONEditor.AbstractEditor.extend({
if(this.enumSource[i].filter) {
this.enumSource[i].filter = this.jsoneditor.compileTemplate(this.enumSource[i].filter, this.template_engine);
}
// if the source yet is an object, then try to pull the $ref
if(this.enumSource[i].source && typeof this.enumSource[i].source == "object") {
var src = this.jsoneditor.expandRefs(this.enumSource[i].source);
this.enumSource[i].source = [];
var j;
var keys = Object.keys(src).sort();
for (j=0; j<keys.length; j++)
{
this.enumSource[i].source.push(src[j]);
}
}
}
}
// Other, not supported
Expand Down
4 changes: 2 additions & 2 deletions src/intro.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*! JSON Editor v0.7.28 - JSON Schema -> HTML Editor
/*! JSON Editor v0.7.29 - JSON Schema -> HTML Editor
* By Jeremy Dorn - https://github.com/jdorn/json-editor/
* Released under the MIT license
*
* Date: 2016-08-07
* Date: 2017-02-01
*/

/**
Expand Down