Sharkey/src/client/app/desktop/views/components/timeline.vue

170 lines
3.8 KiB
Vue
Raw Normal View History

2018-02-11 09:52:37 +02:00
<template>
2018-02-13 01:31:03 +02:00
<div class="mk-timeline">
2018-02-19 12:46:31 +02:00
<mk-friends-maker v-if="alone"/>
2018-02-20 02:48:30 +02:00
<div class="fetching" v-if="fetching">
2018-02-13 02:12:54 +02:00
<mk-ellipsis-icon/>
</div>
2018-04-07 20:30:37 +03:00
<p class="empty" v-if="notes.length == 0 && !fetching">
2018-04-15 14:50:53 +03:00
%fa:R comments%%i18n:@empty%
2018-02-22 13:52:39 +02:00
</p>
2018-04-07 20:30:37 +03:00
<mk-notes :notes="notes" ref="timeline">
2018-03-05 13:09:26 +02:00
<button slot="footer" @click="more" :disabled="moreFetching" :style="{ cursor: moreFetching ? 'wait' : 'pointer' }">
2018-04-15 14:50:53 +03:00
<template v-if="!moreFetching">%i18n:@load-more%</template>
2018-02-22 13:52:39 +02:00
<template v-if="moreFetching">%fa:spinner .pulse .fw%</template>
2018-03-05 13:09:26 +02:00
</button>
2018-04-07 20:30:37 +03:00
</mk-notes>
2018-02-11 16:26:35 +02:00
</div>
2018-02-11 09:52:37 +02:00
</template>
2018-02-11 10:04:03 +02:00
<script lang="ts">
import Vue from 'vue';
2018-03-04 11:50:30 +02:00
import { url } from '../../../config';
2018-02-11 09:52:37 +02:00
2018-02-11 10:04:03 +02:00
export default Vue.extend({
2018-02-13 02:12:54 +02:00
data() {
return {
fetching: true,
moreFetching: false,
2018-02-25 17:39:05 +02:00
existMore: false,
2018-04-07 20:30:37 +03:00
notes: [],
2018-02-13 02:12:54 +02:00
connection: null,
2018-02-19 11:26:20 +02:00
connectionId: null,
2018-04-17 01:40:19 +03:00
date: null
2018-02-13 02:12:54 +02:00
};
},
2018-04-17 01:40:19 +03:00
2018-02-11 10:04:03 +02:00
computed: {
2018-02-13 02:12:54 +02:00
alone(): boolean {
2018-03-29 08:48:47 +03:00
return (this as any).os.i.followingCount == 0;
2018-02-11 10:04:03 +02:00
}
},
2018-04-17 01:40:19 +03:00
2018-02-13 02:12:54 +02:00
mounted() {
2018-02-18 05:35:18 +02:00
this.connection = (this as any).os.stream.getConnection();
this.connectionId = (this as any).os.stream.use();
2018-02-13 02:12:54 +02:00
2018-04-07 20:30:37 +03:00
this.connection.on('note', this.onNote);
2018-02-13 02:12:54 +02:00
this.connection.on('follow', this.onChangeFollowing);
this.connection.on('unfollow', this.onChangeFollowing);
document.addEventListener('keydown', this.onKeydown);
window.addEventListener('scroll', this.onScroll);
this.fetch();
},
2018-04-17 01:40:19 +03:00
2018-02-13 02:12:54 +02:00
beforeDestroy() {
2018-04-07 20:30:37 +03:00
this.connection.off('note', this.onNote);
2018-02-13 02:12:54 +02:00
this.connection.off('follow', this.onChangeFollowing);
this.connection.off('unfollow', this.onChangeFollowing);
2018-02-18 05:35:18 +02:00
(this as any).os.stream.dispose(this.connectionId);
2018-02-13 02:12:54 +02:00
document.removeEventListener('keydown', this.onKeydown);
window.removeEventListener('scroll', this.onScroll);
},
2018-04-17 01:40:19 +03:00
2018-02-11 10:04:03 +02:00
methods: {
2018-02-13 02:12:54 +02:00
fetch(cb?) {
this.fetching = true;
2018-04-07 20:30:37 +03:00
(this as any).api('notes/timeline', {
2018-02-25 17:39:05 +02:00
limit: 11,
2018-03-29 08:48:47 +03:00
untilDate: this.date ? this.date.getTime() : undefined
2018-04-07 20:30:37 +03:00
}).then(notes => {
if (notes.length == 11) {
notes.pop();
2018-02-25 17:39:05 +02:00
this.existMore = true;
}
2018-04-07 20:30:37 +03:00
this.notes = notes;
2018-02-19 16:37:09 +02:00
this.fetching = false;
2018-02-20 18:12:18 +02:00
this.$emit('loaded');
2018-02-13 02:12:54 +02:00
if (cb) cb();
});
},
2018-04-17 01:40:19 +03:00
2018-02-13 02:12:54 +02:00
more() {
2018-04-07 20:30:37 +03:00
if (this.moreFetching || this.fetching || this.notes.length == 0 || !this.existMore) return;
2018-02-13 02:12:54 +02:00
this.moreFetching = true;
2018-04-07 20:30:37 +03:00
(this as any).api('notes/timeline', {
2018-02-25 17:39:05 +02:00
limit: 11,
2018-04-07 20:30:37 +03:00
untilId: this.notes[this.notes.length - 1].id
}).then(notes => {
if (notes.length == 11) {
notes.pop();
2018-02-25 17:39:05 +02:00
} else {
this.existMore = false;
}
2018-04-07 20:30:37 +03:00
this.notes = this.notes.concat(notes);
2018-02-13 02:12:54 +02:00
this.moreFetching = false;
});
},
2018-04-17 01:40:19 +03:00
2018-04-07 20:30:37 +03:00
onNote(note) {
2018-03-04 11:50:30 +02:00
// サウンドを再生する
if ((this as any).os.isEnableSounds) {
2018-04-08 11:15:07 +03:00
const sound = new Audio(`${url}/assets/post.mp3`);
sound.volume = localStorage.getItem('soundVolume') ? parseInt(localStorage.getItem('soundVolume'), 10) / 100 : 0.5;
2018-03-06 11:06:45 +02:00
sound.play();
2018-03-04 11:50:30 +02:00
}
2018-04-07 20:30:37 +03:00
this.notes.unshift(note);
2018-04-17 01:40:19 +03:00
const isTop = window.scrollY > 8;
if (isTop) this.notes.pop();
2018-02-13 02:12:54 +02:00
},
2018-04-17 01:40:19 +03:00
2018-02-13 02:12:54 +02:00
onChangeFollowing() {
this.fetch();
},
2018-04-17 01:40:19 +03:00
2018-02-13 02:12:54 +02:00
onScroll() {
2018-04-07 21:58:11 +03:00
if ((this as any).os.i.clientSettings.fetchOnScroll !== false) {
2018-03-05 13:09:26 +02:00
const current = window.scrollY + window.innerHeight;
if (current > document.body.offsetHeight - 8) this.more();
}
2018-02-13 02:12:54 +02:00
},
2018-04-17 01:40:19 +03:00
2018-02-13 02:12:54 +02:00
onKeydown(e) {
if (e.target.tagName != 'INPUT' && e.target.tagName != 'TEXTAREA') {
if (e.which == 84) { // t
(this.$refs.timeline as any).focus();
}
}
2018-02-19 11:26:20 +02:00
},
2018-04-17 01:40:19 +03:00
2018-02-19 11:26:20 +02:00
warp(date) {
this.date = date;
this.fetch();
2018-02-11 10:04:03 +02:00
}
}
2018-02-11 09:52:37 +02:00
});
</script>
<style lang="stylus" scoped>
.mk-timeline
2018-02-13 02:12:54 +02:00
background #fff
border solid 1px rgba(0, 0, 0, 0.075)
border-radius 6px
2018-02-11 09:52:37 +02:00
2018-02-22 13:52:39 +02:00
> .mk-friends-maker
2018-02-13 02:12:54 +02:00
border-bottom solid 1px #eee
2018-02-11 09:52:37 +02:00
2018-02-20 02:48:30 +02:00
> .fetching
2018-02-13 02:12:54 +02:00
padding 64px 0
2018-02-11 09:52:37 +02:00
2018-02-13 02:12:54 +02:00
> .empty
display block
margin 0 auto
padding 32px
max-width 400px
2018-02-11 09:52:37 +02:00
text-align center
2018-02-13 02:12:54 +02:00
color #999
> [data-fa]
display block
margin-bottom 16px
font-size 3em
color #ccc
2018-02-11 09:52:37 +02:00
</style>