From c7131fd660a421870000be80256fb93adcef342d Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 12 Jan 2017 14:10:43 +0200 Subject: [PATCH] Add dependency system --- src/editor.js | 78 ++++++++++++++++++++++++++++++++++++++++ src/editors/null.js | 3 ++ src/editors/number.js | 3 ++ src/editors/object.js | 3 ++ src/editors/select.js | 3 ++ src/editors/selectize.js | 3 ++ 6 files changed, 93 insertions(+) diff --git a/src/editor.js b/src/editor.js index 7993f783e..594c18121 100644 --- a/src/editor.js +++ b/src/editor.js @@ -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; @@ -332,6 +407,9 @@ JSONEditor.AbstractEditor = Class.extend({ this.value = value; }, getValue: function() { + if (!this.dependenciesFulfilled) { + return undefined; + } return this.value; }, refreshValue: function() { diff --git a/src/editors/null.js b/src/editors/null.js index 3a5ca4350..011c7c2e1 100644 --- a/src/editors/null.js +++ b/src/editors/null.js @@ -1,5 +1,8 @@ JSONEditor.defaults.editors["null"] = JSONEditor.AbstractEditor.extend({ getValue: function() { + if (!this.dependenciesFulfilled) { + return undefined; + } return null; }, setValue: function() { diff --git a/src/editors/number.js b/src/editors/number.js index acc65ee4d..d644b5502 100644 --- a/src/editors/number.js +++ b/src/editors/number.js @@ -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; } }); diff --git a/src/editors/object.js b/src/editors/object.js index 22410db84..68453be90 100644 --- a/src/editors/object.js +++ b/src/editors/object.js @@ -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) { diff --git a/src/editors/select.js b/src/editors/select.js index ee5473b6d..c03e8969d 100644 --- a/src/editors/select.js +++ b/src/editors/select.js @@ -50,6 +50,9 @@ JSONEditor.defaults.editors.select = JSONEditor.AbstractEditor.extend({ } }, getValue: function() { + if (!this.dependenciesFulfilled) { + return undefined; + } return this.value; }, preBuild: function() { diff --git a/src/editors/selectize.js b/src/editors/selectize.js index 5dca5235a..523fc5b7c 100644 --- a/src/editors/selectize.js +++ b/src/editors/selectize.js @@ -54,6 +54,9 @@ JSONEditor.defaults.editors.selectize = JSONEditor.AbstractEditor.extend({ } }, getValue: function() { + if (!this.dependenciesFulfilled) { + return undefined; + } return this.value; }, preBuild: function() {