Skip to content

Commit

Permalink
add multi root support for getPathNodes
Browse files Browse the repository at this point in the history
- old method renamed _getPathNodes
-- fix for nodes on path logic
- make sure we try to find to path on all root nodes
-- the path with the least amount of 'undefined' wins
--- should really be 0 unless something is badly configured
---- all undefined from the beginning of the path is excluded to
     account for missing ancestors

Signed-off-by: Patrik Kullman <patrik.kullman@neovici.se>
  • Loading branch information
Patrik Kullman committed Aug 21, 2017
1 parent c9bd9a3 commit bde8286
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions cosmoz-default-tree.html
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,38 @@
*/
Cosmoz.DefaultTree.prototype.getPathNodes = function (pathLocator, nodeObj, pathLocatorSeparator) {
if (!pathLocator) {
return this._roots;
return nodeObj;
}

var tree = nodeObj || this._treeData;

return Object.keys(tree)
.map(function (key) {
var subTree = {};
subTree[key] = tree[key];
return this._getPathNodes(pathLocator, subTree, pathLocatorSeparator);
}, this)
.filter(function (item) {
return item && item.length > 0;
})
.sort(function (a, b) {
var undefCounter = function (item) {
return item === undefined;
},
aUndefCount = a.filter(undefCounter).length,
bUndefCount = b.filter(undefCounter).length;

if (aUndefCount < bUndefCount) {
return -1;
}
if (aUndefCount > bUndefCount) {
return 1;
}
return 0;
})[0];
},
Cosmoz.DefaultTree.prototype._getPathNodes = function (pathLocator, nodeObj, pathLocatorSeparator) {

// Defaults
pathLocatorSeparator = pathLocatorSeparator || this.pathLocatorSeparator;

Expand All @@ -170,8 +199,11 @@

// Get the nodes on the path
nodes = path.map(function (nodeKey) {
node = pathSegment[nodeKey] !== node ? pathSegment[nodeKey] : undefined;
if (node && this.hasChildren(node)) {
if (!pathSegment) {
return;
}
node = pathSegment[nodeKey];
if (node) {
pathSegment = node[this.childProperty];
}
return node;
Expand Down

0 comments on commit bde8286

Please sign in to comment.