Skip to content

Commit f7fc844

Browse files
committed
Add propertyOrder support, fix SCEditor, fix validator ready bug.
* New schema keword "propertyOrder" that lets you set order of object properties, both in the form and in returned JSON. Fixes #110 * Fix SCEditor bug that wasn't updating the JSON value. Fixes #118 * Fix bug with validation results not being ready immediately for non-ajax uses. Fixes #130. Fixes #124
1 parent fe81013 commit f7fc844

File tree

8 files changed

+90
-16
lines changed

8 files changed

+90
-16
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,42 @@ Show a video preview (using HTML5 video)
404404
The `href` property is a template that gets re-evaluated everytime the value changes.
405405
The variable `self` is always available. Look at the __Dependencies__ section below for how to include other fields or use a custom template engine.
406406

407+
### Property Ordering
408+
409+
There is no way to specify property ordering in JSON Schema (although this may change in v5 of the spec).
410+
411+
JSON Editor introduces a new keyword `propertyOrder` for this purpose. The default property order if unspecified is 1000. Properties with the same order will use normal JSON key ordering.
412+
413+
```json
414+
{
415+
"type": "object",
416+
"properties": {
417+
"prop1": {
418+
"type": "string"
419+
},
420+
"prop2": {
421+
"type": "string",
422+
"propertyOrder": 10
423+
},
424+
"prop3": {
425+
"type": "string",
426+
"propertyOrder": 1001
427+
},
428+
"prop4": {
429+
"type": "string",
430+
"propertyOrder": 1
431+
}
432+
}
433+
}
434+
```
435+
436+
In the above example schema, `prop1` does not have an order specified, so it will default to 1000.
437+
So, the final order of properties in the form (and in returned JSON data) will be:
438+
439+
1. prop4 (order 1)
440+
2. prop2 (order 10)
441+
3. prop1 (order 1000)
442+
4. prop3 (order 1001)
407443

408444
### format
409445

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-editor",
3-
"version": "0.6.12",
3+
"version": "0.6.13",
44
"authors": [
55
"Jeremy Dorn <[email protected]>"
66
],

