iceshrimp-161sh/packages/client/src/components/MkPoll.vue

204 lines
4.1 KiB
Vue
Raw Normal View History

<template>
2023-04-08 02:01:42 +02:00
<div class="tivcixzd" :class="{ done: closed || isVoted }">
<ul>
<li
v-for="(choice, i) in note.poll.choices"
:key="i"
:class="{ voted: choice.voted }"
@click.stop="vote(i)"
>
<div
class="backdrop"
:style="{
width: `${
showResult ? (choice.votes / total) * 100 : 0
}%`,
}"
></div>
<span>
<template v-if="choice.isVoted"
><i class="ph-check ph-bold ph-lg"></i
></template>
<Mfm
:text="choice.text"
:plain="true"
:custom-emojis="note.emojis"
/>
<span v-if="showResult" class="votes"
>({{
i18n.t("_poll.votesCount", { n: choice.votes })
}})</span
>
</span>
</li>
</ul>
<p v-if="!readOnly">
<span>{{ i18n.t("_poll.totalVotes", { n: total }) }}</span>
<span> · </span>
<a
v-if="!closed && !isVoted"
@click.stop="showResult = !showResult"
>{{
showResult ? i18n.ts._poll.vote : i18n.ts._poll.showResult
}}</a
>
<span v-if="isVoted">{{ i18n.ts._poll.voted }}</span>
<span v-else-if="closed">{{ i18n.ts._poll.closed }}</span>
<span v-if="remaining > 0"> · {{ timer }}</span>
</p>
</div>
</template>
2022-07-20 15:24:26 +02:00
<script lang="ts" setup>
2023-04-08 02:01:42 +02:00
import { computed, onUnmounted, ref, toRef } from "vue";
import * as misskey from "calckey-js";
import { sum } from "@/scripts/array";
import { pleaseLogin } from "@/scripts/please-login";
import * as os from "@/os";
import { i18n } from "@/i18n";
import { useInterval } from "@/scripts/use-interval";
2022-07-20 15:24:26 +02:00
const props = defineProps<{
note: misskey.entities.Note;
readOnly?: boolean;
}>();
const remaining = ref(-1);
2023-04-08 02:01:42 +02:00
const total = computed(() => sum(props.note.poll.choices.map((x) => x.votes)));
2022-07-20 15:24:26 +02:00
const closed = computed(() => remaining.value === 0);
2023-04-08 02:01:42 +02:00
const isVoted = computed(
() =>
!props.note.poll.multiple &&
props.note.poll.choices.some((c) => c.isVoted)
);
const timer = computed(() =>
i18n.t(
remaining.value >= 86400
? "_poll.remainingDays"
: remaining.value >= 3600
? "_poll.remainingHours"
: remaining.value >= 60
? "_poll.remainingMinutes"
: "_poll.remainingSeconds",
{
s: Math.floor(remaining.value % 60),
m: Math.floor(remaining.value / 60) % 60,
h: Math.floor(remaining.value / 3600) % 24,
d: Math.floor(remaining.value / 86400),
}
)
);
2022-07-20 15:24:26 +02:00
const showResult = ref(props.readOnly || isVoted.value);
// 期限付きアンケート
if (props.note.poll.expiresAt) {
const tick = () => {
2023-04-08 02:01:42 +02:00
remaining.value = Math.floor(
Math.max(
new Date(props.note.poll.expiresAt).getTime() - Date.now(),
0
) / 1000
);
2022-07-20 15:24:26 +02:00
if (remaining.value === 0) {
showResult.value = true;
}
2022-07-20 15:24:26 +02:00
};
useInterval(tick, 3000, {
immediate: true,
afterMounted: false,
});
}
const vote = async (id) => {
pleaseLogin();
if (props.readOnly || closed.value || isVoted.value) return;
const { canceled } = await os.confirm({
2023-04-08 02:01:42 +02:00
type: "question",
text: i18n.t("voteConfirm", {
choice: props.note.poll.choices[id].text,
}),
2022-07-20 15:24:26 +02:00
});
if (canceled) return;
2021-12-03 08:07:50 +01:00
2023-04-08 02:01:42 +02:00
await os.api("notes/polls/vote", {
2022-07-20 15:24:26 +02:00
noteId: props.note.id,
choice: id,
});
if (!showResult.value) showResult.value = !props.note.poll.multiple;
};
</script>
<style lang="scss" scoped>
.tivcixzd {
> ul {
display: block;
margin: 0;
padding: 0;
list-style: none;
> li {
display: block;
position: relative;
margin: 4px 0;
2021-12-03 08:07:50 +01:00
padding: 4px;
//border: solid 0.5px var(--divider);
background: var(--accentedBg);
border-radius: 4px;
overflow: hidden;
cursor: pointer;
> .backdrop {
position: absolute;
top: 0;
left: 0;
height: 100%;
background: var(--accent);
2023-04-08 02:01:42 +02:00
background: linear-gradient(
90deg,
var(--buttonGradateA),
var(--buttonGradateB)
);
transition: width 1s ease;
}
> span {
position: relative;
2021-12-03 08:07:50 +01:00
display: inline-block;
padding: 3px 5px;
background: var(--panel);
border-radius: 3px;
> i {
margin-right: 4px;
2021-12-03 08:07:50 +01:00
color: var(--accent);
}
> .votes {
margin-left: 4px;
2021-12-03 08:07:50 +01:00
opacity: 0.7;
}
}
}
}
> p {
color: var(--fg);
a {
color: inherit;
}
}
Migrate to Vue3 (#6587) * Update reaction.vue * fix bug * wip * wip * wjio * wip * Revert "wip" This reverts commit e427f2160adf4e8a4147006e25a89854edab0033. * wip * wip * wip * Update init.ts * Update drive-window.vue * wip * wip * Use PascalCase for components * Use PascalCase for components * update dep * wip * wip * wip * Update init.ts * wip * Update paging.ts * Update test.vue * watch deep * wip * lint * wip * wip * wip * wip * wiop * wip * Update webpack.config.ts * alllow null poll * wip * wip * wip * wiop * UI redesign & refactor (#6714) * wip * wip * wip * wip * wip * Update drive.vue * Update word-mute.vue * wip * wip * wip * clean up * wip * Update default.vue * wip * Update notes.vue * Update mfm.ts * Update index.home.vue * Update post-form.vue * Update post-form-attaches.vue * wip * Update post-form.vue * Update sidebar.vue * wip * wip * Update index.vue * wip * Update default.vue * Update index.vue * Update index.vue * wip * Update post-form-attaches.vue * Update note.vue * wip * clean up * Update notes.vue * wip * wip * Update ja-JP.yml * wip * wip * Update index.vue * wip * wip * wip * wip * wip * wip * wip * wip * Update default.vue * wip * Update _dark.json5 * wip * wip * wip * clean up * wip * wip * Update index.vue * Update test.vue * wip * wip * fix * wip * wip * wip * wip * clena yop * wip * wip * Update store.ts * Update messaging-room.vue * Update default.widgets.vue * fix * wip * wip * Update modal.vue * wip * Update os.ts * Update os.ts * Update deck.vue * Update init.ts * wip * Update ja-JP.yml * v-sizeは単にwindowのresizeを監視するだけで良いかもしれない * Update modal.vue * wip * Update tooltip.ts * wip * wip * wip * wip * wip * Update image-viewer.vue * wip * wip * Update style.scss * Update style.scss * Update visitor.vue * wip * Update init.ts * Update init.ts * wip * wip * Update visitor.vue * Update visitor.vue * Update visitor.vue * Update visitor.vue * wip * wip * Update modal.vue * Update header.vue * Update menu.vue * Update about.vue * Update about-misskey.vue * wip * wip * Update visitor.vue * Update tooltip.ts * wip * Update drive.vue * wip * Update style.scss * Update header.vue * wip * wip * Update users.user.vue * Update announcements.vue * wip * wip * wip * Update emojis.vue * wip * Update emojis.vue * Update style.scss * Update users.vue * wip * Update style.scss * wip * Update welcome.entrance.vue * Update radio.vue * Update size.ts * Update emoji-edit-dialog.vue * wip * Update emojis.vue * wip * Update emojis.vue * Update emojis.vue * Update emojis.vue * wip * wip * wip * wip * Update file-dialog.vue * wip * wip * Update token-generate-window.vue * Update notification-setting-window.vue * wip * wip * Update _error_.vue * Update ja-JP.yml * wip * wip * Update store.ts * Update emojis.vue * Update emojis.vue * Update emojis.vue * Update announcements.vue * Update store.ts * wip * Update page-editor.vue * wip * wip * Update modal.vue * wip * Update select-file.ts * Update timeline.vue * Update emojis.vue * Update os.ts * wip * Update user-select.vue * Update mfm.ts * Update get-file-info.ts * Update drive.vue * Update init.ts * Update mfm.ts * wip * wip * Update window.vue * Update note.vue * wip * wip * Update user-info.vue * wip * wip * wip * wip * wip * Update header.vue * Update header.vue * wip * Update explore.vue * wip * wip * wip * Update webpack.config.ts * wip * wip * wip * wip * wip * wip * Update autocomplete.ts * wip * wip * wip * Update toast.vue * wip * Update post-form-dialog.vue * wip * wip * wip * wip * wip * Update users.vue * wip * Update explore.vue * wip * wip * wip * Update package.json * wip * Update icon-dialog.vue * wip * wip * Update user-preview.ts * wip * wip * wip * wip * wip * Update instance.vue * Update user-name.vue * Update federation.vue * Update instance.vue * wip * wip * Update tag.vue * wip * wip * wip * wip * wip * Update instance.vue * wip * Update os.ts * Update os.ts * wip * wip * wip * Update router.ts * wip * Update init.ts * Update note.vue * Update messages.vue * wip * wip * wip * wip * wip * google * wip * wip * wip * wip * Update theme-editor.vue * wip * wip * Update room.vue * Update channel-editor.vue * wip * Update window.vue * Update window.vue * wip * Update window.vue * Update window.vue * wip * Update menu.vue * wip * wip * wip * wip * Update messaging-room.vue * wip * Update post-form.vue * Update default.widgets.vue * Update window.vue * wip
2020-10-17 13:12:00 +02:00
&.done {
> ul > li {
cursor: default;
}
}
}
</style>