Files
thetool/public/plugins/vue/tt-components/tt-modal.js
2024-10-10 08:49:50 +02:00

77 lines
3.2 KiB
JavaScript

Vue.component('tt-modal', {
props: {
show: {type: Boolean, default: false},
title: {type: String, default: 'Überschrift'},
delete: {type: Boolean, default: true},
deleteText: {type: String, default: 'Löschen'},
save: {type: Boolean, default: true},
saveText: {type: String, default: 'Speichern'},
}, watch: {
show(newVal) {
if (!newVal) {
this.$emit('close')
}
// if show now is true then focus the first input element
if (newVal) {
this.$nextTick(() => {
const input = this.$refs.modal.querySelector('input')
if (input) {
if (input.classList.contains('tt-autocomplete')) {
return
}
input.focus()
}
})
}
},
}, // create global listener for esc + return key (if save is enabled)
created() {
document.addEventListener('keydown', this.keydownHandler)
}, destroyed() {
document.removeEventListener('keydown', this.keydownHandler)
}, methods: {
keydownHandler(event) {
if (!this.show) {
return
}
if (event.key === 'Escape') {
this.$emit('update:show', false)
}
if (event.key === 'Enter' && this.save) {
// only submit
this.$emit('submit')
}
}
}, //language=Vue
template: `
<div class="modal show d-block"
role="dialog"
tabindex="-1"
style="background: rgba(0, 0, 0, 0.5);"
ref="modal"
@mousedown="$emit('update:show', false)"
@keydown.esc="$emit('update:show', false)"
v-if="show">
<div class="modal-dialog modal-lg" role="document" @mousedown.stop>
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">{{title}}</h5>
<button type="button" class="close" @click="$emit('update:show', false)">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<slot></slot>
</div>
<div class="modal-footer">
<slot name="footer">
<button v-if="save" class="btn btn-primary" @click="$emit('submit')">{{saveText}}</button>
<button v-if="$props.delete" class="btn btn-danger" @click="$emit('delete')">{{deleteText}}</button>
<button class="btn btn-secondary" @click="$emit('update:show', false)">Close</button>
</slot>
</div>
</div>
</div>
</div>
`
})