egirlskey/src/client/components/reactions-viewer.reaction.vue

178 lines
3.3 KiB
Vue
Raw Normal View History

<template>
2020-02-21 21:43:46 +00:00
<button
class="hkzvhatu _button"
2020-04-15 15:47:17 +00:00
:class="{ reacted: note.myReaction == reaction, canToggle }"
@click="toggleReaction(reaction)"
v-if="count > 0"
@touchstart="onMouseover"
@mouseover="onMouseover"
@mouseleave="onMouseleave"
@touchend="onMouseleave"
ref="reaction"
2020-02-21 21:43:46 +00:00
v-particle
>
<x-reaction-icon :reaction="reaction" :customEmojis="note.emojis" ref="icon"/>
<span>{{ count }}</span>
2020-02-21 21:43:46 +00:00
</button>
</template>
<script lang="ts">
import Vue from 'vue';
import XDetails from './reactions-viewer.details.vue';
import XReactionIcon from './reaction-icon.vue';
export default Vue.extend({
components: {
XReactionIcon
},
props: {
reaction: {
type: String,
required: true,
},
count: {
type: Number,
required: true,
},
isInitial: {
type: Boolean,
required: true,
},
note: {
type: Object,
required: true,
},
},
data() {
return {
details: null,
2019-09-01 22:01:33 +00:00
detailsTimeoutId: null,
isHovering: false
};
},
computed: {
isMe(): boolean {
return this.$store.getters.isSignedIn && this.$store.state.i.id === this.note.userId;
},
2020-04-15 15:47:17 +00:00
canToggle(): boolean {
return !this.reaction.match(/@\w/);
},
},
mounted() {
if (!this.isInitial) this.anime();
},
watch: {
count(newCount, oldCount) {
if (oldCount < newCount) this.anime();
if (this.details != null) this.openDetails();
},
},
methods: {
toggleReaction() {
if (this.isMe) return;
if (!this.canToggle) return;
const oldReaction = this.note.myReaction;
if (oldReaction) {
this.$root.api('notes/reactions/delete', {
noteId: this.note.id
}).then(() => {
if (oldReaction !== this.reaction) {
this.$root.api('notes/reactions/create', {
noteId: this.note.id,
reaction: this.reaction
});
}
});
} else {
this.$root.api('notes/reactions/create', {
noteId: this.note.id,
reaction: this.reaction
});
}
},
onMouseover() {
if (this.isHovering) return;
2019-09-01 22:01:33 +00:00
this.isHovering = true;
this.detailsTimeoutId = setTimeout(this.openDetails, 300);
},
onMouseleave() {
if (!this.isHovering) return;
2019-09-01 22:01:33 +00:00
this.isHovering = false;
clearTimeout(this.detailsTimeoutId);
this.closeDetails();
},
openDetails() {
this.$root.api('notes/reactions', {
2019-09-02 22:15:53 +00:00
noteId: this.note.id,
2019-09-02 22:25:02 +00:00
type: this.reaction,
limit: 11
}).then((reactions: any[]) => {
2019-09-02 22:25:02 +00:00
const users = reactions
.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime())
2019-09-02 20:50:01 +00:00
.map(x => x.user);
this.closeDetails();
2019-09-01 22:01:33 +00:00
if (!this.isHovering) return;
this.details = this.$root.new(XDetails, {
reaction: this.reaction,
users,
2019-09-02 22:25:02 +00:00
count: this.count,
source: this.$refs.reaction
});
});
},
closeDetails() {
if (this.details != null) {
this.details.close();
this.details = null;
}
},
anime() {
if (document.hidden) return;
// TODO
},
}
});
</script>
<style lang="scss" scoped>
2020-02-21 21:43:46 +00:00
.hkzvhatu {
display: inline-block;
height: 32px;
margin: 2px;
padding: 0 6px;
border-radius: 4px;
2019-09-02 21:20:04 +00:00
2020-04-15 15:47:17 +00:00
&.canToggle {
background: rgba(0, 0, 0, 0.05);
2020-04-15 15:47:17 +00:00
&:hover {
background: rgba(0, 0, 0, 0.1);
}
}
2020-04-15 15:47:17 +00:00
&:not(.canToggle) {
cursor: default;
}
2020-04-15 15:47:17 +00:00
&.reacted {
background: var(--accent);
2020-04-17 11:30:12 +00:00
&:hover {
background: var(--accent);
}
2020-04-15 15:47:17 +00:00
> span {
color: #fff;
}
}
> span {
font-size: 0.9em;
line-height: 32px;
}
}
</style>