2018-02-08 05:02:08 +00:00
|
|
|
<template>
|
2019-03-06 13:55:47 +00:00
|
|
|
<div class="mk-poll" :data-done="closed || isVoted">
|
2017-02-14 04:59:26 +00:00
|
|
|
<ul>
|
2019-04-07 12:50:36 +00:00
|
|
|
<li v-for="(choice, i) in poll.choices" :key="i" @click="vote(i)" :class="{ voted: choice.voted }" :title="!closed && !isVoted ? $t('vote-to').replace('{}', choice.text) : ''">
|
2019-03-06 13:55:47 +00:00
|
|
|
<div class="backdrop" :style="{ 'width': `${showResult ? (choice.votes / total * 100) : 0}%` }"></div>
|
2017-02-14 04:59:26 +00:00
|
|
|
<span>
|
2018-11-05 16:40:11 +00:00
|
|
|
<template v-if="choice.isVoted"><fa icon="check"/></template>
|
2019-07-05 15:46:00 +00:00
|
|
|
<mfm :text="choice.text" :plain="true" :custom-emojis="note.emojis"/>
|
2018-11-08 18:44:35 +00:00
|
|
|
<span class="votes" v-if="showResult">({{ $t('vote-count').replace('{}', choice.votes) }})</span>
|
2017-02-14 04:59:26 +00:00
|
|
|
</span>
|
|
|
|
</li>
|
|
|
|
</ul>
|
2019-03-06 13:55:47 +00:00
|
|
|
<p>
|
|
|
|
<span>{{ $t('total-votes').replace('{}', total) }}</span>
|
|
|
|
<span> · </span>
|
|
|
|
<a v-if="!closed && !isVoted" @click="toggleShowResult">{{ showResult ? $t('vote') : $t('show-result') }}</a>
|
2018-11-08 18:44:35 +00:00
|
|
|
<span v-if="isVoted">{{ $t('voted') }}</span>
|
2019-03-06 13:55:47 +00:00
|
|
|
<span v-else-if="closed">{{ $t('closed') }}</span>
|
|
|
|
<span v-if="remaining > 0"> · {{ timer }}</span>
|
2017-02-14 08:17:39 +00:00
|
|
|
</p>
|
2018-02-08 05:02:08 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2018-02-22 13:03:44 +00:00
|
|
|
<script lang="ts">
|
|
|
|
import Vue from 'vue';
|
2018-11-08 18:44:35 +00:00
|
|
|
import i18n from '../../../i18n';
|
2018-09-06 19:21:04 +00:00
|
|
|
import { sum } from '../../../../../prelude/array';
|
2018-02-22 13:03:44 +00:00
|
|
|
export default Vue.extend({
|
2018-11-08 18:44:35 +00:00
|
|
|
i18n: i18n('common/views/components/poll.vue'),
|
2018-04-07 17:30:37 +00:00
|
|
|
props: ['note'],
|
2018-02-22 13:03:44 +00:00
|
|
|
data() {
|
|
|
|
return {
|
2019-03-06 13:55:47 +00:00
|
|
|
remaining: -1,
|
2018-02-22 13:03:44 +00:00
|
|
|
showResult: false
|
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
poll(): any {
|
2018-04-07 17:30:37 +00:00
|
|
|
return this.note.poll;
|
2018-02-08 06:07:55 +00:00
|
|
|
},
|
2018-02-22 13:03:44 +00:00
|
|
|
total(): number {
|
2018-09-06 19:21:04 +00:00
|
|
|
return sum(this.poll.choices.map(x => x.votes));
|
2018-02-08 06:07:55 +00:00
|
|
|
},
|
2019-03-06 13:55:47 +00:00
|
|
|
closed(): boolean {
|
|
|
|
return !this.remaining;
|
|
|
|
},
|
|
|
|
timer(): string {
|
|
|
|
return this.$t(
|
|
|
|
this.remaining > 86400 ? 'remaining-days' :
|
|
|
|
this.remaining > 3600 ? 'remaining-hours' :
|
|
|
|
this.remaining > 60 ? 'remaining-minutes' : 'remaining-seconds')
|
|
|
|
.replace('{s}', Math.floor(this.remaining % 60))
|
|
|
|
.replace('{m}', Math.floor(this.remaining / 60) % 60)
|
|
|
|
.replace('{h}', Math.floor(this.remaining / 3600) % 24)
|
|
|
|
.replace('{d}', Math.floor(this.remaining / 86400));
|
|
|
|
},
|
2018-02-22 13:03:44 +00:00
|
|
|
isVoted(): boolean {
|
2019-03-06 13:55:47 +00:00
|
|
|
return !this.poll.multiple && this.poll.choices.some(c => c.isVoted);
|
2018-02-22 13:03:44 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
created() {
|
|
|
|
this.showResult = this.isVoted;
|
2019-03-06 13:55:47 +00: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();
|
|
|
|
}
|
2018-02-22 13:03:44 +00:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
toggleShowResult() {
|
|
|
|
this.showResult = !this.showResult;
|
2018-02-08 06:07:55 +00:00
|
|
|
},
|
2018-02-22 13:03:44 +00:00
|
|
|
vote(id) {
|
2019-03-06 13:55:47 +00:00
|
|
|
if (this.closed || !this.poll.multiple && this.poll.choices.some(c => c.isVoted)) return;
|
2018-11-08 23:13:34 +00:00
|
|
|
this.$root.api('notes/polls/vote', {
|
2018-04-07 17:30:37 +00:00
|
|
|
noteId: this.note.id,
|
2018-02-22 13:03:44 +00:00
|
|
|
choice: id
|
|
|
|
}).then(() => {
|
2019-03-07 00:35:23 +00:00
|
|
|
if (!this.showResult) this.showResult = !this.poll.multiple;
|
2018-02-22 13:03:44 +00:00
|
|
|
});
|
2018-02-08 06:07:55 +00:00
|
|
|
}
|
2018-02-22 13:03:44 +00:00
|
|
|
}
|
|
|
|
});
|
2018-02-08 05:02:08 +00:00
|
|
|
</script>
|
|
|
|
|
2018-02-08 05:54:16 +00:00
|
|
|
<style lang="stylus" scoped>
|
2018-09-28 10:59:19 +00:00
|
|
|
.mk-poll
|
2018-02-22 13:03:44 +00:00
|
|
|
> ul
|
2018-02-08 05:54:16 +00:00
|
|
|
display block
|
2018-02-22 13:03:44 +00:00
|
|
|
margin 0
|
|
|
|
padding 0
|
|
|
|
list-style none
|
2018-02-08 05:02:08 +00:00
|
|
|
|
2018-02-22 13:03:44 +00:00
|
|
|
> li
|
2017-02-14 04:59:26 +00:00
|
|
|
display block
|
2018-02-22 13:03:44 +00:00
|
|
|
margin 4px 0
|
|
|
|
padding 4px 8px
|
|
|
|
width 100%
|
2018-09-28 10:59:19 +00:00
|
|
|
color var(--pollChoiceText)
|
|
|
|
border solid 1px var(--pollChoiceBorder)
|
2018-02-22 13:03:44 +00:00
|
|
|
border-radius 4px
|
|
|
|
overflow hidden
|
|
|
|
cursor pointer
|
2018-02-08 05:54:16 +00:00
|
|
|
|
2018-02-22 13:03:44 +00:00
|
|
|
&:hover
|
2018-04-28 23:51:17 +00:00
|
|
|
background rgba(#000, 0.05)
|
2018-02-08 05:54:16 +00:00
|
|
|
|
2018-02-22 13:03:44 +00:00
|
|
|
&:active
|
2018-04-28 23:51:17 +00:00
|
|
|
background rgba(#000, 0.1)
|
2018-02-08 05:54:16 +00:00
|
|
|
|
2018-02-22 13:03:44 +00:00
|
|
|
> .backdrop
|
|
|
|
position absolute
|
|
|
|
top 0
|
|
|
|
left 0
|
|
|
|
height 100%
|
2018-09-26 11:19:35 +00:00
|
|
|
background var(--primary)
|
2018-02-22 13:03:44 +00:00
|
|
|
transition width 1s ease
|
2018-02-08 05:54:16 +00:00
|
|
|
|
2018-03-01 21:26:31 +00:00
|
|
|
> span
|
2018-11-05 16:40:11 +00:00
|
|
|
> [data-icon]
|
2018-03-01 21:26:31 +00:00
|
|
|
margin-right 4px
|
|
|
|
|
|
|
|
> .votes
|
|
|
|
margin-left 4px
|
2018-02-08 05:54:16 +00:00
|
|
|
|
2018-02-22 13:03:44 +00:00
|
|
|
> p
|
2018-09-28 10:59:19 +00:00
|
|
|
color var(--text)
|
2018-04-20 04:38:28 +00:00
|
|
|
|
2018-02-22 13:03:44 +00:00
|
|
|
a
|
|
|
|
color inherit
|
2018-02-08 05:54:16 +00:00
|
|
|
|
2019-03-06 13:55:47 +00:00
|
|
|
&[data-done]
|
2018-02-22 13:03:44 +00:00
|
|
|
> ul > li
|
|
|
|
cursor default
|
2018-02-08 05:54:16 +00:00
|
|
|
|
2018-02-22 13:03:44 +00:00
|
|
|
&:hover
|
|
|
|
background transparent
|
2018-02-08 05:54:16 +00:00
|
|
|
|
2018-02-22 13:03:44 +00:00
|
|
|
&:active
|
|
|
|
background transparent
|
2018-02-08 05:54:16 +00:00
|
|
|
|
|
|
|
</style>
|