@ -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 {
@ -12,13 +11,22 @@ class AppSidebarLeft {
@@ -12,13 +11,22 @@ class AppSidebarLeft {
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 ;
private menuEl = this . toolsBtn . querySelector ( '.btn-menu' ) ;
private savedBtn = this . menuEl . querySelector ( '.menu-saved' ) ;
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 ;
@ -29,31 +37,43 @@ class AppSidebarLeft {
@@ -29,31 +37,43 @@ class AppSidebarLeft {
private searchPromise : Promise < void > = null ;
private searchTimeout : number = 0 ;
private query = '' ;
public myID = 0 ;
private query = '' ;
constructor ( ) {
this . listsContainer = scrollable ( this . searchContainer ) ;
this . searchMessagesList = document . createElement ( 'ul ') ;
apiManager . getUserID ( ) . then ( ( id ) = > {
this . myID = id ;
} ) ;
$rootScope . $on ( 'user_auth' , ( e : CustomEvent ) = > {
let userAuth = e . detai l ;
this . myID = userAuth ? userAuth.id : 0 ;
} ) ;
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 ) ;
let { container : chatsScroll , hiddenElements : chatsHidden , onScroll : hiddenScroll } = scrollable ( this . chatsContainer as HTMLDivElement ) ;
this . chatsScroll = chatsScrol l;
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,13 +105,13 @@ class AppSidebarLeft {
@@ -85,13 +105,13 @@ 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 ( ) ) {
return ;
}
this . query = value ;
this . minMsgID = 0 ;
this . loadedCount = 0 ;
@ -113,18 +133,70 @@ class AppSidebarLeft {
@@ -113,18 +133,70 @@ class AppSidebarLeft {
this . peerID = 0 ;
}
} ) ;
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 ) ) {
this . log ( 'Will load more search' ) ;
if ( ! this . searchTimeout ) {
this . searchTimeout = setTimeout ( ( ) = > {
this . searchMore ( ) ;
@ -144,21 +216,23 @@ class AppSidebarLeft {
@@ -144,21 +216,23 @@ class AppSidebarLeft {
this . searchInput . focus ( ) ;
}
private searchMore() {
if ( this . searchPromise ) return this . searchPromise ;
let query = this . query ;
if ( ! query . trim ( ) ) return ;
if ( this . loadedCount != 0 && this . loadedCount >= this . foundCount ) {
return Promise . resolve ( ) ;
}
let maxID = appMessagesIDsManager . getMessageIDInfo ( this . minMsgID ) [ 0 ] ;
return this . searchPromise = appMessagesManager . getSearch ( this . peerID , query , null , maxID , 20 , this . offsetRate ) . then ( res = > {
this . searchPromise = null ;
if ( this . searchInput . value != query ) {
return ;
}
@ -166,7 +240,7 @@ class AppSidebarLeft {
@@ -166,7 +240,7 @@ class AppSidebarLeft {
this . log ( 'input search result:' , this . peerID , query , null , maxID , 20 , res ) ;
let { count , history , next_rate } = res ;
if ( history [ 0 ] == this . minMsgID ) {
history . shift ( ) ;
}
@ -177,7 +251,7 @@ class AppSidebarLeft {
@@ -177,7 +251,7 @@ class AppSidebarLeft {
if ( ! originalDialog ) {
this . log ( 'no original dialog by message:' , message ) ;
originalDialog = {
peerID : message.peerID ,
pFlags : { } ,
@ -188,11 +262,11 @@ class AppSidebarLeft {
@@ -188,11 +262,11 @@ class AppSidebarLeft {
let { dialog , dom } = appDialogsManager . addDialog ( originalDialog , this . searchMessagesList , false ) ;
appDialogsManager . setLastMessage ( dialog , message , dom ) ;
} ) ;
this . minMsgID = history [ history . length - 1 ] ;
this . offsetRate = next_rate ;
this . loadedCount += history . length ;
if ( ! this . foundCount ) {
this . foundCount = count ;
}