2020-01-29 20:37:25 +01:00
|
|
|
<template>
|
2020-10-17 13:12:00 +02:00
|
|
|
<div class="tivcixzd" :class="{ done: closed || isVoted }">
|
2020-01-29 20:37:25 +01:00
|
|
|
<ul>
|
2021-11-19 11:36:12 +01:00
|
|
|
<li v-for="(choice, i) in poll.choices" :key="i" :class="{ voted: choice.voted }" @click="vote(i)">
|
2020-01-29 20:37:25 +01:00
|
|
|
<div class="backdrop" :style="{ 'width': `${showResult ? (choice.votes / total * 100) : 0}%` }"></div>
|
|
|
|
<span>
|
2021-04-20 16:22:59 +02:00
|
|
|
<template v-if="choice.isVoted"><i class="fas fa-check"></i></template>
|
2020-10-17 13:12:00 +02:00
|
|
|
<Mfm :text="choice.text" :plain="true" :custom-emojis="note.emojis"/>
|
2021-11-19 11:36:12 +01:00
|
|
|
<span v-if="showResult" class="votes">({{ $t('_poll.votesCount', { n: choice.votes }) }})</span>
|
2020-01-29 20:37:25 +01:00
|
|
|
</span>
|
|
|
|
</li>
|
|
|
|
</ul>
|
2021-07-09 19:55:12 +02:00
|
|
|
<p v-if="!readOnly">
|
2020-01-29 20:37:25 +01:00
|
|
|
<span>{{ $t('_poll.totalVotes', { n: total }) }}</span>
|
|
|
|
<span> · </span>
|
2020-12-26 02:47:36 +01:00
|
|
|
<a v-if="!closed && !isVoted" @click="toggleShowResult">{{ showResult ? $ts._poll.vote : $ts._poll.showResult }}</a>
|
|
|
|
<span v-if="isVoted">{{ $ts._poll.voted }}</span>
|
|
|
|
<span v-else-if="closed">{{ $ts._poll.closed }}</span>
|
2020-01-29 20:37:25 +01:00
|
|
|
<span v-if="remaining > 0"> · {{ timer }}</span>
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2020-10-17 13:12:00 +02:00
|
|
|
import { defineComponent } from 'vue';
|
2021-11-11 18:02:25 +01:00
|
|
|
import { sum } from '@/scripts/array';
|
|
|
|
import * as os from '@/os';
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2020-10-17 13:12:00 +02:00
|
|
|
export default defineComponent({
|
2020-01-29 20:37:25 +01:00
|
|
|
props: {
|
|
|
|
note: {
|
|
|
|
type: Object,
|
|
|
|
required: true
|
2021-07-09 19:55:12 +02:00
|
|
|
},
|
|
|
|
readOnly: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false,
|
|
|
|
default: false,
|
2020-01-29 20:37:25 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
remaining: -1,
|
|
|
|
showResult: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
poll(): any {
|
|
|
|
return this.note.poll;
|
|
|
|
},
|
|
|
|
total(): number {
|
|
|
|
return sum(this.poll.choices.map(x => x.votes));
|
|
|
|
},
|
|
|
|
closed(): boolean {
|
|
|
|
return !this.remaining;
|
|
|
|
},
|
|
|
|
timer(): string {
|
|
|
|
return this.$t(
|
2020-07-19 17:57:10 +02:00
|
|
|
this.remaining >= 86400 ? '_poll.remainingDays' :
|
|
|
|
this.remaining >= 3600 ? '_poll.remainingHours' :
|
|
|
|
this.remaining >= 60 ? '_poll.remainingMinutes' : '_poll.remainingSeconds', {
|
2020-01-29 20:37:25 +01:00
|
|
|
s: Math.floor(this.remaining % 60),
|
|
|
|
m: Math.floor(this.remaining / 60) % 60,
|
|
|
|
h: Math.floor(this.remaining / 3600) % 24,
|
|
|
|
d: Math.floor(this.remaining / 86400)
|
|
|
|
});
|
|
|
|
},
|
|
|
|
isVoted(): boolean {
|
|
|
|
return !this.poll.multiple && this.poll.choices.some(c => c.isVoted);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
created() {
|
2021-07-09 19:55:12 +02:00
|
|
|
this.showResult = this.readOnly || this.isVoted;
|
2020-01-29 20:37:25 +01:00
|
|
|
|
|
|
|
if (this.note.poll.expiresAt) {
|
|
|
|
const update = () => {
|
|
|
|
if (this.remaining = Math.floor(Math.max(new Date(this.note.poll.expiresAt).getTime() - Date.now(), 0) / 1000))
|
|
|
|
requestAnimationFrame(update);
|
|
|
|
else
|
|
|
|
this.showResult = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
toggleShowResult() {
|
|
|
|
this.showResult = !this.showResult;
|
|
|
|
},
|
2021-11-28 12:23:48 +01:00
|
|
|
async vote(id) {
|
2021-07-09 19:55:12 +02:00
|
|
|
if (this.readOnly || this.closed || !this.poll.multiple && this.poll.choices.some(c => c.isVoted)) return;
|
2021-11-28 12:23:48 +01:00
|
|
|
|
|
|
|
const { canceled } = await os.confirm({
|
|
|
|
type: 'question',
|
|
|
|
text: this.$t('voteConfirm', { choice: this.poll.choices[id].text }),
|
|
|
|
});
|
|
|
|
if (canceled) return;
|
|
|
|
|
|
|
|
await os.api('notes/polls/vote', {
|
2020-01-29 20:37:25 +01:00
|
|
|
noteId: this.note.id,
|
|
|
|
choice: id
|
|
|
|
});
|
2021-11-28 12:23:48 +01:00
|
|
|
if (!this.showResult) this.showResult = !this.poll.multiple;
|
2020-01-29 20:37:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
2020-05-10 10:06:20 +02:00
|
|
|
.tivcixzd {
|
2020-01-29 20:37:25 +01:00
|
|
|
> ul {
|
|
|
|
display: block;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
list-style: none;
|
|
|
|
|
|
|
|
> li {
|
|
|
|
display: block;
|
|
|
|
position: relative;
|
|
|
|
margin: 4px 0;
|
|
|
|
padding: 4px 8px;
|
2021-04-10 05:40:50 +02:00
|
|
|
border: solid 0.5px var(--divider);
|
2020-01-29 20:37:25 +01:00
|
|
|
border-radius: 4px;
|
2021-03-02 14:57:16 +01:00
|
|
|
overflow: hidden;
|
2020-01-29 20:37:25 +01:00
|
|
|
cursor: pointer;
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
background: rgba(#000, 0.05);
|
|
|
|
}
|
|
|
|
|
|
|
|
&:active {
|
|
|
|
background: rgba(#000, 0.1);
|
|
|
|
}
|
|
|
|
|
|
|
|
> .backdrop {
|
|
|
|
position: absolute;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
height: 100%;
|
|
|
|
background: var(--accent);
|
|
|
|
transition: width 1s ease;
|
|
|
|
}
|
|
|
|
|
|
|
|
> span {
|
|
|
|
position: relative;
|
|
|
|
|
2021-04-20 16:22:59 +02:00
|
|
|
> i {
|
2020-01-29 20:37:25 +01:00
|
|
|
margin-right: 4px;
|
|
|
|
}
|
|
|
|
|
|
|
|
> .votes {
|
|
|
|
margin-left: 4px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> p {
|
|
|
|
color: var(--fg);
|
|
|
|
|
|
|
|
a {
|
|
|
|
color: inherit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 13:12:00 +02:00
|
|
|
&.done {
|
2020-01-29 20:37:25 +01:00
|
|
|
> ul > li {
|
|
|
|
cursor: default;
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
background: transparent;
|
|
|
|
}
|
|
|
|
|
|
|
|
&:active {
|
|
|
|
background: transparent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|