Sharkey/src/client/app/desktop/views/widgets/channel.channel.vue

107 lines
2 KiB
Vue
Raw Normal View History

2018-02-21 07:30:03 +01:00
<template>
<div class="channel">
<p v-if="fetching">読み込み中<mk-ellipsis/></p>
2018-04-07 19:30:37 +02:00
<div v-if="!fetching" ref="notes" class="notes">
<p v-if="notes.length == 0">まだ投稿がありません</p>
<x-note class="note" v-for="note in notes.slice().reverse()" :note="note" :key="note.id" @reply="reply"/>
2018-02-21 07:30:03 +01:00
</div>
<x-form class="form" ref="form"/>
</div>
</template>
<script lang="ts">
import Vue from 'vue';
2018-03-07 09:48:32 +01:00
import ChannelStream from '../../../common/scripts/streaming/channel';
2018-02-21 07:30:03 +01:00
import XForm from './channel.channel.form.vue';
2018-04-07 19:30:37 +02:00
import XNote from './channel.channel.note.vue';
2018-02-21 07:30:03 +01:00
export default Vue.extend({
components: {
XForm,
2018-04-07 19:30:37 +02:00
XNote
2018-02-21 07:30:03 +01:00
},
props: ['channel'],
data() {
return {
fetching: true,
2018-04-07 19:30:37 +02:00
notes: [],
2018-02-21 07:30:03 +01:00
connection: null
};
},
watch: {
channel() {
this.zap();
}
},
mounted() {
2018-02-22 21:06:47 +01:00
this.zap();
2018-02-21 07:30:03 +01:00
},
beforeDestroy() {
this.disconnect();
},
methods: {
zap() {
this.fetching = true;
2018-04-07 19:30:37 +02:00
(this as any).api('channels/notes', {
2018-03-29 07:48:47 +02:00
channelId: this.channel.id
2018-04-07 19:30:37 +02:00
}).then(notes => {
this.notes = notes;
2018-02-21 07:30:03 +01:00
this.fetching = false;
2018-02-22 21:06:47 +01:00
this.$nextTick(() => {
this.scrollToBottom();
});
2018-02-21 07:30:03 +01:00
this.disconnect();
2018-03-15 11:53:46 +01:00
this.connection = new ChannelStream((this as any).os, this.channel.id);
2018-04-07 19:30:37 +02:00
this.connection.on('note', this.onNote);
2018-02-21 07:30:03 +01:00
});
},
disconnect() {
if (this.connection) {
2018-04-07 19:30:37 +02:00
this.connection.off('note', this.onNote);
2018-02-21 07:30:03 +01:00
this.connection.close();
}
},
2018-04-07 19:30:37 +02:00
onNote(note) {
this.notes.unshift(note);
2018-02-21 07:30:03 +01:00
this.scrollToBottom();
},
scrollToBottom() {
2018-04-07 19:30:37 +02:00
(this.$refs.notes as any).scrollTop = (this.$refs.notes as any).scrollHeight;
2018-02-21 07:30:03 +01:00
},
2018-04-07 19:30:37 +02:00
reply(note) {
(this.$refs.form as any).text = `>>${ note.index } `;
2018-02-21 07:30:03 +01:00
}
}
});
</script>
<style lang="stylus" scoped>
.channel
> p
margin 0
padding 16px
text-align center
color #aaa
2018-04-07 19:30:37 +02:00
> .notes
2018-02-21 07:30:03 +01:00
height calc(100% - 38px)
overflow auto
font-size 0.9em
2018-04-07 19:30:37 +02:00
> .note
2018-02-21 07:30:03 +01:00
border-bottom solid 1px #eee
&:last-child
border-bottom none
> .form
position absolute
left 0
bottom 0
</style>