Skip to content

Commit 93232a0

Browse files
bnanchenjosdejong
authored andcommitted
Customize object names 2 (#617)
* Customize object names * Fix: the object's name updates when its properties change * Creation of node.prototype.updateObjectName method * Implementation of recursivelyUpdateObjectName method * Callback arguments: path and size * New callback argument - type: array | object * Code improvement and callback function renaming: onNodeName
1 parent 7819405 commit 93232a0

File tree

3 files changed

+59
-11
lines changed

3 files changed

+59
-11
lines changed

src/js/JSONEditor.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ if (typeof Promise === 'undefined') {
6464
* {boolean} sortObjectKeys If true, object keys are
6565
* sorted before display.
6666
* false by default.
67-
* {function} onSelectionChange Callback method,
67+
* {function} onSelectionChange Callback method,
6868
* triggered on node selection change
6969
* Only applicable for modes
7070
* 'tree', 'view', and 'form'
71-
* {function} onTextSelectionChange Callback method,
71+
* {function} onTextSelectionChange Callback method,
7272
* triggered on text selection change
7373
* Only applicable for modes
7474
* {HTMLElement} modalAnchor The anchor element to apply an
@@ -162,7 +162,7 @@ JSONEditor.VALID_OPTIONS = [
162162
'ajv', 'schema', 'schemaRefs','templates',
163163
'ace', 'theme', 'autocomplete',
164164
'onChange', 'onChangeJSON', 'onChangeText',
165-
'onEditable', 'onError', 'onEvent', 'onModeChange', 'onValidate',
165+
'onEditable', 'onError', 'onEvent', 'onModeChange', 'onNodeName', 'onValidate',
166166
'onSelectionChange', 'onTextSelectionChange',
167167
'colorPicker', 'onColorPicker',
168168
'timestampTag',

src/js/Node.js

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2458,13 +2458,12 @@ Node.prototype.updateDom = function (options) {
24582458
// apply value to DOM
24592459
var domValue = this.dom.value;
24602460
if (domValue) {
2461-
var count = this.childs ? this.childs.length : 0;
24622461
if (this.type == 'array') {
2463-
domValue.innerHTML = '[' + count + ']';
2462+
this.updateNodeName();
24642463
util.addClassName(this.dom.tr, 'jsoneditor-expandable');
24652464
}
24662465
else if (this.type == 'object') {
2467-
domValue.innerHTML = '{' + count + '}';
2466+
this.updateNodeName();
24682467
util.addClassName(this.dom.tr, 'jsoneditor-expandable');
24692468
}
24702469
else {
@@ -4464,6 +4463,46 @@ Node.prototype._escapeJSON = function (text) {
44644463
return escaped;
44654464
};
44664465

4466+
/**
4467+
* update the object name according to the callback onNodeName
4468+
* @private
4469+
*/
4470+
Node.prototype.updateNodeName = function () {
4471+
try {
4472+
var count = this.childs ? this.childs.length : 0;
4473+
var nodeName;
4474+
if (this.type === 'object' || this.type === 'array') {
4475+
nodeName = this.editor.options.onNodeName({
4476+
path: this.getPath(),
4477+
size: count,
4478+
type: this.type
4479+
});
4480+
this.dom.value.innerHTML = (this.type === 'object')
4481+
? '{' + (nodeName || count) + '}'
4482+
: '[' + (nodeName || count) + ']';
4483+
}
4484+
}
4485+
catch (err) {
4486+
console.error('Error in onNodeName callback: ', err);
4487+
}
4488+
}
4489+
4490+
/**
4491+
* update recursively the object's and its children's name.
4492+
* @private
4493+
*/
4494+
Node.prototype.recursivelyUpdateNodeName = function () {
4495+
if (this.expanded) {
4496+
this.updateNodeName();
4497+
if (this.childs !== 'undefined') {
4498+
var i;
4499+
for (i in this.childs) {
4500+
this.childs[i].recursivelyUpdateNodeName();
4501+
}
4502+
}
4503+
}
4504+
}
4505+
44674506
// helper function to get the internal path of a node
44684507
function getInternalPath (node) {
44694508
return node.getInternalPath();

src/js/treemode.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,15 @@ treemode._onChange = function () {
549549
console.error('Error in onChangeText callback: ', err);
550550
}
551551
}
552+
553+
// trigger the onNodeName callback
554+
if (this.options.onNodeName && this.node.childs) {
555+
try {
556+
this.node.recursivelyUpdateNodeName();
557+
} catch (err) {
558+
console.error("Error in onNodeName callback: ", err);
559+
}
560+
}
552561
};
553562

554563
/**
@@ -1711,8 +1720,8 @@ treemode.getSelection = function () {
17111720

17121721
/**
17131722
* Callback registration for selection change
1714-
* @param {selectionCallback} callback
1715-
*
1723+
* @param {selectionCallback} callback
1724+
*
17161725
* @callback selectionCallback
17171726
*/
17181727
treemode.onSelectionChange = function (callback) {
@@ -1726,7 +1735,7 @@ treemode.onSelectionChange = function (callback) {
17261735
* For selecting single node send only the start parameter
17271736
* For clear the selection do not send any parameter
17281737
* If the nodes are not from the same level the first common parent will be selected
1729-
* @param {{path: Array.<String>}} start object contains the path for selection start
1738+
* @param {{path: Array.<String>}} start object contains the path for selection start
17301739
* @param {{path: Array.<String>}} end object contains the path for selection end
17311740
*/
17321741
treemode.setSelection = function (start, end) {
@@ -1737,7 +1746,7 @@ treemode.setSelection = function (start, end) {
17371746
}
17381747

17391748
var nodes = this._getNodeInstancesByRange(start, end);
1740-
1749+
17411750
nodes.forEach(function(node) {
17421751
node.expandTo();
17431752
});
@@ -1746,7 +1755,7 @@ treemode.setSelection = function (start, end) {
17461755

17471756
/**
17481757
* Returns a set of Nodes according to a range of selection
1749-
* @param {{path: Array.<String>}} start object contains the path for range start
1758+
* @param {{path: Array.<String>}} start object contains the path for range start
17501759
* @param {{path: Array.<String>}=} end object contains the path for range end
17511760
* @return {Array.<Node>} Node instances on the given range
17521761
* @private

0 commit comments

Comments
 (0)