menu-openwrt2020.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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(node);
  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. document.querySelector('#menubar > .navigation')
  21. .addEventListener('click', ui.createHandlerFn(this, 'handleSidebarToggle'));
  22. },
  23. handleMenuExpand: function(ev) {
  24. var a = ev.target, ul1 = a.parentNode.parentNode, ul2 = a.nextElementSibling;
  25. document.querySelectorAll('ul.mainmenu.l1 > li.active').forEach(function(li) {
  26. if (li !== a.parentNode)
  27. li.classList.remove('active');
  28. });
  29. if (!ul2)
  30. return;
  31. if (ul2.parentNode.offsetLeft + ul2.offsetWidth <= ul1.offsetLeft + ul1.offsetWidth)
  32. ul2.classList.add('align-left');
  33. ul1.classList.add('active');
  34. a.parentNode.classList.add('active');
  35. a.blur();
  36. ev.preventDefault();
  37. ev.stopPropagation();
  38. },
  39. renderMainMenu: function(tree, url, level) {
  40. var l = (level || 0) + 1,
  41. ul = E('ul', { 'class': 'mainmenu l%d'.format(l) }),
  42. children = ui.menu.getChildren(tree);
  43. if (children.length == 0 || l > 2)
  44. return E([]);
  45. for (var i = 0; i < children.length; i++) {
  46. var isActive = (L.env.dispatchpath[l] == children[i].name),
  47. isReadonly = children[i].readonly,
  48. activeClass = 'mainmenu-item-%s%s'.format(children[i].name, isActive ? ' selected' : '');
  49. ul.appendChild(E('li', { 'class': activeClass }, [
  50. E('a', {
  51. 'href': L.url(url, children[i].name),
  52. 'click': (l == 1) ? ui.createHandlerFn(this, 'handleMenuExpand') : null
  53. }, [ _(children[i].title) ]),
  54. this.renderMainMenu(children[i], url + '/' + children[i].name, l)
  55. ]));
  56. }
  57. if (l == 1)
  58. document.querySelector('#mainmenu').appendChild(E('div', [ ul ]));
  59. return ul;
  60. },
  61. renderModeMenu: function(tree) {
  62. var menu = document.querySelector('#modemenu'),
  63. children = ui.menu.getChildren(tree);
  64. for (var i = 0; i < children.length; i++) {
  65. var isActive = (L.env.requestpath.length ? children[i].name == L.env.requestpath[0] : i == 0);
  66. if (i > 0)
  67. menu.appendChild(E([], ['\u00a0|\u00a0']));
  68. menu.appendChild(E('div', { 'class': isActive ? 'active' : null }, [
  69. E('a', { 'href': L.url(children[i].name) }, [ _(children[i].title) ])
  70. ]));
  71. if (isActive)
  72. this.renderMainMenu(children[i], children[i].name);
  73. }
  74. if (menu.children.length > 1)
  75. menu.style.display = '';
  76. },
  77. renderTabMenu: function(tree, url, level) {
  78. var container = document.querySelector('#tabmenu'),
  79. l = (level || 0) + 1,
  80. ul = E('ul', { 'class': 'cbi-tabmenu' }),
  81. children = ui.menu.getChildren(tree),
  82. activeNode = null;
  83. if (children.length == 0)
  84. return E([]);
  85. for (var i = 0; i < children.length; i++) {
  86. var isActive = (L.env.dispatchpath[l + 2] == children[i].name),
  87. activeClass = isActive ? ' cbi-tab' : '',
  88. className = 'tabmenu-item-%s %s'.format(children[i].name, activeClass);
  89. ul.appendChild(E('li', { 'class': className }, [
  90. E('a', { 'href': L.url(url, children[i].name) }, [ _(children[i].title) ] )
  91. ]));
  92. if (isActive)
  93. activeNode = children[i];
  94. }
  95. container.appendChild(ul);
  96. container.style.display = '';
  97. if (activeNode)
  98. container.appendChild(this.renderTabMenu(activeNode, url + '/' + activeNode.name, l));
  99. return ul;
  100. },
  101. handleSidebarToggle: function(ev) {
  102. var btn = ev.currentTarget,
  103. bar = document.querySelector('#mainmenu');
  104. if (btn.classList.contains('active')) {
  105. btn.classList.remove('active');
  106. bar.classList.remove('active');
  107. }
  108. else {
  109. btn.classList.add('active');
  110. bar.classList.add('active');
  111. }
  112. }
  113. });