2023-07-03 17:53:28 +08:00
|
|
|
import { find, findIndex, get, size } from 'lodash'
|
2023-06-27 15:53:29 +08:00
|
|
|
import { defineStore } from 'pinia'
|
|
|
|
|
|
|
|
const useTabStore = defineStore('tab', {
|
|
|
|
/**
|
|
|
|
* @typedef {Object} TabItem
|
|
|
|
* @property {string} name connection name
|
|
|
|
* @property {boolean} blank is blank tab
|
|
|
|
* @property {string} [title] tab title
|
2023-07-21 11:14:40 +08:00
|
|
|
* @property {string} [icon] tab icon
|
|
|
|
* @property {string[]} selectedKeys
|
2023-06-27 15:53:29 +08:00
|
|
|
* @property {string} [type] key type
|
|
|
|
* @property {Object|Array} [value] key value
|
|
|
|
* @property {string} [server] server name
|
|
|
|
* @property {int} [db] database index
|
|
|
|
* @property {string} [key] current key name
|
|
|
|
* @property {int} [ttl] ttl of current key
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @returns {{tabList: TabItem[], activatedTab: string, activatedIndex: number}}
|
|
|
|
*/
|
|
|
|
state: () => ({
|
2023-07-01 02:05:30 +08:00
|
|
|
nav: 'server',
|
|
|
|
asideWidth: 300,
|
2023-06-27 15:53:29 +08:00
|
|
|
tabList: [],
|
|
|
|
activatedTab: '',
|
|
|
|
activatedIndex: 0, // current activated tab index
|
|
|
|
}),
|
|
|
|
getters: {
|
|
|
|
/**
|
|
|
|
* get current tab list item
|
|
|
|
* @returns {TabItem[]}
|
|
|
|
*/
|
|
|
|
tabs() {
|
2023-07-01 02:05:30 +08:00
|
|
|
// if (isEmpty(this.tabList)) {
|
|
|
|
// this.newBlankTab()
|
|
|
|
// }
|
2023-06-27 15:53:29 +08:00
|
|
|
return this.tabList
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get current activated tab item
|
|
|
|
* @returns {TabItem|null}
|
|
|
|
*/
|
|
|
|
currentTab() {
|
2023-07-03 17:53:28 +08:00
|
|
|
return get(this.tabs, this.activatedIndex)
|
2023-06-27 15:53:29 +08:00
|
|
|
// let current = find(this.tabs, {name: this.activatedTab})
|
|
|
|
// if (current == null) {
|
|
|
|
// current = this.tabs[0]
|
|
|
|
// }
|
|
|
|
// return current
|
|
|
|
},
|
2023-07-21 11:14:40 +08:00
|
|
|
|
|
|
|
currentSelectedKeys() {
|
|
|
|
const tab = this.currentTab()
|
|
|
|
return get(tab, 'selectedKeys', [])
|
|
|
|
},
|
2023-06-27 15:53:29 +08:00
|
|
|
},
|
|
|
|
actions: {
|
2023-07-01 02:29:50 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param idx
|
|
|
|
* @param {boolean} [switchNav]
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_setActivatedIndex(idx, switchNav) {
|
2023-07-01 02:05:30 +08:00
|
|
|
this.activatedIndex = idx
|
2023-07-01 02:29:50 +08:00
|
|
|
if (switchNav === true) {
|
2023-07-16 01:50:01 +08:00
|
|
|
this.nav = idx >= 0 ? 'browser' : 'server'
|
2023-07-01 02:29:50 +08:00
|
|
|
} else {
|
|
|
|
if (idx < 0) {
|
|
|
|
this.nav = 'server'
|
|
|
|
}
|
|
|
|
}
|
2023-06-27 15:53:29 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update or insert a new tab if not exists with the same name
|
|
|
|
* @param {string} server
|
2023-07-01 02:05:30 +08:00
|
|
|
* @param {number} [db]
|
|
|
|
* @param {number} [type]
|
|
|
|
* @param {number} [ttl]
|
|
|
|
* @param {string} [key]
|
|
|
|
* @param {*} [value]
|
2023-06-27 15:53:29 +08:00
|
|
|
*/
|
|
|
|
upsertTab({ server, db, type, ttl, key, value }) {
|
|
|
|
let tabIndex = findIndex(this.tabList, { name: server })
|
|
|
|
if (tabIndex === -1) {
|
|
|
|
this.tabList.push({
|
|
|
|
name: server,
|
|
|
|
server,
|
|
|
|
db,
|
|
|
|
type,
|
|
|
|
ttl,
|
|
|
|
key,
|
2023-07-01 02:38:23 +08:00
|
|
|
value,
|
2023-06-27 15:53:29 +08:00
|
|
|
})
|
|
|
|
tabIndex = this.tabList.length - 1
|
|
|
|
}
|
|
|
|
const tab = this.tabList[tabIndex]
|
|
|
|
tab.blank = false
|
2023-07-01 02:05:30 +08:00
|
|
|
// tab.title = db !== undefined ? `${server}/db${db}` : `${server}`
|
|
|
|
tab.title = server
|
2023-06-27 15:53:29 +08:00
|
|
|
tab.server = server
|
|
|
|
tab.db = db
|
|
|
|
tab.type = type
|
|
|
|
tab.ttl = ttl
|
|
|
|
tab.key = key
|
|
|
|
tab.value = value
|
2023-07-01 02:29:50 +08:00
|
|
|
this._setActivatedIndex(tabIndex, true)
|
2023-06-27 15:53:29 +08:00
|
|
|
// this.activatedTab = tab.name
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update ttl by tag
|
|
|
|
* @param {string} server
|
|
|
|
* @param {number} db
|
|
|
|
* @param {string} key
|
|
|
|
* @param {number} ttl
|
|
|
|
*/
|
|
|
|
updateTTL({ server, db, key, ttl }) {
|
|
|
|
let tab = find(this.tabList, { name: server, db, key })
|
|
|
|
if (tab == null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tab.ttl = ttl
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set tab's content to empty
|
|
|
|
* @param {string} name
|
|
|
|
*/
|
|
|
|
emptyTab(name) {
|
|
|
|
const tab = find(this.tabList, { name })
|
|
|
|
if (tab != null) {
|
|
|
|
tab.key = null
|
|
|
|
tab.value = null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
switchTab(tabIndex) {
|
|
|
|
// const len = size(this.tabList)
|
|
|
|
// if (tabIndex < 0 || tabIndex >= len) {
|
|
|
|
// tabIndex = 0
|
|
|
|
// }
|
|
|
|
// this.activatedIndex = tabIndex
|
|
|
|
// const tabIndex = findIndex(this.tabList, {name})
|
|
|
|
// if (tabIndex === -1) {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// this.activatedIndex = tabIndex
|
|
|
|
},
|
2023-07-03 17:53:28 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {number} tabIndex
|
|
|
|
* @returns {*|null}
|
|
|
|
*/
|
2023-06-27 15:53:29 +08:00
|
|
|
removeTab(tabIndex) {
|
|
|
|
const len = size(this.tabs)
|
|
|
|
// ignore remove last blank tab
|
|
|
|
if (len === 1 && this.tabs[0].blank) {
|
2023-07-01 02:05:30 +08:00
|
|
|
return null
|
2023-06-27 15:53:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tabIndex < 0 || tabIndex >= len) {
|
2023-07-01 02:05:30 +08:00
|
|
|
return null
|
2023-06-27 15:53:29 +08:00
|
|
|
}
|
2023-07-01 02:05:30 +08:00
|
|
|
const removed = this.tabList.splice(tabIndex, 1)
|
2023-06-27 15:53:29 +08:00
|
|
|
|
2023-07-01 02:05:30 +08:00
|
|
|
// update select index if removed index equal current selected
|
|
|
|
this.activatedIndex -= 1
|
|
|
|
if (this.activatedIndex < 0) {
|
|
|
|
if (this.tabList.length > 0) {
|
2023-07-01 02:29:50 +08:00
|
|
|
this._setActivatedIndex(0, false)
|
2023-07-01 02:05:30 +08:00
|
|
|
} else {
|
2023-07-01 02:29:50 +08:00
|
|
|
this._setActivatedIndex(-1, false)
|
2023-06-27 15:53:29 +08:00
|
|
|
}
|
2023-07-01 02:05:30 +08:00
|
|
|
} else {
|
2023-07-01 02:29:50 +08:00
|
|
|
this._setActivatedIndex(this.activatedIndex, false)
|
2023-06-27 15:53:29 +08:00
|
|
|
}
|
2023-07-01 02:05:30 +08:00
|
|
|
|
|
|
|
return size(removed) > 0 ? removed[0] : null
|
2023-06-27 15:53:29 +08:00
|
|
|
},
|
2023-07-03 17:53:28 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} tabName
|
|
|
|
*/
|
2023-06-27 15:53:29 +08:00
|
|
|
removeTabByName(tabName) {
|
|
|
|
const idx = findIndex(this.tabs, { name: tabName })
|
|
|
|
if (idx !== -1) {
|
|
|
|
this.removeTab(idx)
|
|
|
|
}
|
|
|
|
},
|
2023-07-03 17:53:28 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2023-06-27 15:53:29 +08:00
|
|
|
removeAllTab() {
|
|
|
|
this.tabList = []
|
2023-07-02 16:52:07 +08:00
|
|
|
this._setActivatedIndex(-1, false)
|
2023-06-27 15:53:29 +08:00
|
|
|
},
|
2023-07-21 11:14:40 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} server
|
|
|
|
* @param {string|string[]} keys
|
|
|
|
*/
|
2023-08-19 01:50:16 +08:00
|
|
|
setSelectedKeys(server, keys = null) {
|
2023-07-21 11:14:40 +08:00
|
|
|
let tab = find(this.tabList, { name: server })
|
|
|
|
if (tab != null) {
|
2023-08-19 01:50:16 +08:00
|
|
|
if (keys == null) {
|
|
|
|
// select nothing
|
|
|
|
tab.selectedKeys = [server]
|
|
|
|
} else if (typeof keys === 'string') {
|
|
|
|
tab.selectedKeys = [keys]
|
|
|
|
} else {
|
|
|
|
tab.selectedKeys = keys
|
|
|
|
}
|
2023-07-21 11:14:40 +08:00
|
|
|
}
|
|
|
|
},
|
2023-06-27 15:53:29 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
export default useTabStore
|