vue-stack-tabs 0.1.35 → 0.1.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,103 +1,116 @@
1
- import { useRouter } from 'vue-router'
2
- import useTabpanel from '../hooks/useTabpanel'
3
- import { throttle } from 'lodash-es'
4
- import type { ITabData } from '../model/TabModel'
5
- import { defu } from 'defu'
6
-
7
- import { encodeTabInfo } from '../utils/TabIdHelper'
8
- import { uriDecode } from '../utils/UriHelper'
9
- import { ulid } from 'ulidx'
10
- import { MittType, useEmitter } from './useTabMitt'
11
-
12
- let iframePath: string
13
- export default () => {
14
- const router = useRouter()
15
- const { active, hasTab, pageShown, reset, canAddTab ,renewTab} = useTabpanel()
16
- const emitter = useEmitter()
17
- /**
18
- * 打开新的TAB页面
19
- * @param tab
20
- * @param to
21
- */
22
- const openNewTab = throttle((tab: ITabData,renew=false) => {
23
- return new Promise((resolve, reject) => {
24
- if (!pageShown.value) {
25
- reject()
26
- return
27
- }
28
- const tabInfo = defu(tab, { refreshable: true, closable: true, iframe: false })
29
- if(tabInfo.id && renew && hasTab(tabInfo.id!)){
30
- renewTab(tab)
31
- }
32
- /**
33
- * In the function of openNewTab, if the tab already 'ACTIVE' then do nothing.
34
- */
35
- if (hasTab(tabInfo.id!) && !renew) {
36
- emitter.emit(MittType.TAB_ACTIVE, { id: tabInfo.id! })
37
- // active(tabInfo.id!)
38
- resolve(tabInfo.id)
39
- return
40
- }
41
- if (!tabInfo.id) {
42
- tabInfo.id = ulid()
43
- }
44
- if (!canAddTab()) {
45
- reject('Max Size')
46
- return
47
- }
48
-
49
- // if (initialed) {
50
- // // 增加tab
51
- // let activeTab: ITabItem | null = null
52
- // for (const tab of unref(tabs)) {
53
- // tab.active = false
54
- // }
55
- // activeTab = {
56
- // id: tabInfo.id!,
57
- // title: tabInfo.title,
58
- // closable: tabInfo.closable!,
59
- // refreshable: tabInfo.refreshable!,
60
- // iframe: tabInfo.iframe!,
61
- // url: tabInfo.iframe ? tab.path : null,
62
- // active: true,
63
- // pages: new Stack<ITabPage>()
64
- // } as ITabItem
65
- // addTab(activeTab).finally(() => {
66
- // toPage(tab, tabInfo)
67
- // resolve(tabInfo.id)
68
- // })
69
- // } else {
70
- toPage(tab, tabInfo)
71
- resolve(tabInfo.id)
72
- // }
73
- })
74
- }, 500)
75
- const toPage = (tab: ITabData, tabInfo: any) => {
76
- const __tab = encodeTabInfo(tabInfo)
77
- let query = defu(
78
- {
79
- __tab
80
- },
81
- tab.query
82
- ) as Record<string, string>
83
- let path
84
- if (!tab.iframe) {
85
- const url = uriDecode(tab.path)
86
- path = url.path
87
- query = defu(url.query, query)
88
- } else {
89
- const __src = encodeURIComponent(tab.path as string)
90
- path = iframePath
91
- query['__src'] = __src
92
- }
93
- router.push({
94
- path,
95
- query
96
- })
97
- }
98
- const setIFramePath = (path: string) => {
99
- iframePath = path
100
- }
101
-
102
- return { openNewTab, setIFramePath, active, reset }
103
- }
1
+ import { useRouter } from 'vue-router'
2
+ import useTabpanel from '../hooks/useTabpanel'
3
+ import { throttle } from 'throttle-debounce'
4
+ import type { ITabData } from '../model/TabModel'
5
+ import { defu } from 'defu'
6
+
7
+ import { encodeTabInfo } from '../utils/TabIdHelper'
8
+ import { uriDecode } from '../utils/UriHelper'
9
+ import { ulid } from 'ulid'
10
+ import { MittType, useEmitter } from './useTabMitt'
11
+ import { nextTick } from 'vue'
12
+
13
+ let iframePath: string
14
+ export default () => {
15
+ const router = useRouter()
16
+ const { active, hasTab, pageShown, reset, canAddTab, renewTab, getTab } = useTabpanel()
17
+ const emitter = useEmitter()
18
+ /**
19
+ * 打开新的TAB页面
20
+ * @param tab
21
+ * @param to
22
+ */
23
+ const openNewTab = throttle(500, (tab: ITabData, renew = false) => {
24
+ return new Promise((resolve, reject) => {
25
+ if (!pageShown.value) {
26
+ reject()
27
+ return
28
+ }
29
+ const tabInfo = defu(tab, {
30
+ refreshable: true,
31
+ closable: true,
32
+ iframe: false
33
+ })
34
+ if (tabInfo.id && renew && hasTab(tabInfo.id!)) {
35
+ renewTab(tab)
36
+ const currentTab = getTab(tab.id!)
37
+ // if current tab is active, then must reload the component of router view for complete remove the cache!!!
38
+ if (currentTab?.active) {
39
+ pageShown.value = false
40
+ nextTick(() => {
41
+ pageShown.value = true
42
+ })
43
+ }
44
+ }
45
+ /**
46
+ * In the function of openNewTab, if the tab already 'ACTIVE' then do nothing.
47
+ */
48
+ if (hasTab(tabInfo.id!) && !renew) {
49
+ emitter.emit(MittType.TAB_ACTIVE, { id: tabInfo.id! })
50
+ // active(tabInfo.id!)
51
+ resolve(tabInfo.id)
52
+ return
53
+ }
54
+ if (!tabInfo.id) {
55
+ tabInfo.id = ulid()
56
+ }
57
+ if (!canAddTab()) {
58
+ reject('Max Size')
59
+ return
60
+ }
61
+
62
+ // if (initialed) {
63
+ // // 增加tab
64
+ // let activeTab: ITabItem | null = null
65
+ // for (const tab of unref(tabs)) {
66
+ // tab.active = false
67
+ // }
68
+ // activeTab = {
69
+ // id: tabInfo.id!,
70
+ // title: tabInfo.title,
71
+ // closable: tabInfo.closable!,
72
+ // refreshable: tabInfo.refreshable!,
73
+ // iframe: tabInfo.iframe!,
74
+ // url: tabInfo.iframe ? tab.path : null,
75
+ // active: true,
76
+ // pages: new Stack<ITabPage>()
77
+ // } as ITabItem
78
+ // addTab(activeTab).finally(() => {
79
+ // toPage(tab, tabInfo)
80
+ // resolve(tabInfo.id)
81
+ // })
82
+ // } else {
83
+ toPage(tab, tabInfo)
84
+ resolve(tabInfo.id)
85
+ // }
86
+ })
87
+ })
88
+ const toPage = (tab: ITabData, tabInfo: any) => {
89
+ const __tab = encodeTabInfo(tabInfo)
90
+ let query = defu(
91
+ {
92
+ __tab
93
+ },
94
+ tab.query
95
+ ) as Record<string, string>
96
+ let path
97
+ if (!tab.iframe) {
98
+ const url = uriDecode(tab.path)
99
+ path = url.path
100
+ query = defu(url.query, query)
101
+ } else {
102
+ const __src = encodeURIComponent(tab.path as string)
103
+ path = iframePath
104
+ query['__src'] = __src
105
+ }
106
+ router.push({
107
+ path,
108
+ query
109
+ })
110
+ }
111
+ const setIFramePath = (path: string) => {
112
+ iframePath = path
113
+ }
114
+
115
+ return { openNewTab, setIFramePath, active, reset }
116
+ }
@@ -6,12 +6,14 @@ import { type RouteLocationPathRaw, useRoute, useRouter } from 'vue-router'
6
6
 