dist/jsoneditor.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! JSON Editor v0.6.12 - JSON Schema -> HTML Editor
1+
/*! JSON Editor v0.6.13 - JSON Schema -> HTML Editor
22
* By Jeremy Dorn - https://github.com/jdorn/json-editor/
33
* Released under the MIT license
44
*
@@ -234,6 +234,8 @@ JSONEditor.prototype = {
234234
// Starting data
235235
if(self.options.startval) self.root.setValue(self.options.startval);
236236

237+
self.validation_results = self.validator.validate(self.root.getValue());
238+
self.root.showValidationErrors(self.validation_results);
237239
self.ready = true;
238240

239241
// Fire ready event asynchronously
@@ -2179,6 +2181,7 @@ JSONEditor.defaults.editors.string = JSONEditor.AbstractEditor.extend({
21792181
$('#sceditor-start-marker,#sceditor-end-marker,.sceditor-nlf',val).remove();
21802182
// Set the value and update
21812183
self.input.value = val.html();
2184+
self.value = self.input.value;
21822185
if(self.parent) self.parent.onChildEditorChange(self);
21832186
else self.jsoneditor.onChange();
21842187
self.jsoneditor.notifyWatchers(self.path);
@@ -2723,11 +2726,6 @@ JSONEditor.defaults.editors.object = JSONEditor.AbstractEditor.extend({
27232726
self.maxwidth += self.editors[key].getNumColumns();
27242727
});
27252728

2726-
// Initial layout
2727-
this.layoutEditors();
2728-
// Do it again now that we know the approximate heights of elements
2729-
this.layoutEditors();
2730-
27312729
// Control buttons
27322730
this.title_controls = this.getTheme().getHeaderButtonHolder();
27332731
this.editjson_controls = this.getTheme().getHeaderButtonHolder();
@@ -2792,6 +2790,27 @@ JSONEditor.defaults.editors.object = JSONEditor.AbstractEditor.extend({
27922790
this.refreshAddProperties();
27932791
}
27942792

2793+
// Sort editors by propertyOrder
2794+
var sorted = {};
2795+
var keys = Object.keys(this.editors);
2796+
keys = keys.sort(function(a,b) {
2797+
var ordera = self.editors[a].schema.propertyOrder;
2798+
var orderb = self.editors[b].schema.propertyOrder;
2799+
if(typeof ordera !== "number") ordera = 1000;
2800+
if(typeof orderb !== "number") orderb = 1000;
2801+
2802+
return ordera - orderb;
2803+
});
2804+
for(var i=0; i<keys.length; i++) {
2805+
sorted[keys[i]] = this.editors[keys[i]];
2806+
}
2807+
this.editors = sorted;
2808+
2809+
// Initial layout
2810+
this.layoutEditors();
2811+
// Do it again now that we know the approximate heights of elements
2812+
this.layoutEditors();
2813+
27952814
this.jsoneditor.notifyWatchers(this.path);
27962815
},
27972816
showEditJSON: function() {

dist/jsoneditor.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ JSONEditor.prototype = {
4949
// Starting data
5050
if(self.options.startval) self.root.setValue(self.options.startval);
5151

52+
self.validation_results = self.validator.validate(self.root.getValue());
53+
self.root.showValidationErrors(self.validation_results);
5254
self.ready = true;
5355

5456
// Fire ready event asynchronously

src/editors/object.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,6 @@ JSONEditor.defaults.editors.object = JSONEditor.AbstractEditor.extend({
304304
self.maxwidth += self.editors[key].getNumColumns();
305305
});
306306

307-
// Initial layout
308-
this.layoutEditors();
309-
// Do it again now that we know the approximate heights of elements
310-
this.layoutEditors();
311-
312307
// Control buttons
313308
this.title_controls = this.getTheme().getHeaderButtonHolder();
314309
this.editjson_controls = this.getTheme().getHeaderButtonHolder();
@@ -373,6 +368,27 @@ JSONEditor.defaults.editors.object = JSONEditor.AbstractEditor.extend({
373368
this.refreshAddProperties();
374369
}
375370

371+
// Sort editors by propertyOrder
372+
var sorted = {};
373+
var keys = Object.keys(this.editors);
374+
keys = keys.sort(function(a,b) {
375+
var ordera = self.editors[a].schema.propertyOrder;
376+
var orderb = self.editors[b].schema.propertyOrder;
377+
if(typeof ordera !== "number") ordera = 1000;
378+
if(typeof orderb !== "number") orderb = 1000;
379+
380+
return ordera - orderb;
381+
});
382+
for(var i=0; i<keys.length; i++) {
383+
sorted[keys[i]] = this.editors[keys[i]];
384+
}
385+
this.editors = sorted;
386+
387+
// Initial layout
388+
this.layoutEditors();
389+
// Do it again now that we know the approximate heights of elements
390+
this.layoutEditors();
391+
376392
this.jsoneditor.notifyWatchers(this.path);
377393
},
378394
showEditJSON: function() {

src/editors/string.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ JSONEditor.defaults.editors.string = JSONEditor.AbstractEditor.extend({
351351
$('#sceditor-start-marker,#sceditor-end-marker,.sceditor-nlf',val).remove();
352352
// Set the value and update
353353
self.input.value = val.html();
354+
self.value = self.input.value;
354355
if(self.parent) self.parent.onChildEditorChange(self);
355356
else self.jsoneditor.onChange();
356357
self.jsoneditor.notifyWatchers(self.path);

src/intro.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! JSON Editor v0.6.12 - JSON Schema -> HTML Editor
1+
/*! JSON Editor v0.6.13 - JSON Schema -> HTML Editor
22
* By Jeremy Dorn - https://github.com/jdorn/json-editor/
33
* Released under the MIT license
44
*

0 commit comments

Comments
 (0)