From 4eee1f521074164244c16ec1f40a1efaed395dea Mon Sep 17 00:00:00 2001 From: ansuz Date: Mon, 22 Feb 2016 17:56:17 +0100 Subject: [PATCH] add orderOfNodes function to tree library --- www/common/treesome.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/www/common/treesome.js b/www/common/treesome.js index 1c6b69ab5..08743b1c7 100644 --- a/www/common/treesome.js +++ b/www/common/treesome.js @@ -146,7 +146,7 @@ define([], function () { if (root && el.parentNode === root.childNodes[0]) { return null; } return rightmostNode(previousNode(el.parentNode)); } else { - return rightmostNode(el.parentNode.childNodes[i-1]) + return rightmostNode(el.parentNode.childNodes[i-1]); } }; @@ -162,5 +162,22 @@ define([], function () { } }; + var orderOfNodes = tree.orderOfNodes = function (a, b, root) { + // b might not be supplied + if (!b) { return; } + // a and b might be the same element + if (a === b) { return 0; } + + var cur = b; + while (cur) { + cur = previousNode(cur, root); + // if you find 'a' while traversing backwards + // they are in the expected order + if (cur === a) { return 1; } + } + // otherwise + return -1; + }; + return tree; });