diff --git a/src/core.js b/src/core.js index b98241638..846700d76 100644 --- a/src/core.js +++ b/src/core.js @@ -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) { @@ -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; diff --git a/src/editors/select.js b/src/editors/select.js index ee5473b6d..2d1228bb0 100644 --- a/src/editors/select.js +++ b/src/editors/select.js @@ -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 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 */ /**