Skip to content

Commit

Permalink
Cleaned up display_required_only code, add option to demo.html.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdorn committed Jun 21, 2016
1 parent c7e2a78 commit 7ba59c8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Here are all the available options:
</tr>
<tr>
<td>display_required_only</td>
<td>If <code>true</code>, JSON Editor will only display required JSON properties by default.</td>
<td>If <code>true</code>, only required properties will be included by default.</td>
<td><code>false</code></td>
</tr>
</tbody>
Expand Down
1 change: 1 addition & 0 deletions demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ <h2>Options</h2>
<label>Boolean options</label>
<select multiple size=9 id='boolean_options' style='width: 100%;' class='form-control'>
<option value='required_by_default'>Object properties required by default</option>
<option value='display_required_only'>Only show required properties by default</option>
<option value='no_additional_properties'>No additional object properties</option>
<option value='ajax'>Allow loading schemas via Ajax</option>
<option value='disable_edit_json'>Disable "Edit JSON" buttons</option>
Expand Down
23 changes: 13 additions & 10 deletions src/editors/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,21 +235,24 @@ JSONEditor.defaults.editors.object = JSONEditor.AbstractEditor.extend({
}
// If the object should be rendered as a div
else {
var includeKeys;
if(this.schema.defaultProperties) {
includeKeys = this.schema.defaultProperties;
}
else if(this.jsoneditor.options.display_required_only && this.schema.required) {
includeKeys = this.schema.required;
}
else {
Object.keys(this.schema.properties);
if(!this.schema.defaultProperties) {
if(this.jsoneditor.options.display_required_only || this.options.display_required_only) {
this.schema.defaultProperties = [];
$each(this.schema.properties, function(k,s) {
if(self.isRequired({key: k, schema: s})) {
self.schema.defaultProperties.push(k);
}
});
}
else {
self.schema.defaultProperties = Object.keys(self.schema.properties);
}
}

// Increase the grid width to account for padding
self.maxwidth += 1;

$each(includeKeys, function(i,key) {
$each(this.schema.defaultProperties, function(i,key) {
self.addObjectProperty(key, true);

if(self.editors[key]) {
Expand Down

0 comments on commit 7ba59c8

Please sign in to comment.