vue-stack-tabs 0.1.13 → 0.1.15

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,516 +1,516 @@
1
- import type { ITabData, ITabItem, ITabPage } from '../model/TabModel'
2
- import { defineComponent, onActivated, onUnmounted, ref, unref } from 'vue'
3
- import type { DefineComponent, VNode } from 'vue'
4
- import { useRouter } from 'vue-router'
5
- import type { RouteLocationNormalizedLoaded } from 'vue-router'
6
- import { ulid } from 'ulidx'
7
- import { encodeTabInfo, createPageId, decodeTabInfo } from '../utils/TabIdHelper'
8
- import { defu } from 'defu'
9
- import { Stack } from '../model/TabModel'
10
- import { uriDecode } from '../utils/UriHelper'
11
-
12
- const tabs = ref<ITabItem[]>([])
13
- const defaultTabs: ITabItem[] = []
14
- // cache
15
- const caches = ref<string[]>([])
16
- // Dynamic components
17
- const components = new Map<string, any>()
18
- const deletableCache = new Set<String>()
19
- const pageShown = ref<boolean>(true)
20
- const SESSION_TAB_NAME = 'stacktab-active-tab'
21
- let max =0;
22
- export default () => {
23
- const router = useRouter()
24
- const size = (): number => {
25
- return tabs.value.length
26
- }
27
- /**
28
- * Init the Tab list
29
- */
30
- const initial = (staticTabs: ITabData[]) => {
31
- // add default tabs
32
- defaultTabs.splice(0)
33
- for (const item of staticTabs) {
34
- // const id = ulid()
35
- const fullItem = defu(item, { id: ulid() })
36
- const __tab = encodeTabInfo(fullItem)
37
- const uri = uriDecode(fullItem.path)
38
- const config = defu(fullItem, {
39
- closable: true,
40
- refreshable: true,
41
- iframe: false
42
- })
43
- const cacheName = createPageId(fullItem.id, uri.path, uri.query)
44
- const page: ITabPage = {
45
- id: cacheName,
46
- tabId: fullItem.id,
47
- path: uri.path,
48
- query: defu(uri.query, { __tab })
49
- }
50
- const pages = new Stack<ITabPage>()
51
- pages.push(page)
52
- const tab: ITabItem = {
53
- id: fullItem.id,
54
- title: config.title,
55
- closable: config.closable,
56
- refreshable: config.refreshable,
57
- iframe: config.iframe,
58
- active: false,
59
- pages
60
- }
61
-
62
- defaultTabs.push(tab)
63
- tabs.value.push(tab)
64
- caches.value.push(cacheName)
65
- }
66
-
67
- // add temp tab from session
68
- const tempTab = window.sessionStorage.getItem(SESSION_TAB_NAME)
69
- if (tempTab !== null && tempTab !== undefined) {
70
- // const tempItems = JSON.parse(window.sessionStorage.getItem('tabItems')!)
71
- const temp= JSON.parse(tempTab,(k,v)=>{
72
- if(k==='pages'){
73
- return new Stack<ITabPage>(v)
74
- }else{
75
- return v;
76
- }
77
- })
78
- // const temp = defu({ pages: new Stack<ITabPage>() }, tempItems) as ITabItem
79
- let hasTab = false
80
- for (const tab of tabs.value) {
81
- if (tab.id === temp.id) {
82
- hasTab = true
83
- break
84
- }
85
- }
86
- if (!hasTab) {
87
- tabs.value.push(temp)
88
- }
89
- }
90
- }
91
- const hasTab = (id: string) => {
92
- for (const tab of tabs.value) {
93
- if (tab.id === id) {
94
- return true
95
- }
96
- }
97
- return false
98
- }
99
- const canAddTab = ()=>{
100
- return (max<=0||(max>0 && max> tabs.value.length))
101
- }
102
- const addTab = (tab: ITabItem) => {
103
- return new Promise((resolve)=>{
104
- tabs.value.push(tab)
105
- resolve(true)
106
- })
107
-
108
- }
109
- const getTab = (id: string) => {
110
- for (const tab of tabs.value) {
111
- if (tab.id === id) {
112
- return tab
113
- }
114
- }
115
- return null
116
- }
117
- const addPage = (route: RouteLocationNormalizedLoaded, component: VNode): DefineComponent => {
118
- let cacheComponent: DefineComponent
119
- const tabInfo = decodeTabInfo(route.query.__tab as string)
120
- const cacheName = createPageId(tabInfo.id!, route.path, route.query.valueOf())
121
- const src = route.query.__src
122
- // 增加tab
123
- let activeTab: ITabItem | null = null
124
- for (const tab of unref(tabs)) {
125
- tab.active = false
126
- if (tab.id === tabInfo.id) {
127
- activeTab = tab as ITabItem
128
- }
129
- }
130
- const page: ITabPage = {
131
- id: cacheName,
132
- tabId: tabInfo.id!,
133
- path: route.path,
134
- query: route.query as Record<string, string>
135
- }
136
-
137
- if (activeTab === null) {
138
- const pages = new Stack<ITabPage>()
139
- pages.push(page)
140
- activeTab = {
141
- id: tabInfo.id!,
142
- title: tabInfo.title,
143
- closable: tabInfo.closable!,
144
- refreshable: tabInfo.refreshable!,
145
- iframe: tabInfo.iframe!,
146
- url: decodeURIComponent(src as string),
147
- active: true,
148
- pages
149
- }
150
- addTab(activeTab)
151
- } else {
152
- activeTab.active = true
153
- if (activeTab.pages.isEmpty() || activeTab.pages.peek()!.id !== page.id) {
154
- activeTab.pages.push(page)
155
- }
156
- }
157
- updateSession(activeTab)
158
- // const cacheSet = new Set(caches.value)
159
- if (components.has(cacheName)) {
160
- cacheComponent = components.get(cacheName)!
161
- } else {
162
- // 增加控件
163
- cacheComponent = defineComponent({
164
- name: cacheName!,
165
- components: {
166
- DynamicComponent: component
167
- },
168
- setup() {
169
- // onMounted(() => {
170
- // addCache(cacheName)
171
- // })
172
- onActivated(() => {
173
- removeDeletableCache()
174
- component.props = null
175
- })
176
- onUnmounted(() => {
177
- console.log('on unMounted', cacheName)
178
- // setTimeout(() => {
179
- // 刷新时重置路由
180
- // pageShown.value = true
181
- // }, 500)
182
- })
183
- return () => (
184
- <div class="cache-page-wrapper" id={tabInfo.id}>
185
- <dynamic-component tId={tabInfo.id} pId={cacheName} />
186
- </div>
187
- )
188
- }
189
- })
190
- components.set(cacheName, cacheComponent)
191
-
192
-
193
- //
194
- }
195
-
196
- // 加入缓存
197
- // const cacheSet = new Set(caches.value)
198
- // if (routerAlive.value) {
199
- // if (!cacheSet.has(cacheName)) {
200
- // caches.value.push(cacheName)
201
- // }
202
- // }
203
- addCache(cacheName)
204
- return cacheComponent
205
- }
206
- /**
207
- * if id is null , then remove all tab that deleted is true
208
- * @param id
209
- */
210
- const removeTab = (id: string): string => {
211
- let activeTabId = ''
212
- for (let i = tabs.value.length - 1; i >= 0; i--) {
213
- if (id === unref(tabs)[i].id) {
214
- //if it's not closable then return
215
- if (!unref(tabs)[i].closable) {
216
- return id
217
- }
218
- for (const item of unref(tabs)[i].pages.list()) {
219
- removeComponent(item.id)
220
- markDeletableCache(item.id)
221
- }
222
- unref(tabs)[i].pages.clear()
223
- if (tabs.value.length > 1 && unref(tabs)[i].active) {
224
- if (i === 0) {
225
- activeTabId = unref(tabs)[i + 1].id
226
- } else {
227
- activeTabId = unref(tabs)[i - 1].id
228
- }
229
- }
230
- unref(tabs).splice(i, 1)
231
-
232
- break
233
- }
234
- }
235
- if (activeTabId !== '') {
236
- active(activeTabId)
237
- }
238
- // if remove inactive tab,then we need remove the cache manually.
239
- removeDeletableCache()
240
- return activeTabId
241
- }
242
-
243
- const removeAllTabs = () => {
244
- const uTabs = unref(tabs)
245
- for (let i = uTabs.length - 1; i >= 0; i--) {
246
- if (uTabs[i].closable) {
247
- for (const item of uTabs[i].pages.list()) {
248
- removeComponent(item.id)
249
- markDeletableCache(item.id)
250
- }
251
- unref(tabs)[i].pages.clear()
252
- unref(tabs).splice(i, 1)
253
- }
254
- }
255
- for (const stay of uTabs) {
256
- if (stay.active) {
257
- removeDeletableCache()
258
- return
259
- }
260
- }
261
- // 如果都是非Active状态,就把最后一个active
262
- active(uTabs[uTabs.length - 1].id)
263
- }
264
- const removeOtherTabs = (id: string) => {
265
- const uTabs = unref(tabs)
266
- let activeTab
267
- for (let i = uTabs.length - 1; i >= 0; i--) {
268
- if (uTabs[i].id === id) {
269
- activeTab = uTabs[i]
270
- } else if (uTabs[i].closable && uTabs[i].id !== id) {
271
- for (const item of uTabs[i].pages.list()) {
272
- removeComponent(item.id)
273
- markDeletableCache(item.id)
274
- }
275
- unref(tabs)[i].pages.clear()
276
- unref(tabs).splice(i, 1)
277
- }
278
- }
279
- if (!activeTab!.active) {
280
- active(id)
281
- } else {
282
- removeDeletableCache()
283
- }
284
- }
285
- /**
286
- * 关闭左边
287
- * @param id
288
- */
289
- const removeLeftTabs = (id: string) => {
290
- let startIndex = -1
291
- const uTabs = unref(tabs)
292
- for (let i = 0; i < uTabs.length; i++) {
293
- if (uTabs[i].id === id) {
294
- startIndex = i - 1
295
- break
296
- }
297
- }
298
- for (let i = startIndex; i >= 0; i--) {
299
- if (uTabs[i].closable) {
300
- for (const item of uTabs[i].pages.list()) {
301
- removeComponent(item.id)
302
- markDeletableCache(item.id)
303
- }
304
- unref(tabs)[i].pages.clear()
305
- unref(tabs).splice(i, 1)
306
- }
307
- }
308
- for (const stay of uTabs) {
309
- if (stay.active) {
310
- removeDeletableCache()
311
- return
312
- }
313
- }
314
- // 如果都是非Active状态,就把最后一个active
315
- active(uTabs[0].id)
316
- }
317
-
318
- /**
319
- * 关闭右边
320
- * @param id
321
- */
322
- const removeRightTabs = (id: string) => {
323
- const uTabs = unref(tabs)
324
- let startIndex = uTabs.length
325
- for (let i = 0; i < uTabs.length; i++) {
326
- if (uTabs[i].id === id) {
327
- startIndex = i + 1
328
- break
329
- }
330
- }
331
- for (let i = uTabs.length - 1; i >= startIndex; i--) {
332
- if (uTabs[i].closable) {
333
- for (const item of uTabs[i].pages.list()) {
334
- removeComponent(item.id)
335
- markDeletableCache(item.id)
336
- }
337
- unref(tabs)[i].pages.clear()
338
- unref(tabs).splice(i, 1)
339
- }
340
- }
341
- for (const stay of uTabs) {
342
- if (stay.active) {
343
- removeDeletableCache()
344
- return
345
- }
346
- }
347
- active(uTabs[uTabs.length - 1].id)
348
- }
349
-
350
- /**
351
- * 刷新
352
- */
353
- const refreshTab = (id: string) => {
354
- // const uTabs = unref(tabs)
355
- for (const uTabs of unref(tabs)) {
356
- if (uTabs.id === id) {
357
- const currentPageId = uTabs.pages.peek()?.id
358
- for (let i = caches.value.length - 1; i >= 0; i--) {
359
- const cacheId = caches.value[i]
360
- if (cacheId === currentPageId) {
361
- caches.value.splice(i, 1)
362
- // 如果要刷新的是显示的TAB
363
- if (uTabs.active) {
364
- pageShown.value = false
365
- }
366
- break
367
- }
368
- }
369
- break
370
- }
371
- }
372
- }
373
- /**
374
- * 全部刷新
375
- */
376
- const refreshAllTabs = () => {
377
- const refreshPage = new Set<string>()
378
- tabs.value.forEach((value) => {
379
- refreshPage.add(value.pages.peek()!.id)
380
- })
381
- for (let i = caches.value.length - 1; i >= 0; i--) {
382
- const cacheId = caches.value[i]
383
- if (refreshPage.has(cacheId)) {
384
- caches.value.splice(i, 1)
385
- }
386
- }
387
- pageShown.value = false
388
- }
389
-
390
- const addComponent = (id: string, comp: DefineComponent) => {
391
- components.set(id, comp)
392
- }
393
- const getComponent = (id: string) => {
394
- return components.get(id)
395
- }
396
- const removeComponent = (id: string) => {
397
- components.delete(id)
398
- }
399
-
400
- /**
401
- * Delete the cache after new Page loaded.
402
- * @param cacheName
403
- */
404
- const markDeletableCache = (cacheName: string) => {
405
- deletableCache.add(cacheName)
406
- }
407
- const removeDeletableCache = () => {
408
- for (let i = caches.value.length - 1; i >= 0; i--) {
409
- if (deletableCache.has(caches.value[i])) {
410
- unref(caches).splice(i, 1)
411
- }
412
- }
413
- deletableCache.clear()
414
- }
415
- const removeCache = (cacheName: string) => {
416
- for (let i = caches.value.length - 1; i >= 0; i--) {
417
- if (cacheName === caches.value[i]) {
418
- unref(caches).splice(i, 1)
419
- }
420
- }
421
- }
422
- const addCache = (cacheName: string) => {
423
- const cacheSet = new Set(caches.value)
424
- if (!cacheSet.has(cacheName)) {
425
- caches.value.push(cacheName)
426
- }
427
- }
428
-
429
- /**
430
- * active the tab,
431
- * @param id
432
- * @param route
433
- * @return true if already actived
434
- */
435
- const active = (id: string, route = true) => {
436
- return new Promise((resolve)=>{
437
- for (let i = tabs.value.length - 1; i >= 0; i--) {
438
- const tab = tabs.value[i] as ITabItem
439
- if (tab.id === id) {
440
- if (tab.active) {
441
- break
442
- } else {
443
- tab.active = true
444
- pageShown.value = false
445
- updateSession(tab)
446
- if (route) {
447
- const top = tab.pages.peek()
448
- router.push({
449
- path: top!.path,
450
- query: top!.query
451
- })
452
- }
453
- }
454
- } else {
455
- tabs.value[i].active = false
456
- }
457
- }
458
- resolve(true)
459
- })
460
-
461
- }
462
- /**
463
- * save current tab info into Browser's session
464
- * @param id
465
- */
466
- const updateSession = (tab: ITabItem) => {
467
- // window.sessionStorage.setItem('tabItems', JSON.stringify(currentItems?.values()))
468
- window.sessionStorage.setItem(SESSION_TAB_NAME, JSON.stringify(tab))
469
- }
470
- const reset = () => {
471
- destroy()
472
- unref(tabs).push(...defaultTabs)
473
- active(defaultTabs[0].id)
474
- }
475
- const destroy = () => {
476
- unref(tabs).splice(0)
477
- components.clear()
478
- deletableCache.clear()
479
- unref(caches).splice(0)
480
- pageShown.value = true
481
- }
482
- const setMaxSize=(size:number)=>{
483
- max =size
484
- }
485
-
486
- return {
487
- tabs,
488
- caches,
489
- pageShown,
490
- setMaxSize,
491
- canAddTab,
492
- initial,
493
- size,
494
- active,
495
- destroy,
496
- reset,
497
- addCache,
498
- removeCache,
499
- getTab,
500
- addTab,
501
- removeTab,
502
- removeAllTabs,
503
- removeLeftTabs,
504
- removeRightTabs,
505
- removeOtherTabs,
506
- hasTab,
507
- addPage,
508
- addComponent,
509
- getComponent,
510
- removeComponent,
511
- markDeletableCache,
512
- removeDeletableCache,
513
- refreshTab,
514
- refreshAllTabs
515
- }
516
- }
1
+ import type { ITabData, ITabItem, ITabPage } from '../model/TabModel'
2
+ import { defineComponent, onActivated, onUnmounted, ref, unref } from 'vue'
3
+ import type { DefineComponent, VNode } from 'vue'
4
+ import { useRouter } from 'vue-router'
5
+ import type { RouteLocationNormalizedLoaded } from 'vue-router'
6
+ import { ulid } from 'ulidx'
7
+ import { encodeTabInfo, createPageId, decodeTabInfo } from '../utils/TabIdHelper'
8
+ import { defu } from 'defu'
9
+ import { Stack } from '../model/TabModel'
10
+ import { uriDecode } from '../utils/UriHelper'
11
+
12
+ const tabs = ref<ITabItem[]>([])
13
+ const defaultTabs: ITabItem[] = []
14
+ // cache
15
+ const caches = ref<string[]>([])
16
+ // Dynamic components
17
+ const components = new Map<string, any>()
18
+ const deletableCache = new Set<String>()
19
+ const pageShown = ref<boolean>(true)
20
+ const SESSION_TAB_NAME = 'stacktab-active-tab'
21
+ let max =0;
22
+ export default () => {
23
+ const router = useRouter()
24
+ const size = (): number => {
25
+ return tabs.value.length
26
+ }
27
+ /**
28
+ * Init the Tab list
29
+ */
30
+ const initial = (staticTabs: ITabData[]) => {
31
+ // add default tabs
32
+ defaultTabs.splice(0)
33
+ for (const item of staticTabs) {
34
+ // const id = ulid()
35
+ const fullItem = defu(item, { id: ulid() })
36
+ const __tab = encodeTabInfo(fullItem)
37
+ const uri = uriDecode(fullItem.path)
38
+ const config = defu(fullItem, {
39
+ closable: true,
40
+ refreshable: true,
41
+ iframe: false
42
+ })
43
+ const cacheName = createPageId(fullItem.id, uri.path, uri.query)
44
+ const page: ITabPage = {
45
+ id: cacheName,
46
+ tabId: fullItem.id,
47
+ path: uri.path,
48
+ query: defu(uri.query, { __tab })
49
+ }
50
+ const pages = new Stack<ITabPage>()
51
+ pages.push(page)
52
+ const tab: ITabItem = {
53
+ id: fullItem.id,
54
+ title: config.title,
55
+ closable: config.closable,
56
+ refreshable: config.refreshable,
57
+ iframe: config.iframe,
58
+ active: false,
59
+ pages
60
+ }
61
+
62
+ defaultTabs.push(tab)
63
+ tabs.value.push(tab)
64
+ caches.value.push(cacheName)
65
+ }
66
+
67
+ // add temp tab from session
68
+ const tempTab = window.sessionStorage.getItem(SESSION_TAB_NAME)
69
+ if (tempTab !== null && tempTab !== undefined) {
70
+ // const tempItems = JSON.parse(window.sessionStorage.getItem('tabItems')!)
71
+ const temp= JSON.parse(tempTab,(k,v)=>{
72
+ if(k==='pages'){
73
+ return new Stack<ITabPage>(v)
74
+ }else{
75
+ return v;
76
+ }
77
+ })
78
+ // const temp = defu({ pages: new Stack<ITabPage>() }, tempItems) as ITabItem
79
+ let hasTab = false
80
+ for (const tab of tabs.value) {
81
+ if (tab.id === temp.id) {
82
+ hasTab = true
83
+ break
84
+ }
85
+ }
86
+ if (!hasTab) {
87
+ tabs.value.push(temp)
88
+ }
89
+ }
90
+ }
91
+ const hasTab = (id: string) => {
92
+ for (const tab of tabs.value) {
93
+ if (tab.id === id) {
94
+ return true
95
+ }
96
+ }
97
+ return false
98
+ }
99
+ const canAddTab = ()=>{
100
+ return (max<=0||(max>0 && max> tabs.value.length))
101
+ }
102
+ const addTab = (tab: ITabItem) => {
103
+ return new Promise((resolve)=>{
104
+ tabs.value.push(tab)
105
+ resolve(true)
106
+ })
107
+
108
+ }
109
+ const getTab = (id: string) => {
110
+ for (const tab of tabs.value) {
111
+ if (tab.id === id) {
112
+ return tab
113
+ }
114
+ }
115
+ return null
116
+ }
117
+ const addPage = (route: RouteLocationNormalizedLoaded, component: VNode): DefineComponent => {
118
+ let cacheComponent: DefineComponent
119
+ const tabInfo = decodeTabInfo(route.query.__tab as string)
120
+ const cacheName = createPageId(tabInfo.id!, route.path, route.query.valueOf())
121
+ const src = route.query.__src
122
+ // 增加tab
123
+ let activeTab: ITabItem | null = null
124
+ for (const tab of unref(tabs)) {
125
+ tab.active = false
126
+ if (tab.id === tabInfo.id) {
127
+ activeTab = tab as ITabItem
128
+ }
129
+ }
130
+ const page: ITabPage = {
131
+ id: cacheName,
132
+ tabId: tabInfo.id!,
133
+ path: route.path,
134
+ query: route.query as Record<string, string>
135
+ }
136
+
137
+ if (activeTab === null) {
138
+ const pages = new Stack<ITabPage>()
139
+ pages.push(page)
140
+ activeTab = {
141
+ id: tabInfo.id!,
142
+ title: tabInfo.title,
143
+ closable: tabInfo.closable!,
144
+ refreshable: tabInfo.refreshable!,
145
+ iframe: tabInfo.iframe!,
146
+ url: decodeURIComponent(src as string),
147
+ active: true,
148
+ pages
149
+ }
150
+ addTab(activeTab)
151
+ } else {
152
+ activeTab.active = true
153
+ if (activeTab.pages.isEmpty() || activeTab.pages.peek()!.id !== page.id) {
154
+ activeTab.pages.push(page)
155
+ }
156
+ }
157
+ updateSession(activeTab)
158
+ // const cacheSet = new Set(caches.value)
159
+ if (components.has(cacheName)) {
160
+ cacheComponent = components.get(cacheName)!
161
+ } else {
162
+ // 增加控件
163
+ cacheComponent = defineComponent({
164
+ name: cacheName!,
165
+ components: {
166
+ DynamicComponent: component
167
+ },
168
+ setup() {
169
+ // onMounted(() => {
170
+ // addCache(cacheName)
171
+ // })
172
+ onActivated(() => {
173
+ removeDeletableCache()
174
+ component.props = null
175
+ })
176
+ onUnmounted(() => {
177
+ console.log('on unMounted', cacheName)
178
+ // setTimeout(() => {
179
+ // 刷新时重置路由
180
+ // pageShown.value = true
181
+ // }, 500)
182
+ })
183
+ return () => (
184
+ <div class="cache-page-wrapper" id={'W-'+tabInfo.id}>
185
+ <dynamic-component tId={tabInfo.id} pId={cacheName} />
186
+ </div>
187
+ )
188
+ }
189
+ })
190
+ components.set(cacheName, cacheComponent)
191
+
192
+
193
+ //
194
+ }
195
+
196
+ // 加入缓存
197
+ // const cacheSet = new Set(caches.value)
198
+ // if (routerAlive.value) {
199
+ // if (!cacheSet.has(cacheName)) {
200
+ // caches.value.push(cacheName)
201
+ // }
202
+ // }
203
+ addCache(cacheName)
204
+ return cacheComponent
205
+ }
206
+ /**
207
+ * if id is null , then remove all tab that deleted is true
208
+ * @param id
209
+ */
210
+ const removeTab = (id: string): string => {
211
+ let activeTabId = ''
212
+ for (let i = tabs.value.length - 1; i >= 0; i--) {
213
+ if (id === unref(tabs)[i].id) {
214
+ //if it's not closable then return
215
+ if (!unref(tabs)[i].closable) {
216
+ return id
217
+ }
218
+ for (const item of unref(tabs)[i].pages.list()) {
219
+ removeComponent(item.id)
220
+ markDeletableCache(item.id)
221
+ }
222
+ unref(tabs)[i].pages.clear()
223
+ if (tabs.value.length > 1 && unref(tabs)[i].active) {
224
+ if (i === 0) {
225
+ activeTabId = unref(tabs)[i + 1].id
226
+ } else {
227
+ activeTabId = unref(tabs)[i - 1].id
228
+ }
229
+ }
230
+ unref(tabs).splice(i, 1)
231
+
232
+ break
233
+ }
234
+ }
235
+ if (activeTabId !== '') {
236
+ active(activeTabId)
237
+ }
238
+ // if remove inactive tab,then we need remove the cache manually.
239
+ removeDeletableCache()
240
+ return activeTabId
241
+ }
242
+
243
+ const removeAllTabs = () => {
244
+ const uTabs = unref(tabs)
245
+ for (let i = uTabs.length - 1; i >= 0; i--) {
246
+ if (uTabs[i].closable) {
247
+ for (const item of uTabs[i].pages.list()) {
248
+ removeComponent(item.id)
249
+ markDeletableCache(item.id)
250
+ }
251
+ unref(tabs)[i].pages.clear()
252
+ unref(tabs).splice(i, 1)
253
+ }
254
+ }
255
+ for (const stay of uTabs) {
256
+ if (stay.active) {
257
+ removeDeletableCache()
258
+ return
259
+ }
260
+ }
261
+ // 如果都是非Active状态,就把最后一个active
262
+ active(uTabs[uTabs.length - 1].id)
263
+ }
264
+ const removeOtherTabs = (id: string) => {
265
+ const uTabs = unref(tabs)
266
+ let activeTab
267
+ for (let i = uTabs.length - 1; i >= 0; i--) {
268
+ if (uTabs[i].id === id) {
269
+ activeTab = uTabs[i]
270
+ } else if (uTabs[i].closable && uTabs[i].id !== id) {
271
+ for (const item of uTabs[i].pages.list()) {
272
+ removeComponent(item.id)
273
+ markDeletableCache(item.id)
274
+ }
275
+ unref(tabs)[i].pages.clear()
276
+ unref(tabs).splice(i, 1)
277
+ }
278
+ }
279
+ if (!activeTab!.active) {
280
+ active(id)
281
+ } else {
282
+ removeDeletableCache()
283
+ }
284
+ }
285
+ /**
286
+ * 关闭左边
287
+ * @param id
288
+ */
289
+ const removeLeftTabs = (id: string) => {
290
+ let startIndex = -1
291
+ const uTabs = unref(tabs)
292
+ for (let i = 0; i < uTabs.length; i++) {
293
+ if (uTabs[i].id === id) {
294
+ startIndex = i - 1
295
+ break
296
+ }
297
+ }
298
+ for (let i = startIndex; i >= 0; i--) {
299
+ if (uTabs[i].closable) {
300
+ for (const item of uTabs[i].pages.list()) {
301
+ removeComponent(item.id)
302
+ markDeletableCache(item.id)
303
+ }
304
+ unref(tabs)[i].pages.clear()
305
+ unref(tabs).splice(i, 1)
306
+ }
307
+ }
308
+ for (const stay of uTabs) {
309
+ if (stay.active) {
310
+ removeDeletableCache()
311
+ return
312
+ }
313
+ }
314
+ // 如果都是非Active状态,就把最后一个active
315
+ active(uTabs[0].id)
316
+ }
317
+
318
+ /**
319
+ * 关闭右边
320
+ * @param id
321
+ */
322
+ const removeRightTabs = (id: string) => {
323
+ const uTabs = unref(tabs)
324
+ let startIndex = uTabs.length
325
+ for (let i = 0; i < uTabs.length; i++) {
326
+ if (uTabs[i].id === id) {
327
+ startIndex = i + 1
328
+ break
329
+ }
330
+ }
331
+ for (let i = uTabs.length - 1; i >= startIndex; i--) {
332
+ if (uTabs[i].closable) {
333
+ for (const item of uTabs[i].pages.list()) {
334
+ removeComponent(item.id)
335
+ markDeletableCache(item.id)
336
+ }
337
+ unref(tabs)[i].pages.clear()
338
+ unref(tabs).splice(i, 1)
339
+ }
340
+ }
341
+ for (const stay of uTabs) {
342
+ if (stay.active) {
343
+ removeDeletableCache()
344
+ return
345
+ }
346
+ }
347
+ active(uTabs[uTabs.length - 1].id)
348
+ }
349
+
350
+ /**
351
+ * 刷新
352
+ */
353
+ const refreshTab = (id: string) => {
354
+ // const uTabs = unref(tabs)
355
+ for (const uTabs of unref(tabs)) {
356
+ if (uTabs.id === id) {
357
+ const currentPageId = uTabs.pages.peek()?.id
358
+ for (let i = caches.value.length - 1; i >= 0; i--) {
359
+ const cacheId = caches.value[i]
360
+ if (cacheId === currentPageId) {
361
+ caches.value.splice(i, 1)
362
+ // 如果要刷新的是显示的TAB
363
+ if (uTabs.active) {
364
+ pageShown.value = false
365
+ }
366
+ break
367
+ }
368
+ }
369
+ break
370
+ }
371
+ }
372
+ }
373
+ /**
374
+ * 全部刷新
375
+ */
376
+ const refreshAllTabs = () => {
377
+ const refreshPage = new Set<string>()
378
+ tabs.value.forEach((value) => {
379
+ refreshPage.add(value.pages.peek()!.id)
380
+ })
381
+ for (let i = caches.value.length - 1; i >= 0; i--) {
382
+ const cacheId = caches.value[i]
383
+ if (refreshPage.has(cacheId)) {
384
+ caches.value.splice(i, 1)
385
+ }
386
+ }
387
+ pageShown.value = false
388
+ }
389
+
390
+ const addComponent = (id: string, comp: DefineComponent) => {
391
+ components.set(id, comp)
392
+ }
393
+ const getComponent = (id: string) => {
394
+ return components.get(id)
395
+ }
396
+ const removeComponent = (id: string) => {
397
+ components.delete(id)
398
+ }
399
+
400
+ /**
401
+ * Delete the cache after new Page loaded.
402
+ * @param cacheName
403
+ */
404
+ const markDeletableCache = (cacheName: string) => {
405
+ deletableCache.add(cacheName)
406
+ }
407
+ const removeDeletableCache = () => {
408
+ for (let i = caches.value.length - 1; i >= 0; i--) {
409
+ if (deletableCache.has(caches.value[i])) {
410
+ unref(caches).splice(i, 1)
411
+ }
412
+ }
413
+ deletableCache.clear()
414
+ }
415
+ const removeCache = (cacheName: string) => {
416
+ for (let i = caches.value.length - 1; i >= 0; i--) {
417
+ if (cacheName === caches.value[i]) {
418
+ unref(caches).splice(i, 1)
419
+ }
420
+ }
421
+ }
422
+ const addCache = (cacheName: string) => {
423
+ const cacheSet = new Set(caches.value)
424
+ if (!cacheSet.has(cacheName)) {
425
+ caches.value.push(cacheName)
426
+ }
427
+ }
428
+
429
+ /**
430
+ * active the tab,
431
+ * @param id
432
+ * @param route
433
+ * @return true if already actived
434
+ */
435
+ const active = (id: string, route = true) => {
436
+ return new Promise((resolve)=>{
437
+ for (let i = tabs.value.length - 1; i >= 0; i--) {
438
+ const tab = tabs.value[i] as ITabItem
439
+ if (tab.id === id) {
440
+ if (tab.active) {
441
+ break
442
+ } else {
443
+ tab.active = true
444
+ pageShown.value = false
445
+ updateSession(tab)
446
+ if (route) {
447
+ const top = tab.pages.peek()
448
+ router.push({
449
+ path: top!.path,
450
+ query: top!.query
451
+ })
452
+ }
453
+ }
454
+ } else {
455
+ tabs.value[i].active = false
456
+ }
457
+ }
458
+ resolve(true)
459
+ })
460
+
461
+ }
462
+ /**
463
+ * save current tab info into Browser's session
464
+ * @param id
465
+ */
466
+ const updateSession = (tab: ITabItem) => {
467
+ // window.sessionStorage.setItem('tabItems', JSON.stringify(currentItems?.values()))
468
+ window.sessionStorage.setItem(SESSION_TAB_NAME, JSON.stringify(tab))
469
+ }
470
+ const reset = () => {
471
+ destroy()
472
+ unref(tabs).push(...defaultTabs)
473
+ active(defaultTabs[0].id)
474
+ }
475
+ const destroy = () => {
476
+ unref(tabs).splice(0)
477
+ components.clear()
478
+ deletableCache.clear()
479
+ unref(caches).splice(0)
480
+ pageShown.value = true
481
+ }
482
+ const setMaxSize=(size:number)=>{
483
+ max =size
484
+ }
485
+
486
+ return {
487
+ tabs,
488
+ caches,
489
+ pageShown,
490
+ setMaxSize,
491
+ canAddTab,
492
+ initial,
493
+ size,
494
+ active,
495
+ destroy,
496
+ reset,
497
+ addCache,
498
+ removeCache,
499
+ getTab,
500
+ addTab,
501
+ removeTab,
502
+ removeAllTabs,
503
+ removeLeftTabs,
504
+ removeRightTabs,
505
+ removeOtherTabs,
506
+ hasTab,
507
+ addPage,
508
+ addComponent,
509
+ getComponent,
510
+ removeComponent,
511
+ markDeletableCache,
512
+ removeDeletableCache,
513
+ refreshTab,
514
+ refreshAllTabs
515
+ }
516
+ }