Very convenient objects hierarchies creation library.
var nano = require('nano-tree');
function MenuPage(type, id) {
nano.Node.call(this, type, id);
}
MenuPage.prototype = {
toString: function () { return this.type+(this.id.charAt(0) !== '$' ? '#'+this.id : ''); }
};
function MenuGroup(type, id) {
nano.Group.call(this, type, id);
};
MenuGroup.prototype = {
toString: function () {
var cs = this.children;
return MenuPage.prototype.toString.call(this)+':['+Object.keys(cs).map(function (key) {
return cs[key].toString();
}).join(',')+']';
}
};
nano.Node.expand(MenuPage);
nano.Group.expand(MenuGroup);
MenuGroup.install('page', MenuPage);
MenuGroup.install('group', MenuGroup);
var menu =
(new MenuGroup('menu', 'root'))
.group('internet')
.page('wan')
.up
.group('system')
.page('options')
.page('reboot')
.page('options') // test of duplicated id
.up
.group()
.page()
.up;
assert.strictEqual('menu#root:[group#internet:[page#wan],group#system:[page#reboot,page#options],group:[page]]', menu.toString());