menu-openwrt.js 3.3 KB

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