gancio-upstream/components/Confirm.vue

96 lines
2.2 KiB
Vue
Raw Normal View History

2020-07-28 12:24:39 +02:00
<template lang="pug">
2022-07-01 15:55:09 +02:00
v-dialog(v-model='show'
:fullscreen='$vuetify.breakpoint.xsOnly'
:color='options.color'
:title='title'
:max-width='options.width'
:style="{ zIndex: options.zIndex, position: 'absolute' }"
@keydown.esc='cancel')
v-card
v-card-title {{ title }}
2024-03-18 09:47:59 +01:00
v-card-text(v-show='!!message')
span(v-html='message')
v-textarea(v-if='options.is_prompt' v-model='prompt')
2022-07-01 15:55:09 +02:00
v-card-actions
v-spacer
v-btn(outlined color='error' @click='cancel') {{$t('common.cancel')}}
2024-03-28 13:15:43 +01:00
v-btn(outlined color='primary' @click='agree' :disabled="options.is_prompt && !prompt") {{$t('common.ok')}}
2020-07-28 12:24:39 +02:00
</template>
<script>
/**
* Vuetify Confirm Dialog component
*
* Call it:
* this.$refs.confirm.open('Delete', 'Are you sure?', { color: 'red' }).then((confirm) => {})
*
* Or use await:
* if (await this.$refs.confirm.open('Delete', 'Are you sure?', { color: 'red' })) {
* // yes
* }
* else {
* // cancel
* }
*
*/
export default {
data: () => ({
dialog: false,
resolve: null,
reject: null,
message: null,
title: null,
2024-03-18 09:47:59 +01:00
prompt: '',
2020-07-28 12:24:39 +02:00
options: {
2024-03-18 09:47:59 +01:00
is_prompt: false,
2020-07-28 12:24:39 +02:00
color: 'danger',
2020-08-05 17:05:10 +02:00
width: 450,
2020-07-28 12:24:39 +02:00
zIndex: 500
}
}),
computed: {
show: {
get () {
return this.dialog
},
set (value) {
this.dialog = value
if (value === false) {
this.cancel()
}
}
}
},
created () {
this.$root.$confirm = this.open
2024-03-18 09:47:59 +01:00
this.$root.$prompt = this.openPrompt
2020-07-28 12:24:39 +02:00
},
methods: {
2024-03-18 09:47:59 +01:00
openPrompt (message, options ) {
return this.open(message, { ...options, is_prompt: true })
},
2020-10-07 13:03:01 +02:00
open (message, options = {}) {
2020-07-28 12:24:39 +02:00
this.dialog = true
2020-11-13 00:12:14 +01:00
this.title = options.title || this.$t('common.confirm')
2020-10-07 11:12:13 +02:00
this.message = this.$t(message, options)
2020-07-28 12:24:39 +02:00
this.options = Object.assign(this.options, options)
return new Promise((resolve, reject) => {
this.resolve = resolve
this.reject = reject
})
},
agree () {
2024-03-18 09:47:59 +01:00
this.resolve(this.options.is_prompt ? this.prompt : true)
this.prompt = ''
2020-07-28 12:24:39 +02:00
this.dialog = false
},
cancel () {
this.resolve(false)
2024-03-18 09:47:59 +01:00
this.prompt = ''
2020-07-28 12:24:39 +02:00
this.dialog = false
}
}
}
</script>