side-nav.vue 6.5 KB

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