Skip to content
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
78 changes: 78 additions & 0 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,81 @@ JSONEditor.AbstractEditor = Class.extend({
this.link_watchers = [];

if(options.container) this.setContainer(options.container);
this.registerDependencies();
},
registerDependencies: function() {
this.dependenciesFulfilled = true;
var deps = this.options.dependencies;
if (!deps) {
return;
}

var self = this;
Object.keys(deps).forEach(function(dependency) {
var path = self.path.split('.');
path[path.length - 1] = dependency;
path = path.join('.');
var choices = deps[dependency];
self.jsoneditor.watch(path, function() {
self.checkDependency(path, choices);
});
});
},
checkDependency: function(path, choices) {
var wrapper = this.control || this.container;
if (this.path === path || !wrapper) {
return;
}

var self = this;
var editor = this.jsoneditor.getEditor(path);
var value = editor ? editor.getValue() : undefined;
var previousStatus = this.dependenciesFulfilled;
this.dependenciesFulfilled = false;

if (!editor || !editor.dependenciesFulfilled) {
this.dependenciesFulfilled = false;
} else if (Array.isArray(choices)) {
choices.some(function(choice) {
if (value === choice) {
self.dependenciesFulfilled = true;
return true;
}
});
} else if (typeof choices === 'object') {
if (typeof value !== 'object') {
this.dependenciesFulfilled = choices === value;
} else {
Object.keys(choices).some(function(key) {
if (!choices.hasOwnProperty(key)) {
return false;
}
if (!value.hasOwnProperty(key) || choices[key] !== value[key]) {
self.dependenciesFulfilled = false;
return true;
}
self.dependenciesFulfilled = true;
});
}
} else if (typeof choices === 'string' || typeof choices === 'number') {
this.dependenciesFulfilled = value === choices;
} else if (typeof choices === 'boolean') {
if (choices) {
this.dependenciesFulfilled = value && value.length > 0;
} else {
this.dependenciesFulfilled = !value || value.length === 0;
}
}

if (this.dependenciesFulfilled !== previousStatus) {
this.notify();
}

if (this.dependenciesFulfilled) {
wrapper.style.display = 'block';
} else {
wrapper.style.display = 'none';
}
},
setContainer: function(container) {
this.container = container;
Expand Down Expand Up @@ -332,6 +407,9 @@ JSONEditor.AbstractEditor = Class.extend({
this.value = value;
},
getValue: function() {
if (!this.dependenciesFulfilled) {
return undefined;
}
return this.value;
},
refreshValue: function() {
Expand Down
3 changes: 3 additions & 0 deletions src/editors/null.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
JSONEditor.defaults.editors["null"] = JSONEditor.AbstractEditor.extend({
getValue: function() {
if (!this.dependenciesFulfilled) {
return undefined;
}
return null;
},
setValue: function() {
Expand Down
3 changes: 3 additions & 0 deletions src/editors/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ JSONEditor.defaults.editors.number = JSONEditor.defaults.editors.string.extend({
return 2;
},
getValue: function() {
if (!this.dependenciesFulfilled) {
return undefined;
}
return this.value*1;
}
});
3 changes: 3 additions & 0 deletions src/editors/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,9 @@ JSONEditor.defaults.editors.object = JSONEditor.AbstractEditor.extend({
this._super();
},
getValue: function() {
if (!this.dependenciesFulfilled) {
return undefined;
}
var result = this._super();
if(this.jsoneditor.options.remove_empty_properties || this.options.remove_empty_properties) {
for(var i in result) {
Expand Down
3 changes: 3 additions & 0 deletions src/editors/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ JSONEditor.defaults.editors.select = JSONEditor.AbstractEditor.extend({
}
},
getValue: function() {
if (!this.dependenciesFulfilled) {
return undefined;
}
return this.value;
},
preBuild: function() {
Expand Down
3 changes: 3 additions & 0 deletions src/editors/selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ JSONEditor.defaults.editors.selectize = JSONEditor.AbstractEditor.extend({
}
},
getValue: function() {
if (!this.dependenciesFulfilled) {
return undefined;
}
return this.value;
},
preBuild: function() {
Expand Down