7
7
  import useTabpanel from '@/lib/hooks/useTabpanel'
8
8
  import { uriDecode } from '@/lib/utils/UriHelper'
9
+ import { useEmitter } from '@/lib/hooks/useTabMitt'
9
10
  export default () => {
10
11
  const { attrs, props } = getCurrentInstance() as ComponentInternalInstance
11
12
  const { pageShown, getTab, markDeletableCache, removeComponent, getComponent, addPageScroller } =
12
13
  useTabpanel()
13
14
  const route = useRoute()
14
15
  const router = useRouter()
16
+ const emitter = useEmitter()
15
17
  let currentId = ''
16
18
  // const currentPageId = ''
17
19
  let currentTab = ''
@@ -31,6 +33,7 @@ export default () => {
31
33
  },
32
34
  to.query
33
35
  )
36
+ emitter.emit('FORWARD')
34
37
  router.push({
35
38
  path: to.path,
36
39
  query
@@ -85,6 +88,7 @@ export default () => {
85
88
  const sourceComponent = component.components.DynamicComponent
86
89
  sourceComponent.props = defu(query, sourceComponent.props)
87
90
  }
91
+ emitter.emit('BACKWARD')
88
92
  router.push({
89
93
  path: pageRoute!.path,
90
94
  query: pageRoute!.query
@@ -12,7 +12,7 @@ import {
12
12
  import type { DefineComponent, VNode } from 'vue'
13
13
  import { useRouter } from 'vue-router'
14
14
  import type { RouteLocationNormalizedLoaded } from 'vue-router'
15
- import { ulid } from 'ulidx'
15
+ import { ulid } from 'ulid'
16
16
  import { encodeTabInfo, createPageId, decodeTabInfo } from '../utils/TabIdHelper'
17
17
  import { defu } from 'defu'
18
18
  import { Stack } from '../model/TabModel'
@@ -588,6 +588,7 @@ export default () => {
588
588
  */
589
589
  const active = (id: string, route = true) => {
590
590
  // return new Promise((resolve)=>{
591
+ emitter.emit('FORWARD')
591
592
  for (let i = tabs.value.length - 1; i >= 0; i--) {
592
593
  const tab = tabs.value[i] as ITabItem
593
594
  if (tab.id === id) {