Created
July 3, 2017 15:23
-
-
Save gladisihor/b8068ae7918c6da36c6e6cd39549da33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function drawNestedSetsTree(data, node) { | |
var subItems = [], | |
itemsRelations = [], | |
subArr = [], | |
dataArr = [], | |
mainArray = []; | |
// Find the deepest elements | |
data.forEach(({ title, left, right }) => { | |
if (right - left === 1) { | |
subItems.push({ | |
title: title, | |
left: left, | |
right: right | |
}); | |
} | |
}); | |
// Find all parents of the deepest elements and push them into array | |
for (var index in subItems) { | |
var relationsArray = [subItems[index]]; | |
getRelations(subItems[index], relationsArray); | |
relationsArray = relationsArray.reverse(); | |
subArr.push(relationsArray); | |
} | |
// Create related array with sorted unique elements | |
for (var itemIndex in subArr) { | |
var tempSubArray = subArr[itemIndex]; | |
for (var subItemIndex in tempSubArray) { | |
var hasItem = dataArr.find(function(itemObj) { | |
var result = itemObj.title === tempSubArray[subItemIndex].title; | |
return result; | |
}); | |
if (!hasItem) { | |
dataArr.push({ | |
title: tempSubArray[subItemIndex].title, | |
deph: subItemIndex, | |
from: tempSubArray[subItemIndex - 1] ? tempSubArray[subItemIndex - 1].title : false, | |
children: [] | |
}); | |
} | |
} | |
} | |
// Create main array with structured unique elements and its children | |
for (var j in dataArr) { | |
var elem = dataArr[j]; | |
if (!elem.from) { | |
mainArray.push(elem); | |
} | |
pasteChildren(elem); | |
} | |
// Find children and push into corresponding item in the main array | |
function pasteChildren(parentElem) { | |
var childElems = []; | |
for (var i in dataArr) { | |
if (parentElem.title === dataArr[i].from) { | |
childElems.push(dataArr[i]); | |
} | |
} | |
if (childElems.length) { | |
parentElem.children = childElems; | |
} | |
} | |
// Build chain of parents | |
function getRelations(item, array) { | |
function findParent(index) { | |
var itemParent = data.find(function(itemObj) { | |
return itemObj.left === index; | |
}); | |
if (!itemParent) { | |
if (index > 0) { | |
return findParent(--index); | |
} else { | |
return array; | |
} | |
} else { | |
if (itemParent.right > item.right) { | |
array.push(itemParent); | |
getRelations(itemParent, array); | |
} else { | |
if (index > 0) { | |
return findParent(--index); | |
} else { | |
return array; | |
} | |
} | |
} | |
} | |
findParent(item.left - 1); | |
return array; | |
} | |
// Created DOM tree using main array | |
function createHtml(array, place) { | |
var ul = document.createElement('ul'); | |
for (var finalIndex in array) { | |
var li = document.createElement('li'); | |
li.textContent = array[finalIndex].title; | |
ul.appendChild(li); | |
if (array[finalIndex].children.length) { | |
createHtml(array[finalIndex].children, li); | |
} | |
} | |
place.appendChild(ul); | |
} | |
createHtml(mainArray, node); | |
} | |
if (typeof module !== 'undefined') { | |
module.exports = drawNestedSetsTree; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment