@ -1,10 +1,9 @@
@@ -1,10 +1,9 @@
import { logger } from "../polyfill" ;
import { scrollable } from "../../components/misc" ;
import { scrollable , putPreloader } from "../../components/misc" ;
import appMessagesManager from "./appMessagesManager" ;
import appDialogsManager from "./appDialogsManager" ;
import { isElementInViewport , $rootScope } from "../utils" ;
import { isElementInViewport } from "../utils" ;
import appMessagesIDsManager from "./appMessagesIDsManager" ;
import apiManager from '../mtproto/apiManager' ;
import appImManager from "./appImManager" ;
class AppSidebarLeft {
@ -19,6 +18,15 @@ class AppSidebarLeft {
@@ -19,6 +18,15 @@ class AppSidebarLeft {
private listsContainer : HTMLDivElement = 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 peerID = 0 ;
@ -32,28 +40,40 @@ class AppSidebarLeft {
@@ -32,28 +40,40 @@ class AppSidebarLeft {
private query = '' ;
public myID = 0 ;
constructor ( ) {
this . listsContainer = scrollable ( this . searchContainer ) ;
this . searchMessagesList = document . createElement ( 'ul' ) ;
this . chatsPreloader = document . createElement ( 'div' ) ;
this . chatsPreloader . classList . add ( 'preloader' ) ;
putPreloader ( this . chatsPreloader ) ;
this . chatsContainer . append ( this . chatsPreloader ) ;
apiManager . getUserID ( ) . then ( ( id ) = > {
this . myID = id ;
} ) ;
this . chatsLoadCount = Math . round ( document . body . scrollHeight / 70 * 1.5 ) ;
$rootScope . $on ( 'user_auth' , ( e : CustomEvent ) = > {
let userAuth = e . detail ;
this . myID = userAuth ? userAuth.id : 0 ;
} ) ;
let { container : chatsScroll , hiddenElements : chatsHidden , onScroll : hiddenScroll } = scrollable ( this . chatsContainer as HTMLDivElement ) ;
this . chatsScroll = chatsScroll ;
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' , ( ) = > {
appImManager . setPeer ( this . myID ) ;
appImManager . setPeer ( appImManager . myID ) ;
} ) ;
/ * t h i s . l i s t s C o n t a i n e r . i n s e r t B e f o r e ( t h i s . s e a r c h M e s s a g e s L i s t , t h i s . l i s t s C o n t a i n e r . l a s t E l e m e n t C h i l d ) ;
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 . searchContainer . append ( this . listsContainer ) ;
//this.searchContainer.append(this.listsContainer);
appDialogsManager . setListClickListener ( this . searchMessagesList ) ;
@ -85,7 +105,7 @@ class AppSidebarLeft {
@@ -85,7 +105,7 @@ class AppSidebarLeft {
this . log ( 'input' , value ) ;
if ( this . listsContainer . contains ( this . searchMessagesList ) ) {
this . listsContainer . removeChild ( this . searchMessagesList )
this . listsContainer . removeChild ( this . searchMessagesList ) ;
}
if ( ! value . trim ( ) ) {
@ -115,11 +135,63 @@ class AppSidebarLeft {
@@ -115,11 +135,63 @@ class AppSidebarLeft {
} ) ;
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() {
if ( ! this . query . trim ( ) ) return ;
let elements = Array . from ( this . searchMessagesList . childNodes ) . slice ( - 5 ) ;
for ( let li of elements ) {
if ( isElementInViewport ( li ) ) {
@ -150,6 +222,8 @@ class AppSidebarLeft {
@@ -150,6 +222,8 @@ class AppSidebarLeft {
let query = this . query ;
if ( ! query . trim ( ) ) return ;
if ( this . loadedCount != 0 && this . loadedCount >= this . foundCount ) {
return Promise . resolve ( ) ;
}