1
0
mirror of https://github.com/lana-k/sqliteviz.git synced 2025-12-07 10:38:54 +08:00

refactor MainMenu methods

This commit is contained in:
lana-k
2021-01-04 21:49:30 +01:00
parent e98fa8793b
commit 9035cae19f
2 changed files with 66 additions and 34 deletions

View File

@@ -52,9 +52,9 @@
</template>
<script>
import { nanoid } from 'nanoid'
import TextField from '@/components/TextField'
import CloseIcon from '@/components/svg/close'
import queryTab from '@/queryTab'
export default {
name: 'MainMenu',
@@ -100,14 +100,7 @@ export default {
},
methods: {
createNewQuery () {
const tab = {
id: nanoid(),
name: null,
tempName: this.$store.state.untitledLastIndex
? `Untitled ${this.$store.state.untitledLastIndex}`
: 'Untitled',
isUnsaved: true
}
const tab = queryTab.newBlankTab()
this.$store.commit('addTab', tab)
this.$store.commit('setCurrentTabId', tab.id)
},
@@ -117,46 +110,27 @@ export default {
},
checkQueryBeforeSave () {
this.errorMsg = null
const isFromScratch = !this.currentQuery.initName
this.name = ''
if (isFromScratch || this.isPredefined) {
this.name = ''
if (queryTab.isTabNeedName(this.currentQuery)) {
this.$modal.show('save')
} else {
this.saveQuery()
}
},
saveQuery () {
const isFromScratch = !this.currentQuery.initName
if ((isFromScratch || this.isPredefined) && !this.name) {
const isNeedName = queryTab.isTabNeedName(this.currentQuery)
if (isNeedName && !this.name) {
this.errorMsg = 'Query name can\'t be empty'
return
}
const dataSet = this.currentQuery.result
const tabView = this.currentQuery.view
// Prepare query
const value = {
id: this.isPredefined ? nanoid() : this.currentQuery.id,
query: this.currentQuery.query,
chart: this.currentQuery.getChartSatateForSave(),
name: (!this.isPredefined && this.currentQuery.initName) || this.name,
createdAt: new Date()
}
// Save query
let myQueries = JSON.parse(localStorage.getItem('myQueries'))
if (!myQueries) {
myQueries = [value]
} else if (isFromScratch || this.isPredefined) {
myQueries.push(value)
} else {
const queryIndex = myQueries.findIndex(query => query.id === this.currentQuery.id)
value.createdAt = myQueries[queryIndex].createdAt
myQueries[queryIndex] = value
}
localStorage.setItem('myQueries', JSON.stringify(myQueries))
const value = queryTab.save(this.currentQuery, this.name)
// Update tab
// Update tab in store
this.$store.commit('updateTab', {
index: this.currentQuery.tabIndex,
name: value.name,

58
src/queryTab.js Normal file
View 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
}
}