p-tabs.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <template>
  2. <div>
  3. <transition-group class="p-tabs" name="tab" tag="div" ref="ptab">
  4. <div
  5. class="tab-item"
  6. v-for="(item, index) in list"
  7. :key="item.name + item.rountPath || item.jspUrl"
  8. :class="{
  9. 'tab-item-active':
  10. (item.rountPath == undefined
  11. ? item.jspUrl
  12. : item.rountPath) == getDefaultActive,
  13. }"
  14. @click="handleClick($event, item, index)"
  15. @contextmenu.prevent="clickright($event, item, index)"
  16. >
  17. <!-- <div class="tab-item-shap"></div> -->
  18. <el-tooltip
  19. placement="bottom"
  20. effect="dark"
  21. :content="item.name || item.functionName"
  22. :visible-arrow="false"
  23. :open-delay="1000"
  24. >
  25. <div class="text">
  26. {{ item.name || item.functionName }}
  27. </div>
  28. </el-tooltip>
  29. <i
  30. class="el-icon-error close"
  31. @click="handleClose($event, item, index)"
  32. ></i>
  33. </div>
  34. </transition-group>
  35. <div class="rightCbox" :style="cbox" v-if="rcbStatus">
  36. <el-card shadow="always">
  37. <span @click="closetab(1)">关闭其它所有标签</span>
  38. <span @click="closetab(2)">关闭左侧所有标签</span>
  39. <span @click="closetab(3)">关闭右侧所有标签</span>
  40. </el-card>
  41. </div>
  42. </div>
  43. </template>
  44. <script>
  45. export default {
  46. props: {
  47. list: {
  48. required: false,
  49. type: Array,
  50. default() {
  51. return [];
  52. },
  53. },
  54. },
  55. data() {
  56. return {
  57. isDrag: false,
  58. moveX: 0,
  59. touchX: 0,
  60. lastTouchX: 0,
  61. eachWidth: 0,
  62. offsetWidth: 0,
  63. scrollWidth: 0,
  64. mouseDownTime: 0,
  65. canClick: true,
  66. scrolling: false,
  67. cbox: {
  68. top: 0,
  69. left: 0,
  70. },
  71. rcbStatus: false,
  72. tabsobj: {},
  73. };
  74. },
  75. computed: {
  76. getDefaultActive() {
  77. return this.$store.state.defaultActive;
  78. },
  79. },
  80. mounted() {
  81. this.initData();
  82. this.$nextTick(() => {
  83. document.querySelector("body").addEventListener("click", this.closetab);
  84. });
  85. },
  86. watch: {
  87. /**
  88. * 监听默认选中对象值的变化,当菜单切换时自动滚动到指定tab
  89. * @param {Object} v
  90. */
  91. getDefaultActive(v) {
  92. for (let i = 0; i < this.list.length; i++) {
  93. if (this.list[i].rountPath == v) {
  94. this.scrollTo(i);
  95. }
  96. }
  97. },
  98. },
  99. updated() {
  100. this.initData();
  101. },
  102. methods: {
  103. initData() {
  104. if (this.list.length > 0) {
  105. this.eachWidth = this.$refs.ptab.$el.children[0].offsetWidth;
  106. this.offsetWidth = this.$refs.ptab.$el.offsetWidth;
  107. this.scrollWidth = this.$refs.ptab.$el.scrollWidth;
  108. let _this = this;
  109. this.$refs.ptab.$el.onmousedown = (e) => {
  110. _this.mouseDown(e);
  111. };
  112. this.$refs.ptab.$el.onmousemove = (e) => {
  113. _this.mouseMove(e);
  114. };
  115. this.$refs.ptab.$el.onmouseup = (e) => {
  116. _this.mouseUp(e);
  117. };
  118. this.$refs.ptab.$el.onmouseout = (e) => {
  119. _this.mouseUp(e);
  120. };
  121. }
  122. },
  123. /**
  124. * 删除tab元素触发关闭页面的逻辑
  125. * @param {Object} e 事件对象
  126. * @param {Object} item 当前点击的tab元素数据
  127. * @param {Object} index 当前tab的下标
  128. */
  129. handleClose(e, item, index) {
  130. this.$store.commit(
  131. "removePagesByName",
  132. (item.rountPath == undefined ? item.jspUrl : item.rountPath).replace(
  133. "/",
  134. ""
  135. )
  136. );
  137. e.preventDefault();
  138. e.stopPropagation();
  139. let list = [...this.list];
  140. if (list.length > 1) {
  141. if (
  142. (item.rountPath == undefined ? item.jspUrl : item.rountPath) !=
  143. this.getDefaultActive
  144. ) {
  145. list.splice(index, 1);
  146. this.$store.commit("setTabList", list);
  147. } else {
  148. if (list[index - 1]) {
  149. let rountPath = list[index - 1].hasOwnProperty("rountPath")
  150. ? list[index - 1].rountPath
  151. : list[index - 1].jspUrl;
  152. this.$store.commit("setDefaultActive", rountPath);
  153. list.splice(index, 1);
  154. this.$store.commit("setTabList", list);
  155. this.$router.push(rountPath);
  156. } else if (list[index + 1]) {
  157. let rountPath = list[index + 1].hasOwnProperty("rountPath")
  158. ? list[index + 1].rountPath
  159. : list[index + 1].jspUrl;
  160. this.$store.commit("setDefaultActive", rountPath);
  161. list.splice(index, 1);
  162. this.$store.commit("setTabList", list);
  163. this.$router.push(rountPath);
  164. } else {
  165. list.splice(index, 1);
  166. this.$store.commit("setTabList", list);
  167. this.$router.push("/");
  168. }
  169. }
  170. }
  171. },
  172. /**
  173. * 点击tab触发的事件
  174. * @param {Object} item 事件对象
  175. * @param {Object} index 点击的tab对象的下标
  176. */
  177. handleClick(e, item, index) {
  178. e.preventDefault();
  179. if (this.canClick) {
  180. let clickTime = new Date().getTime();
  181. if (clickTime - this.mouseDownTime < 500) {
  182. if (item.rountPath != undefined) {
  183. if (this.getDefaultActive != item.rountPath) {
  184. this.$store.commit("setDefaultActive", item.rountPath);
  185. this.scrollTo(index);
  186. this.$router.push(item.rountPath);
  187. }
  188. } else {
  189. if (this.getDefaultActive != item.jspUrl) {
  190. this.$store.commit("setDefaultActive", item.jspUrl);
  191. this.scrollTo(index);
  192. this.$router.push(item.jspUrl);
  193. }
  194. }
  195. }
  196. }
  197. this.canClick = true;
  198. },
  199. /**
  200. * 自动滚动到指定下标的tab对象
  201. * @param {Object} index 目标对象的下标
  202. * @param {Object} callback 滚动完毕触发的回调
  203. */
  204. scrollTo(index, callback) {
  205. let _from = Math.abs(this.lastTouchX);
  206. let _this = this;
  207. if (this.scrolling == false) {
  208. if (this.isDrag == false) {
  209. if (this.offsetWidth < this.scrollWidth) {
  210. let to = Math.abs(index * (this.eachWidth + 20));
  211. let interval = (to - _from) / 20;
  212. let grow = false;
  213. if (to > _from) {
  214. grow = true;
  215. } else {
  216. grow = false;
  217. }
  218. let runScroll = () => {
  219. let rn = setTimeout(() => {
  220. _this.$refs.ptab.$el.scrollLeft = _from;
  221. _this.lastTouchX = _from * -1;
  222. _from += interval;
  223. if (grow) {
  224. if (_from < to) {
  225. runScroll();
  226. return;
  227. } else {
  228. _from = to;
  229. this.scrolling = false;
  230. if (callback) {
  231. callback();
  232. }
  233. }
  234. } else {
  235. if (_from > to) {
  236. runScroll();
  237. return;
  238. } else {
  239. _from = to;
  240. this.scrolling = false;
  241. if (callback) {
  242. callback();
  243. }
  244. }
  245. }
  246. _this.$refs.ptab.$el.scrollLeft = _from;
  247. _this.lastTouchX = _from * -1;
  248. clearTimeout(rn);
  249. }, 16);
  250. };
  251. this.scrolling = true;
  252. runScroll();
  253. }
  254. }
  255. }
  256. },
  257. mouseDown(e) {
  258. this.touchX = e.clientX;
  259. this.isDrag = true;
  260. this.mouseDownTime = new Date().getTime();
  261. },
  262. mouseMove(e) {
  263. if (this.isDrag && this.offsetWidth < this.scrollWidth) {
  264. this.moveX = e.clientX;
  265. let scrollLeft = this.moveX - this.touchX + this.lastTouchX;
  266. this.$refs.ptab.$el.scrollLeft = scrollLeft * -1;
  267. this.canClick = false;
  268. } else {
  269. this.moveX = 0;
  270. this.touchX = 0;
  271. }
  272. },
  273. mouseUp() {
  274. this.isDrag = false;
  275. if (this.moveX == 0) {
  276. this.touchX = 0;
  277. }
  278. this.lastTouchX = this.moveX - this.touchX + this.lastTouchX;
  279. if (this.lastTouchX <= this.offsetWidth - this.scrollWidth) {
  280. this.lastTouchX = this.offsetWidth - this.scrollWidth;
  281. }
  282. if (this.lastTouchX >= 0) {
  283. this.lastTouchX = 0;
  284. }
  285. },
  286. clickright(event, item, index) {
  287. this.tabsobj = item;
  288. this.rcbStatus = true;
  289. if (event.clientX + 200 > document.body.clientWidth) {
  290. this.cbox.left = event.clientX - 168 + "px";
  291. } else {
  292. this.cbox.left = event.clientX + "px";
  293. }
  294. this.cbox.top = event.clientY + "px";
  295. },
  296. closetab(v) {
  297. if (this.rcbStatus) {
  298. this.rcbStatus = false;
  299. let list = [];
  300. if (v == 1) {
  301. list.push(this.tabsobj);
  302. this.$store.commit("setTabList", list);
  303. this.$router.push(this.tabsobj.rountPath);
  304. } else if (v == 2) {
  305. for (let i = 0; i < this.$store.state.tabList.length; i++) {
  306. if (
  307. this.$store.state.tabList[i].name == this.tabsobj.name ||
  308. list.length > 0
  309. ) {
  310. list.push(this.$store.state.tabList[i]);
  311. }
  312. }
  313. this.$store.commit("setTabList", list);
  314. this.$router.push(this.tabsobj.rountPath);
  315. } else if (v == 3) {
  316. for (let i = 0; i < this.$store.state.tabList.length; i++) {
  317. list.push(this.$store.state.tabList[i]);
  318. if (this.$store.state.tabList[i].name == this.tabsobj.name) {
  319. this.$store.commit("setTabList", list);
  320. this.$router.push(this.tabsobj.rountPath);
  321. return;
  322. }
  323. }
  324. }
  325. }
  326. },
  327. },
  328. };
  329. </script>
  330. <style scoped="scoped" lang="scss">
  331. .rightCbox {
  332. position: absolute;
  333. z-index: 1000;
  334. .el-card__body {
  335. padding: 0 !important;
  336. }
  337. span {
  338. display: block;
  339. padding: 5px 10px;
  340. }
  341. span:hover {
  342. color: #409eff;
  343. background: #f4f4f4;
  344. cursor: pointer;
  345. }
  346. }
  347. .tab-move {
  348. transition: 0.5s;
  349. }
  350. .tab-enter-active {
  351. opacity: 1;
  352. transition: 0.5s;
  353. position: absolute;
  354. }
  355. .tab-leave-active {
  356. opacity: 0;
  357. transition: 0.5s;
  358. position: absolute !important;
  359. }
  360. .tab-leave {
  361. opacity: 1;
  362. }
  363. .tab-enter {
  364. opacity: 0;
  365. }
  366. .p-tabs {
  367. box-sizing: border-box;
  368. width: 100%;
  369. height: 38px;
  370. white-space: nowrap;
  371. overflow: hidden;
  372. margin-top: 15px;
  373. // padding-left: 40px;
  374. .tab-item {
  375. display: inline-block;
  376. height: 38px;
  377. line-height: 38px;
  378. color: black;
  379. position: relative;
  380. width: 134px;
  381. overflow: hidden;
  382. border-radius: 0px 5px 0px 0px;
  383. margin-left: 15px;
  384. cursor: pointer;
  385. .tab-item-shap {
  386. position: absolute;
  387. width: 100%;
  388. height: 38px;
  389. transition: 0.3s;
  390. background: rgba(255, 255, 255, 0.4);
  391. border-radius: 5px 5px 0px 0px;
  392. transform: skew(-30deg);
  393. left: 20px;
  394. }
  395. .text {
  396. text-align: center;
  397. overflow-x: hidden;
  398. white-space: nowrap;
  399. text-overflow: ellipsis;
  400. // padding: 0px 16px;
  401. font-size: 14px;
  402. letter-spacing: 0;
  403. position: absolute;
  404. width: 100%;
  405. height: 38px;
  406. text-align: center;
  407. background-color: white;
  408. }
  409. .close {
  410. font-size: 12px;
  411. position: absolute;
  412. right: 3px;
  413. top: 3px;
  414. color: #fff;
  415. }
  416. .close:hover {
  417. color: #107cff;
  418. }
  419. }
  420. .tab-item:hover {
  421. .tab-item-shap {
  422. background: rgba(255, 255, 255, 0.7);
  423. }
  424. }
  425. .tab-item-active {
  426. .skew {
  427. background: #fff;
  428. }
  429. .tab-item-shap {
  430. background: #fff;
  431. font-family: PingFangSC-Regular;
  432. font-size: 14px;
  433. letter-spacing: 0;
  434. }
  435. .text {
  436. text-align: center;
  437. overflow-x: hidden;
  438. white-space: nowrap;
  439. text-overflow: ellipsis;
  440. // padding: 0px 16px;
  441. color: #107cff;
  442. }
  443. .close {
  444. color: #107cff;
  445. }
  446. }
  447. .tab-item-active:hover {
  448. .tab-item-shap {
  449. background: rgba(255, 255, 255, 1);
  450. }
  451. }
  452. }
  453. </style>