Last active
August 4, 2017 13:37
-
-
Save dalex01/d213a8f9e7779e8136be561f726b65d5 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) { | |
data.sort(function(a, b) { | |
return a.left > b.left ? 1 : -1; | |
}); | |
let depth = 0; | |
let prev_elem = null; | |
let html = '<ul>' | |
data.forEach((elem) => { | |
if (prev_elem) { | |
if (prev_elem.right > elem.left) { | |
depth += 1; | |
} else if (prev_elem.right + 1 < elem.left) { | |
let step = elem.left - (prev_elem.right + 1); | |
depth -= step; | |
html += '</ul></li>'.repeat(step); | |
} | |
} | |
if (elem.right - elem.left == 1) { | |
html += '<li>' + elem.title + '</li>'; | |
} else { | |
html += '<li>' + elem.title + '<ul>'; | |
} | |
prev_elem = elem; | |
}); | |
html += '</ul>'; | |
node.innerHTML += html; | |
// Удалите весь код ниже и замените его на свою реализацию | |
//const ul = document.createElement('ul'); | |
//data.forEach(({ title }) => { | |
// const li = document.createElement('li'); | |
// li.textContent = title; | |
// ul.appendChild(li); | |
//}); | |
//node.appendChild(ul); | |
} | |
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
Приветствую!
Поздравляю, ваш код прошел все наши тесты.
Пожалуйста, заполните форму регистрации на курс:
https://goo.gl/BEq8sP