2020-01-29 20:37:25 +01:00
|
|
|
<template>
|
2023-02-19 00:34:41 +01:00
|
|
|
<MkModal ref="modal" :prefer-type="'dialog'" :z-priority="'high'" @click="done(true)" @closed="emit('closed')">
|
|
|
|
<div :class="$style.root">
|
|
|
|
<div v-if="icon" :class="$style.icon">
|
|
|
|
<i :class="icon"></i>
|
2022-10-26 07:44:38 +02:00
|
|
|
</div>
|
2023-02-19 00:34:41 +01:00
|
|
|
<div v-else-if="!input && !select" :class="[$style.icon, $style['type_' + type]]">
|
2023-03-11 22:01:04 +01:00
|
|
|
<i v-if="type === 'success'" :class="$style.iconInner" class="ph-check ph-bold ph-lg"></i>
|
|
|
|
<i v-else-if="type === 'error'" :class="$style.iconInner" class="ph-circle-wavy-warning ph-bold ph-lg"></i>
|
|
|
|
<i v-else-if="type === 'warning'" :class="$style.iconInner" class="ph-warning ph-bold ph-lg"></i>
|
|
|
|
<i v-else-if="type === 'info'" :class="$style.iconInner" class="ph-info ph-bold ph-lg"></i>
|
|
|
|
<i v-else-if="type === 'question'" :class="$style.iconInner" class="ph-circle-question ph-bold ph-lg"></i>
|
2023-02-19 00:34:41 +01:00
|
|
|
<MkLoading v-else-if="type === 'waiting'" :class="$style.iconInner" :em="true"/>
|
|
|
|
</div>
|
|
|
|
<header v-if="title" :class="$style.title"><Mfm :text="title"/></header>
|
2023-04-02 06:04:15 +02:00
|
|
|
<header v-if="title == null && input.type === 'password'" :class="$style.title"><Mfm :text="i18n.ts.password"/></header>
|
2023-02-19 00:34:41 +01:00
|
|
|
<div v-if="text" :class="$style.text"><Mfm :text="text"/></div>
|
2023-03-19 08:22:28 +01:00
|
|
|
<MkInput v-if="input && input.type !== 'paragraph'" v-model="inputValue" autofocus :type="input.type || 'text'" :placeholder="input.placeholder || undefined" @keydown="onInputKeydown">
|
2023-03-11 22:01:04 +01:00
|
|
|
<template v-if="input.type === 'password'" #prefix><i class="ph-password ph-bold ph-lg"></i></template>
|
2023-02-19 00:34:41 +01:00
|
|
|
</MkInput>
|
2023-03-19 08:22:28 +01:00
|
|
|
<MkTextarea v-if="input && input.type === 'paragraph'" v-model="inputValue" autofocus :type="paragraph" :placeholder="input.placeholder || undefined">
|
|
|
|
</MkTextarea>
|
2023-02-19 00:34:41 +01:00
|
|
|
<MkSelect v-if="select" v-model="selectedValue" autofocus>
|
|
|
|
<template v-if="select.items">
|
|
|
|
<option v-for="item in select.items" :value="item.value">{{ item.text }}</option>
|
|
|
|
</template>
|
|
|
|
<template v-else>
|
|
|
|
<optgroup v-for="groupedItem in select.groupedItems" :label="groupedItem.label">
|
|
|
|
<option v-for="item in groupedItem.items" :value="item.value">{{ item.text }}</option>
|
|
|
|
</optgroup>
|
|
|
|
</template>
|
|
|
|
</MkSelect>
|
|
|
|
<div v-if="(showOkButton || showCancelButton) && !actions" :class="$style.buttons">
|
|
|
|
<div v-if="!isYesNo">
|
|
|
|
<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>
|
|
|
|
</div>
|
|
|
|
<div v-else>
|
|
|
|
<MkButton v-if="showOkButton" inline primary :autofocus="!input && !select" @click="ok">{{ i18n.ts.yes }}</MkButton>
|
|
|
|
<MkButton v-if="showCancelButton || input || select" inline @click="cancel">{{ i18n.ts.no }}</MkButton>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div v-if="actions" :class="$style.buttons">
|
|
|
|
<MkButton v-for="action in actions" :key="action.text" inline :primary="action.primary" @click="() => { action.callback(); modal?.close(); }">{{ action.text }}</MkButton>
|
2022-10-26 07:44:38 +02:00
|
|
|
</div>
|
2020-10-17 13:12:00 +02:00
|
|
|
</div>
|
2023-02-19 00:34:41 +01:00
|
|
|
</MkModal>
|
2020-01-29 20:37:25 +01:00
|
|
|
</template>
|
|
|
|
|
2022-01-10 16:05:18 +01:00
|
|
|
<script lang="ts" setup>
|
2023-02-19 00:34:41 +01:00
|
|
|
import { onBeforeUnmount, onMounted, ref, shallowRef } from 'vue';
|
2022-09-06 11:21:49 +02:00
|
|
|
import MkModal from '@/components/MkModal.vue';
|
|
|
|
import MkButton from '@/components/MkButton.vue';
|
2021-11-11 18:02:25 +01:00
|
|
|
import MkInput from '@/components/form/input.vue';
|
2023-03-19 08:22:28 +01:00
|
|
|
import MkTextarea from '@/components/form/textarea.vue';
|
2021-11-11 18:02:25 +01:00
|
|
|
import MkSelect from '@/components/form/select.vue';
|
2022-01-18 15:06:16 +01:00
|
|
|
import { i18n } from '@/i18n';
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2022-01-10 16:05:18 +01: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;
|
2022-10-26 07:44:38 +02:00
|
|
|
isYesNo?: boolean;
|
2023-02-19 00:34:41 +01:00
|
|
|
|
2022-01-10 16:05:18 +01:00
|
|
|
cancelableByBgClick?: boolean;
|
2023-02-19 00:34:41 +01:00
|
|
|
okText?: string;
|
|
|
|
cancelText?: string;
|
2022-01-10 16:05:18 +01:00
|
|
|
}>(), {
|
|
|
|
type: 'info',
|
|
|
|
showOkButton: true,
|
|
|
|
showCancelButton: false,
|
2022-10-26 07:44:38 +02:00
|
|
|
isYesNo: false,
|
2023-02-19 00:34:41 +01:00
|
|
|
|
2022-01-10 16:05:18 +01:00
|
|
|
cancelableByBgClick: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
2022-05-26 15:53:09 +02:00
|
|
|
(ev: 'done', v: { canceled: boolean; result: any }): void;
|
|
|
|
(ev: 'closed'): void;
|
2022-01-10 16:05:18 +01:00
|
|
|
}>();
|
|
|
|
|
2023-02-19 00:34:41 +01:00
|
|
|
const modal = shallowRef<InstanceType<typeof MkModal>>();
|
2022-01-10 16:05:18 +01:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
*/
|
2022-05-26 15:53:09 +02:00
|
|
|
function onKeydown(evt: KeyboardEvent) {
|
|
|
|
if (evt.key === 'Escape') cancel();
|
2022-01-10 16:05:18 +01:00
|
|
|
}
|
|
|
|
|
2022-05-26 15:53:09 +02:00
|
|
|
function onInputKeydown(evt: KeyboardEvent) {
|
|
|
|
if (evt.key === 'Enter') {
|
|
|
|
evt.preventDefault();
|
|
|
|
evt.stopPropagation();
|
2022-01-10 16:05:18 +01:00
|
|
|
ok();
|
2020-01-29 20:37:25 +01:00
|
|
|
}
|
2022-01-10 16:05:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
document.addEventListener('keydown', onKeydown);
|
|
|
|
});
|
|
|
|
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
document.removeEventListener('keydown', onKeydown);
|
2020-01-29 20:37:25 +01:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2023-02-19 00:34:41 +01:00
|
|
|
<style lang="scss" module>
|
|
|
|
.root {
|
2020-10-17 13:12:00 +02:00
|
|
|
position: relative;
|
2023-02-19 00:34:41 +01:00
|
|
|
margin: auto;
|
2020-10-17 13:12:00 +02:00
|
|
|
padding: 32px;
|
|
|
|
min-width: 320px;
|
|
|
|
max-width: 480px;
|
|
|
|
box-sizing: border-box;
|
|
|
|
text-align: center;
|
|
|
|
background: var(--panel);
|
|
|
|
border-radius: var(--radius);
|
2023-02-19 00:34:41 +01:00
|
|
|
}
|
2020-10-17 13:12:00 +02:00
|
|
|
|
2023-02-19 00:34:41 +01:00
|
|
|
.icon {
|
|
|
|
font-size: 24px;
|
2021-11-28 12:07:37 +01:00
|
|
|
|
2023-02-19 00:34:41 +01:00
|
|
|
& + .title {
|
|
|
|
margin-top: 8px;
|
|
|
|
}
|
|
|
|
}
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-02-19 00:34:41 +01:00
|
|
|
.iconInner {
|
|
|
|
display: block;
|
|
|
|
margin: 0 auto;
|
|
|
|
}
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-02-19 00:34:41 +01:00
|
|
|
.type_info {
|
|
|
|
color: var(--accent);
|
|
|
|
}
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-02-19 00:34:41 +01:00
|
|
|
.type_success {
|
|
|
|
color: var(--success);
|
|
|
|
}
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-02-19 00:34:41 +01:00
|
|
|
.type_error {
|
|
|
|
color: var(--error);
|
|
|
|
}
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-02-19 00:34:41 +01:00
|
|
|
.type_warning {
|
|
|
|
color: var(--warn);
|
|
|
|
}
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-02-19 00:34:41 +01:00
|
|
|
.title {
|
|
|
|
margin: 0 0 8px 0;
|
|
|
|
font-weight: bold;
|
|
|
|
font-size: 1.1em;
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-02-19 00:34:41 +01:00
|
|
|
& + .text {
|
|
|
|
margin-top: 8px;
|
2020-10-17 13:12:00 +02:00
|
|
|
}
|
2023-02-19 00:34:41 +01:00
|
|
|
}
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-02-19 00:34:41 +01:00
|
|
|
.text {
|
|
|
|
margin: 16px 0 0 0;
|
|
|
|
}
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-02-19 00:34:41 +01:00
|
|
|
.buttons {
|
|
|
|
margin-top: 16px;
|
|
|
|
display: flex;
|
|
|
|
gap: 8px;
|
|
|
|
flex-wrap: wrap;
|
|
|
|
justify-content: center;
|
2020-01-29 20:37:25 +01:00
|
|
|
}
|
|
|
|
</style>
|