2020-01-29 21:37:25 +02:00
|
|
|
<template>
|
2022-01-18 16:06:16 +02:00
|
|
|
<MkModal ref="modal" :prefer-type="'dialog'" :z-priority="'high'" @click="done(true)" @closed="emit('closed')">
|
2020-10-17 14:12:00 +03:00
|
|
|
<div class="mk-dialog">
|
2021-11-19 12:36:12 +02:00
|
|
|
<div v-if="icon" class="icon">
|
2021-04-20 17:22:59 +03:00
|
|
|
<i :class="icon"></i>
|
2020-10-17 14:12:00 +03:00
|
|
|
</div>
|
2021-11-19 12:36:12 +02:00
|
|
|
<div v-else-if="!input && !select" class="icon" :class="type">
|
2021-04-20 17:22:59 +03:00
|
|
|
<i v-if="type === 'success'" class="fas fa-check"></i>
|
|
|
|
<i v-else-if="type === 'error'" class="fas fa-times-circle"></i>
|
|
|
|
<i v-else-if="type === 'warning'" class="fas fa-exclamation-triangle"></i>
|
2021-04-23 06:17:04 +03:00
|
|
|
<i v-else-if="type === 'info'" class="fas fa-info-circle"></i>
|
2021-04-20 17:22:59 +03:00
|
|
|
<i v-else-if="type === 'question'" class="fas fa-question-circle"></i>
|
|
|
|
<i v-else-if="type === 'waiting'" class="fas fa-spinner fa-pulse"></i>
|
2020-10-17 14:12:00 +03:00
|
|
|
</div>
|
2020-10-25 05:25:13 +02:00
|
|
|
<header v-if="title"><Mfm :text="title"/></header>
|
2021-11-19 12:36:12 +02:00
|
|
|
<div v-if="text" class="body"><Mfm :text="text"/></div>
|
2022-01-10 17:05:18 +02:00
|
|
|
<MkInput v-if="input" v-model="inputValue" autofocus :type="input.type || 'text'" :placeholder="input.placeholder || undefined" @keydown="onInputKeydown">
|
2021-11-28 13:07:37 +02:00
|
|
|
<template v-if="input.type === 'password'" #prefix><i class="fas fa-lock"></i></template>
|
|
|
|
</MkInput>
|
2021-08-06 16:29:19 +03:00
|
|
|
<MkSelect v-if="select" v-model="selectedValue" autofocus>
|
2020-10-17 14:12:00 +03:00
|
|
|
<template v-if="select.items">
|
|
|
|
<option v-for="item in select.items" :value="item.value">{{ item.text }}</option>
|
2020-01-29 21:37:25 +02:00
|
|
|
</template>
|
|
|
|
<template v-else>
|
2020-10-17 14:12:00 +03:00
|
|
|
<optgroup v-for="groupedItem in select.groupedItems" :label="groupedItem.label">
|
|
|
|
<option v-for="item in groupedItem.items" :value="item.value">{{ item.text }}</option>
|
|
|
|
</optgroup>
|
2020-01-29 21:37:25 +02:00
|
|
|
</template>
|
2020-10-17 14:12:00 +03:00
|
|
|
</MkSelect>
|
2021-11-19 12:36:12 +02:00
|
|
|
<div v-if="(showOkButton || showCancelButton) && !actions" class="buttons">
|
2022-01-28 04:39:49 +02:00
|
|
|
<MkButton v-if="showOkButton" inline primary :autofocus="!input && !select" @click="ok">{{ (showCancelButton || input || select) ? i18n.ts.ok : i18n.ts.gotIt }}</MkButton>
|
|
|
|
<MkButton v-if="showCancelButton || input || select" inline @click="cancel">{{ i18n.ts.cancel }}</MkButton>
|
2020-10-17 14:12:00 +03:00
|
|
|
</div>
|
2021-11-19 12:36:12 +02:00
|
|
|
<div v-if="actions" class="buttons">
|
|
|
|
<MkButton v-for="action in actions" :key="action.text" inline :primary="action.primary" @click="() => { action.callback(); close(); }">{{ action.text }}</MkButton>
|
2020-01-29 21:37:25 +02:00
|
|
|
</div>
|
2020-10-17 14:12:00 +03:00
|
|
|
</div>
|
|
|
|
</MkModal>
|
2020-01-29 21:37:25 +02:00
|
|
|
</template>
|
|
|
|
|
2022-01-10 17:05:18 +02:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import { onBeforeUnmount, onMounted, ref } from 'vue';
|
2021-11-11 19:02:25 +02:00
|
|
|
import MkModal from '@/components/ui/modal.vue';
|
|
|
|
import MkButton from '@/components/ui/button.vue';
|
|
|
|
import MkInput from '@/components/form/input.vue';
|
|
|
|
import MkSelect from '@/components/form/select.vue';
|
2022-01-18 16:06:16 +02:00
|
|
|
import { i18n } from '@/i18n';
|
2020-01-29 21:37:25 +02:00
|
|
|
|
2022-01-10 17:05:18 +02:00
|
|
|
type Input = {
|
|
|
|
type: HTMLInputElement['type'];
|
|
|
|
placeholder?: string | null;
|
|
|
|
default: any | null;
|
|
|
|
};
|
|
|
|
|
|
|
|
type Select = {
|
|
|
|
items: {
|
|
|
|
value: string;
|
|
|
|
text: string;
|
|
|
|
}[];
|
|
|
|
groupedItems: {
|
|
|
|
label: string;
|
|
|
|
items: {
|
|
|
|
value: string;
|
|
|
|
text: string;
|
|
|
|
}[];
|
|
|
|
}[];
|
|
|
|
default: string | null;
|
|
|
|
};
|
|
|
|
|
|
|
|
const props = withDefaults(defineProps<{
|
|
|
|
type?: 'success' | 'error' | 'warning' | 'info' | 'question' | 'waiting';
|
|
|
|
title: string;
|
|
|
|
text?: string;
|
|
|
|
input?: Input;
|
|
|
|
select?: Select;
|
|
|
|
icon?: string;
|
|
|
|
actions?: {
|
|
|
|
text: string;
|
|
|
|
primary?: boolean,
|
|
|
|
callback: (...args: any[]) => void;
|
|
|
|
}[];
|
|
|
|
showOkButton?: boolean;
|
|
|
|
showCancelButton?: boolean;
|
|
|
|
cancelableByBgClick?: boolean;
|
|
|
|
}>(), {
|
|
|
|
type: 'info',
|
|
|
|
showOkButton: true,
|
|
|
|
showCancelButton: false,
|
|
|
|
cancelableByBgClick: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
|
|
|
(e: 'done', v: { canceled: boolean; result: any }): void;
|
|
|
|
(e: 'closed'): void;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
const modal = ref<InstanceType<typeof MkModal>>();
|
|
|
|
|
|
|
|
const inputValue = ref(props.input?.default || null);
|
|
|
|
const selectedValue = ref(props.select?.default || null);
|
|
|
|
|
|
|
|
function done(canceled: boolean, result?) {
|
|
|
|
emit('done', { canceled, result });
|
|
|
|
modal.value?.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function ok() {
|
|
|
|
if (!props.showOkButton) return;
|
|
|
|
|
|
|
|
const result =
|
|
|
|
props.input ? inputValue.value :
|
|
|
|
props.select ? selectedValue.value :
|
|
|
|
true;
|
|
|
|
done(false, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
function cancel() {
|
|
|
|
done(true);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
function onBgClick() {
|
|
|
|
if (props.cancelableByBgClick) cancel();
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
function onKeydown(e: KeyboardEvent) {
|
|
|
|
if (e.key === 'Escape') cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
function onInputKeydown(e: KeyboardEvent) {
|
|
|
|
if (e.key === 'Enter') {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
ok();
|
2020-01-29 21:37:25 +02:00
|
|
|
}
|
2022-01-10 17:05:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
document.addEventListener('keydown', onKeydown);
|
|
|
|
});
|
|
|
|
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
document.removeEventListener('keydown', onKeydown);
|
2020-01-29 21:37:25 +02:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
.mk-dialog {
|
2020-10-17 14:12:00 +03:00
|
|
|
position: relative;
|
|
|
|
padding: 32px;
|
|
|
|
min-width: 320px;
|
|
|
|
max-width: 480px;
|
|
|
|
box-sizing: border-box;
|
|
|
|
text-align: center;
|
|
|
|
background: var(--panel);
|
|
|
|
border-radius: var(--radius);
|
|
|
|
|
|
|
|
> .icon {
|
|
|
|
font-size: 32px;
|
|
|
|
|
2021-11-28 13:07:37 +02:00
|
|
|
&.info {
|
|
|
|
color: #55c4dd;
|
|
|
|
}
|
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
&.success {
|
2020-11-01 15:09:16 +02:00
|
|
|
color: var(--success);
|
2020-10-17 14:12:00 +03:00
|
|
|
}
|
2020-01-29 21:37:25 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
&.error {
|
2020-11-01 15:09:16 +02:00
|
|
|
color: var(--error);
|
2020-10-17 14:12:00 +03:00
|
|
|
}
|
2020-01-29 21:37:25 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
&.warning {
|
2020-11-01 15:09:16 +02:00
|
|
|
color: var(--warn);
|
2020-10-17 14:12:00 +03:00
|
|
|
}
|
2020-01-29 21:37:25 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
> * {
|
|
|
|
display: block;
|
|
|
|
margin: 0 auto;
|
|
|
|
}
|
2020-01-29 21:37:25 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
& + header {
|
|
|
|
margin-top: 16px;
|
2020-01-29 21:37:25 +02:00
|
|
|
}
|
2020-10-17 14:12:00 +03:00
|
|
|
}
|
2020-01-29 21:37:25 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
> header {
|
|
|
|
margin: 0 0 8px 0;
|
|
|
|
font-weight: bold;
|
|
|
|
font-size: 20px;
|
2020-01-29 21:37:25 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
& + .body {
|
|
|
|
margin-top: 8px;
|
2020-01-29 21:37:25 +02:00
|
|
|
}
|
2020-10-17 14:12:00 +03:00
|
|
|
}
|
2020-01-29 21:37:25 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
> .body {
|
|
|
|
margin: 16px 0 0 0;
|
|
|
|
}
|
2020-01-29 21:37:25 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
> .buttons {
|
|
|
|
margin-top: 16px;
|
2020-01-29 21:37:25 +02:00
|
|
|
|
2020-10-17 14:12:00 +03:00
|
|
|
> * {
|
|
|
|
margin: 0 8px;
|
2020-01-29 21:37:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|