Skip to content

Commit

Permalink
Ask user for confirmation when leaving with unsaved changed
Browse files Browse the repository at this point in the history
  • Loading branch information
hvangeffen authored and wkramer committed Apr 25, 2024
1 parent 105975b commit 7c7c609
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/components/table/TimeSeriesTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ const itemsPerPageOptions = [
{ value: -1, title: '$vuetify.dataFooter.itemsPerPageAll' },
]
const emit = defineEmits(['change'])
const emit = defineEmits(['change', 'update:isEditing'])
const store = useFewsPropertiesStore()
const configStore = useConfigStore()
Expand Down Expand Up @@ -196,6 +196,10 @@ onBeforeMount(() => {
store.loadFlagSources()
})
watch(isEditing, (value) => {
emit('update:isEditing', value)
})
watch(props.config, () => {
if (props.config === undefined) return
seriesIds.value = getUniqueSeriesIds(props.config.series)
Expand Down
30 changes: 29 additions & 1 deletion src/components/timeseries/TimeSeriesComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
:key="tableConfig.title"
class="single"
@change="(event) => onDataChange(event)"
@update:isEditing="isEditing = $event"
>
</TimeSeriesTable>
</v-window-item>
Expand All @@ -47,7 +48,7 @@
</template>

<script setup lang="ts">
import { computed, ref, watch } from 'vue'
import { computed, onUnmounted, ref, watch } from 'vue'
import TimeSeriesChart from '../charts/TimeSeriesChart.vue'
import ElevationChart from '../charts/ElevationChart.vue'
import TimeSeriesTable from '../table/TimeSeriesTable.vue'
Expand All @@ -65,6 +66,7 @@ import { configManager } from '../../services/application-config'
import { useSystemTimeStore } from '@/stores/systemTime'
import type { TimeSeriesEvent } from '@deltares/fews-pi-requests'
import { useDisplay } from 'vuetify'
import { onBeforeRouteLeave } from 'vue-router'
interface Props {
config?: DisplayConfig
Expand Down Expand Up @@ -103,6 +105,7 @@ const props = withDefaults(defineProps<Props>(), {
const store = useSystemTimeStore()
const lastUpdated = ref<Date>(new Date())
const isEditing = ref(false)
const { xs } = useDisplay()
const options = computed<UseTimeSeriesOptions>(() => {
Expand Down Expand Up @@ -170,6 +173,31 @@ watch(
tab.value = props.displayType
},
)
watch(isEditing, () => {
// Can't set a custom message in modern browsers
window.onbeforeunload = isEditing.value ? () => true : null
})
onUnmounted(() => {
window.onbeforeunload = null
})
onBeforeRouteLeave(() => {
if (isEditing.value) {
// For multiple simultaneous leaves set to false since confirm dialog is async
isEditing.value = false
const answer = window.confirm(
'You have unsaved changes. Are you sure you want to leave?',
)
if (!answer) {
isEditing.value = true
return false
}
}
})
</script>

<style scoped>
Expand Down

0 comments on commit 7c7c609

Please sign in to comment.