2019-05-20 20:07:11 +02:00
|
|
|
import Vue from 'vue';
|
|
|
|
|
|
|
|
export default (opts) => ({
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
items: [],
|
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,
|
|
|
|
backed: false,
|
2019-05-20 20:07:11 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
computed: {
|
|
|
|
empty(): boolean {
|
|
|
|
return this.items.length == 0 && !this.fetching && this.inited;
|
|
|
|
},
|
|
|
|
|
|
|
|
error(): boolean {
|
|
|
|
return !this.fetching && !this.inited;
|
2020-01-29 20:37:25 +01:00
|
|
|
},
|
2019-05-20 20:07:11 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
watch: {
|
|
|
|
pagination() {
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
created() {
|
|
|
|
opts.displayLimit = opts.displayLimit || 30;
|
|
|
|
this.init();
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
2020-01-29 20:37:25 +01:00
|
|
|
isScrollTop() {
|
|
|
|
return window.scrollY <= 8;
|
|
|
|
},
|
|
|
|
|
2019-05-20 20:07:11 +02:00
|
|
|
updateItem(i, item) {
|
|
|
|
Vue.set((this as any).items, i, item);
|
|
|
|
},
|
|
|
|
|
|
|
|
reload() {
|
|
|
|
this.items = [];
|
|
|
|
this.init();
|
|
|
|
},
|
|
|
|
|
|
|
|
async init() {
|
|
|
|
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-01-29 20:37:25 +01:00
|
|
|
const endpoint = typeof this.pagination.endpoint === 'function' ? this.pagination.endpoint() : this.pagination.endpoint;
|
|
|
|
await this.$root.api(endpoint, {
|
|
|
|
limit: this.pagination.noPaging ? (this.pagination.limit || 10) : (this.pagination.limit || 10) + 1,
|
2019-05-20 20:07:11 +02:00
|
|
|
...params
|
|
|
|
}).then(x => {
|
2020-01-29 20:37:25 +01:00
|
|
|
if (!this.pagination.noPaging && (x.length === (this.pagination.limit || 10) + 1)) {
|
2019-05-20 20:07:11 +02:00
|
|
|
x.pop();
|
|
|
|
this.items = x;
|
|
|
|
this.more = true;
|
|
|
|
} else {
|
|
|
|
this.items = x;
|
|
|
|
this.more = false;
|
|
|
|
}
|
2019-06-13 11:15:35 +02:00
|
|
|
this.offset = x.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() {
|
|
|
|
if (!this.more || this.moreFetching || this.items.length === 0) return;
|
|
|
|
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;
|
|
|
|
await this.$root.api(endpoint, {
|
2019-05-20 20:07:11 +02:00
|
|
|
limit: (this.pagination.limit || 10) + 1,
|
2020-01-29 20:37:25 +01:00
|
|
|
...(this.pagination.offsetMode ? {
|
2019-06-13 11:15:35 +02:00
|
|
|
offset: this.offset,
|
|
|
|
} : {
|
|
|
|
untilId: this.items[this.items.length - 1].id,
|
|
|
|
}),
|
2019-05-20 20:07:11 +02:00
|
|
|
...params
|
|
|
|
}).then(x => {
|
2020-01-29 20:37:25 +01:00
|
|
|
if (x.length === (this.pagination.limit || 10) + 1) {
|
2019-05-20 20:07:11 +02:00
|
|
|
x.pop();
|
|
|
|
this.items = this.items.concat(x);
|
|
|
|
this.more = true;
|
|
|
|
} else {
|
|
|
|
this.items = this.items.concat(x);
|
|
|
|
this.more = false;
|
|
|
|
}
|
2019-06-13 11:15:35 +02:00
|
|
|
this.offset += x.length;
|
2019-05-20 20:07:11 +02:00
|
|
|
this.moreFetching = false;
|
|
|
|
}, e => {
|
|
|
|
this.moreFetching = false;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
prepend(item, silent = false) {
|
|
|
|
if (opts.onPrepend) {
|
|
|
|
const cancel = opts.onPrepend(this, item, silent);
|
|
|
|
if (cancel) return;
|
|
|
|
}
|
|
|
|
|
2020-01-29 20:37:25 +01:00
|
|
|
// Prepend the item
|
|
|
|
this.items.unshift(item);
|
2019-05-20 20:07:11 +02:00
|
|
|
|
2020-01-29 20:37:25 +01:00
|
|
|
if (this.isScrollTop()) {
|
2020-02-11 18:52:43 +01:00
|
|
|
// オーバーフローしたら古いアイテムは捨てる
|
2019-05-20 20:07:11 +02:00
|
|
|
if (this.items.length >= opts.displayLimit) {
|
|
|
|
this.items = this.items.slice(0, opts.displayLimit);
|
|
|
|
this.more = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
append(item) {
|
|
|
|
this.items.push(item);
|
|
|
|
},
|
|
|
|
|
2020-01-29 20:37:25 +01:00
|
|
|
remove(find) {
|
|
|
|
this.items = this.items.filter(x => !find(x));
|
2019-05-20 20:07:11 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|