2023-07-27 00:31:52 -05:00
|
|
|
<!--
|
2024-02-13 09:50:11 -06:00
|
|
|
SPDX-FileCopyrightText: syuilo and other misskey contributors
|
2023-07-27 00:31:52 -05:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2020-01-29 13:37:25 -06:00
|
|
|
<template>
|
2023-05-18 23:58:09 -05:00
|
|
|
<MkModal ref="modal" :preferType="'dialog'" :zPriority="'high'" @click="done(true)" @closed="emit('closed')">
|
2023-01-09 18:41:53 -06:00
|
|
|
<div :class="$style.root">
|
|
|
|
<div v-if="icon" :class="$style.icon">
|
2021-04-20 09:22:59 -05:00
|
|
|
<i :class="icon"></i>
|
2020-10-17 06:12:00 -05:00
|
|
|
</div>
|
2023-06-01 03:19:46 -05:00
|
|
|
<div
|
|
|
|
v-else-if="!input && !select"
|
|
|
|
:class="[$style.icon, {
|
|
|
|
[$style.type_success]: type === 'success',
|
|
|
|
[$style.type_error]: type === 'error',
|
|
|
|
[$style.type_warning]: type === 'warning',
|
|
|
|
[$style.type_info]: type === 'info',
|
|
|
|
}]"
|
|
|
|
>
|
2023-01-10 05:12:38 -06:00
|
|
|
<i v-if="type === 'success'" :class="$style.iconInner" class="ti ti-check"></i>
|
|
|
|
<i v-else-if="type === 'error'" :class="$style.iconInner" class="ti ti-circle-x"></i>
|
|
|
|
<i v-else-if="type === 'warning'" :class="$style.iconInner" class="ti ti-alert-triangle"></i>
|
|
|
|
<i v-else-if="type === 'info'" :class="$style.iconInner" class="ti ti-info-circle"></i>
|
2023-04-21 18:00:37 -05:00
|
|
|
<i v-else-if="type === 'question'" :class="$style.iconInner" class="ti ti-help-circle"></i>
|
2023-01-10 05:12:38 -06:00
|
|
|
<MkLoading v-else-if="type === 'waiting'" :class="$style.iconInner" :em="true"/>
|
2020-10-17 06:12:00 -05:00
|
|
|
</div>
|
2023-01-09 18:41:53 -06:00
|
|
|
<header v-if="title" :class="$style.title"><Mfm :text="title"/></header>
|
|
|
|
<div v-if="text" :class="$style.text"><Mfm :text="text"/></div>
|
2023-02-20 01:40:24 -06:00
|
|
|
<MkInput v-if="input" v-model="inputValue" autofocus :type="input.type || 'text'" :placeholder="input.placeholder || undefined" :autocomplete="input.autocomplete" @keydown="onInputKeydown">
|
2022-12-19 04:01:30 -06:00
|
|
|
<template v-if="input.type === 'password'" #prefix><i class="ti ti-lock"></i></template>
|
2023-02-20 01:40:24 -06:00
|
|
|
<template #caption>
|
2024-01-30 04:53:53 -06:00
|
|
|
<span v-if="okButtonDisabledReason === 'charactersExceeded'" v-text="i18n.tsx._dialog.charactersExceeded({ current: (inputValue as string)?.length ?? 0, max: input.maxLength ?? 'NaN' })"/>
|
|
|
|
<span v-else-if="okButtonDisabledReason === 'charactersBelow'" v-text="i18n.tsx._dialog.charactersBelow({ current: (inputValue as string)?.length ?? 0, min: input.minLength ?? 'NaN' })"/>
|
2023-02-20 01:40:24 -06:00
|
|
|
</template>
|
2021-11-28 05:07:37 -06:00
|
|
|
</MkInput>
|
2021-08-06 08:29:19 -05:00
|
|
|
<MkSelect v-if="select" v-model="selectedValue" autofocus>
|
2020-10-17 06:12:00 -05:00
|
|
|
<template v-if="select.items">
|
|
|
|
<option v-for="item in select.items" :value="item.value">{{ item.text }}</option>
|
2020-01-29 13:37:25 -06:00
|
|
|
</template>
|
|
|
|
<template v-else>
|
2020-10-17 06:12:00 -05: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 13:37:25 -06:00
|
|
|
</template>
|
2020-10-17 06:12:00 -05:00
|
|
|
</MkSelect>
|
2023-01-09 18:41:53 -06:00
|
|
|
<div v-if="(showOkButton || showCancelButton) && !actions" :class="$style.buttons">
|
2023-12-06 23:42:09 -06:00
|
|
|
<MkButton v-if="showOkButton" data-cy-modal-dialog-ok inline primary rounded :autofocus="!input && !select" :disabled="okButtonDisabledReason" @click="ok">{{ okText ?? ((showCancelButton || input || select) ? i18n.ts.ok : i18n.ts.gotIt) }}</MkButton>
|
2023-05-08 18:01:54 -05:00
|
|
|
<MkButton v-if="showCancelButton || input || select" data-cy-modal-dialog-cancel inline rounded @click="cancel">{{ cancelText ?? i18n.ts.cancel }}</MkButton>
|
2020-10-17 06:12:00 -05:00
|
|
|
</div>
|
2023-01-09 18:41:53 -06:00
|
|
|
<div v-if="actions" :class="$style.buttons">
|
2023-04-08 01:49:29 -05:00
|
|
|
<MkButton v-for="action in actions" :key="action.text" inline rounded :primary="action.primary" :danger="action.danger" @click="() => { action.callback(); modal?.close(); }">{{ action.text }}</MkButton>
|
2020-01-29 13:37:25 -06:00
|
|
|
</div>
|
2020-10-17 06:12:00 -05:00
|
|
|
</div>
|
|
|
|
</MkModal>
|
2020-01-29 13:37:25 -06:00
|
|
|
</template>
|
|
|
|
|
2022-01-10 09:05:18 -06:00
|
|
|
<script lang="ts" setup>
|
2023-12-06 23:42:09 -06:00
|
|
|
import { onBeforeUnmount, onMounted, ref, shallowRef, computed } from 'vue';
|
2022-09-06 04:21:49 -05:00
|
|
|
import MkModal from '@/components/MkModal.vue';
|
|
|
|
import MkButton from '@/components/MkButton.vue';
|
2023-01-07 00:09:46 -06:00
|
|
|
import MkInput from '@/components/MkInput.vue';
|
|
|
|
import MkSelect from '@/components/MkSelect.vue';
|
2023-09-19 02:37:43 -05:00
|
|
|
import { i18n } from '@/i18n.js';
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2022-01-10 09:05:18 -06:00
|
|
|
type Input = {
|
2023-02-20 01:40:24 -06:00
|
|
|
type: 'text' | 'number' | 'password' | 'email' | 'url' | 'date' | 'time' | 'search' | 'datetime-local';
|
2022-01-10 09:05:18 -06:00
|
|
|
placeholder?: string | null;
|
2023-02-20 01:40:24 -06:00
|
|
|
autocomplete?: string;
|
|
|
|
default: string | number | null;
|
|
|
|
minLength?: number;
|
|
|
|
maxLength?: number;
|
2022-01-10 09:05:18 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
2023-04-05 00:30:03 -05:00
|
|
|
danger?: boolean,
|
2022-01-10 09:05:18 -06:00
|
|
|
callback: (...args: any[]) => void;
|
|
|
|
}[];
|
|
|
|
showOkButton?: boolean;
|
|
|
|
showCancelButton?: boolean;
|
|
|
|
cancelableByBgClick?: boolean;
|
2023-02-09 19:45:32 -06:00
|
|
|
okText?: string;
|
|
|
|
cancelText?: string;
|
2022-01-10 09:05:18 -06:00
|
|
|
}>(), {
|
|
|
|
type: 'info',
|
|
|
|
showOkButton: true,
|
|
|
|
showCancelButton: false,
|
|
|
|
cancelableByBgClick: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
2022-05-26 08:53:09 -05:00
|
|
|
(ev: 'done', v: { canceled: boolean; result: any }): void;
|
|
|
|
(ev: 'closed'): void;
|
2022-01-10 09:05:18 -06:00
|
|
|
}>();
|
|
|
|
|
2023-01-02 22:37:32 -06:00
|
|
|
const modal = shallowRef<InstanceType<typeof MkModal>>();
|
2022-01-10 09:05:18 -06:00
|
|
|
|
2023-02-20 01:40:24 -06:00
|
|
|
const inputValue = ref<string | number | null>(props.input?.default ?? null);
|
|
|
|
const selectedValue = ref(props.select?.default ?? null);
|
|
|
|
|
2023-12-06 23:42:09 -06:00
|
|
|
const okButtonDisabledReason = computed<null | 'charactersExceeded' | 'charactersBelow'>(() => {
|
2023-02-20 01:40:24 -06:00
|
|
|
if (props.input) {
|
|
|
|
if (props.input.minLength) {
|
2024-01-30 04:53:53 -06:00
|
|
|
if (inputValue.value == null || (inputValue.value as string).length < props.input.minLength) {
|
2023-12-06 23:42:09 -06:00
|
|
|
return 'charactersBelow';
|
2023-02-20 01:40:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (props.input.maxLength) {
|
|
|
|
if (inputValue.value && (inputValue.value as string).length > props.input.maxLength) {
|
2023-12-06 23:42:09 -06:00
|
|
|
return 'charactersExceeded';
|
2023-02-20 01:40:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-06 23:42:09 -06:00
|
|
|
return null;
|
2023-02-20 01:40:24 -06:00
|
|
|
});
|
2022-01-10 09:05:18 -06:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2023-10-29 02:09:20 -05:00
|
|
|
|
2022-01-10 09:05:18 -06:00
|
|
|
/*
|
|
|
|
function onBgClick() {
|
|
|
|
if (props.cancelableByBgClick) cancel();
|
|
|
|
}
|
|
|
|
*/
|
2022-05-26 08:53:09 -05:00
|
|
|
function onKeydown(evt: KeyboardEvent) {
|
|
|
|
if (evt.key === 'Escape') cancel();
|
2022-01-10 09:05:18 -06:00
|
|
|
}
|
|
|
|
|
2022-05-26 08:53:09 -05:00
|
|
|
function onInputKeydown(evt: KeyboardEvent) {
|
|
|
|
if (evt.key === 'Enter') {
|
|
|
|
evt.preventDefault();
|
|
|
|
evt.stopPropagation();
|
2022-01-10 09:05:18 -06:00
|
|
|
ok();
|
2020-01-29 13:37:25 -06:00
|
|
|
}
|
2022-01-10 09:05:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
document.addEventListener('keydown', onKeydown);
|
|
|
|
});
|
|
|
|
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
document.removeEventListener('keydown', onKeydown);
|
2020-01-29 13:37:25 -06:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2023-01-09 18:41:53 -06:00
|
|
|
<style lang="scss" module>
|
|
|
|
.root {
|
2020-10-17 06:12:00 -05:00
|
|
|
position: relative;
|
2023-01-14 06:04:20 -06:00
|
|
|
margin: auto;
|
2020-10-17 06:12:00 -05:00
|
|
|
padding: 32px;
|
|
|
|
min-width: 320px;
|
|
|
|
max-width: 480px;
|
|
|
|
box-sizing: border-box;
|
|
|
|
text-align: center;
|
|
|
|
background: var(--panel);
|
2023-05-04 18:37:20 -05:00
|
|
|
border-radius: 16px;
|
2023-01-09 18:41:53 -06:00
|
|
|
}
|
2020-10-17 06:12:00 -05:00
|
|
|
|
2023-01-09 18:41:53 -06:00
|
|
|
.icon {
|
|
|
|
font-size: 24px;
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2023-01-10 05:12:38 -06:00
|
|
|
& + .title {
|
|
|
|
margin-top: 8px;
|
2023-01-09 18:41:53 -06:00
|
|
|
}
|
2023-01-10 05:12:38 -06:00
|
|
|
}
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2023-01-10 05:12:38 -06:00
|
|
|
.iconInner {
|
|
|
|
display: block;
|
|
|
|
margin: 0 auto;
|
|
|
|
}
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2023-01-10 05:12:38 -06:00
|
|
|
.type_info {
|
|
|
|
color: #55c4dd;
|
|
|
|
}
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2023-01-10 05:12:38 -06:00
|
|
|
.type_success {
|
|
|
|
color: var(--success);
|
|
|
|
}
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2023-01-10 05:12:38 -06:00
|
|
|
.type_error {
|
|
|
|
color: var(--error);
|
|
|
|
}
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2023-01-10 05:12:38 -06:00
|
|
|
.type_warning {
|
|
|
|
color: var(--warn);
|
2023-01-09 18:41:53 -06:00
|
|
|
}
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2023-01-09 18:41:53 -06:00
|
|
|
.title {
|
|
|
|
margin: 0 0 8px 0;
|
|
|
|
font-weight: bold;
|
|
|
|
font-size: 1.1em;
|
2020-01-29 13:37:25 -06:00
|
|
|
|
2023-01-09 18:41:53 -06:00
|
|
|
& + .text {
|
|
|
|
margin-top: 8px;
|
2020-01-29 13:37:25 -06:00
|
|
|
}
|
|
|
|
}
|
2023-01-09 18:41:53 -06:00
|
|
|
|
|
|
|
.text {
|
|
|
|
margin: 16px 0 0 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
.buttons {
|
|
|
|
margin-top: 16px;
|
|
|
|
display: flex;
|
|
|
|
gap: 8px;
|
|
|
|
flex-wrap: wrap;
|
|
|
|
justify-content: center;
|
|
|
|
}
|
2020-01-29 13:37:25 -06:00
|
|
|
</style>
|