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