Use extended path with type for onEvent
This commit is contained in:
parent
9de11c04cd
commit
ef74ede75e
|
@ -233,15 +233,16 @@ Constructs a new JSONEditor.
|
||||||
a JSON field or value.
|
a JSON field or value.
|
||||||
|
|
||||||
In case of field event, node information will be
|
In case of field event, node information will be
|
||||||
`{field: string, path: string[]}`.
|
`{field: String, path: [{name: String, type: String}]}`.
|
||||||
In case of value event, node information will be
|
In case of value event, node information will be
|
||||||
`{field: string, path: string[], value: string}`
|
`{field: String, path: [{name: String, type: String}], value: String}`
|
||||||
|
|
||||||
signature should be:
|
signature should be:
|
||||||
```js
|
```js
|
||||||
/**
|
/**
|
||||||
* @param {Node} the Node where event has been triggered
|
* @param {Node} the Node where event has been triggered
|
||||||
identified by {field: string, path: string[] [, value: string]}`
|
identified by {field: String, path: [{name: String, type: String}] [,
|
||||||
|
value: String]}`
|
||||||
* @param {event} the event fired
|
* @param {event} the event fired
|
||||||
*/
|
*/
|
||||||
function onEvent(node, event) {
|
function onEvent(node, event) {
|
||||||
|
|
|
@ -1,86 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
|
|
||||||
|
|
||||||
<link href="../dist/jsoneditor.css" rel="stylesheet" type="text/css">
|
|
||||||
<script src="../dist/jsoneditor.js"></script>
|
|
||||||
|
|
||||||
<style type="text/css">
|
|
||||||
body {
|
|
||||||
font: 10.5pt arial;
|
|
||||||
color: #4d4d4d;
|
|
||||||
line-height: 150%;
|
|
||||||
width: 500px;
|
|
||||||
padding-left: 40px;
|
|
||||||
}
|
|
||||||
|
|
||||||
code {
|
|
||||||
background-color: #f5f5f5;
|
|
||||||
}
|
|
||||||
|
|
||||||
#jsoneditor {
|
|
||||||
width: 500px;
|
|
||||||
height: 500px;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
When clicking on a field or leaf, a log message will be shown in console.
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<form>
|
|
||||||
<div id="jsoneditor"></div>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
var container, options, json, editor;
|
|
||||||
|
|
||||||
container = document.getElementById('jsoneditor');
|
|
||||||
|
|
||||||
options = {
|
|
||||||
mode: 'tree',
|
|
||||||
modes: ['code', 'form', 'text', 'tree', 'view'], // allowed modes
|
|
||||||
onError: function (err) {
|
|
||||||
alert(err.toString());
|
|
||||||
},
|
|
||||||
extendEvent: function(node, event) {
|
|
||||||
if (event.type === 'click') {
|
|
||||||
const isField = checkField(node, event.target);
|
|
||||||
const isLeafValue = checkLeaf(node, event.target);
|
|
||||||
if (!isField && !isLeafValue) return;
|
|
||||||
console.log('click on ' + ((isField) ? 'field' : 'leaf value') +
|
|
||||||
' under path: ' + node.getPath());
|
|
||||||
}
|
|
||||||
function checkField(node, element) {
|
|
||||||
return node.parent && element === node.dom.field;
|
|
||||||
}
|
|
||||||
function checkLeaf(node, element) {
|
|
||||||
return (!node._hasChilds() && element === node.dom.value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
json = {
|
|
||||||
"array": [1, 2, [3,4,5]],
|
|
||||||
"boolean": true,
|
|
||||||
"htmlcode": '"',
|
|
||||||
"escaped_unicode": '\\u20b9',
|
|
||||||
"unicode": '\u20b9,\uD83D\uDCA9',
|
|
||||||
"return": '\n',
|
|
||||||
"null": null,
|
|
||||||
"number": 123,
|
|
||||||
"object": {"a": "b", "c": "d"},
|
|
||||||
"string": "Hello World",
|
|
||||||
"url": "http://jsoneditoronline.org"
|
|
||||||
};
|
|
||||||
|
|
||||||
window.editor = new JSONEditor(container, options, json);
|
|
||||||
|
|
||||||
console.log('json', json);
|
|
||||||
console.log('string', JSON.stringify(json));
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -44,36 +44,68 @@
|
||||||
options = {
|
options = {
|
||||||
mode: 'tree',
|
mode: 'tree',
|
||||||
modes: ['code', 'form', 'text', 'tree', 'view'], // allowed modes
|
modes: ['code', 'form', 'text', 'tree', 'view'], // allowed modes
|
||||||
|
name: "jsonContent",
|
||||||
onError: function (err) {
|
onError: function (err) {
|
||||||
alert(err.toString());
|
alert(err.toString());
|
||||||
},
|
},
|
||||||
onEvent: function(node, event) {
|
onEvent: function(node, event) {
|
||||||
if (event.type === 'click') {
|
if (event.type === 'click') {
|
||||||
var message = 'click on <' + node.field +
|
var message = 'click on <' + node.field +
|
||||||
'> under path <' + node.path + '>';
|
'> under path <' + printPath(node.path) +
|
||||||
|
'> with pretty path: <' + prettyPrintPath(node.path) + '>';
|
||||||
if (node.value) message += ' with value <' + node.value + '>';
|
if (node.value) message += ' with value <' + node.value + '>';
|
||||||
console.log(message);
|
console.log(message);
|
||||||
}
|
}
|
||||||
|
function prettyPrintPath(path) {
|
||||||
|
var str = '';
|
||||||
|
for (var i=0; i<path.length; i++) {
|
||||||
|
var element = path[i];
|
||||||
|
if (element.name === undefined) continue;
|
||||||
|
if (i>0 && (path[i-1].type === 'array')) {
|
||||||
|
str += '[' + element.name + ']'
|
||||||
|
} else {
|
||||||
|
str += (str.length > 0) ? ','+element.name : element.name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
function printPath(path) {
|
||||||
|
var str = '';
|
||||||
|
for (var i=0; i<path.length; i++) {
|
||||||
|
var element = path[i];
|
||||||
|
str += '[' + element.name + ',' + element.type + ']';
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
json = {
|
//json = 1;
|
||||||
"array": [1, 2, [3,4,5]]
|
|
||||||
};
|
|
||||||
|
|
||||||
// json = {
|
//json = "zero";
|
||||||
// "array": [1, 2, [3,4,5]],
|
|
||||||
// "boolean": true,
|
//json = ["zero", "one", "two", [3,4,5,6]];
|
||||||
// "htmlcode": '"',
|
|
||||||
// "escaped_unicode": '\\u20b9',
|
//json = {"zero": 0}
|
||||||
// "unicode": '\u20b9,\uD83D\uDCA9',
|
|
||||||
// "return": '\n',
|
//json = {
|
||||||
// "null": null,
|
// "myarray": [1, 2, [3, 4, 5]]
|
||||||
// "number": 123,
|
//};
|
||||||
// "object": {"a": "b", "c": "d"},
|
|
||||||
// "string": "Hello World",
|
json = {
|
||||||
// "url": "http://jsoneditoronline.org"
|
"array": [1, 2, [3,4,5]],
|
||||||
// };
|
"boolean": true,
|
||||||
|
"htmlcode": '"',
|
||||||
|
"escaped_unicode": '\\u20b9',
|
||||||
|
"unicode": '\u20b9,\uD83D\uDCA9',
|
||||||
|
"return": '\n',
|
||||||
|
"null": null,
|
||||||
|
"number": 123,
|
||||||
|
"object": {"a": "b", "c": "d", "e": [1, 2, 3]},
|
||||||
|
"string": "Hello World",
|
||||||
|
"url": "http://jsoneditoronline.org",
|
||||||
|
"[0]": "zero"
|
||||||
|
};
|
||||||
|
|
||||||
window.editor = new JSONEditor(container, options, json);
|
window.editor = new JSONEditor(container, options, json);
|
||||||
|
|
||||||
|
|
|
@ -105,6 +105,27 @@ Node.prototype.getPath = function () {
|
||||||
return path;
|
return path;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the path of this node with the type for each element in path.
|
||||||
|
* @return {[{name: String, type: String}]} Array containing the path to this
|
||||||
|
* node including, for each element, its type
|
||||||
|
*/
|
||||||
|
Node.prototype.getExtendedPath = function () {
|
||||||
|
var node = this;
|
||||||
|
var path = [];
|
||||||
|
while (node) {
|
||||||
|
var name = node.getName();
|
||||||
|
// note: name is undefined if it is an array
|
||||||
|
var element = {
|
||||||
|
name: name,
|
||||||
|
type: node.type
|
||||||
|
};
|
||||||
|
path.unshift(element);
|
||||||
|
node = node.parent;
|
||||||
|
}
|
||||||
|
return path;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get node serializable name
|
* Get node serializable name
|
||||||
* @returns {String|Number}
|
* @returns {String|Number}
|
||||||
|
@ -2559,20 +2580,18 @@ Node.prototype.onEvent = function (event) {
|
||||||
/**
|
/**
|
||||||
* Trigger external onEvent provided in options if node is a JSON field or
|
* Trigger external onEvent provided in options if node is a JSON field or
|
||||||
* value.
|
* value.
|
||||||
* Information provided depends on the element:
|
* Information provided depends on the element, value is only included if
|
||||||
* - If event occurs in a field, {field: string, path: string[]}
|
* event occurs in a JSON value:
|
||||||
* - If event occurs in a value, {field: string, path: string[], value:
|
* {field: String, path: [{name: String, type: String}] [, value: String]}
|
||||||
* string}
|
|
||||||
* @param {Event} event
|
* @param {Event} event
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
Node.prototype._onEvent = function (event) {
|
Node.prototype._onEvent = function (event) {
|
||||||
var element = event.target;
|
var element = event.target;
|
||||||
if (this.parent &&
|
if (element === this.dom.field || element === this.dom.value) {
|
||||||
(element === this.dom.field || element === this.dom.value)) {
|
|
||||||
var info = {
|
var info = {
|
||||||
field: this.getField(),
|
field: this.getField(),
|
||||||
path: this.getPath()
|
path: this.getExtendedPath()
|
||||||
};
|
};
|
||||||
// For leaf values, include value
|
// For leaf values, include value
|
||||||
if (!this._hasChilds() &&element === this.dom.value) {
|
if (!this._hasChilds() &&element === this.dom.value) {
|
||||||
|
|
Loading…
Reference in New Issue