menu-bootstrap.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict';
  2. 'require baseclass';
  3. 'require ui';
  4. return baseclass.extend({
  5. __init__: function() {
  6. ui.menu.load().then(L.bind(this.render, this));
  7. },
  8. render: function(tree) {
  9. var node = tree,
  10. url = '';
  11. this.renderModeMenu(tree);
  12. if (L.env.dispatchpath.length >= 3) {
  13. for (var i = 0; i < 3 && node; i++) {
  14. node = node.children[L.env.dispatchpath[i]];
  15. url = url + (url ? '/' : '') + L.env.dispatchpath[i];
  16. }
  17. if (node)
  18. this.renderTabMenu(node, url);
  19. }
  20. },
  21. renderTabMenu: function(tree, url, level) {
  22. var container = document.querySelector('#tabmenu'),
  23. ul = E('ul', { 'class': 'tabs' }),
  24. children = ui.menu.getChildren(tree),
  25. activeNode = null;
  26. for (var i = 0; i < children.length; i++) {
  27. var isActive = (L.env.dispatchpath[3 + (level || 0)] == children[i].name),
  28. activeClass = isActive ? ' active' : '',
  29. className = 'tabmenu-item-%s %s'.format(children[i].name, activeClass);
  30. ul.appendChild(E('li', { 'class': className }, [
  31. E('a', { 'href': L.url(url, children[i].name) }, [ _(children[i].title) ] )]));
  32. if (isActive)
  33. activeNode = children[i];
  34. }
  35. if (ul.children.length == 0)
  36. return E([]);
  37. container.appendChild(ul);
  38. container.style.display = '';
  39. if (activeNode)
  40. this.renderTabMenu(activeNode, url + '/' + activeNode.name, (level || 0) + 1);
  41. return ul;
  42. },
  43. renderMainMenu: function(tree, url, level) {
  44. var ul = level ? E('ul', { 'class': 'dropdown-menu' }) : document.querySelector('#topmenu'),
  45. children = ui.menu.getChildren(tree);
  46. if (children.length == 0 || level > 1)
  47. return E([]);
  48. for (var i = 0; i < children.length; i++) {
  49. var submenu = this.renderMainMenu(children[i], url + '/' + children[i].name, (level || 0) + 1),
  50. subclass = (!level && submenu.firstElementChild) ? 'dropdown' : null,
  51. linkclass = (!level && submenu.firstElementChild) ? 'menu' : null,
  52. linkurl = submenu.firstElementChild ? '#' : L.url(url, children[i].name);
  53. var li = E('li', { 'class': subclass }, [
  54. E('a', { 'class': linkclass, 'href': linkurl }, [ _(children[i].title) ]),
  55. submenu
  56. ]);
  57. ul.appendChild(li);
  58. }
  59. ul.style.display = '';
  60. return ul;
  61. },
  62. renderModeMenu: function(tree) {
  63. var ul = document.querySelector('#modemenu'),
  64. children = ui.menu.getChildren(tree);
  65. for (var i = 0; i < children.length; i++) {
  66. var isActive = (L.env.requestpath.length ? children[i].name == L.env.requestpath[0] : i == 0);
  67. ul.appendChild(E('li', { 'class': isActive ? 'active' : null }, [
  68. E('a', { 'href': L.url(children[i].name) }, [ _(children[i].title) ])
  69. ]));
  70. if (isActive)
  71. this.renderMainMenu(children[i], children[i].name);
  72. }
  73. if (ul.children.length > 1)
  74. ul.style.display = '';
  75. }
  76. });