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