TagsView.vue 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <template>
  2. <div class="tags-view-container">
  3. <!-- <scroll-pane class="tags-view-wrapper" ref="scrollPane">
  4. <router-link
  5. ref="tag"
  6. class="tags-view-item"
  7. :class="isActive(tag) ? 'active' : ''"
  8. v-for="tag in Array.from(visitedViews)"
  9. :to="tag"
  10. :key="tag.path"
  11. @contextmenu.prevent.native="openMenu(tag, $event)"
  12. >
  13. {{ generateTitle(tag.title) }}
  14. <span
  15. class="el-icon-close"
  16. @click.prevent.stop="closeSelectedTag(tag)"
  17. ></span>
  18. </router-link>
  19. </scroll-pane>
  20. <ul
  21. class="contextmenu"
  22. v-show="visible"
  23. :style="{ left: left + 'px', top: top + 'px' }"
  24. >
  25. <li @click="closeSelectedTag(selectedTag)">{{ $t("tagsView.close") }}</li>
  26. <li @click="closeOthersTags">{{ $t("tagsView.closeOthers") }}</li>
  27. <li @click="closeAllTags">{{ $t("tagsView.closeAll") }}</li>
  28. </ul>-->
  29. <div class="tags-view-wrapper">
  30. <div style="height:100%">
  31. <div style="height:100%" v-if="newIndex == 9">
  32. <div class="tagsBox" v-if="routerList.length > 0 && newNum != 999">
  33. <div
  34. class="tagsOne"
  35. v-if="routerList[newIndex].meta"
  36. v-for="(item, i) in routerList[newIndex].children"
  37. :key="i"
  38. >
  39. <router-link :to="item.path" v-if="item.num ? item.num == newNum : false">
  40. <p
  41. class="newTags"
  42. :class="activeClass == i ? 'active' : ''"
  43. @click="onClick(i)"
  44. >{{ generateTitle(item.meta.title) }}</p>
  45. </router-link>
  46. </div>
  47. </div>
  48. <div class="tagsBox" v-if="newNum == 999">
  49. <div
  50. class="tagsOne"
  51. v-if="permission_routers[newIndex].meta"
  52. v-for="(item, i) in permission_routers[newIndex].children"
  53. :key="i"
  54. >
  55. <router-link :to="item.path" v-if="item.num ? item.num == newNum : false">
  56. <p
  57. class="newTags"
  58. :class="activeClass == i ? 'active' : ''"
  59. @click="onClick(i)"
  60. >{{ generateTitle(item.meta.title) }}</p>
  61. </router-link>
  62. </div>
  63. </div>
  64. </div>
  65. <div class="tagsBox" v-if="newIndex != 9">
  66. <div
  67. class="tagsOne"
  68. v-if="permission_routers[newIndex].meta"
  69. v-for="(item, i) in permission_routers[newIndex].children"
  70. :key="i"
  71. >
  72. <router-link :to="item.path" v-if="!item.hidden && !item.is_menu">
  73. <p
  74. class="newTags"
  75. :class="activeClass == i ? 'active' : ''"
  76. @click="onClick(i)"
  77. >{{ generateTitle(item.meta.title) }}</p>
  78. </router-link>
  79. </div>
  80. <div v-else class="tagsOne">
  81. <p class="newTags" :class="activeClass == i ? 'active' : ''" @click="onClick(i)">
  82. {{
  83. generateTitle(permission_routers[newIndex].children[0].meta.title)
  84. }}
  85. </p>
  86. </div>
  87. </div>
  88. </div>
  89. </div>
  90. </div>
  91. </template>
  92. <script>
  93. import ScrollPane from "@/components/ScrollPane";
  94. import { generateTitle } from "@/utils/i18n";
  95. //
  96. import { mapGetters } from "vuex";
  97. export default {
  98. props: {
  99. index: Number,
  100. num: Number
  101. },
  102. components: { ScrollPane },
  103. data() {
  104. return {
  105. visible: false,
  106. top: 0,
  107. left: 0,
  108. selectedTag: {},
  109. newIndex: 0,
  110. activeClass: 0,
  111. newNum: 1
  112. };
  113. },
  114. computed: {
  115. // visitedViews() {
  116. // return this.$store.state.tagsView.visitedViews;
  117. // },
  118. ...mapGetters(["permission_routers"])
  119. },
  120. watch: {
  121. // $route() {
  122. // this.addViewTags();
  123. // this.moveToCurrentTag();
  124. // },
  125. // visible(value) {
  126. // if (value) {
  127. // document.body.addEventListener("click", this.closeMenu);
  128. // } else {
  129. // document.body.removeEventListener("click", this.closeMenu);
  130. // }
  131. // },
  132. index(newVal) {
  133. if (this.newIndex != newVal) {
  134. this.activeClass = 0;
  135. }
  136. this.newIndex = this.index;
  137. },
  138. num(newVal) {
  139. this.newNum = newVal;
  140. if (newVal == 999) {
  141. this.routerList = [];
  142. } else {
  143. this.routerList = this.permission_routers;
  144. }
  145. if (newVal == 1) {
  146. this.activeClass = 2;
  147. }
  148. if (newVal == 2) {
  149. this.activeClass = 6;
  150. }
  151. }
  152. },
  153. created() {
  154. this.routerList = this.permission_routers;
  155. },
  156. mounted() {
  157. // this.addViewTags();
  158. },
  159. methods: {
  160. generateTitle, // generateTitle by vue-i18n
  161. generateRoute() {
  162. if (this.$route.name) {
  163. return this.$route;
  164. }
  165. return false;
  166. },
  167. isActive(route) {
  168. return route.path === this.$route.path;
  169. },
  170. addViewTags() {
  171. const route = this.generateRoute();
  172. if (!route) {
  173. return false;
  174. }
  175. this.$store.dispatch("addVisitedViews", route);
  176. },
  177. moveToCurrentTag() {
  178. const tags = this.$refs.tag;
  179. this.$nextTick(() => {
  180. for (const tag of tags) {
  181. if (tag.to.path === this.$route.path) {
  182. this.$refs.scrollPane.moveToTarget(tag.$el);
  183. break;
  184. }
  185. }
  186. });
  187. },
  188. closeSelectedTag(view) {
  189. this.$store.dispatch("delVisitedViews", view).then(views => {
  190. if (this.isActive(view)) {
  191. const latestView = views.slice(-1)[0];
  192. if (latestView) {
  193. this.$router.push(latestView);
  194. } else {
  195. this.$router.push("/");
  196. }
  197. }
  198. });
  199. },
  200. closeOthersTags() {
  201. this.$router.push(this.selectedTag);
  202. this.$store.dispatch("delOthersViews", this.selectedTag).then(() => {
  203. this.moveToCurrentTag();
  204. });
  205. },
  206. closeAllTags() {
  207. this.$store.dispatch("delAllViews");
  208. this.$router.push("/");
  209. },
  210. openMenu(tag, e) {
  211. this.visible = true;
  212. this.selectedTag = tag;
  213. const offsetLeft = this.$el.getBoundingClientRect().left; // container margin left
  214. this.left = e.clientX - offsetLeft + 15; // 15: margin right
  215. this.top = e.clientY;
  216. },
  217. closeMenu() {
  218. this.visible = false;
  219. },
  220. onClick(index) {
  221. console.log(index);
  222. this.activeClass = index;
  223. }
  224. }
  225. };
  226. </script>
  227. <style rel="stylesheet/scss" lang="scss" scoped>
  228. .tags-view-container {
  229. height: 60px;
  230. .tags-view-wrapper {
  231. background: #fff;
  232. height: 50px;
  233. border-bottom: 1px solid #d8dce5;
  234. box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.12), 0 0 3px 0 rgba(0, 0, 0, 0.04);
  235. position: fixed;
  236. z-index: 1000;
  237. top: 50px;
  238. overflow-x: scroll;
  239. white-space: normal;
  240. // right: 0;
  241. // left: 150px;
  242. width: 100%;
  243. /* 针对缺省样式 (必须的) */
  244. &::-webkit-scrollbar {
  245. width: 6px;
  246. height: 2px;
  247. }
  248. /* 滚动条的滑轨背景颜色 */
  249. &::-webkit-scrollbar-track {
  250. background-color: #fff;
  251. border-radius: 3px;
  252. -moz-border-radius: 3px;
  253. -webkit-border-radius: 3px;
  254. }
  255. /* 滑块颜色 */
  256. &::-webkit-scrollbar-thumb {
  257. background: #fff;
  258. border-radius: 3px;
  259. -moz-border-radius: 3px;
  260. -webkit-border-radius: 3px;
  261. }
  262. /*内层轨道的颜色*/
  263. &::-webkit-scrollbar-track-piece {
  264. background-color: #fff;
  265. border-radius: 3px;
  266. -moz-border-radius: 3px;
  267. -webkit-border-radius: 3px;
  268. }
  269. /* 滑轨两头的监听按钮颜色 */
  270. &::-webkit-scrollbar-button {
  271. background-color: #fff;
  272. width: 0;
  273. height: 0;
  274. }
  275. /* 横向滚动条和纵向滚动条相交处尖角的颜色 */
  276. &::-webkit-scrollbar-corner {
  277. background-color: #fff;
  278. }
  279. .tags-view-item {
  280. display: inline-block;
  281. position: relative;
  282. border: 1px solid #d8dce5;
  283. color: #495060;
  284. background: #fff;
  285. padding: 0 4px 0 8px;
  286. font-size: 12px;
  287. margin: 12px 0 12px 5px;
  288. border-radius: 4px;
  289. height: 28px;
  290. line-height: 28px;
  291. &:first-of-type {
  292. margin-left: 15px;
  293. }
  294. &.active {
  295. background-color: #409efe;
  296. color: #fff;
  297. border-color: #409efe;
  298. &::before {
  299. content: "";
  300. background: #fff;
  301. display: inline-block;
  302. width: 8px;
  303. height: 8px;
  304. border-radius: 50%;
  305. position: relative;
  306. margin-right: 2px;
  307. }
  308. }
  309. }
  310. }
  311. .contextmenu {
  312. margin: 0;
  313. background: #fff;
  314. z-index: 100;
  315. position: absolute;
  316. list-style-type: none;
  317. padding: 5px 0;
  318. border-radius: 4px;
  319. font-size: 12px;
  320. font-weight: 400;
  321. color: #333;
  322. box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, 0.3);
  323. li {
  324. margin: 0;
  325. padding: 7px 16px;
  326. cursor: pointer;
  327. &:hover {
  328. background: #eee;
  329. }
  330. }
  331. }
  332. .tagsBox {
  333. display: flex;
  334. align-items: center;
  335. height: 100%;
  336. .tagsOne {
  337. .newTags {
  338. border: 1px solid rgba(235, 238, 240, 1);
  339. border-radius: 2px;
  340. padding: 8px 12px;
  341. margin-left: 10px;
  342. font-size: 14px;
  343. }
  344. }
  345. .active {
  346. border: 1px solid rgba(64, 158, 255, 1);
  347. background: rgba(64, 158, 255, 1);
  348. border-radius: 2px;
  349. color: #fff;
  350. }
  351. }
  352. }
  353. .scroll-wrapper {
  354. right: 0;
  355. position: fixed !important;
  356. }
  357. </style>
  358. <style rel="stylesheet/scss" lang="scss">
  359. //reset element css of el-icon-close
  360. .tags-view-wrapper {
  361. .tags-view-item {
  362. .el-icon-close {
  363. width: 16px;
  364. height: 16px;
  365. vertical-align: 2px;
  366. border-radius: 50%;
  367. text-align: center;
  368. transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
  369. transform-origin: 100% 50%;
  370. &:before {
  371. transform: scale(0.6);
  372. display: inline-block;
  373. vertical-align: -3px;
  374. }
  375. &:hover {
  376. background-color: #b4bccc;
  377. color: #fff;
  378. }
  379. }
  380. }
  381. }
  382. </style>