paricafe/src/web/app/desktop/views/components/timeline.vue

128 lines
2.9 KiB
Vue
Raw Normal View History

2018-02-11 01:52:37 -06:00
<template>
2018-02-12 17:31:03 -06:00
<div class="mk-timeline">
2018-02-12 18:12:54 -06:00
<mk-following-setuper v-if="alone"/>
<div class="loading" v-if="fetching">
<mk-ellipsis-icon/>
</div>
<p class="empty" v-if="posts.length == 0 && !fetching">%fa:R comments%自分の投稿や自分がフォローしているユーザーの投稿が表示されます</p>
<mk-posts :posts="posts" ref="timeline"/>
2018-02-11 08:26:35 -06:00
</div>
2018-02-11 01:52:37 -06:00
</template>
2018-02-11 02:04:03 -06:00
<script lang="ts">
import Vue from 'vue';
2018-02-11 01:52:37 -06:00
2018-02-11 02:04:03 -06:00
export default Vue.extend({
2018-02-12 17:24:44 -06:00
props: {
2018-02-12 18:12:54 -06:00
date: {
type: Date,
required: false
2018-02-12 17:24:44 -06:00
}
},
2018-02-12 18:12:54 -06:00
data() {
return {
fetching: true,
moreFetching: false,
posts: [],
connection: null,
connectionId: null
};
},
2018-02-11 02:04:03 -06:00
computed: {
2018-02-12 18:12:54 -06:00
alone(): boolean {
return this.$root.$data.os.i.following_count == 0;
2018-02-11 02:04:03 -06:00
}
},
2018-02-12 18:12:54 -06:00
mounted() {
this.connection = this.$root.$data.os.stream.getConnection();
this.connectionId = this.$root.$data.os.stream.use();
this.connection.on('post', this.onPost);
this.connection.on('follow', this.onChangeFollowing);
this.connection.on('unfollow', this.onChangeFollowing);
document.addEventListener('keydown', this.onKeydown);
window.addEventListener('scroll', this.onScroll);
this.fetch();
},
beforeDestroy() {
this.connection.off('post', this.onPost);
this.connection.off('follow', this.onChangeFollowing);
this.connection.off('unfollow', this.onChangeFollowing);
this.$root.$data.os.stream.dispose(this.connectionId);
document.removeEventListener('keydown', this.onKeydown);
window.removeEventListener('scroll', this.onScroll);
},
2018-02-11 02:04:03 -06:00
methods: {
2018-02-12 18:12:54 -06:00
fetch(cb?) {
this.fetching = true;
this.$root.$data.os.api('posts/timeline', {
until_date: this.date ? (this.date as any).getTime() : undefined
}).then(posts => {
this.fetching = false;
this.posts = posts;
if (cb) cb();
});
},
more() {
if (this.moreFetching || this.fetching || this.posts.length == 0) return;
this.moreFetching = true;
this.$root.$data.os.api('posts/timeline', {
until_id: this.posts[this.posts.length - 1].id
}).then(posts => {
this.moreFetching = false;
this.posts.unshift(posts);
});
},
onPost(post) {
this.posts.unshift(post);
},
onChangeFollowing() {
this.fetch();
},
onScroll() {
const current = window.scrollY + window.innerHeight;
if (current > document.body.offsetHeight - 8) this.more();
},
onKeydown(e) {
if (e.target.tagName != 'INPUT' && e.target.tagName != 'TEXTAREA') {
if (e.which == 84) { // t
(this.$refs.timeline as any).focus();
}
}
2018-02-11 02:04:03 -06:00
}
}
2018-02-11 01:52:37 -06:00
});
</script>
<style lang="stylus" scoped>
.mk-timeline
2018-02-12 18:12:54 -06:00
background #fff
border solid 1px rgba(0, 0, 0, 0.075)
border-radius 6px
2018-02-11 01:52:37 -06:00
2018-02-12 18:12:54 -06:00
> mk-following-setuper
border-bottom solid 1px #eee
2018-02-11 01:52:37 -06:00
2018-02-12 18:12:54 -06:00
> .loading
padding 64px 0
2018-02-11 01:52:37 -06:00
2018-02-12 18:12:54 -06:00
> .empty
display block
margin 0 auto
padding 32px
max-width 400px
2018-02-11 01:52:37 -06:00
text-align center
2018-02-12 18:12:54 -06:00
color #999
> [data-fa]
display block
margin-bottom 16px
font-size 3em
color #ccc
2018-02-11 01:52:37 -06:00
</style>