side-nav.vue 6.1 KB

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