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

Add Warn about unsaved tab(s) #23

This commit is contained in:
lana-k
2020-12-24 14:31:45 +01:00
parent c1504dc2b3
commit 07cc00671f
2 changed files with 71 additions and 3 deletions

View File

@@ -28,7 +28,7 @@
<modal name="save" classes="dialog" height="auto"> <modal name="save" classes="dialog" height="auto">
<div class="dialog-header"> <div class="dialog-header">
Save query Save query
<close-icon @click="$modal.hide('save')"/> <close-icon @click="cancelSave"/>
</div> </div>
<div class="dialog-body"> <div class="dialog-body">
<div v-show="isPredefined" id="save-note"> <div v-show="isPredefined" id="save-note">
@@ -44,7 +44,7 @@
/> />
</div> </div>
<div class="dialog-buttons-container"> <div class="dialog-buttons-container">
<button class="secondary" @click="$modal.hide('save')">Cancel</button> <button class="secondary" @click="cancelSave">Cancel</button>
<button class="primary" @click="saveQuery">Save</button> <button class="primary" @click="saveQuery">Save</button>
</div> </div>
</modal> </modal>
@@ -82,6 +82,7 @@ export default {
}, },
created () { created () {
this.$root.$on('createNewQuery', this.createNewQuery) this.$root.$on('createNewQuery', this.createNewQuery)
this.$root.$on('saveQuery', this.checkQueryBeforeSave)
}, },
methods: { methods: {
createNewQuery () { createNewQuery () {
@@ -96,6 +97,10 @@ export default {
this.$store.commit('addTab', tab) this.$store.commit('addTab', tab)
this.$store.commit('setCurrentTabId', tab.id) this.$store.commit('setCurrentTabId', tab.id)
}, },
cancelSave () {
this.$modal.hide('save')
this.$root.$off('querySaved')
},
checkQueryBeforeSave () { checkQueryBeforeSave () {
this.errorMsg = null this.errorMsg = null
const isFromScratch = !this.currentQuery.initName const isFromScratch = !this.currentQuery.initName
@@ -159,6 +164,9 @@ export default {
// Hide dialog // Hide dialog
this.$modal.hide('save') this.$modal.hide('save')
// Signal about saving
this.$root.$emit('querySaved')
} }
} }
} }

View File

@@ -13,7 +13,7 @@
<span v-else class="tab-untitled">{{ tab.tempName }}</span> <span v-else class="tab-untitled">{{ tab.tempName }}</span>
</div> </div>
<div> <div>
<close-icon class="close-icon" :size="10" @click="closeTab(index)"/> <close-icon class="close-icon" :size="10" @click="beforeCloseTab(index)"/>
</div> </div>
</div> </div>
</div> </div>
@@ -32,6 +32,32 @@
a new query from scratch or open the one from a new query from scratch or open the one from
<router-link class="link" to="/my-queries">My queries</router-link> <router-link class="link" to="/my-queries">My queries</router-link>
</div> </div>
<!--Close tab warning dialog -->
<modal name="close-warn" classes="dialog" height="auto">
<div class="dialog-header">
Close tab {{
closingTabIndex !== null
? (tabs[closingTabIndex].name || `[${tabs[closingTabIndex].tempName}]`)
: ''
}}
<close-icon @click="$modal.hide('close-warn')"/>
</div>
<div class="dialog-body">
You have unsaved changes. Save changes in {{
closingTabIndex !== null
? (tabs[closingTabIndex].name || `[${tabs[closingTabIndex].tempName}]`)
: ''
}} before closing?
</div>
<div class="dialog-buttons-container">
<button class="secondary" @click="closeTab(closingTabIndex)">
Close without saving
</button>
<button class="secondary" @click="$modal.hide('close-warn')">Cancel</button>
<button class="primary" @click="saveAndClose(closingTabIndex)">Save and close</button>
</div>
</modal>
</div> </div>
</template> </template>
@@ -46,6 +72,7 @@ export default {
}, },
data () { data () {
return { return {
closingTabIndex: null
} }
}, },
computed: { computed: {
@@ -56,12 +83,45 @@ export default {
return this.$store.state.currentTabId return this.$store.state.currentTabId
} }
}, },
created () {
window.addEventListener('beforeunload', this.leavingSqliteviz)
},
unmounted () {
window.removeEventListener('beforeunload', this.leavingSqliteviz)
},
methods: { methods: {
leavingSqliteviz (event) {
if (this.tabs.some(tab => tab.isUnsaved)) {
event.preventDefault()
event.returnValue = ''
}
},
selectTab (id) { selectTab (id) {
this.$store.commit('setCurrentTabId', id) this.$store.commit('setCurrentTabId', id)
}, },
beforeCloseTab (index) {
this.closingTabIndex = index
if (this.tabs[index].isUnsaved) {
this.$modal.show('close-warn')
} else {
this.closeTab(index)
}
},
closeTab (index) { closeTab (index) {
this.$modal.hide('close-warn')
this.closingTabIndex = null
this.$store.commit('deleteTab', index) this.$store.commit('deleteTab', index)
},
saveAndClose (index) {
this.$root.$on('querySaved', () => {
this.closeTab(index)
this.$root.$off('querySaved')
})
this.selectTab(this.tabs[index].id)
this.$modal.hide('close-warn')
this.$nextTick(() => {
this.$root.$emit('saveQuery')
})
} }
} }
} }