sharkey/src/client/app/common/views/components/poll.vue

128 lines
2.6 KiB
Vue
Raw Normal View History

2018-02-08 14:02:08 +09:00
<template>
2018-02-22 22:03:44 +09:00
<div class="mk-poll" :data-is-voted="isVoted">
2017-02-14 13:59:26 +09:00
<ul>
<li v-for="choice in poll.choices" :key="choice.id" @click="vote(choice.id)" :class="{ voted: choice.voted }" :title="!isVoted ? $t('vote-to').replace('{}', choice.text) : ''">
2018-02-22 22:03:44 +09:00
<div class="backdrop" :style="{ 'width': (showResult ? (choice.votes / total * 100) : 0) + '%' }"></div>
2017-02-14 13:59:26 +09:00
<span>
<template v-if="choice.isVoted"><fa icon="check"/></template>
<mfm :text="choice.text" :should-break="false" :plain-text="true" :custom-emojis="note.emojis"/>
<span class="votes" v-if="showResult">({{ $t('vote-count').replace('{}', choice.votes) }})</span>
2017-02-14 13:59:26 +09:00
</span>
</li>
</ul>
2018-02-07 18:34:43 +09:00
<p v-if="total > 0">
<span>{{ $t('total-users').replace('{}', total) }}</span>
2018-03-02 06:26:31 +09:00
<span></span>
<a v-if="!isVoted" @click="toggleShowResult">{{ showResult ? $t('vote') : $t('show-result') }}</a>
<span v-if="isVoted">{{ $t('voted') }}</span>
2017-02-14 17:17:39 +09:00
</p>
2018-02-08 14:02:08 +09:00
</div>
</template>
2018-02-22 22:03:44 +09:00
<script lang="ts">
import Vue from 'vue';
import i18n from '../../../i18n';
2018-09-07 04:21:04 +09:00
import { sum } from '../../../../../prelude/array';
2018-02-22 22:03:44 +09:00
export default Vue.extend({
i18n: i18n('common/views/components/poll.vue'),
2018-04-08 02:30:37 +09:00
props: ['note'],
2018-02-22 22:03:44 +09:00
data() {
return {
showResult: false
};
},
computed: {
poll(): any {
2018-04-08 02:30:37 +09:00
return this.note.poll;
2018-02-08 15:07:55 +09:00
},
2018-02-22 22:03:44 +09:00
total(): number {
2018-09-07 04:21:04 +09:00
return sum(this.poll.choices.map(x => x.votes));
2018-02-08 15:07:55 +09:00
},
2018-02-22 22:03:44 +09:00
isVoted(): boolean {
2018-03-29 14:48:47 +09:00
return this.poll.choices.some(c => c.isVoted);
2018-02-22 22:03:44 +09:00
}
},
created() {
this.showResult = this.isVoted;
},
methods: {
toggleShowResult() {
this.showResult = !this.showResult;
2018-02-08 15:07:55 +09:00
},
2018-02-22 22:03:44 +09:00
vote(id) {
2018-03-29 14:48:47 +09:00
if (this.poll.choices.some(c => c.isVoted)) return;
2018-11-09 08:13:34 +09:00
this.$root.api('notes/polls/vote', {
2018-04-08 02:30:37 +09:00
noteId: this.note.id,
2018-02-22 22:03:44 +09:00
choice: id
}).then(() => {
for (const c of this.poll.choices) {
2018-02-22 22:03:44 +09:00
if (c.id == id) {
c.votes++;
2018-03-29 14:48:47 +09:00
Vue.set(c, 'isVoted', true);
2018-02-22 22:03:44 +09:00
}
}
2018-02-22 22:03:44 +09:00
this.showResult = true;
});
2018-02-08 15:07:55 +09:00
}
2018-02-22 22:03:44 +09:00
}
});
2018-02-08 14:02:08 +09:00
</script>
2018-02-08 14:54:16 +09:00
<style lang="stylus" scoped>
2018-09-28 19:59:19 +09:00
.mk-poll
2018-02-22 22:03:44 +09:00
> ul
2018-02-08 14:54:16 +09:00
display block
2018-02-22 22:03:44 +09:00
margin 0
padding 0
list-style none
2018-02-08 14:02:08 +09:00
2018-02-22 22:03:44 +09:00
> li
2017-02-14 13:59:26 +09:00
display block
2018-02-22 22:03:44 +09:00
margin 4px 0
padding 4px 8px
width 100%
2018-09-28 19:59:19 +09:00
color var(--pollChoiceText)
border solid 1px var(--pollChoiceBorder)
2018-02-22 22:03:44 +09:00
border-radius 4px
overflow hidden
cursor pointer
2018-02-08 14:54:16 +09:00
2018-02-22 22:03:44 +09:00
&:hover
2018-04-29 08:51:17 +09:00
background rgba(#000, 0.05)
2018-02-08 14:54:16 +09:00
2018-02-22 22:03:44 +09:00
&:active
2018-04-29 08:51:17 +09:00
background rgba(#000, 0.1)
2018-02-08 14:54:16 +09:00
2018-02-22 22:03:44 +09:00
> .backdrop
position absolute
top 0
left 0
height 100%
2018-09-26 20:19:35 +09:00
background var(--primary)
2018-02-22 22:03:44 +09:00
transition width 1s ease
2018-02-08 14:54:16 +09:00
2018-03-02 06:26:31 +09:00
> span
> [data-icon]
2018-03-02 06:26:31 +09:00
margin-right 4px
> .votes
margin-left 4px
2018-02-08 14:54:16 +09:00
2018-02-22 22:03:44 +09:00
> p
2018-09-28 19:59:19 +09:00
color var(--text)
2018-04-20 13:38:28 +09:00
2018-02-22 22:03:44 +09:00
a
color inherit
2018-02-08 14:54:16 +09:00
2018-02-22 22:03:44 +09:00
&[data-is-voted]
> ul > li
cursor default
2018-02-08 14:54:16 +09:00
2018-02-22 22:03:44 +09:00
&:hover
background transparent
2018-02-08 14:54:16 +09:00
2018-02-22 22:03:44 +09:00
&:active
background transparent
2018-02-08 14:54:16 +09:00
</style>