|
|
@ -1,10 +1,9 @@ |
|
|
|
import { logger } from "../polyfill"; |
|
|
|
import { logger } from "../polyfill"; |
|
|
|
import { scrollable } from "../../components/misc"; |
|
|
|
import { scrollable, putPreloader } from "../../components/misc"; |
|
|
|
import appMessagesManager from "./appMessagesManager"; |
|
|
|
import appMessagesManager from "./appMessagesManager"; |
|
|
|
import appDialogsManager from "./appDialogsManager"; |
|
|
|
import appDialogsManager from "./appDialogsManager"; |
|
|
|
import { isElementInViewport, $rootScope } from "../utils"; |
|
|
|
import { isElementInViewport } from "../utils"; |
|
|
|
import appMessagesIDsManager from "./appMessagesIDsManager"; |
|
|
|
import appMessagesIDsManager from "./appMessagesIDsManager"; |
|
|
|
import apiManager from '../mtproto/apiManager'; |
|
|
|
|
|
|
|
import appImManager from "./appImManager"; |
|
|
|
import appImManager from "./appImManager"; |
|
|
|
|
|
|
|
|
|
|
|
class AppSidebarLeft { |
|
|
|
class AppSidebarLeft { |
|
|
@ -12,13 +11,22 @@ class AppSidebarLeft { |
|
|
|
private searchInput = document.getElementById('global-search') as HTMLInputElement; |
|
|
|
private searchInput = document.getElementById('global-search') as HTMLInputElement; |
|
|
|
private toolsBtn = this.sidebarEl.querySelector('.sidebar-tools-button') as HTMLButtonElement; |
|
|
|
private toolsBtn = this.sidebarEl.querySelector('.sidebar-tools-button') as HTMLButtonElement; |
|
|
|
private searchContainer = this.sidebarEl.querySelector('#search-container') as HTMLDivElement; |
|
|
|
private searchContainer = this.sidebarEl.querySelector('#search-container') as HTMLDivElement; |
|
|
|
|
|
|
|
|
|
|
|
private menuEl = this.toolsBtn.querySelector('.btn-menu'); |
|
|
|
private menuEl = this.toolsBtn.querySelector('.btn-menu'); |
|
|
|
private savedBtn = this.menuEl.querySelector('.menu-saved'); |
|
|
|
private savedBtn = this.menuEl.querySelector('.menu-saved'); |
|
|
|
|
|
|
|
|
|
|
|
private listsContainer: HTMLDivElement = null; |
|
|
|
private listsContainer: HTMLDivElement = null; |
|
|
|
private searchMessagesList: HTMLUListElement = null; |
|
|
|
private searchMessagesList: HTMLUListElement = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private chatsContainer = document.getElementById('chats-container') as HTMLDivElement; |
|
|
|
|
|
|
|
private chatsOffsetIndex = 0; |
|
|
|
|
|
|
|
private chatsScroll: HTMLDivElement; |
|
|
|
|
|
|
|
private chatsHidden: any; |
|
|
|
|
|
|
|
private chatsPreloader: HTMLDivElement; |
|
|
|
|
|
|
|
private chatsLoadCount = 0; |
|
|
|
|
|
|
|
private loadDialogsPromise: Promise<any>; |
|
|
|
|
|
|
|
private hiddenScroll: any; |
|
|
|
|
|
|
|
|
|
|
|
private log = logger('SL'); |
|
|
|
private log = logger('SL'); |
|
|
|
|
|
|
|
|
|
|
|
private peerID = 0; |
|
|
|
private peerID = 0; |
|
|
@ -29,31 +37,43 @@ class AppSidebarLeft { |
|
|
|
|
|
|
|
|
|
|
|
private searchPromise: Promise<void> = null; |
|
|
|
private searchPromise: Promise<void> = null; |
|
|
|
private searchTimeout: number = 0; |
|
|
|
private searchTimeout: number = 0; |
|
|
|
|
|
|
|
|
|
|
|
private query = ''; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public myID = 0; |
|
|
|
private query = ''; |
|
|
|
|
|
|
|
|
|
|
|
constructor() { |
|
|
|
constructor() { |
|
|
|
this.listsContainer = scrollable(this.searchContainer); |
|
|
|
this.chatsPreloader = document.createElement('div'); |
|
|
|
this.searchMessagesList = document.createElement('ul'); |
|
|
|
this.chatsPreloader.classList.add('preloader'); |
|
|
|
|
|
|
|
putPreloader(this.chatsPreloader); |
|
|
|
apiManager.getUserID().then((id) => { |
|
|
|
this.chatsContainer.append(this.chatsPreloader); |
|
|
|
this.myID = id; |
|
|
|
|
|
|
|
}); |
|
|
|
this.chatsLoadCount = Math.round(document.body.scrollHeight / 70 * 1.5); |
|
|
|
|
|
|
|
|
|
|
|
$rootScope.$on('user_auth', (e: CustomEvent) => { |
|
|
|
let {container: chatsScroll, hiddenElements: chatsHidden, onScroll: hiddenScroll} = scrollable(this.chatsContainer as HTMLDivElement); |
|
|
|
let userAuth = e.detail; |
|
|
|
this.chatsScroll = chatsScroll; |
|
|
|
this.myID = userAuth ? userAuth.id : 0; |
|
|
|
this.chatsHidden = chatsHidden; |
|
|
|
}); |
|
|
|
this.hiddenScroll = hiddenScroll; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
appDialogsManager.chatsHidden = this.chatsHidden; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
chatsScroll.addEventListener('scroll', this.onChatsScroll.bind(this)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.listsContainer = scrollable(this.searchContainer).container; |
|
|
|
|
|
|
|
this.searchMessagesList = document.createElement('ul'); |
|
|
|
|
|
|
|
|
|
|
|
this.savedBtn.addEventListener('click', () => { |
|
|
|
this.savedBtn.addEventListener('click', () => { |
|
|
|
appImManager.setPeer(this.myID); |
|
|
|
appImManager.setPeer(appImManager.myID); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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)); |
|
|
|
this.listsContainer.addEventListener('scroll', this.onSidebarScroll.bind(this)); |
|
|
|
|
|
|
|
|
|
|
|
this.searchContainer.append(this.listsContainer); |
|
|
|
//this.searchContainer.append(this.listsContainer);
|
|
|
|
|
|
|
|
|
|
|
|
appDialogsManager.setListClickListener(this.searchMessagesList); |
|
|
|
appDialogsManager.setListClickListener(this.searchMessagesList); |
|
|
|
|
|
|
|
|
|
|
@ -85,13 +105,13 @@ class AppSidebarLeft { |
|
|
|
this.log('input', value); |
|
|
|
this.log('input', value); |
|
|
|
|
|
|
|
|
|
|
|
if(this.listsContainer.contains(this.searchMessagesList)) { |
|
|
|
if(this.listsContainer.contains(this.searchMessagesList)) { |
|
|
|
this.listsContainer.removeChild(this.searchMessagesList) |
|
|
|
this.listsContainer.removeChild(this.searchMessagesList); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if(!value.trim()) { |
|
|
|
if(!value.trim()) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.query = value; |
|
|
|
this.query = value; |
|
|
|
this.minMsgID = 0; |
|
|
|
this.minMsgID = 0; |
|
|
|
this.loadedCount = 0; |
|
|
|
this.loadedCount = 0; |
|
|
@ -113,18 +133,70 @@ class AppSidebarLeft { |
|
|
|
this.peerID = 0; |
|
|
|
this.peerID = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
window.addEventListener('resize', () => { |
|
|
|
window.addEventListener('resize', () => { |
|
|
|
setTimeout(() => this.onSidebarScroll(), 0); |
|
|
|
this.chatsLoadCount = Math.round(document.body.scrollHeight / 70 * 1.5); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setTimeout(() => { |
|
|
|
|
|
|
|
this.onSidebarScroll(); |
|
|
|
|
|
|
|
this.onChatsScroll(); |
|
|
|
|
|
|
|
}, 0); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.chatsHidden); |
|
|
|
|
|
|
|
this.hiddenScroll(); |
|
|
|
|
|
|
|
} catch(err) { |
|
|
|
|
|
|
|
this.log.error(err); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.chatsPreloader.remove(); |
|
|
|
|
|
|
|
this.loadDialogsPromise = undefined; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public onChatsScroll() { |
|
|
|
|
|
|
|
if(this.chatsHidden.down.length > 0/* || 1 == 1 */) return; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public onSidebarScroll() { |
|
|
|
public onSidebarScroll() { |
|
|
|
|
|
|
|
if(!this.query.trim()) return; |
|
|
|
|
|
|
|
|
|
|
|
let elements = Array.from(this.searchMessagesList.childNodes).slice(-5); |
|
|
|
let elements = Array.from(this.searchMessagesList.childNodes).slice(-5); |
|
|
|
for(let li of elements) { |
|
|
|
for(let li of elements) { |
|
|
|
if(isElementInViewport(li)) { |
|
|
|
if(isElementInViewport(li)) { |
|
|
|
this.log('Will load more search'); |
|
|
|
this.log('Will load more search'); |
|
|
|
|
|
|
|
|
|
|
|
if(!this.searchTimeout) { |
|
|
|
if(!this.searchTimeout) { |
|
|
|
this.searchTimeout = setTimeout(() => { |
|
|
|
this.searchTimeout = setTimeout(() => { |
|
|
|
this.searchMore(); |
|
|
|
this.searchMore(); |
|
|
@ -144,21 +216,23 @@ class AppSidebarLeft { |
|
|
|
|
|
|
|
|
|
|
|
this.searchInput.focus(); |
|
|
|
this.searchInput.focus(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private searchMore() { |
|
|
|
private searchMore() { |
|
|
|
if(this.searchPromise) return this.searchPromise; |
|
|
|
if(this.searchPromise) return this.searchPromise; |
|
|
|
|
|
|
|
|
|
|
|
let query = this.query; |
|
|
|
let query = this.query; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!query.trim()) return; |
|
|
|
|
|
|
|
|
|
|
|
if(this.loadedCount != 0 && this.loadedCount >= this.foundCount) { |
|
|
|
if(this.loadedCount != 0 && this.loadedCount >= this.foundCount) { |
|
|
|
return Promise.resolve(); |
|
|
|
return Promise.resolve(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let maxID = appMessagesIDsManager.getMessageIDInfo(this.minMsgID)[0]; |
|
|
|
let maxID = appMessagesIDsManager.getMessageIDInfo(this.minMsgID)[0]; |
|
|
|
|
|
|
|
|
|
|
|
return this.searchPromise = appMessagesManager.getSearch(this.peerID, query, null, maxID, 20, this.offsetRate).then(res => { |
|
|
|
return this.searchPromise = appMessagesManager.getSearch(this.peerID, query, null, maxID, 20, this.offsetRate).then(res => { |
|
|
|
this.searchPromise = null; |
|
|
|
this.searchPromise = null; |
|
|
|
|
|
|
|
|
|
|
|
if(this.searchInput.value != query) { |
|
|
|
if(this.searchInput.value != query) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
@ -166,7 +240,7 @@ class AppSidebarLeft { |
|
|
|
this.log('input search result:', this.peerID, query, null, maxID, 20, res); |
|
|
|
this.log('input search result:', this.peerID, query, null, maxID, 20, res); |
|
|
|
|
|
|
|
|
|
|
|
let {count, history, next_rate} = res; |
|
|
|
let {count, history, next_rate} = res; |
|
|
|
|
|
|
|
|
|
|
|
if(history[0] == this.minMsgID) { |
|
|
|
if(history[0] == this.minMsgID) { |
|
|
|
history.shift(); |
|
|
|
history.shift(); |
|
|
|
} |
|
|
|
} |
|
|
@ -177,7 +251,7 @@ class AppSidebarLeft { |
|
|
|
|
|
|
|
|
|
|
|
if(!originalDialog) { |
|
|
|
if(!originalDialog) { |
|
|
|
this.log('no original dialog by message:', message); |
|
|
|
this.log('no original dialog by message:', message); |
|
|
|
|
|
|
|
|
|
|
|
originalDialog = { |
|
|
|
originalDialog = { |
|
|
|
peerID: message.peerID, |
|
|
|
peerID: message.peerID, |
|
|
|
pFlags: {}, |
|
|
|
pFlags: {}, |
|
|
@ -188,11 +262,11 @@ class AppSidebarLeft { |
|
|
|
let {dialog, dom} = appDialogsManager.addDialog(originalDialog, this.searchMessagesList, false); |
|
|
|
let {dialog, dom} = appDialogsManager.addDialog(originalDialog, this.searchMessagesList, false); |
|
|
|
appDialogsManager.setLastMessage(dialog, message, dom); |
|
|
|
appDialogsManager.setLastMessage(dialog, message, dom); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
this.minMsgID = history[history.length - 1]; |
|
|
|
this.minMsgID = history[history.length - 1]; |
|
|
|
this.offsetRate = next_rate; |
|
|
|
this.offsetRate = next_rate; |
|
|
|
this.loadedCount += history.length; |
|
|
|
this.loadedCount += history.length; |
|
|
|
|
|
|
|
|
|
|
|
if(!this.foundCount) { |
|
|
|
if(!this.foundCount) { |
|
|
|
this.foundCount = count; |
|
|
|
this.foundCount = count; |
|
|
|
} |
|
|
|
} |
|
|
|