menu-material.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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('.showSide')
  21. .addEventListener('click', ui.createHandlerFn(this, 'handleSidebarToggle'));
  22. document.querySelector('.darkMask')
  23. .addEventListener('click', ui.createHandlerFn(this, 'handleSidebarToggle'));
  24. document.querySelector(".main > .loading").style.opacity = '0';
  25. document.querySelector(".main > .loading").style.visibility = 'hidden';
  26. if (window.innerWidth <= 1152)
  27. document.querySelector('.main-left').style.width = '0';
  28. window.addEventListener('resize', this.handleSidebarToggle, true);
  29. },
  30. handleMenuExpand: function(ev) {
  31. var a = ev.target, ul1 = a.parentNode, ul2 = a.nextElementSibling;
  32. document.querySelectorAll('li.slide.active').forEach(function(li) {
  33. if (li !== a.parentNode || li == ul1) {
  34. li.classList.remove('active');
  35. li.childNodes[0].classList.remove('active');
  36. }
  37. if (li == ul1)
  38. return;
  39. });
  40. if (!ul2)
  41. return;
  42. if (ul2.parentNode.offsetLeft + ul2.offsetWidth <= ul1.offsetLeft + ul1.offsetWidth)
  43. ul2.classList.add('align-left');
  44. ul1.classList.add('active');
  45. a.classList.add('active');
  46. a.blur();
  47. ev.preventDefault();
  48. ev.stopPropagation();
  49. },
  50. renderMainMenu: function(tree, url, level) {
  51. var l = (level || 0) + 1,
  52. ul = E('ul', { 'class': level ? 'slide-menu' : 'nav' }),
  53. children = ui.menu.getChildren(tree);
  54. if (children.length == 0 || l > 2)
  55. return E([]);
  56. for (var i = 0; i < children.length; i++) {
  57. var isActive = (L.env.dispatchpath[l] == children[i].name),
  58. submenu = this.renderMainMenu(children[i], url + '/' + children[i].name, l),
  59. hasChildren = submenu.children.length;
  60. ul.appendChild(E('li', { 'class': (hasChildren ? 'slide' + (isActive ? ' active' : '') : (isActive ? ' active' : ''))}, [
  61. E('a', {
  62. 'href': hasChildren ? '#' : L.url(url, children[i].name),
  63. 'class': hasChildren ? 'menu' + (isActive ? ' active' : '') : null,
  64. 'click': hasChildren ? ui.createHandlerFn(this, 'handleMenuExpand') : null,
  65. 'data-title': hasChildren ? null : _(children[i].title),
  66. }, [ _(children[i].title) ]),
  67. submenu
  68. ]));
  69. }
  70. if (l == 1) {
  71. var container = document.querySelector('#mainmenu');
  72. container.appendChild(ul);
  73. container.style.display = '';
  74. }
  75. return ul;
  76. },
  77. renderModeMenu: function(tree) {
  78. var ul = document.querySelector('#modemenu'),
  79. children = ui.menu.getChildren(tree);
  80. for (var i = 0; i < children.length; i++) {
  81. var isActive = (L.env.requestpath.length ? children[i].name == L.env.requestpath[0] : i == 0);
  82. ul.appendChild(E('li', {}, [
  83. E('a', {
  84. 'href': L.url(children[i].name),
  85. 'class': isActive ? 'active' : null
  86. }, [ _(children[i].title) ])
  87. ]));
  88. if (isActive)
  89. this.renderMainMenu(children[i], children[i].name);
  90. if (i > 0 && i < children.length)
  91. ul.appendChild(E('li', {'class': 'divider'}, [E('span')]))
  92. }
  93. if (children.length > 1)
  94. ul.parentElement.style.display = '';
  95. },
  96. renderTabMenu: function(tree, url, level) {
  97. var container = document.querySelector('#tabmenu'),
  98. l = (level || 0) + 1,
  99. ul = E('ul', { 'class': 'tabs' }),
  100. children = ui.menu.getChildren(tree),
  101. activeNode = null;
  102. if (children.length == 0)
  103. return E([]);
  104. for (var i = 0; i < children.length; i++) {
  105. var isActive = (L.env.dispatchpath[l + 2] == children[i].name),
  106. activeClass = isActive ? ' active' : '',
  107. className = 'tabmenu-item-%s %s'.format(children[i].name, activeClass);
  108. ul.appendChild(E('li', { 'class': className }, [
  109. E('a', { 'href': L.url(url, children[i].name) }, [ _(children[i].title) ] )
  110. ]));
  111. if (isActive)
  112. activeNode = children[i];
  113. }
  114. container.appendChild(ul);
  115. container.style.display = '';
  116. if (activeNode)
  117. container.appendChild(this.renderTabMenu(activeNode, url + '/' + activeNode.name, l));
  118. return ul;
  119. },
  120. handleSidebarToggle: function(ev) {
  121. var width = window.innerWidth,
  122. darkMask = document.querySelector('.darkMask'),
  123. mainRight = document.querySelector('.main-right'),
  124. mainLeft = document.querySelector('.main-left'),
  125. open = mainLeft.style.width == '';
  126. if (width > 1152 || ev.type == 'resize')
  127. open = true;
  128. darkMask.style.visibility = open ? '' : 'visible';
  129. darkMask.style.opacity = open ? '': 1;
  130. if (width <= 1152)
  131. mainLeft.style.width = open ? '0' : '';
  132. else
  133. mainLeft.style.width = ''
  134. mainLeft.style.visibility = open ? '' : 'visible';
  135. mainRight.style['overflow-y'] = open ? 'visible' : 'hidden';
  136. }
  137. });