tweb/src/lib/appManagers/appSidebarLeft.ts

291 lines
9.6 KiB
TypeScript
Raw Normal View History

2020-02-06 16:43:07 +01:00
import { logger } from "../polyfill";
import { putPreloader } from "../../components/misc";
import Scrollable from '../../components/scrollable';
2020-02-06 16:43:07 +01:00
import appMessagesManager from "./appMessagesManager";
import appDialogsManager from "./appDialogsManager";
2020-02-08 12:58:22 +01:00
import { isElementInViewport } from "../utils";
2020-02-07 07:38:55 +01:00
import appMessagesIDsManager from "./appMessagesIDsManager";
import appImManager from "./appImManager";
2020-02-06 16:43:07 +01:00
class AppSidebarLeft {
private sidebarEl = document.querySelector('.page-chats .chats-container') as HTMLDivElement;
private searchInput = document.getElementById('global-search') as HTMLInputElement;
private toolsBtn = this.sidebarEl.querySelector('.sidebar-tools-button') as HTMLButtonElement;
private searchContainer = this.sidebarEl.querySelector('#search-container') as HTMLDivElement;
2020-02-08 12:58:22 +01:00
private menuEl = this.toolsBtn.querySelector('.btn-menu');
private savedBtn = this.menuEl.querySelector('.menu-saved');
2020-02-07 07:38:55 +01:00
2020-02-06 16:43:07 +01:00
private listsContainer: HTMLDivElement = null;
private searchMessagesList: HTMLUListElement = null;
2020-02-07 07:38:55 +01:00
2020-02-08 12:58:22 +01:00
private chatsContainer = document.getElementById('chats-container') as HTMLDivElement;
private chatsOffsetIndex = 0;
private chatsPreloader: HTMLDivElement;
private chatsLoadCount = 0;
private loadDialogsPromise: Promise<any>;
2020-02-06 16:43:07 +01:00
private log = logger('SL');
2020-02-07 07:38:55 +01:00
2020-02-06 16:43:07 +01:00
private peerID = 0;
2020-02-07 07:38:55 +01:00
private minMsgID = 0;
private loadedCount = 0;
private foundCount = 0;
private offsetRate = 0;
2020-02-06 16:43:07 +01:00
private searchPromise: Promise<void> = null;
2020-02-07 07:38:55 +01:00
private searchTimeout: number = 0;
2020-02-08 12:58:22 +01:00
private query = '';
public scroll: Scrollable = null;
2020-02-06 16:43:07 +01:00
constructor() {
2020-02-08 12:58:22 +01:00
this.chatsPreloader = document.createElement('div');
this.chatsPreloader.classList.add('preloader');
putPreloader(this.chatsPreloader);
this.chatsContainer.append(this.chatsPreloader);
this.chatsLoadCount = Math.round(document.body.scrollHeight / 70 * 1.5);
this.scroll = new Scrollable(this.chatsContainer as HTMLDivElement);
this.scroll.setVirtualContainer(appDialogsManager.chatList);
appDialogsManager.chatsHidden = this.scroll.hiddenElements;
2020-02-08 12:58:22 +01:00
this.scroll.container.addEventListener('scroll', this.onChatsScroll.bind(this));
2020-02-08 12:58:22 +01:00
this.listsContainer = new Scrollable(this.searchContainer).container;
2020-02-08 12:58:22 +01:00
this.searchMessagesList = document.createElement('ul');
this.savedBtn.addEventListener('click', () => {
2020-02-08 12:58:22 +01:00
appImManager.setPeer(appImManager.myID);
});
2020-02-07 07:38:55 +01:00
2020-02-08 12:58:22 +01:00
/* this.listsContainer.insertBefore(this.searchMessagesList, this.listsContainer.lastElementChild);
for(let i = 0; i < 25; ++i) {
let li = document.createElement('li');
li.innerHTML = `<div class="user-avatar is-online" style="font-size: 0px;"><img src="assets/img/camomile.jpg"></div><div class="user-caption"><p><span class="user-title">Влад</span><span><span class="message-status"></span><span class="message-time">14:41</span></span></p><p><span class="user-last-message">это важно</span><span class="tgico-pinnedchat"></span></p></div><div class="c-ripple"><span class="c-ripple__circle" style="top: 65px; left: 338.5px;"></span></div>`;
this.searchMessagesList.append(li);
} */
this.listsContainer.addEventListener('scroll', this.onSidebarScroll.bind(this));
2020-02-07 07:38:55 +01:00
2020-02-08 12:58:22 +01:00
//this.searchContainer.append(this.listsContainer);
2020-02-07 07:38:55 +01:00
2020-02-06 16:43:07 +01:00
appDialogsManager.setListClickListener(this.searchMessagesList);
2020-02-07 07:38:55 +01:00
2020-02-09 11:30:24 +01:00
let clickTimeout = 0;
2020-02-06 16:43:07 +01:00
this.searchInput.addEventListener('focus', (e) => {
2020-02-09 11:30:24 +01:00
this.toolsBtn.classList.remove('tgico-menu', 'btn-menu-toggle');
2020-02-06 16:43:07 +01:00
this.toolsBtn.classList.add('tgico-back');
this.searchContainer.classList.add('active');
2020-02-07 07:38:55 +01:00
2020-02-06 16:43:07 +01:00
if(!this.searchInput.value) {
this.searchMessagesList.innerHTML = '';
}
2020-02-09 11:30:24 +01:00
2020-02-06 16:43:07 +01:00
this.searchInput.addEventListener('blur', (e) => {
if(!this.searchInput.value) {
this.toolsBtn.classList.add('tgico-menu');
this.toolsBtn.classList.remove('tgico-back');
this.searchContainer.classList.remove('active');
2020-02-09 11:30:24 +01:00
setTimeout(() => {
//this.toolsBtn.click();
this.toolsBtn.classList.add('btn-menu-toggle');
}, 200);
2020-02-06 16:43:07 +01:00
}
2020-02-07 07:38:55 +01:00
/* this.peerID = 0;
this.loadedCount = 0;
this.minMsgID = 0; */
2020-02-06 16:43:07 +01:00
}, {once: true});
});
2020-02-07 07:38:55 +01:00
2020-02-06 16:43:07 +01:00
this.searchInput.addEventListener('input', (e) => {
//console.log('messageInput input', this.innerText, serializeNodes(Array.from(messageInput.childNodes)));
let value = this.searchInput.value;
this.log('input', value);
2020-02-07 07:38:55 +01:00
2020-02-06 16:43:07 +01:00
if(this.listsContainer.contains(this.searchMessagesList)) {
2020-02-08 12:58:22 +01:00
this.listsContainer.removeChild(this.searchMessagesList);
2020-02-06 16:43:07 +01:00
}
2020-02-07 07:38:55 +01:00
2020-02-06 16:43:07 +01:00
if(!value.trim()) {
return;
}
2020-02-08 12:58:22 +01:00
2020-02-07 07:38:55 +01:00
this.query = value;
this.minMsgID = 0;
this.loadedCount = 0;
this.foundCount = 0;
this.offsetRate = 0;
this.searchMessagesList.innerHTML = '';
this.searchPromise = null;
this.searchMore().then(() => {
2020-02-06 16:43:07 +01:00
this.listsContainer.append(this.searchMessagesList);
});
});
2020-02-07 07:38:55 +01:00
2020-02-09 11:30:24 +01:00
this.toolsBtn.addEventListener('click', (e) => {
this.log('click', this.toolsBtn.classList.contains('tgico-back'));
2020-02-06 16:43:07 +01:00
if(this.toolsBtn.classList.contains('tgico-back')) {
this.searchInput.value = '';
2020-02-09 11:30:24 +01:00
this.toolsBtn.classList.add('tgico-menu', 'btn-menu-toggle');
2020-02-06 16:43:07 +01:00
this.toolsBtn.classList.remove('tgico-back');
this.searchContainer.classList.remove('active');
2020-02-07 08:39:00 +01:00
this.peerID = 0;
2020-02-09 11:30:24 +01:00
e.stopPropagation();
e.cancelBubble = true;
e.preventDefault();
return false;
2020-02-06 16:43:07 +01:00
}
2020-02-09 11:30:24 +01:00
}, true);
2020-02-08 12:58:22 +01:00
2020-02-07 07:38:55 +01:00
window.addEventListener('resize', () => {
2020-02-08 12:58:22 +01:00
this.chatsLoadCount = Math.round(document.body.scrollHeight / 70 * 1.5);
setTimeout(() => {
this.onSidebarScroll();
this.onChatsScroll();
}, 0);
2020-02-07 07:38:55 +01:00
});
}
2020-02-08 12:58:22 +01:00
public async loadDialogs() {
if(this.loadDialogsPromise/* || 1 == 1 */) return this.loadDialogsPromise;
this.chatsContainer.append(this.chatsPreloader);
//let offset = appMessagesManager.generateDialogIndex();/* appMessagesManager.dialogsNum */;
try {
this.loadDialogsPromise = appMessagesManager.getConversations('', this.chatsOffsetIndex, this.chatsLoadCount);
let result = await this.loadDialogsPromise;
if(result && result.dialogs && result.dialogs.length) {
this.chatsOffsetIndex = result.dialogs[result.dialogs.length - 1].index;
result.dialogs.forEach((dialog: any) => {
appDialogsManager.addDialog(dialog);
});
}
this.log('loaded ' + this.chatsLoadCount + ' dialogs by offset:', this.chatsOffsetIndex, result, this.scroll.hiddenElements);
this.scroll.onScroll();
2020-02-08 12:58:22 +01:00
} catch(err) {
this.log.error(err);
}
this.chatsPreloader.remove();
this.loadDialogsPromise = undefined;
}
public onChatsScroll() {
this.log(this.scroll);
if(this.scroll.hiddenElements.down.length > 0/* || 1 == 1 */) return;
2020-02-08 12:58:22 +01:00
if(!this.loadDialogsPromise) {
let d = Array.from(appDialogsManager.chatList.childNodes).slice(-5);
for(let node of d) {
if(isElementInViewport(node)) {
this.loadDialogs();
break;
}
}
//console.log('last 5 dialogs:', d);
}
}
2020-02-07 07:38:55 +01:00
public onSidebarScroll() {
2020-02-08 12:58:22 +01:00
if(!this.query.trim()) return;
2020-02-07 07:38:55 +01:00
let elements = Array.from(this.searchMessagesList.childNodes).slice(-5);
for(let li of elements) {
if(isElementInViewport(li)) {
this.log('Will load more search');
2020-02-08 12:58:22 +01:00
2020-02-07 07:38:55 +01:00
if(!this.searchTimeout) {
this.searchTimeout = setTimeout(() => {
this.searchMore();
this.searchTimeout = 0;
}, 0);
}
break;
}
}
}
2020-02-06 16:43:07 +01:00
public beginSearch(peerID?: number) {
if(peerID) {
this.peerID = peerID;
}
2020-02-07 07:38:55 +01:00
2020-02-06 16:43:07 +01:00
this.searchInput.focus();
}
2020-02-08 12:58:22 +01:00
2020-02-07 07:38:55 +01:00
private searchMore() {
if(this.searchPromise) return this.searchPromise;
2020-02-08 12:58:22 +01:00
2020-02-07 07:38:55 +01:00
let query = this.query;
2020-02-08 12:58:22 +01:00
if(!query.trim()) return;
2020-02-07 07:38:55 +01:00
if(this.loadedCount != 0 && this.loadedCount >= this.foundCount) {
return Promise.resolve();
}
2020-02-07 08:39:00 +01:00
let maxID = appMessagesIDsManager.getMessageIDInfo(this.minMsgID)[0];
2020-02-08 12:58:22 +01:00
2020-02-07 07:38:55 +01:00
return this.searchPromise = appMessagesManager.getSearch(this.peerID, query, null, maxID, 20, this.offsetRate).then(res => {
this.searchPromise = null;
2020-02-08 12:58:22 +01:00
2020-02-07 07:38:55 +01:00
if(this.searchInput.value != query) {
return;
}
this.log('input search result:', this.peerID, query, null, maxID, 20, res);
let {count, history, next_rate} = res;
2020-02-08 12:58:22 +01:00
2020-02-07 07:38:55 +01:00
if(history[0] == this.minMsgID) {
history.shift();
}
history.forEach((msgID: number) => {
let message = appMessagesManager.getMessage(msgID);
let originalDialog = appMessagesManager.getDialogByPeerID(message.peerID)[0];
if(!originalDialog) {
2020-02-07 08:39:00 +01:00
this.log('no original dialog by message:', message);
2020-02-08 12:58:22 +01:00
2020-02-07 08:39:00 +01:00
originalDialog = {
peerID: message.peerID,
pFlags: {},
peer: message.to_id
};
2020-02-07 07:38:55 +01:00
}
let {dialog, dom} = appDialogsManager.addDialog(originalDialog, this.searchMessagesList, false);
appDialogsManager.setLastMessage(dialog, message, dom);
});
2020-02-08 12:58:22 +01:00
2020-02-07 07:38:55 +01:00
this.minMsgID = history[history.length - 1];
this.offsetRate = next_rate;
this.loadedCount += history.length;
2020-02-08 12:58:22 +01:00
2020-02-07 07:38:55 +01:00
if(!this.foundCount) {
this.foundCount = count;
}
}).catch(err => {
this.log.error('search error', err);
this.searchPromise = null;
});
}
2020-02-06 16:43:07 +01:00
}
export default new AppSidebarLeft();