2020-01-30 04:37:25 +09:00
|
|
|
<template>
|
2023-04-08 09:01:42 +09:00
|
|
|
<div class="zmdxowus">
|
|
|
|
<p v-if="choices.length < 2" class="caution">
|
|
|
|
<i class="ph-warning ph-bold ph-lg"></i
|
|
|
|
>{{ i18n.ts._poll.noOnlyOneChoice }}
|
|
|
|
</p>
|
|
|
|
<ul>
|
|
|
|
<li v-for="(choice, i) in choices" :key="i">
|
|
|
|
<MkInput
|
|
|
|
class="input"
|
|
|
|
small
|
|
|
|
:model-value="choice"
|
|
|
|
:placeholder="i18n.t('_poll.choiceN', { n: i + 1 })"
|
|
|
|
@update:modelValue="onInput(i, $event)"
|
|
|
|
>
|
2020-10-17 20:12:00 +09:00
|
|
|
</MkInput>
|
2023-04-08 09:01:42 +09:00
|
|
|
<button class="_button" @click="remove(i)">
|
|
|
|
<i class="ph-x ph-bold ph-lg"></i>
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
<MkButton v-if="choices.length < 10" class="add" @click="add">{{
|
|
|
|
i18n.ts.add
|
|
|
|
}}</MkButton>
|
|
|
|
<MkButton v-else class="add" disabled>{{
|
|
|
|
i18n.ts._poll.noMore
|
|
|
|
}}</MkButton>
|
|
|
|
<MkSwitch v-model="multiple">{{
|
|
|
|
i18n.ts._poll.canMultipleVote
|
|
|
|
}}</MkSwitch>
|
|
|
|
<section>
|
|
|
|
<div>
|
|
|
|
<MkSelect v-model="expiration" small>
|
|
|
|
<template #label>{{ i18n.ts._poll.expiration }}</template>
|
|
|
|
<option value="infinite">
|
|
|
|
{{ i18n.ts._poll.infinite }}
|
|
|
|
</option>
|
|
|
|
<option value="at">{{ i18n.ts._poll.at }}</option>
|
|
|
|
<option value="after">{{ i18n.ts._poll.after }}</option>
|
2020-10-17 20:12:00 +09:00
|
|
|
</MkSelect>
|
2023-04-08 09:01:42 +09:00
|
|
|
<section v-if="expiration === 'at'">
|
|
|
|
<MkInput v-model="atDate" small type="date" class="input">
|
|
|
|
<template #label>{{
|
|
|
|
i18n.ts._poll.deadlineDate
|
|
|
|
}}</template>
|
|
|
|
</MkInput>
|
|
|
|
<MkInput v-model="atTime" small type="time" class="input">
|
|
|
|
<template #label>{{
|
|
|
|
i18n.ts._poll.deadlineTime
|
|
|
|
}}</template>
|
|
|
|
</MkInput>
|
|
|
|
</section>
|
|
|
|
<section v-else-if="expiration === 'after'">
|
|
|
|
<MkInput v-model="after" small type="number" class="input">
|
|
|
|
<template #label>{{ i18n.ts._poll.duration }}</template>
|
|
|
|
</MkInput>
|
|
|
|
<MkSelect v-model="unit" small>
|
|
|
|
<option value="second">
|
|
|
|
{{ i18n.ts._time.second }}
|
|
|
|
</option>
|
|
|
|
<option value="minute">
|
|
|
|
{{ i18n.ts._time.minute }}
|
|
|
|
</option>
|
|
|
|
<option value="hour">{{ i18n.ts._time.hour }}</option>
|
|
|
|
<option value="day">{{ i18n.ts._time.day }}</option>
|
|
|
|
</MkSelect>
|
|
|
|
</section>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
</div>
|
2020-01-30 04:37:25 +09:00
|
|
|
</template>
|
|
|
|
|
2022-01-26 03:26:12 +09:00
|
|
|
<script lang="ts" setup>
|
2023-04-08 09:01:42 +09:00
|
|
|
import { ref, watch } from "vue";
|
|
|
|
import MkInput from "./form/input.vue";
|
|
|
|
import MkSelect from "./form/select.vue";
|
|
|
|
import MkSwitch from "./form/switch.vue";
|
|
|
|
import MkButton from "./MkButton.vue";
|
|
|
|
import { formatDateTimeString } from "@/scripts/format-time-string";
|
|
|
|
import { addTime } from "@/scripts/time";
|
|
|
|
import { i18n } from "@/i18n";
|
2020-01-30 04:37:25 +09:00
|
|
|
|
2022-01-26 03:26:12 +09:00
|
|
|
const props = defineProps<{
|
|
|
|
modelValue: {
|
|
|
|
expiresAt: string;
|
|
|
|
expiredAfter: number;
|
|
|
|
choices: string[];
|
|
|
|
multiple: boolean;
|
|
|
|
};
|
|
|
|
}>();
|
|
|
|
const emit = defineEmits<{
|
2023-04-08 09:01:42 +09:00
|
|
|
(
|
|
|
|
ev: "update:modelValue",
|
|
|
|
v: {
|
|
|
|
expiresAt: string;
|
|
|
|
expiredAfter: number;
|
|
|
|
choices: string[];
|
|
|
|
multiple: boolean;
|
|
|
|
}
|
|
|
|
): void;
|
2022-01-26 03:26:12 +09:00
|
|
|
}>();
|
|
|
|
|
|
|
|
const choices = ref(props.modelValue.choices);
|
|
|
|
const multiple = ref(props.modelValue.multiple);
|
2023-04-08 09:01:42 +09:00
|
|
|
const expiration = ref("infinite");
|
|
|
|
const atDate = ref(
|
|
|
|
formatDateTimeString(addTime(new Date(), 1, "day"), "yyyy-MM-dd")
|
|
|
|
);
|
|
|
|
const atTime = ref("00:00");
|
2022-01-26 03:26:12 +09:00
|
|
|
const after = ref(0);
|
2023-04-08 09:01:42 +09:00
|
|
|
const unit = ref("second");
|
2022-01-26 03:26:12 +09:00
|
|
|
|
|
|
|
if (props.modelValue.expiresAt) {
|
2023-04-08 09:01:42 +09:00
|
|
|
expiration.value = "at";
|
2022-01-26 03:26:12 +09:00
|
|
|
atDate.value = atTime.value = props.modelValue.expiresAt;
|
2023-04-08 09:01:42 +09:00
|
|
|
} else if (typeof props.modelValue.expiredAfter === "number") {
|
|
|
|
expiration.value = "after";
|
2022-01-26 03:26:12 +09:00
|
|
|
after.value = props.modelValue.expiredAfter / 1000;
|
|
|
|
} else {
|
2023-04-08 09:01:42 +09:00
|
|
|
expiration.value = "infinite";
|
2022-01-26 03:26:12 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
function onInput(i, value) {
|
|
|
|
choices.value[i] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function add() {
|
2023-04-08 09:01:42 +09:00
|
|
|
choices.value.push("");
|
2022-01-26 03:26:12 +09:00
|
|
|
// TODO
|
|
|
|
// nextTick(() => {
|
|
|
|
// (this.$refs.choices as any).childNodes[this.choices.length - 1].childNodes[0].focus();
|
|
|
|
// });
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove(i) {
|
2022-05-26 22:53:09 +09:00
|
|
|
choices.value = choices.value.filter((_, _i) => _i !== i);
|
2022-01-26 03:26:12 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
function get() {
|
|
|
|
const calcAt = () => {
|
|
|
|
return new Date(`${atDate.value} ${atTime.value}`).getTime();
|
|
|
|
};
|
|
|
|
|
|
|
|
const calcAfter = () => {
|
|
|
|
let base = parseInt(after.value);
|
|
|
|
switch (unit.value) {
|
2023-04-08 09:01:42 +09:00
|
|
|
case "day":
|
|
|
|
base *= 24;
|
|
|
|
// fallthrough
|
|
|
|
case "hour":
|
|
|
|
base *= 60;
|
|
|
|
// fallthrough
|
|
|
|
case "minute":
|
|
|
|
base *= 60;
|
|
|
|
// fallthrough
|
|
|
|
case "second":
|
|
|
|
return (base *= 1000);
|
|
|
|
default:
|
|
|
|
return null;
|
2020-10-18 15:52:34 +09:00
|
|
|
}
|
2022-01-26 03:26:12 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
choices: choices.value,
|
|
|
|
multiple: multiple.value,
|
2023-04-08 09:01:42 +09:00
|
|
|
...(expiration.value === "at"
|
|
|
|
? { expiresAt: calcAt() }
|
|
|
|
: expiration.value === "after"
|
|
|
|
? { expiredAfter: calcAfter() }
|
|
|
|
: {}),
|
2022-01-26 03:26:12 +09:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-08 09:01:42 +09:00
|
|
|
watch(
|
|
|
|
[choices, multiple, expiration, atDate, atTime, after, unit],
|
|
|
|
() => emit("update:modelValue", get()),
|
|
|
|
{
|
|
|
|
deep: true,
|
|
|
|
}
|
|
|
|
);
|
2020-01-30 04:37:25 +09:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
.zmdxowus {
|
2022-01-26 03:31:10 +09:00
|
|
|
padding: 8px 16px;
|
2020-01-30 04:37:25 +09:00
|
|
|
|
|
|
|
> .caution {
|
|
|
|
margin: 0 0 8px 0;
|
|
|
|
font-size: 0.8em;
|
|
|
|
color: #f00;
|
|
|
|
|
2021-04-20 23:22:59 +09:00
|
|
|
> i {
|
2020-01-30 04:37:25 +09:00
|
|
|
margin-right: 4px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> ul {
|
|
|
|
display: block;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
list-style: none;
|
|
|
|
|
|
|
|
> li {
|
|
|
|
display: flex;
|
|
|
|
margin: 8px 0;
|
|
|
|
padding: 0;
|
|
|
|
width: 100%;
|
|
|
|
|
|
|
|
> .input {
|
|
|
|
flex: 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
> button {
|
|
|
|
width: 32px;
|
|
|
|
padding: 4px 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .add {
|
2022-01-26 03:26:12 +09:00
|
|
|
margin: 8px 0;
|
2020-01-30 04:37:25 +09:00
|
|
|
z-index: 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
> section {
|
2021-12-03 13:55:30 +09:00
|
|
|
margin: 16px 0 0 0;
|
2020-01-30 04:37:25 +09:00
|
|
|
|
|
|
|
> div {
|
|
|
|
margin: 0 8px;
|
2022-01-26 03:26:12 +09:00
|
|
|
display: flex;
|
|
|
|
flex-direction: row;
|
|
|
|
flex-wrap: wrap;
|
|
|
|
gap: 12px;
|
2020-01-30 04:37:25 +09:00
|
|
|
|
|
|
|
&:last-child {
|
|
|
|
flex: 1 0 auto;
|
|
|
|
|
2022-01-26 03:26:12 +09:00
|
|
|
> div {
|
|
|
|
flex-grow: 1;
|
|
|
|
}
|
|
|
|
|
2020-01-30 04:37:25 +09:00
|
|
|
> section {
|
2022-01-26 03:26:12 +09:00
|
|
|
// MAGIC: Prevent div above from growing unless wrapped to its own line
|
|
|
|
flex-grow: 9999;
|
|
|
|
align-items: end;
|
2020-01-30 04:37:25 +09:00
|
|
|
display: flex;
|
2022-01-26 03:26:12 +09:00
|
|
|
gap: 4px;
|
2020-01-30 04:37:25 +09:00
|
|
|
|
|
|
|
> .input {
|
2022-01-26 03:26:12 +09:00
|
|
|
flex: 1 1 auto;
|
2020-01-30 04:37:25 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|