mirror of
https://github.com/lana-k/sqliteviz.git
synced 2025-12-08 02:58:54 +08:00
refactor MainMenu methods
This commit is contained in:
58
src/queryTab.js
Normal file
58
src/queryTab.js
Normal file
@@ -0,0 +1,58 @@
|
||||
import { nanoid } from 'nanoid'
|
||||
import store from '@/store'
|
||||
|
||||
export default {
|
||||
newBlankTab () {
|
||||
return {
|
||||
id: nanoid(),
|
||||
name: null,
|
||||
tempName: store.state.untitledLastIndex
|
||||
? `Untitled ${store.state.untitledLastIndex}`
|
||||
: 'Untitled',
|
||||
isUnsaved: true
|
||||
}
|
||||
},
|
||||
|
||||
isTabNeedName (queryTab) {
|
||||
const isFromScratch = !queryTab.initName
|
||||
return isFromScratch || queryTab.isPredefined
|
||||
},
|
||||
|
||||
getDataToSave (queryTab, newName) {
|
||||
return {
|
||||
id: queryTab.isPredefined ? nanoid() : queryTab.id,
|
||||
query: queryTab.query,
|
||||
chart: queryTab.getChartSatateForSave(),
|
||||
name: newName || queryTab.initName
|
||||
}
|
||||
},
|
||||
|
||||
save (queryTab, newName) {
|
||||
const value = this.getDataToSave(queryTab, newName)
|
||||
const isNeedName = this.isTabNeedName(queryTab)
|
||||
|
||||
// Get queries from local storage
|
||||
let myQueries = JSON.parse(localStorage.getItem('myQueries'))
|
||||
|
||||
// Set createdAt
|
||||
if (isNeedName) {
|
||||
value.createdAt = new Date()
|
||||
} else {
|
||||
var queryIndex = myQueries.findIndex(oldQuery => oldQuery.id === queryTab.id)
|
||||
value.createdAt = myQueries[queryIndex].createdAt
|
||||
}
|
||||
|
||||
// Insert in queries list
|
||||
if (!myQueries) {
|
||||
myQueries = [value]
|
||||
} else if (isNeedName) {
|
||||
myQueries.push(value)
|
||||
} else {
|
||||
myQueries[queryIndex] = value
|
||||
}
|
||||
|
||||
// Save to local storage
|
||||
localStorage.setItem('myQueries', JSON.stringify(myQueries))
|
||||
return value
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user