function drawPath(selector, path) {
function draw(directions) {
return $("<div>").addClass(directions).append($("<div>"),$("<div>"),$("<div>"),$("<div>"));
}
let lineNo = -1;
let lines = [];
for (let row of path) {
line = [];
lineNo++;
for (let obj of row) {
if (obj.parent) lineNo = 0;
line.push(draw("east west"), $("<div>").addClass("box").text(obj.name));
}
if (!lineNo) {
lines.push(line.slice(1));
} else if (lineNo == 1) {
lines[lines.length-1] = lines[lines.length-1].concat(line);
} else {
lines[lines.length-1][1].addClass("south");
lines.push([$("<div>"), draw("north west")].concat(line.slice(1)));
}
}
let length = lines.reduce((len, line) => Math.max(len, line.length), 0);
let pct = (100/length-0.01).toFixed(2);
// pad lines to the longest length
lines = lines.map(line => Array.from({length}, (_, i) => line[i] || $("<div>")));
// flatten
let $elems = $([].concat(...lines).map(jq => jq.css({flexBasis: pct + "%"}).get(0)));
$(selector).append($elems);
}
// Demo
var path = [
[{name:"S",color:"blue",parent:true}],
[{name:"A",color:"blue"},{name:"B",color:"blue"},{name:"C",color:"blue"}],
[{name:"D",color:"blue"},{name:"E",color:"blue"}],
[{name:"E",color:"blue"},{name:"F",color:"blue"}],
[{name:"S",color:"blue",parent:true}],
[{name:"A",color:"blue"},{name:"B",color:"blue"},{name:"C",color:"blue"},{name:"D",color:"blue"},{name:"E",color:"blue"}],
[{name:"E",color:"blue"},{name:"F",color:"blue"}]
];
drawPath(".container", path);
.container {
display: flex;
flex-flow: row;
flex-wrap: wrap;
color: rgba(15,30,200,.6);
}
.container>* {
flex: 0 0;
box-sizing: border-box;
display: flex;
flex-flow: row;
flex-wrap: wrap;
height: 50px;
}
.box {
border: 1px solid;
height: 30px;
margin-top: 10px;
margin-bottom: 10px;
justify-content: center;
flex-direction: column;
text-align: center;
}
.container>div>div {
flex: 0 0 50%;
height: 50%;
box-sizing: border-box;
}
.north>div:first-child {
border-right: 1px solid;
}
.south>div:nth-child(3) {
border-right: 1px solid;
}
.east>div:first-child {
border-bottom: 1px solid;
}
.west>div:nth-child(2) {
border-bottom: 1px solid;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container"></div>