diff --git a/modules/dynamic/editors/cultures-editor.js b/modules/dynamic/editors/cultures-editor.js index 8cc01007..44565449 100644 --- a/modules/dynamic/editors/cultures-editor.js +++ b/modules/dynamic/editors/cultures-editor.js @@ -633,7 +633,7 @@ function togglePercentageMode() { async function showHierarchy() { if (customization) return; - const HeirarchyTree = await import("../hierarchy-tree.js?v=1.87.01"); + const HeirarchyTree = await import("../hierarchy-tree.js?v=1.88.06"); const getDescription = culture => { const {name, type, rural, urban} = culture; diff --git a/modules/dynamic/editors/religions-editor.js b/modules/dynamic/editors/religions-editor.js index da6c290d..4cf1903f 100644 --- a/modules/dynamic/editors/religions-editor.js +++ b/modules/dynamic/editors/religions-editor.js @@ -539,7 +539,7 @@ function togglePercentageMode() { async function showHierarchy() { if (customization) return; - const HeirarchyTree = await import("../hierarchy-tree.js?v=1.87.01"); + const HeirarchyTree = await import("../hierarchy-tree.js?v=1.88.06"); const getDescription = religion => { const {name, type, form, rural, urban} = religion; diff --git a/modules/dynamic/hierarchy-tree.js b/modules/dynamic/hierarchy-tree.js index 621f4427..df01fcd4 100644 --- a/modules/dynamic/hierarchy-tree.js +++ b/modules/dynamic/hierarchy-tree.js @@ -29,8 +29,7 @@ export function open(props) { closeDialogs("#hierarchyTree, .stable"); dataElements = props.data; - dataElements[0].origins = [null]; - validElements = dataElements.filter(r => !r.removed); + validElements = cleanupOrigins(dataElements); if (validElements.length < 3) return tip(`Not enough ${props.type} to show hierarchy`, false, "error"); onNodeEnter = props.onNodeEnter; @@ -39,6 +38,8 @@ export function open(props) { getShape = props.getShape; const root = getRoot(); + if (!root) return; + const treeWidth = root.leaves().length * 50; const treeHeight = root.height * 50; @@ -175,14 +176,30 @@ function insertHtml() { byId("dialogs").insertAdjacentHTML("beforeend", html); } -function getRoot() { - const root = d3 - .stratify() - .id(d => d.i) - .parentId(d => d.origins[0])(validElements); +function cleanupOrigins(elements) { + const existingElements = elements.filter(d => !d.removed); - oldRoot = root; - return root; + return existingElements.map(d => { + if (d.i === 0) d.origins = [null]; // root element + else if (!d.origins.length) d.origins = [0]; + else if (!existingElements.find(el => d.origins[0] === el.i)) d.origins = [0]; + return d; + }); +} + +function getRoot() { + try { + const root = d3 + .stratify() + .id(d => d.i) + .parentId(d => d.origins[0])(validElements); + + oldRoot = root; + return root; + } catch (error) { + tip("Hierarchy data issue. " + error, false, "error", 6000); + return oldRoot; + } } function getLinkKey(d) { diff --git a/modules/ui/editors.js b/modules/ui/editors.js index df7291f1..74ed5906 100644 --- a/modules/ui/editors.js +++ b/modules/ui/editors.js @@ -1182,12 +1182,12 @@ async function editStates() { async function editCultures() { if (customization) return; - const Editor = await import("../dynamic/editors/cultures-editor.js?v=1.87.06"); + const Editor = await import("../dynamic/editors/cultures-editor.js?v=1.88.06"); Editor.open(); } async function editReligions() { if (customization) return; - const Editor = await import("../dynamic/editors/religions-editor.js?v=1.87.01"); + const Editor = await import("../dynamic/editors/religions-editor.js?v=1.88.06"); Editor.open(); }