2020-08-05 22:08:55 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-04-21 00:56:39 +05:30
|
|
|
import {ListView} from "../../general/ListView.js";
|
|
|
|
import {GapView} from "./timeline/GapView.js";
|
|
|
|
import {TextMessageView} from "./timeline/TextMessageView.js";
|
2020-05-09 23:32:08 +05:30
|
|
|
import {ImageView} from "./timeline/ImageView.js";
|
2020-04-21 00:56:39 +05:30
|
|
|
import {AnnouncementView} from "./timeline/AnnouncementView.js";
|
2019-06-16 18:51:20 +05:30
|
|
|
|
2020-04-21 00:56:39 +05:30
|
|
|
export class TimelineList extends ListView {
|
2020-08-17 20:04:25 +05:30
|
|
|
constructor(viewModel) {
|
|
|
|
const options = {
|
|
|
|
className: "Timeline",
|
|
|
|
list: viewModel.tiles,
|
|
|
|
}
|
2019-06-16 18:51:20 +05:30
|
|
|
super(options, entry => {
|
|
|
|
switch (entry.shape) {
|
|
|
|
case "gap": return new GapView(entry);
|
|
|
|
case "announcement": return new AnnouncementView(entry);
|
2020-05-09 23:32:08 +05:30
|
|
|
case "message": return new TextMessageView(entry);
|
|
|
|
case "image": return new ImageView(entry);
|
2019-06-16 18:51:20 +05:30
|
|
|
}
|
|
|
|
});
|
|
|
|
this._atBottom = false;
|
2019-06-16 20:59:33 +05:30
|
|
|
this._onScroll = this._onScroll.bind(this);
|
|
|
|
this._topLoadingPromise = null;
|
2020-08-17 20:04:25 +05:30
|
|
|
this._viewModel = viewModel;
|
2019-06-16 20:59:33 +05:30
|
|
|
}
|
|
|
|
|
2020-08-17 20:04:58 +05:30
|
|
|
async _loadAtTopWhile(predicate) {
|
2020-08-17 21:28:30 +05:30
|
|
|
if (this._topLoadingPromise) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-17 20:04:58 +05:30
|
|
|
try {
|
|
|
|
while (predicate()) {
|
|
|
|
// fill, not enough content to fill timeline
|
|
|
|
this._topLoadingPromise = this._viewModel.loadAtTop();
|
2020-08-17 21:11:10 +05:30
|
|
|
const startReached = await this._topLoadingPromise;
|
|
|
|
if (startReached) {
|
|
|
|
break;
|
|
|
|
}
|
2020-08-17 20:04:58 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
//ignore error, as it is handled in the VM
|
|
|
|
}
|
|
|
|
finally {
|
2019-06-16 20:59:33 +05:30
|
|
|
this._topLoadingPromise = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 20:04:58 +05:30
|
|
|
async _onScroll() {
|
|
|
|
const PAGINATE_OFFSET = 100;
|
|
|
|
const root = this.root();
|
|
|
|
if (root.scrollTop < PAGINATE_OFFSET && !this._topLoadingPromise && this._viewModel) {
|
|
|
|
// to calculate total amountGrown to check when we stop loading
|
|
|
|
let beforeContentHeight = root.scrollHeight;
|
|
|
|
// to adjust scrollTop every time
|
|
|
|
let lastContentHeight = beforeContentHeight;
|
|
|
|
// load until pagination offset is reached again
|
|
|
|
this._loadAtTopWhile(() => {
|
|
|
|
const contentHeight = root.scrollHeight;
|
|
|
|
const amountGrown = contentHeight - beforeContentHeight;
|
|
|
|
root.scrollTop = root.scrollTop + (contentHeight - lastContentHeight);
|
|
|
|
lastContentHeight = contentHeight;
|
|
|
|
return amountGrown < PAGINATE_OFFSET;
|
|
|
|
});
|
2019-06-16 20:59:33 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mount() {
|
|
|
|
const root = super.mount();
|
|
|
|
root.addEventListener("scroll", this._onScroll);
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
unmount() {
|
|
|
|
this.root().removeEventListener("scroll", this._onScroll);
|
|
|
|
super.unmount();
|
2019-06-16 18:51:20 +05:30
|
|
|
}
|
|
|
|
|
2020-08-17 21:28:39 +05:30
|
|
|
async loadList() {
|
2019-06-16 20:58:51 +05:30
|
|
|
super.loadList();
|
2019-06-16 20:09:20 +05:30
|
|
|
const root = this.root();
|
2020-08-17 21:28:39 +05:30
|
|
|
// yield so the browser can render the list
|
|
|
|
// and we can measure the content below
|
|
|
|
await Promise.resolve();
|
2020-08-17 20:04:58 +05:30
|
|
|
const {scrollHeight, clientHeight} = root;
|
|
|
|
if (scrollHeight > clientHeight) {
|
|
|
|
root.scrollTop = root.scrollHeight;
|
|
|
|
}
|
|
|
|
// load while viewport is not filled
|
|
|
|
this._loadAtTopWhile(() => {
|
|
|
|
const {scrollHeight, clientHeight} = root;
|
|
|
|
return scrollHeight <= clientHeight;
|
|
|
|
});
|
2019-06-16 20:09:20 +05:30
|
|
|
}
|
|
|
|
|
2019-06-16 18:51:20 +05:30
|
|
|
onBeforeListChanged() {
|
2019-06-16 20:59:33 +05:30
|
|
|
const fromBottom = this._distanceFromBottom();
|
2019-06-16 18:51:20 +05:30
|
|
|
this._atBottom = fromBottom < 1;
|
|
|
|
}
|
|
|
|
|
2019-06-16 20:59:33 +05:30
|
|
|
_distanceFromBottom() {
|
|
|
|
const root = this.root();
|
|
|
|
return root.scrollHeight - root.scrollTop - root.clientHeight;
|
|
|
|
}
|
|
|
|
|
2019-06-16 18:51:20 +05:30
|
|
|
onListChanged() {
|
2020-08-17 20:04:58 +05:30
|
|
|
const root = this.root();
|
2019-06-16 18:51:20 +05:30
|
|
|
if (this._atBottom) {
|
|
|
|
root.scrollTop = root.scrollHeight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|