jormungandr-bite/src/client/app/common/views/components/poll.vue

134 lines
2.6 KiB
Vue
Raw Normal View History

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