2020-10-17 13:12:00 +02:00
|
|
|
import { markRaw } from 'vue';
|
2021-03-23 09:30:14 +01:00
|
|
|
import * as os from '@client/os';
|
2021-02-14 14:26:07 +01:00
|
|
|
import { onScrollTop, isTopVisible, getScrollPosition, getScrollContainer } from './scroll';
|
2020-02-11 22:01:59 +01:00
|
|
|
|
2020-02-16 12:54:25 +01:00
|
|
|
const SECOND_FETCH_LIMIT = 30;
|
|
|
|
|
2021-02-14 14:26:07 +01:00
|
|
|
// reversed: items 配列の中身を逆順にする(新しい方が最後)
|
|
|
|
|
2019-05-20 20:07:11 +02:00
|
|
|
export default (opts) => ({
|
2020-10-17 13:12:00 +02:00
|
|
|
emits: ['queue'],
|
|
|
|
|
2019-05-20 20:07:11 +02:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
items: [],
|
2020-02-11 22:01:59 +01:00
|
|
|
queue: [],
|
2019-06-13 11:15:35 +02:00
|
|
|
offset: 0,
|
2019-05-20 20:07:11 +02:00
|
|
|
fetching: true,
|
|
|
|
moreFetching: false,
|
|
|
|
inited: false,
|
2020-02-09 14:00:38 +01:00
|
|
|
more: false,
|
2020-07-11 03:13:11 +02:00
|
|
|
backed: false, // 遡り中か否か
|
2020-02-11 22:01:59 +01:00
|
|
|
isBackTop: false,
|
2019-05-20 20:07:11 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
computed: {
|
|
|
|
empty(): boolean {
|
2020-04-04 01:46:54 +02:00
|
|
|
return this.items.length === 0 && !this.fetching && this.inited;
|
2019-05-20 20:07:11 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
error(): boolean {
|
|
|
|
return !this.fetching && !this.inited;
|
2020-01-29 20:37:25 +01:00
|
|
|
},
|
2019-05-20 20:07:11 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
watch: {
|
2020-10-17 13:12:00 +02:00
|
|
|
pagination: {
|
|
|
|
handler() {
|
|
|
|
this.init();
|
|
|
|
},
|
|
|
|
deep: true
|
2020-02-18 20:08:35 +01:00
|
|
|
},
|
|
|
|
|
2020-10-17 13:12:00 +02:00
|
|
|
queue: {
|
|
|
|
handler(a, b) {
|
|
|
|
if (a.length === 0 && b.length === 0) return;
|
|
|
|
this.$emit('queue', this.queue.length);
|
|
|
|
},
|
|
|
|
deep: true
|
2019-05-20 20:07:11 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
created() {
|
|
|
|
opts.displayLimit = opts.displayLimit || 30;
|
|
|
|
this.init();
|
|
|
|
},
|
|
|
|
|
2020-10-17 13:12:00 +02:00
|
|
|
activated() {
|
|
|
|
this.isBackTop = false;
|
2020-05-31 05:53:06 +02:00
|
|
|
},
|
|
|
|
|
2020-10-17 13:12:00 +02:00
|
|
|
deactivated() {
|
|
|
|
this.isBackTop = window.scrollY === 0;
|
2020-05-31 05:53:06 +02:00
|
|
|
},
|
|
|
|
|
2019-05-20 20:07:11 +02:00
|
|
|
methods: {
|
|
|
|
reload() {
|
|
|
|
this.items = [];
|
|
|
|
this.init();
|
|
|
|
},
|
|
|
|
|
2020-10-17 13:12:00 +02:00
|
|
|
replaceItem(finder, data) {
|
|
|
|
const i = this.items.findIndex(finder);
|
|
|
|
this.items[i] = data;
|
|
|
|
},
|
|
|
|
|
|
|
|
removeItem(finder) {
|
|
|
|
const i = this.items.findIndex(finder);
|
|
|
|
this.items.splice(i, 1);
|
|
|
|
},
|
|
|
|
|
2019-05-20 20:07:11 +02:00
|
|
|
async init() {
|
2020-02-22 18:28:07 +01:00
|
|
|
this.queue = [];
|
2019-05-20 20:07:11 +02:00
|
|
|
this.fetching = true;
|
2020-01-29 20:37:25 +01:00
|
|
|
if (opts.before) opts.before(this);
|
2019-05-20 20:07:11 +02:00
|
|
|
let params = typeof this.pagination.params === 'function' ? this.pagination.params(true) : this.pagination.params;
|
|
|
|
if (params && params.then) params = await params;
|
2020-10-17 13:12:00 +02:00
|
|
|
if (params === null) return;
|
2020-01-29 20:37:25 +01:00
|
|
|
const endpoint = typeof this.pagination.endpoint === 'function' ? this.pagination.endpoint() : this.pagination.endpoint;
|
2020-10-17 13:12:00 +02:00
|
|
|
await os.api(endpoint, {
|
2020-02-16 14:15:49 +01:00
|
|
|
...params,
|
2020-01-29 20:37:25 +01:00
|
|
|
limit: this.pagination.noPaging ? (this.pagination.limit || 10) : (this.pagination.limit || 10) + 1,
|
2020-02-16 14:15:49 +01:00
|
|
|
}).then(items => {
|
2021-05-04 14:15:57 +02:00
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
const item = items[i];
|
2020-10-17 13:12:00 +02:00
|
|
|
markRaw(item);
|
2021-07-29 16:59:45 +02:00
|
|
|
if (this.pagination.reversed) {
|
|
|
|
if (i === items.length - 2) item._shouldInsertAd_ = true;
|
|
|
|
} else {
|
|
|
|
if (i === 3) item._shouldInsertAd_ = true;
|
|
|
|
}
|
2020-07-27 01:46:21 +02:00
|
|
|
}
|
2020-02-18 10:19:11 +01:00
|
|
|
if (!this.pagination.noPaging && (items.length > (this.pagination.limit || 10))) {
|
2020-02-16 14:15:49 +01:00
|
|
|
items.pop();
|
|
|
|
this.items = this.pagination.reversed ? [...items].reverse() : items;
|
2019-05-20 20:07:11 +02:00
|
|
|
this.more = true;
|
|
|
|
} else {
|
2020-02-16 14:15:49 +01:00
|
|
|
this.items = this.pagination.reversed ? [...items].reverse() : items;
|
2019-05-20 20:07:11 +02:00
|
|
|
this.more = false;
|
|
|
|
}
|
2020-02-16 14:15:49 +01:00
|
|
|
this.offset = items.length;
|
2019-05-20 20:07:11 +02:00
|
|
|
this.inited = true;
|
|
|
|
this.fetching = false;
|
2020-01-29 20:37:25 +01:00
|
|
|
if (opts.after) opts.after(this, null);
|
2019-05-20 20:07:11 +02:00
|
|
|
}, e => {
|
|
|
|
this.fetching = false;
|
2020-01-29 20:37:25 +01:00
|
|
|
if (opts.after) opts.after(this, e);
|
2019-05-20 20:07:11 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
async fetchMore() {
|
2020-10-17 13:12:00 +02:00
|
|
|
if (!this.more || this.fetching || this.moreFetching || this.items.length === 0) return;
|
2019-05-20 20:07:11 +02:00
|
|
|
this.moreFetching = true;
|
2020-02-09 14:00:38 +01:00
|
|
|
this.backed = true;
|
2019-05-20 20:07:11 +02:00
|
|
|
let params = typeof this.pagination.params === 'function' ? this.pagination.params(false) : this.pagination.params;
|
|
|
|
if (params && params.then) params = await params;
|
2020-01-29 20:37:25 +01:00
|
|
|
const endpoint = typeof this.pagination.endpoint === 'function' ? this.pagination.endpoint() : this.pagination.endpoint;
|
2020-10-17 13:12:00 +02:00
|
|
|
await os.api(endpoint, {
|
2020-02-16 14:15:49 +01:00
|
|
|
...params,
|
2020-02-16 12:54:25 +01:00
|
|
|
limit: SECOND_FETCH_LIMIT + 1,
|
2020-01-29 20:37:25 +01:00
|
|
|
...(this.pagination.offsetMode ? {
|
2019-06-13 11:15:35 +02:00
|
|
|
offset: this.offset,
|
|
|
|
} : {
|
2021-02-14 14:26:07 +01:00
|
|
|
untilId: this.pagination.reversed ? this.items[0].id : this.items[this.items.length - 1].id,
|
2019-06-13 11:15:35 +02:00
|
|
|
}),
|
2020-02-16 14:15:49 +01:00
|
|
|
}).then(items => {
|
2021-05-04 14:15:57 +02:00
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
const item = items[i];
|
2020-10-17 13:12:00 +02:00
|
|
|
markRaw(item);
|
2021-07-29 16:59:45 +02:00
|
|
|
if (this.pagination.reversed) {
|
|
|
|
if (i === items.length - 9) item._shouldInsertAd_ = true;
|
|
|
|
} else {
|
|
|
|
if (i === 10) item._shouldInsertAd_ = true;
|
|
|
|
}
|
2020-07-27 01:46:21 +02:00
|
|
|
}
|
2020-02-18 10:19:11 +01:00
|
|
|
if (items.length > SECOND_FETCH_LIMIT) {
|
2020-02-16 14:15:49 +01:00
|
|
|
items.pop();
|
|
|
|
this.items = this.pagination.reversed ? [...items].reverse().concat(this.items) : this.items.concat(items);
|
2019-05-20 20:07:11 +02:00
|
|
|
this.more = true;
|
|
|
|
} else {
|
2020-02-16 14:15:49 +01:00
|
|
|
this.items = this.pagination.reversed ? [...items].reverse().concat(this.items) : this.items.concat(items);
|
2019-05-20 20:07:11 +02:00
|
|
|
this.more = false;
|
|
|
|
}
|
2020-02-16 14:15:49 +01:00
|
|
|
this.offset += items.length;
|
2019-05-20 20:07:11 +02:00
|
|
|
this.moreFetching = false;
|
|
|
|
}, e => {
|
|
|
|
this.moreFetching = false;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2021-02-14 14:26:07 +01:00
|
|
|
async fetchMoreFeature() {
|
|
|
|
if (!this.more || this.fetching || this.moreFetching || this.items.length === 0) return;
|
|
|
|
this.moreFetching = true;
|
|
|
|
let params = typeof this.pagination.params === 'function' ? this.pagination.params(false) : this.pagination.params;
|
|
|
|
if (params && params.then) params = await params;
|
|
|
|
const endpoint = typeof this.pagination.endpoint === 'function' ? this.pagination.endpoint() : this.pagination.endpoint;
|
|
|
|
await os.api(endpoint, {
|
|
|
|
...params,
|
|
|
|
limit: SECOND_FETCH_LIMIT + 1,
|
|
|
|
...(this.pagination.offsetMode ? {
|
|
|
|
offset: this.offset,
|
|
|
|
} : {
|
|
|
|
sinceId: this.pagination.reversed ? this.items[0].id : this.items[this.items.length - 1].id,
|
|
|
|
}),
|
|
|
|
}).then(items => {
|
|
|
|
for (const item of items) {
|
|
|
|
markRaw(item);
|
|
|
|
}
|
|
|
|
if (items.length > SECOND_FETCH_LIMIT) {
|
|
|
|
items.pop();
|
|
|
|
this.items = this.pagination.reversed ? [...items].reverse().concat(this.items) : this.items.concat(items);
|
2019-05-20 20:07:11 +02:00
|
|
|
this.more = true;
|
2021-02-14 14:26:07 +01:00
|
|
|
} else {
|
|
|
|
this.items = this.pagination.reversed ? [...items].reverse().concat(this.items) : this.items.concat(items);
|
|
|
|
this.more = false;
|
|
|
|
}
|
|
|
|
this.offset += items.length;
|
|
|
|
this.moreFetching = false;
|
|
|
|
}, e => {
|
|
|
|
this.moreFetching = false;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
prepend(item) {
|
|
|
|
if (this.pagination.reversed) {
|
|
|
|
const container = getScrollContainer(this.$el);
|
|
|
|
const pos = getScrollPosition(this.$el);
|
|
|
|
const viewHeight = container.clientHeight;
|
|
|
|
const height = container.scrollHeight;
|
|
|
|
const isBottom = (pos + viewHeight > height - 32);
|
|
|
|
if (isBottom) {
|
|
|
|
// オーバーフローしたら古いアイテムは捨てる
|
|
|
|
if (this.items.length >= opts.displayLimit) {
|
2021-08-10 08:07:15 +02:00
|
|
|
// このやり方だとVue 3.2以降アニメーションが動かなくなる
|
|
|
|
//this.items = this.items.slice(-opts.displayLimit);
|
|
|
|
while (this.items.length >= opts.displayLimit) {
|
|
|
|
this.items.shift();
|
|
|
|
}
|
2021-02-14 14:26:07 +01:00
|
|
|
this.more = true;
|
|
|
|
}
|
2019-05-20 20:07:11 +02:00
|
|
|
}
|
2021-02-14 14:26:07 +01:00
|
|
|
this.items.push(item);
|
|
|
|
// TODO
|
2020-02-11 22:01:59 +01:00
|
|
|
} else {
|
2021-02-14 14:26:07 +01:00
|
|
|
const isTop = this.isBackTop || (document.body.contains(this.$el) && isTopVisible(this.$el));
|
|
|
|
|
|
|
|
if (isTop) {
|
|
|
|
// Prepend the item
|
|
|
|
this.items.unshift(item);
|
|
|
|
|
|
|
|
// オーバーフローしたら古いアイテムは捨てる
|
|
|
|
if (this.items.length >= opts.displayLimit) {
|
2021-08-10 08:07:15 +02:00
|
|
|
// このやり方だとVue 3.2以降アニメーションが動かなくなる
|
|
|
|
//this.items = this.items.slice(0, opts.displayLimit);
|
|
|
|
while (this.items.length >= opts.displayLimit) {
|
|
|
|
this.items.pop();
|
|
|
|
}
|
2021-02-14 14:26:07 +01:00
|
|
|
this.more = true;
|
2020-02-11 22:01:59 +01:00
|
|
|
}
|
2021-02-14 14:26:07 +01:00
|
|
|
} else {
|
|
|
|
this.queue.push(item);
|
|
|
|
onScrollTop(this.$el, () => {
|
|
|
|
for (const item of this.queue) {
|
|
|
|
this.prepend(item);
|
|
|
|
}
|
|
|
|
this.queue = [];
|
|
|
|
});
|
|
|
|
}
|
2019-05-20 20:07:11 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
append(item) {
|
|
|
|
this.items.push(item);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|