Use getPath in onEvent

This commit is contained in:
Cristina 2018-07-30 13:09:27 +02:00
parent ef74ede75e
commit 5adf77a193
2 changed files with 16 additions and 41 deletions

View File

@ -51,7 +51,7 @@
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 <' + printPath(node.path) + '> under path <' + node.path +
'> with pretty path: <' + prettyPrintPath(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);
@ -60,37 +60,33 @@
var str = ''; var str = '';
for (var i=0; i<path.length; i++) { for (var i=0; i<path.length; i++) {
var element = path[i]; var element = path[i];
if (element.name === undefined) continue; if (typeof element === 'number') {
if (i>0 && (path[i-1].type === 'array')) { str += '[' + element + ']'
str += '[' + element.name + ']'
} else { } else {
str += (str.length > 0) ? ','+element.name : element.name; if (str.length > 0) str += ',';
str += element;
} }
} }
return str; 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 = 1; // json = 1;
//json = "zero"; // json = "zero";
//json = ["zero", "one", "two", [3,4,5,6]]; // json = ["zero", "one", "two", [3,4,5,6]];
//json = {"zero": 0} // json = ["zero"];
//json = { // json = {"0": "zero"};
// json = {"[0]": "zero"};
// json = {
// "myarray": [1, 2, [3, 4, 5]] // "myarray": [1, 2, [3, 4, 5]]
//}; // };
json = { json = {
"array": [1, 2, [3,4,5]], "array": [1, 2, [3,4,5]],

View File

@ -105,27 +105,6 @@ 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}
@ -2591,7 +2570,7 @@ Node.prototype._onEvent = function (event) {
if (element === this.dom.field || element === this.dom.value) { if (element === this.dom.field || element === this.dom.value) {
var info = { var info = {
field: this.getField(), field: this.getField(),
path: this.getExtendedPath() path: this.getPath()
}; };
// For leaf values, include value // For leaf values, include value
if (!this._hasChilds() &&element === this.dom.value) { if (!this._hasChilds() &&element === this.dom.value) {