side-nav.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #code-sponsor-widget {
  95. margin: 50px 0 0 -20px;
  96. }
  97. }
  98. .nav-dropdown-list {
  99. width: 120px;
  100. margin-top: -8px;
  101. li {
  102. font-size: 14px;
  103. }
  104. }
  105. </style>
  106. <template>
  107. <div
  108. class="side-nav"
  109. @mouseenter="isFade = false"
  110. :style="navStyle">
  111. <ul>
  112. <li class="nav-item" v-for="item in data">
  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 class="nav-item" v-for="navItem in item.children">
  124. <router-link
  125. class=""
  126. active-class="active"
  127. :to="base + navItem.path"
  128. exact
  129. v-text="navItem.title || navItem.name">
  130. </router-link>
  131. </li>
  132. </ul>
  133. <template v-if="item.groups">
  134. <div class="nav-group" v-for="group in item.groups">
  135. <div class="nav-group__title" @click="expandMenu">{{group.groupName}}</div>
  136. <ul class="pure-menu-list">
  137. <li
  138. class="nav-item"
  139. v-for="navItem in group.list"
  140. v-if="!navItem.disabled">
  141. <router-link
  142. active-class="active"
  143. :to="base + navItem.path"
  144. exact
  145. v-text="navItem.title"></router-link>
  146. </li>
  147. </ul>
  148. </div>
  149. </template>
  150. </li>
  151. </ul>
  152. <div id="code-sponsor-widget"></div>
  153. </div>
  154. </template>
  155. <script>
  156. import bus from '../bus';
  157. import compoLang from '../i18n/component.json';
  158. import { version } from 'main/index.js';
  159. export default {
  160. props: {
  161. data: Array,
  162. base: {
  163. type: String,
  164. default: ''
  165. }
  166. },
  167. data() {
  168. return {
  169. highlights: [],
  170. navState: [],
  171. isSmallScreen: false,
  172. versions: [],
  173. version,
  174. dropdownVisible: false,
  175. isFade: false
  176. };
  177. },
  178. watch: {
  179. '$route.path'() {
  180. this.handlePathChange();
  181. },
  182. isFade(val) {
  183. bus.$emit('navFade', val);
  184. }
  185. },
  186. computed: {
  187. navStyle() {
  188. const style = {};
  189. if (this.isSmallScreen) {
  190. style.paddingBottom = '60px';
  191. }
  192. if (this.isFade) {
  193. style.opacity = '0.5';
  194. }
  195. return style;
  196. },
  197. isComponentPage() {
  198. return /^component-/.test(this.$route.name);
  199. },
  200. langConfig() {
  201. return compoLang.filter(config => config.lang === this.$route.meta.lang)[0]['nav'];
  202. }
  203. },
  204. methods: {
  205. switchVersion(version) {
  206. if (version === this.version) return;
  207. location.href = `${ location.origin }/${ this.versions[version] }/${ location.hash } `;
  208. },
  209. handleResize() {
  210. this.isSmallScreen = document.documentElement.clientWidth < 768;
  211. this.handlePathChange();
  212. },
  213. handlePathChange() {
  214. if (!this.isSmallScreen) {
  215. this.expandAllMenu();
  216. return;
  217. }
  218. this.$nextTick(() => {
  219. this.hideAllMenu();
  220. let activeAnchor = this.$el.querySelector('a.active');
  221. let ul = activeAnchor.parentNode;
  222. while (ul.tagName !== 'UL') {
  223. ul = ul.parentNode;
  224. }
  225. ul.style.height = 'auto';
  226. });
  227. },
  228. hideAllMenu() {
  229. [].forEach.call(this.$el.querySelectorAll('.pure-menu-list'), ul => {
  230. ul.style.height = '0';
  231. });
  232. },
  233. expandAllMenu() {
  234. [].forEach.call(this.$el.querySelectorAll('.pure-menu-list'), ul => {
  235. ul.style.height = 'auto';
  236. });
  237. },
  238. expandMenu(event) {
  239. if (!this.isSmallScreen) return;
  240. let target = event.currentTarget;
  241. if (!target.nextElementSibling || target.nextElementSibling.tagName !== 'UL') return;
  242. this.hideAllMenu();
  243. event.currentTarget.nextElementSibling.style.height = 'auto';
  244. },
  245. handleDropdownToggle(visible) {
  246. this.dropdownVisible = visible;
  247. }
  248. },
  249. created() {
  250. bus.$on('fadeNav', () => {
  251. this.isFade = true;
  252. });
  253. const xhr = new XMLHttpRequest();
  254. xhr.onreadystatechange = _ => {
  255. if (xhr.readyState === 4 && xhr.status === 200) {
  256. this.versions = JSON.parse(xhr.responseText);
  257. }
  258. };
  259. xhr.open('GET', '/versions.json');
  260. xhr.send();
  261. },
  262. mounted() {
  263. this.handleResize();
  264. window.addEventListener('resize', this.handleResize);
  265. },
  266. beforeDestroy() {
  267. window.removeEventListener('resize', this.handleResize);
  268. }
  269. };
  270. </script>