From daf41ab17e245cfdc4064c4d69c89712ae7d126c Mon Sep 17 00:00:00 2001 From: jomo Date: Fri, 5 Aug 2022 02:08:51 +0200 Subject: [PATCH] detect indirect loops in tree --- static/index.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/static/index.js b/static/index.js index 3e40dd6..c0fac55 100644 --- a/static/index.js +++ b/static/index.js @@ -39,18 +39,27 @@ function renderItems() { } function getLocString(items, item) { - if (item.is_in == item.id) { - return ['in ↻', `${item.id} is in itself`]; - } let ancestors = []; let next = item; + let loop = false; while (next) { + if (ancestors.includes(next)) { + loop = true; + if (ancestors.length == 1) { + ancestors.push(null); + } + break; + } ancestors.unshift(next); next = items[next.is_in]; } ancestors.pop(); - const loc = ancestors.map(i => i.id).join(' ➜ ') || '⬚'; + let loc = ancestors.map(i => i.id).join(' ➜ ') || '⬚'; let longloc = ancestors.map(i => `${i.type || ''} ${i.id} ${i.name && `(${i.name})` || ''}`).join(' ➜ ') || 'universe'; + if (loop) { + loc += ' ↻'; + longloc += ' ↻'; + } return [loc, `${item.id} is in ${longloc}`]; }