Skip to content

Commit 78558b7

Browse files
committed
Merge remote-tracking branch 'origin/develop' into develop
2 parents d74df9c + d9c3220 commit 78558b7

File tree

5 files changed

+162
-1
lines changed

5 files changed

+162
-1
lines changed

docs/api.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ Constructs a new JSONEditor.
9898

9999
In order to update css classes when they depend on external state, you can call `editor.refresh()`.
100100

101+
- `{function} onExpand({ path, isExpand, recursive })`
102+
103+
Set a callback function to be invoked when a node is expanded/collapsed (not programtically via APIs). Only applicable when option `mode` is `tree`, `form`, or `view`.
104+
105+
The callback is invoked with an object containing `path`, `isExpand` and `recursive`:
106+
107+
```
108+
{
109+
path: string[],
110+
isExpand: boolean,
111+
recursive: boolean
112+
}
113+
```
114+
101115
- `{function} onEditable({ path, field, value })`
102116

103117
Set a callback function to determine whether individual nodes are editable or read-only. Only applicable when option `mode` is `tree`, `text`, or `code`.
@@ -679,6 +693,24 @@ Destroy the editor. Clean up DOM, event listeners, and web workers.
679693

680694
Expand all fields. Only applicable for mode 'tree', 'view', and 'form'.
681695

696+
#### `JSONEditor.expand(options)`
697+
698+
Expand/collapse a given JSON node. Only applicable for mode 'tree', 'view' and 'form'.
699+
700+
*`options` fields:*
701+
702+
- `{Array.<String>} path`
703+
704+
Path for the node to expand/collapse
705+
706+
- `{Boolean} isExpand`
707+
708+
Whether to expand the node (else collapse)
709+
710+
- `{Boolean} recursive`
711+
712+
Whether to expand/collapse child nodes recursively
713+
682714
#### `JSONEditor.focus()`
683715

684716
Set focus to the JSONEditor.

examples/25_sync_node_expand.html

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<!DOCTYPE HTML>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
6+
<title>JSONEditor | Sync Node Expand</title>
7+
8+
<link href="../dist/jsoneditor.css" rel="stylesheet" type="text/css">
9+
<script src="../dist/jsoneditor.js"></script>
10+
11+
<style type="text/css">
12+
#jsoneditor-left {
13+
width: 45%;
14+
height: 90%;
15+
float: left;
16+
margin-left: 2%;
17+
}
18+
#jsoneditor-right {
19+
width: 45%;
20+
height: 90%;
21+
float: right;
22+
margin-right: 2%;
23+
}
24+
</style>
25+
</head>
26+
<body>
27+
28+
<div>
29+
<div id="jsoneditor-left"></div>
30+
<div id="jsoneditor-right"></div>
31+
</div>
32+
33+
<script>
34+
const left_json = {
35+
'student_details': {
36+
'name': {
37+
'first_name': 'foo',
38+
'last_name': 'bar'
39+
},
40+
'school': {
41+
'name': 'foo',
42+
'address': 'bar'
43+
},
44+
'contact': '434343',
45+
'age': '39'
46+
},
47+
'marks': [50, 49, 36]
48+
}
49+
50+
const right_json = {
51+
'marks': [50, 49],
52+
'student_details': {
53+
'name': 'foo bar',
54+
'address': {
55+
'street': 'foo',
56+
'city': 'bar',
57+
'zip': '444444'
58+
},
59+
'school': {
60+
'name': 'foo',
61+
'address': 'bar'
62+
},
63+
'age': '39',
64+
'contact': ['434355', '343433', '324343']
65+
}
66+
}
67+
68+
const editor_left = new JSONEditor(document.getElementById('jsoneditor-left'), {
69+
mode: "tree",
70+
onExpand: (options) => {
71+
if (editor_right) {
72+
editor_right.expand(options)
73+
}
74+
}
75+
}, left_json)
76+
77+
const editor_right = new JSONEditor(document.getElementById('jsoneditor-right'), {
78+
mode: "tree",
79+
onExpand: (options) => {
80+
if (editor_left) {
81+
editor_left.expand(options)
82+
}
83+
}
84+
}, right_json)
85+
</script>
86+
</body>
87+
</html>

src/js/JSONEditor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ JSONEditor.prototype.DEBOUNCE_INTERVAL = 150
177177
JSONEditor.VALID_OPTIONS = [
178178
'ajv', 'schema', 'schemaRefs', 'templates',
179179
'ace', 'theme', 'autocomplete',
180-
'onChange', 'onChangeJSON', 'onChangeText',
180+
'onChange', 'onChangeJSON', 'onChangeText', 'onExpand',
181181
'onEditable', 'onError', 'onEvent', 'onModeChange', 'onNodeName', 'onValidate', 'onCreateMenu',
182182
'onSelectionChange', 'onTextSelectionChange', 'onClassName',
183183
'onFocus', 'onBlur',

src/js/Node.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3112,6 +3112,14 @@ export class Node {
31123112
frame.appendChild(table)
31133113
frame.scrollTop = scrollTop
31143114
}
3115+
3116+
if (typeof this.editor.options.onExpand === 'function') {
3117+
this.editor.options.onExpand({
3118+
path: this.getPath(),
3119+
isExpand: this.expanded,
3120+
recursive: recurse
3121+
})
3122+
}
31153123
}
31163124

31173125
/**

src/js/treemode.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,26 @@ treemode.collapseAll = function () {
454454
}
455455
}
456456

457+
/**
458+
* Expand/collapse a given JSON node.
459+
* @param {Object} [options] Available parameters:
460+
* {Array<String>} [path] Path for the node to expand/collapse.
461+
* {Boolean} [isExpand] Whether to expand the node (else collapse).
462+
* {Boolean} [recursive] Whether to expand/collapse child nodes recursively.
463+
*/
464+
treemode.expand = function (options) {
465+
if (!options) return
466+
467+
const node = this.node ? this.node.findNodeByPath(options.path) : null
468+
if (!node) return
469+
470+
if (options.isExpand) {
471+
node.expand(options.recursive)
472+
} else {
473+
node.collapse(options.recursive)
474+
}
475+
}
476+
457477
/**
458478
* The method onChange is called whenever a field or value is changed, created,
459479
* deleted, duplicated, etc.
@@ -988,6 +1008,13 @@ treemode._createFrame = function () {
9881008
expandAll.title = translate('expandAll')
9891009
expandAll.onclick = () => {
9901010
editor.expandAll()
1011+
if (typeof this.options.onExpand === 'function') {
1012+
this.options.onExpand({
1013+
path: [],
1014+
isExpand: true,
1015+
recursive: true
1016+
})
1017+
}
9911018
}
9921019
this.menu.appendChild(expandAll)
9931020

@@ -998,6 +1025,13 @@ treemode._createFrame = function () {
9981025
collapseAll.className = 'jsoneditor-collapse-all'
9991026
collapseAll.onclick = () => {
10001027
editor.collapseAll()
1028+
if (typeof this.options.onExpand === 'function') {
1029+
this.options.onExpand({
1030+
path: [],
1031+
isExpand: false,
1032+
recursive: true
1033+
})
1034+
}
10011035
}
10021036
this.menu.appendChild(collapseAll)
10031037

0 commit comments

Comments
 (0)