2019-02-06 09:05:49 -08:00
|
|
|
<template>
|
|
|
|
<span
|
|
|
|
class="reaction"
|
|
|
|
:class="{ reacted: note.myReaction == reaction }"
|
|
|
|
@click="toggleReaction(reaction)"
|
|
|
|
v-if="count > 0"
|
|
|
|
v-particle="!isMe"
|
2019-08-30 11:04:36 -07:00
|
|
|
@mouseover="onMouseover"
|
|
|
|
@mouseleave="onMouseleave"
|
|
|
|
ref="reaction"
|
2019-02-06 09:05:49 -08:00
|
|
|
>
|
|
|
|
<mk-reaction-icon :reaction="reaction" ref="icon"/>
|
|
|
|
<span>{{ count }}</span>
|
|
|
|
</span>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
|
|
|
import Vue from 'vue';
|
|
|
|
import Icon from './reaction-icon.vue';
|
|
|
|
import anime from 'animejs';
|
2019-08-30 11:04:36 -07:00
|
|
|
import XDetails from './reactions-viewer.details.vue';
|
2019-02-06 09:05:49 -08:00
|
|
|
|
|
|
|
export default Vue.extend({
|
|
|
|
props: {
|
|
|
|
reaction: {
|
|
|
|
type: String,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
count: {
|
|
|
|
type: Number,
|
|
|
|
required: true,
|
|
|
|
},
|
2019-08-28 13:11:26 -07:00
|
|
|
isInitial: {
|
|
|
|
type: Boolean,
|
|
|
|
required: true,
|
|
|
|
},
|
2019-02-06 09:05:49 -08:00
|
|
|
note: {
|
|
|
|
type: Object,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
canToggle: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false,
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
},
|
2019-08-30 11:04:36 -07:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
details: null,
|
2019-09-01 15:01:33 -07:00
|
|
|
detailsTimeoutId: null,
|
|
|
|
isHovering: false
|
2019-08-30 11:04:36 -07:00
|
|
|
};
|
|
|
|
},
|
2019-02-06 09:05:49 -08:00
|
|
|
computed: {
|
|
|
|
isMe(): boolean {
|
|
|
|
return this.$store.getters.isSignedIn && this.$store.state.i.id === this.note.userId;
|
|
|
|
},
|
|
|
|
},
|
2019-08-28 13:11:26 -07:00
|
|
|
mounted() {
|
|
|
|
if (!this.isInitial) this.anime();
|
|
|
|
},
|
2019-02-06 09:05:49 -08:00
|
|
|
watch: {
|
2019-08-28 13:20:27 -07:00
|
|
|
count(newCount, oldCount) {
|
|
|
|
if (oldCount < newCount) this.anime();
|
2019-08-30 11:04:36 -07:00
|
|
|
if (this.details != null) this.openDetails();
|
2019-02-06 09:05:49 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2019-08-30 11:04:36 -07:00
|
|
|
onMouseover() {
|
2019-09-01 15:01:33 -07:00
|
|
|
this.isHovering = true;
|
2019-08-30 11:04:36 -07:00
|
|
|
this.detailsTimeoutId = setTimeout(this.openDetails, 300);
|
|
|
|
},
|
|
|
|
onMouseleave() {
|
2019-09-01 15:01:33 -07:00
|
|
|
this.isHovering = false;
|
2019-08-30 11:04:36 -07:00
|
|
|
clearTimeout(this.detailsTimeoutId);
|
|
|
|
this.closeDetails();
|
|
|
|
},
|
|
|
|
openDetails() {
|
2019-09-02 14:20:52 -07:00
|
|
|
if (this.$root.isMobile) return;
|
2019-08-30 11:04:36 -07:00
|
|
|
this.$root.api('notes/reactions', {
|
2019-09-02 15:15:53 -07:00
|
|
|
noteId: this.note.id,
|
2019-09-02 15:25:02 -07:00
|
|
|
type: this.reaction,
|
|
|
|
limit: 11
|
2019-08-30 11:04:36 -07:00
|
|
|
}).then((reactions: any[]) => {
|
2019-09-02 15:25:02 -07:00
|
|
|
const users = reactions
|
2019-08-30 11:04:36 -07:00
|
|
|
.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime())
|
2019-09-02 13:50:01 -07:00
|
|
|
.map(x => x.user);
|
2019-08-30 11:04:36 -07:00
|
|
|
|
|
|
|
this.closeDetails();
|
2019-09-01 15:01:33 -07:00
|
|
|
if (!this.isHovering) return;
|
2019-08-30 11:04:36 -07:00
|
|
|
this.details = this.$root.new(XDetails, {
|
|
|
|
reaction: this.reaction,
|
|
|
|
users,
|
2019-09-02 15:25:02 -07:00
|
|
|
count: this.count,
|
2019-08-30 11:04:36 -07:00
|
|
|
source: this.$refs.reaction
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
closeDetails() {
|
|
|
|
if (this.details != null) {
|
|
|
|
this.details.close();
|
|
|
|
this.details = null;
|
|
|
|
}
|
|
|
|
},
|
2019-02-06 09:05:49 -08:00
|
|
|
anime() {
|
|
|
|
if (this.$store.state.device.reduceMotion) return;
|
|
|
|
if (document.hidden) return;
|
|
|
|
|
|
|
|
this.$nextTick(() => {
|
2019-08-28 13:13:03 -07:00
|
|
|
if (this.$refs.icon == null) return;
|
|
|
|
|
2019-02-06 09:05:49 -08:00
|
|
|
const rect = this.$refs.icon.$el.getBoundingClientRect();
|
|
|
|
|
|
|
|
const x = rect.left;
|
|
|
|
const y = rect.top;
|
|
|
|
|
|
|
|
const icon = new Icon({
|
|
|
|
parent: this,
|
|
|
|
propsData: {
|
|
|
|
reaction: this.reaction
|
|
|
|
}
|
|
|
|
}).$mount();
|
|
|
|
|
|
|
|
icon.$el.style.position = 'absolute';
|
|
|
|
icon.$el.style.zIndex = 100;
|
|
|
|
icon.$el.style.top = (y + window.scrollY) + 'px';
|
|
|
|
icon.$el.style.left = (x + window.scrollX) + 'px';
|
|
|
|
icon.$el.style.fontSize = window.getComputedStyle(this.$refs.icon.$el).fontSize;
|
|
|
|
|
|
|
|
document.body.appendChild(icon.$el);
|
|
|
|
|
|
|
|
anime({
|
|
|
|
targets: icon.$el,
|
|
|
|
opacity: [1, 0],
|
|
|
|
translateY: [0, -64],
|
|
|
|
duration: 1000,
|
|
|
|
easing: 'linear',
|
|
|
|
complete: () => {
|
|
|
|
icon.destroyDom();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="stylus" scoped>
|
|
|
|
.reaction
|
|
|
|
display inline-block
|
|
|
|
height 32px
|
|
|
|
margin 2px
|
|
|
|
padding 0 6px
|
|
|
|
border-radius 4px
|
|
|
|
cursor pointer
|
|
|
|
|
2019-09-02 14:20:04 -07:00
|
|
|
&, *
|
|
|
|
-webkit-touch-callout none
|
|
|
|
-webkit-user-select none
|
|
|
|
-khtml-user-select none
|
|
|
|
-moz-user-select none
|
|
|
|
-ms-user-select none
|
|
|
|
user-select none
|
|
|
|
|
2019-02-06 09:05:49 -08:00
|
|
|
*
|
|
|
|
user-select none
|
|
|
|
pointer-events none
|
|
|
|
|
|
|
|
&.reacted
|
|
|
|
background var(--primary)
|
|
|
|
|
|
|
|
> span
|
|
|
|
color var(--primaryForeground)
|
|
|
|
|
|
|
|
&:not(.reacted)
|
|
|
|
background var(--reactionViewerButtonBg)
|
|
|
|
|
|
|
|
&:hover
|
|
|
|
background var(--reactionViewerButtonHoverBg)
|
|
|
|
|
|
|
|
> span
|
|
|
|
font-size 1.1em
|
|
|
|
line-height 32px
|
|
|
|
color var(--text)
|
|
|
|
</style>
|