yumechi-no-kuni/src/web/app/common/views/components/poll.vue

119 lines
2.5 KiB
Vue
Raw Normal View History

2018-02-07 23:02:08 -06:00
<template>
<div :data-is-voted="isVoted">
2017-02-13 22:59:26 -06:00
<ul>
2018-02-07 23:54:16 -06:00
<li v-for="choice in poll.choices" :key="choice.id" @click="vote.bind(choice.id)" :class="{ voted: choice.voted }" :title="!isVoted ? '%i18n:common.tags.mk-poll.vote-to%'.replace('{}', choice.text) : ''">
2018-02-08 00:07:55 -06:00
<div class="backdrop" :style="{ 'width:' + (showResult ? (choice.votes / total * 100) : 0) + '%' }"></div>
2017-02-13 22:59:26 -06:00
<span>
2018-02-07 23:54:16 -06:00
<template v-if="choice.is_voted">%fa:check%</template>
2018-02-07 23:50:18 -06:00
{{ text }}
2018-02-08 00:07:55 -06:00
<span class="votes" v-if="showResult">({{ '%i18n:common.tags.mk-poll.vote-count%'.replace('{}', choice.votes) }})</span>
2017-02-13 22:59:26 -06:00
</span>
</li>
</ul>
2018-02-07 03:34:43 -06:00
<p v-if="total > 0">
2018-02-07 23:54:16 -06:00
<span>{{ '%i18n:common.tags.mk-poll.total-users%'.replace('{}', total) }}</span>
2017-02-14 02:17:39 -06:00
2018-02-08 00:07:55 -06:00
<a v-if="!isVoted" @click="toggleShowResult">{{ showResult ? '%i18n:common.tags.mk-poll.vote%' : '%i18n:common.tags.mk-poll.show-result%' }}</a>
2018-02-07 03:34:43 -06:00
<span v-if="isVoted">%i18n:common.tags.mk-poll.voted%</span>
2017-02-14 02:17:39 -06:00
</p>
2018-02-07 23:02:08 -06:00
</div>
</template>
<script lang="typescript">
2018-02-08 00:07:55 -06:00
export default {
props: ['post'],
data() {
return {
showResult: false
};
},
computed: {
poll() {
return this.post.poll;
},
total() {
return this.poll.choices.reduce((a, b) => a + b.votes, 0);
},
isVoted() {
return this.poll.choices.some(c => c.is_voted);
}
},
created() {
this.showResult = this.isVoted;
},
methods: {
toggleShowResult() {
this.showResult = !this.showResult;
},
vote(id) {
if (this.poll.choices.some(c => c.is_voted)) return;
2018-02-17 21:35:18 -06:00
(this as any).api('posts/polls/vote', {
2018-02-08 00:07:55 -06:00
post_id: this.post.id,
choice: id
}).then(() => {
this.poll.choices.forEach(c => {
if (c.id == id) {
c.votes++;
c.is_voted = true;
}
});
this.showResult = true;
});
}
}
2018-02-07 23:02:08 -06:00
};
</script>
2018-02-07 23:54:16 -06:00
<style lang="stylus" scoped>
:scope
display block
2018-02-07 23:02:08 -06:00
2018-02-07 23:54:16 -06:00
> ul
2017-02-13 22:59:26 -06:00
display block
2018-02-07 23:54:16 -06:00
margin 0
padding 0
list-style none
2017-02-13 22:59:26 -06:00
2018-02-07 23:54:16 -06:00
> li
2017-02-13 22:59:26 -06:00
display block
2018-02-07 23:54:16 -06:00
margin 4px 0
padding 4px 8px
width 100%
border solid 1px #eee
border-radius 4px
overflow hidden
cursor pointer
&:hover
background rgba(0, 0, 0, 0.05)
&:active
background rgba(0, 0, 0, 0.1)
> .backdrop
position absolute
top 0
left 0
height 100%
background $theme-color
transition width 1s ease
> .votes
margin-left 4px
> p
a
color inherit
&[data-is-voted]
> ul > li
cursor default
&:hover
background transparent
&:active
background transparent
</style>