iceshrimp-legacy/src/web/app/common/views/components/poll.vue

125 lines
2.5 KiB
Vue
Raw Normal View History

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