This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Zaimki/components/DialogueBox.vue

120 lines
3.8 KiB
Vue
Raw Normal View History

2020-10-24 13:32:12 -07:00
<template>
2022-03-19 14:10:26 -07:00
<div :class="['modal', shown ? 'd-block' : '', shownFull ? 'modal-shown' : '']" @click="hideClick">
2020-10-24 13:32:12 -07:00
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content shadow">
<div class="modal-header" v-if="choice">
2020-10-24 13:32:12 -07:00
<p class="h5 modal-title">
<Icon v="map-marker-question"/>
{{$t('confirm.header')}}
</p>
</div>
<div class="modal-body" v-if="message">
2020-10-24 13:32:12 -07:00
<p class="py-5 text-center" v-html="message"></p>
</div>
<div v-if="choice" class="modal-footer">
2020-10-24 13:32:12 -07:00
<button class="btn btn-outline-dark" @click="cancel">
{{$t('confirm.no')}}
</button>
<button :class="'btn btn-' + (color || 'primary')" @click="confirm">
{{$t('confirm.yes')}}
</button>
</div>
<div v-else class="modal-footer">
<button :class="'btn btn-' + (color || 'primary')" @click="confirm">
{{$t('confirm.ok')}}
</button>
</div>
2020-10-24 13:32:12 -07:00
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
shown: false,
2022-03-19 14:10:26 -07:00
shownFull: false,
choice: false,
2020-10-24 13:32:12 -07:00
message: undefined,
resolve: undefined,
reject: undefined,
color: null,
}
},
mounted() {
if (process.client) {
window.addEventListener('keydown', e => {
if (!this.shown) {
return;
}
2020-10-24 13:32:12 -07:00
if (e.keyCode === 27) {
e.stopPropagation();
e.preventDefault();
2020-10-24 13:32:12 -07:00
this.cancel();
} else if (e.keyCode === 13) {
e.stopPropagation();
e.preventDefault();
2020-10-24 13:32:12 -07:00
this.confirm();
}
});
}
},
methods: {
show(choice, message, color, resolve, reject) {
this.choice = choice;
2020-10-24 13:32:12 -07:00
this.message = message;
this.resolve = resolve;
this.reject = reject;
this.color = color;
this.shown = true;
2022-03-19 14:10:26 -07:00
requestAnimationFrame(() => this.shownFull = true);
2020-10-24 13:32:12 -07:00
},
confirm() {
const resolve = this.resolve;
2022-03-19 14:10:26 -07:00
this.hide();
2020-10-24 13:32:12 -07:00
if (resolve) {
resolve();
}
},
cancel(event) {
const reject = this.reject;
2022-03-19 14:10:26 -07:00
this.hide();
2020-10-24 13:32:12 -07:00
if (reject) {
reject();
}
},
2022-03-19 14:10:26 -07:00
hide() {
this.shownFull = false;
setTimeout(() => {
this.shown = false;
this.choice = false;
this.message = undefined;
this.resolve = undefined;
this.reject = undefined;
this.color = null;
}, 500);
},
2020-10-24 13:32:12 -07:00
hideClick(event) {
if (event.target === this.$el) {
this.cancel();
}
},
},
};
</script>
<style lang="scss" scoped>
@import "assets/variables";
2020-10-24 13:32:12 -07:00
.modal {
background-color: rgba($black, 0.5);
2022-03-19 14:10:26 -07:00
opacity: 0;
transition: opacity .3s ease-in-out;
&.modal-shown {
opacity: 1;
}
2020-10-24 13:32:12 -07:00
}
</style>