side-nav.vue 6.2 KB

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