side-nav.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <style>
  2. .side-nav {
  3. width: 100%;
  4. box-sizing: border-box;
  5. padding-right: 30px;
  6. transition: opacity .5s;
  7. &:hover {
  8. opacity: 1 !important;
  9. }
  10. li {
  11. list-style: none;
  12. }
  13. ul {
  14. padding: 0;
  15. margin: 0;
  16. overflow: hidden;
  17. }
  18. .nav-item {
  19. a {
  20. font-size: 16px;
  21. color: #333;
  22. line-height: 40px;
  23. height: 40px;
  24. margin: 0;
  25. padding: 0;
  26. text-decoration: none;
  27. display: block;
  28. position: relative;
  29. transition: .15s ease-out;
  30. font-weight: bold;
  31. &.active {
  32. color: #409EFF;
  33. }
  34. }
  35. .nav-item {
  36. a {
  37. display: block;
  38. height: 40px;
  39. color: #666;
  40. line-height: 40px;
  41. font-size: 14px;
  42. overflow: hidden;
  43. white-space: nowrap;
  44. text-overflow: ellipsis;
  45. font-weight: normal;
  46. &:hover,
  47. &.active {
  48. color: #409EFF;
  49. }
  50. }
  51. }
  52. }
  53. .nav-group__title {
  54. font-size: 12px;
  55. color: #999;
  56. line-height: 26px;
  57. margin-top: 15px;
  58. }
  59. #code-sponsor-widget {
  60. margin: 0 0 0 -20px;
  61. }
  62. }
  63. .nav-dropdown-list {
  64. width: 120px;
  65. margin-top: -8px;
  66. li {
  67. font-size: 14px;
  68. }
  69. }
  70. </style>
  71. <template>
  72. <div
  73. class="side-nav"
  74. @mouseenter="isFade = false"
  75. :style="navStyle">
  76. <ul>
  77. <li class="nav-item" v-for="item in data">
  78. <a v-if="!item.path && !item.href" @click="expandMenu">{{item.name}}</a>
  79. <a v-if="item.href" :href="item.href" target="_blank">{{item.name}}</a>
  80. <router-link
  81. v-if="item.path"
  82. active-class="active"
  83. :to="base + item.path"
  84. exact
  85. v-text="item.title || item.name">
  86. </router-link>
  87. <ul class="pure-menu-list sub-nav" v-if="item.children">
  88. <li class="nav-item" v-for="navItem in item.children">
  89. <router-link
  90. class=""
  91. active-class="active"
  92. :to="base + navItem.path"
  93. exact
  94. v-text="navItem.title || navItem.name">
  95. </router-link>
  96. </li>
  97. </ul>
  98. <template v-if="item.groups">
  99. <div class="nav-group" v-for="group in item.groups">
  100. <div class="nav-group__title" @click="expandMenu">{{group.groupName}}</div>
  101. <ul class="pure-menu-list">
  102. <li
  103. class="nav-item"
  104. v-for="navItem in group.list"
  105. v-if="!navItem.disabled">
  106. <router-link
  107. active-class="active"
  108. :to="base + navItem.path"
  109. exact
  110. v-text="navItem.title"></router-link>
  111. </li>
  112. </ul>
  113. </div>
  114. </template>
  115. </li>
  116. </ul>
  117. <div id="code-sponsor-widget"></div>
  118. </div>
  119. </template>
  120. <script>
  121. import bus from '../bus';
  122. import compoLang from '../i18n/component.json';
  123. export default {
  124. props: {
  125. data: Array,
  126. base: {
  127. type: String,
  128. default: ''
  129. }
  130. },
  131. data() {
  132. return {
  133. highlights: [],
  134. navState: [],
  135. isSmallScreen: false,
  136. isFade: false
  137. };
  138. },
  139. watch: {
  140. '$route.path'() {
  141. this.handlePathChange();
  142. },
  143. isFade(val) {
  144. bus.$emit('navFade', val);
  145. }
  146. },
  147. computed: {
  148. navStyle() {
  149. const style = {};
  150. if (this.isSmallScreen) {
  151. style.paddingBottom = '60px';
  152. }
  153. if (this.isFade) {
  154. style.opacity = '0.5';
  155. }
  156. return style;
  157. },
  158. langConfig() {
  159. return compoLang.filter(config => config.lang === this.$route.meta.lang)[0]['nav'];
  160. }
  161. },
  162. methods: {
  163. handleResize() {
  164. this.isSmallScreen = document.documentElement.clientWidth < 768;
  165. this.handlePathChange();
  166. },
  167. handlePathChange() {
  168. if (!this.isSmallScreen) {
  169. this.expandAllMenu();
  170. return;
  171. }
  172. this.$nextTick(() => {
  173. this.hideAllMenu();
  174. let activeAnchor = this.$el.querySelector('a.active');
  175. let ul = activeAnchor.parentNode;
  176. while (ul.tagName !== 'UL') {
  177. ul = ul.parentNode;
  178. }
  179. ul.style.height = 'auto';
  180. });
  181. },
  182. hideAllMenu() {
  183. [].forEach.call(this.$el.querySelectorAll('.pure-menu-list'), ul => {
  184. ul.style.height = '0';
  185. });
  186. },
  187. expandAllMenu() {
  188. [].forEach.call(this.$el.querySelectorAll('.pure-menu-list'), ul => {
  189. ul.style.height = 'auto';
  190. });
  191. },
  192. expandMenu(event) {
  193. if (!this.isSmallScreen) return;
  194. let target = event.currentTarget;
  195. if (!target.nextElementSibling || target.nextElementSibling.tagName !== 'UL') return;
  196. this.hideAllMenu();
  197. event.currentTarget.nextElementSibling.style.height = 'auto';
  198. }
  199. },
  200. created() {
  201. bus.$on('fadeNav', () => {
  202. this.isFade = true;
  203. });
  204. },
  205. mounted() {
  206. this.handleResize();
  207. window.addEventListener('resize', this.handleResize);
  208. },
  209. beforeDestroy() {
  210. window.removeEventListener('resize', this.handleResize);
  211. }
  212. };
  213. </script>