TreeModel

by itsazzad

HTML

<script src="https://unpkg.com/[email protected]/dist/TreeModel-min.js"></script>

JavaScript

// hint: TreeModel, tree and root are
// globally available on this page
var tree = new TreeModel();
var root = tree.parse({
});

//Add new root
root = tree.parse({
  id: 'Sun'
});

//Add new root
root = tree.parse({
  id: 'Mercury'
});

//Add new root
root = tree.parse({
  id: 'Venus'
});



//Add new root
root = tree.parse({
  id: 'Earth'
});

// Add new child
root.addChild(tree.parse({id: 'Afro-Eurasia'}));

// Add new child to the previous one
var parent = root.first(function (node) {
    return node.model.id === 'Afro-Eurasia';
});
parent.addChild(tree.parse({id: 'Africa'}));

//Ask parent to remove all the siblings
parent.all(function (node) {
    //return not parent, i.e: children
    return node.model.id !== parent.model.id;
}).forEach(function (node) {
  node.drop();
});
parent.addChild(tree.parse({id: 'Eurasia'}));

var parent = root.first(function (node) {
    return node.model.id === 'Eurasia';
});
parent.addChild(tree.parse({id: 'Asia'}));

//Ask parent to remove all the siblings
parent.all(function (node) {
    //return not parent, i.e: children
    return node.model.id !== parent.model.id;
}).forEach(function (node) {
  node.drop();
});
parent.addChild(tree.parse({id: 'Europe'}));

var parent = root.first(function (node) {
    return node.model.id === 'Earth';
});
//Ask parent to remove all the siblings
parent.all(function (node) {
    //return not parent, i.e: children
    return node.model.id !== parent.model.id;
}).forEach(function (node) {
  node.drop();
});
parent.addChild(tree.parse({id: 'America'}));
console.log(root);



//Add new root
root = tree.parse({
  id: 'Mars'
});
//console.log(root);

//Add new root
root = tree.parse({
  id: 'Jupiter'
});
//console.log(root);

//Add new root
root = tree.parse({
  id: 'Saturn'
});
//console.log(root);

//Add new root
root = tree.parse({
  id: 'Uranus'
});
//console.log(root);

//Add new root
root = tree.parse({
  id: 'Neptune'
});
//console.log(root);