Browse Source

find level for each person

subDesTagesMitExtraKaese 3 weeks ago
parent
commit
a3c5373b1d
1 changed files with 19 additions and 8 deletions
  1. 19 8
      static/stammbaum/main.js

+ 19 - 8
static/stammbaum/main.js

@@ -141,6 +141,24 @@ window.onload = function() {
     return levels
   }
   const levels = calculateLevels(persons)
+
+  function findLevel(person, levels) {
+    const date = +person.birth_date || person.death_date - 3600000 * 24 * 365 * 80
+    if(person.birth_date == null && person.death_date == null) {
+      let minLevel = null
+      for (const child of person.children) {
+        const level = findLevel(child, levels)
+        if ((level !== null && level < minLevel) || minLevel === null)
+          minLevel = level
+      }
+      if (minLevel === null)
+        return null
+      return minLevel - 1
+    } else {
+      return levels.findIndex(l => l >= date)
+    }
+  }
+
   /**
    * calculate position of cards
    * @param {*} personList current persons
@@ -160,14 +178,7 @@ window.onload = function() {
       const person = personList[j];
 
       // set y
-      const date = +person.birth_date || person.death_date - 3600000 * 24 * 365 * 80
-      if(person.birth_date == null && person.death_date == null)
-        person.level = level
-      else
-        person.level = Math.max(level, levels.findIndex(l => l >= date));
-      if(person.partner && person.partner.level > person.level) {
-        person.level = person.partner.level;
-      }
+      person.level = Math.max(level, findLevel(person, levels))
       person.y = person.level * (tree.cardHeight + 40);
 
       // set x