summaryrefslogtreecommitdiff
path: root/src/server/web/app/desktop/views/widgets/channel.channel.vue
blob: e9fb9e3fd74828fa3aa4a2cfbf4b1becd5c59d42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<template>
<div class="channel">
	<p v-if="fetching">読み込み中<mk-ellipsis/></p>
	<div v-if="!fetching" ref="posts" class="posts">
		<p v-if="posts.length == 0">まだ投稿がありません</p>
		<x-post class="post" v-for="post in posts.slice().reverse()" :post="post" :key="post.id" @reply="reply"/>
	</div>
	<x-form class="form" ref="form"/>
</div>
</template>

<script lang="ts">
import Vue from 'vue';
import ChannelStream from '../../../common/scripts/streaming/channel';
import XForm from './channel.channel.form.vue';
import XPost from './channel.channel.post.vue';

export default Vue.extend({
	components: {
		XForm,
		XPost
	},
	props: ['channel'],
	data() {
		return {
			fetching: true,
			posts: [],
			connection: null
		};
	},
	watch: {
		channel() {
			this.zap();
		}
	},
	mounted() {
		this.zap();
	},
	beforeDestroy() {
		this.disconnect();
	},
	methods: {
		zap() {
			this.fetching = true;

			(this as any).api('channels/posts', {
				channelId: this.channel.id
			}).then(posts => {
				this.posts = posts;
				this.fetching = false;

				this.$nextTick(() => {
					this.scrollToBottom();
				});

				this.disconnect();
				this.connection = new ChannelStream((this as any).os, this.channel.id);
				this.connection.on('post', this.onPost);
			});
		},
		disconnect() {
			if (this.connection) {
				this.connection.off('post', this.onPost);
				this.connection.close();
			}
		},
		onPost(post) {
			this.posts.unshift(post);
			this.scrollToBottom();
		},
		scrollToBottom() {
			(this.$refs.posts as any).scrollTop = (this.$refs.posts as any).scrollHeight;
		},
		reply(post) {
			(this.$refs.form as any).text = `>>${ post.index } `;
		}
	}
});
</script>

<style lang="stylus" scoped>
.channel

	> p
		margin 0
		padding 16px
		text-align center
		color #aaa

	> .posts
		height calc(100% - 38px)
		overflow auto
		font-size 0.9em

		> .post
			border-bottom solid 1px #eee

			&:last-child
				border-bottom none

	> .form
		position absolute
		left 0
		bottom 0

</style>