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

update submitted json when editing select fields via json manually #752

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
41 changes: 21 additions & 20 deletions src/editors/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ JSONEditor.defaults.editors.select = JSONEditor.AbstractEditor.extend({
if(this.select2) this.select2.select2('val',this.input.value);
this.value = sanitized;
this.onChange();
this.change();
},
register: function() {
this._super();
Expand Down Expand Up @@ -63,7 +64,7 @@ JSONEditor.defaults.editors.select = JSONEditor.AbstractEditor.extend({
// Enum options enumerated
if(this.schema["enum"]) {
var display = this.schema.options && this.schema.options.enum_titles || [];

$each(this.schema["enum"],function(i,option) {
self.enum_options[i] = ""+option;
self.enum_display[i] = ""+(display[i] || option);
Expand All @@ -75,28 +76,28 @@ JSONEditor.defaults.editors.select = JSONEditor.AbstractEditor.extend({
self.enum_options.unshift('undefined');
self.enum_values.unshift(undefined);
}

}
// Boolean
else if(this.schema.type === "boolean") {
self.enum_display = this.schema.options && this.schema.options.enum_titles || ['true','false'];
self.enum_options = ['1',''];
self.enum_values = [true,false];

if(!this.isRequired()){
self.enum_display.unshift(' ');
self.enum_options.unshift('undefined');
self.enum_values.unshift(undefined);
}

}
// Dynamic Enum
else if(this.schema.enumSource) {
this.enumSource = [];
this.enum_display = [];
this.enum_options = [];
this.enum_values = [];

// Shortcut declaration for using a single array
if(!(Array.isArray(this.schema.enumSource))) {
if(this.schema.enumValue) {
Expand Down Expand Up @@ -132,7 +133,7 @@ JSONEditor.defaults.editors.select = JSONEditor.AbstractEditor.extend({
}
}
}

// Now, enumSource is an array of sources
// Walk through this array and fix up the values
for(i=0; i<this.enumSource.length; i++) {
Expand Down Expand Up @@ -224,13 +225,13 @@ JSONEditor.defaults.editors.select = JSONEditor.AbstractEditor.extend({
},
onWatchedFieldChange: function() {
var self = this, vars, j;

// If this editor uses a dynamic select box
if(this.enumSource) {
vars = this.getWatchedFieldValues();
var select_options = [];
var select_titles = [];

for(var i=0; i<this.enumSource.length; i++) {
// Constant values
if(Array.isArray(this.enumSource[i])) {
Expand All @@ -246,7 +247,7 @@ JSONEditor.defaults.editors.select = JSONEditor.AbstractEditor.extend({
} else {
items = vars[this.enumSource[i].source];
}

if(items) {
// Only use a predefined part of the array
if(this.enumSource[i].slice) {
Expand All @@ -260,12 +261,12 @@ JSONEditor.defaults.editors.select = JSONEditor.AbstractEditor.extend({
}
items = new_items;
}

var item_titles = [];
var item_values = [];
for(j=0; j<items.length; j++) {
var item = items[j];

// Rendered value
if(this.enumSource[i].value) {
item_values[j] = this.enumSource[i].value({
Expand All @@ -277,7 +278,7 @@ JSONEditor.defaults.editors.select = JSONEditor.AbstractEditor.extend({
else {
item_values[j] = items[j];
}

// Rendered title
if(this.enumSource[i].title) {
item_titles[j] = this.enumSource[i].title({
Expand All @@ -290,26 +291,26 @@ JSONEditor.defaults.editors.select = JSONEditor.AbstractEditor.extend({
item_titles[j] = item_values[j];
}
}

// TODO: sort

select_options = select_options.concat(item_values);
select_titles = select_titles.concat(item_titles);
}
}
}

var prev_value = this.value;

this.theme.setSelectOptions(this.input, select_options, select_titles);
this.enum_options = select_options;
this.enum_display = select_titles;
this.enum_values = select_options;

if(this.select2) {
this.select2.select2('destroy');
}

// If the previous value is still in the new select options, stick with it
if(select_options.indexOf(prev_value) !== -1) {
this.input.value = prev_value;
Expand All @@ -318,12 +319,12 @@ JSONEditor.defaults.editors.select = JSONEditor.AbstractEditor.extend({
// Otherwise, set the value to the first select option
else {
this.input.value = select_options[0];
this.value = select_options[0] || "";
this.value = select_options[0] || "";
if(this.parent) this.parent.onChildEditorChange(this);
else this.jsoneditor.onChange();
this.jsoneditor.notifyWatchers(this.path);
}

this.setupSelect2();
}

Expand Down