@ -1,12 +1,17 @@
import Scrollable from "./scrollable_new" ;
import Scrollable from "./scrollable_new" ;
import appMessagesManager , { Dialog } from "../lib/appManagers/appMessagesManager" ;
import appMessagesManager , { Dialog } from "../lib/appManagers/appMessagesManager" ;
import { $rootScope , cancelEvent , findUpClassName , findUpTag , findUp Attribute } from "../lib/utils" ;
import { $rootScope , cancelEvent , findUpClassName , findUpAttribute } from "../lib/utils" ;
import appDialogsManager from "../lib/appManagers/appDialogsManager" ;
import appDialogsManager from "../lib/appManagers/appDialogsManager" ;
import appChatsManager from "../lib/appManagers/appChatsManager" ;
import appChatsManager from "../lib/appManagers/appChatsManager" ;
import appUsersManager from "../lib/appManagers/appUsersManager" ;
import appUsersManager from "../lib/appManagers/appUsersManager" ;
import appPeersManager from "../lib/appManagers/appPeersManager" ;
import appPeersManager from "../lib/appManagers/appPeersManager" ;
import appPhotosManager from "../lib/appManagers/appPhotosManager" ;
import appPhotosManager from "../lib/appManagers/appPhotosManager" ;
type PeerType = 'contacts' | 'dialogs' ;
// TODO: правильная сортировка для addMembers, т.е. для peerType: 'contacts', потому что там идут сначала контакты - потом неконтакты, а должно всё сортироваться по имени
let loadedAllDialogs = false ;
export class AppSelectPeers {
export class AppSelectPeers {
public container = document . createElement ( 'div' ) ;
public container = document . createElement ( 'div' ) ;
public list = document . createElement ( 'ul' ) ;
public list = document . createElement ( 'ul' ) ;
@ -22,8 +27,6 @@ export class AppSelectPeers {
public freezed = false ;
public freezed = false ;
private myID = $rootScope . myID ;
private folderID = 0 ;
private folderID = 0 ;
private offsetIndex = 0 ;
private offsetIndex = 0 ;
private promise : Promise < any > ;
private promise : Promise < any > ;
@ -33,7 +36,7 @@ export class AppSelectPeers {
private loadedWhat : Partial < { [ k in 'dialogs' | 'archived' | 'contacts' ] : true } > = { } ;
private loadedWhat : Partial < { [ k in 'dialogs' | 'archived' | 'contacts' ] : true } > = { } ;
constructor ( private appendTo : HTMLElement , private onChange ? : ( length : number ) = > void , private peerType : 'contacts' | 'dialogs' | 'both' = 'dialogs' , onFirstRender ? : ( ) = > void , private renderResultsFunc ? : ( peerIDs : number [ ] ) = > void ) {
constructor ( private appendTo : HTMLElement , private onChange ? : ( length : number ) = > void , private peerType : PeerType [ ] = [ 'dialogs' ] , onFirstRender ? : ( ) = > void , private renderResultsFunc ? : ( peerIDs : number [ ] ) = > void ) {
this . container . classList . add ( 'selector' ) ;
this . container . classList . add ( 'selector' ) ;
if ( ! this . renderResultsFunc ) {
if ( ! this . renderResultsFunc ) {
@ -44,7 +47,7 @@ export class AppSelectPeers {
topContainer . classList . add ( 'selector-search-container' ) ;
topContainer . classList . add ( 'selector-search-container' ) ;
this . selectedContainer . classList . add ( 'selector-search' ) ;
this . selectedContainer . classList . add ( 'selector-search' ) ;
this . input . placeholder = peerType == 'contacts' ? 'Add People...' : 'Select chat' ;
this . input . placeholder = ! peerType . includes ( 'dialogs' ) ? 'Add People...' : 'Select chat' ;
this . input . type = 'text' ;
this . input . type = 'text' ;
this . selectedContainer . append ( this . input ) ;
this . selectedContainer . append ( this . input ) ;
topContainer . append ( this . selectedContainer ) ;
topContainer . append ( this . selectedContainer ) ;
@ -94,27 +97,23 @@ export class AppSelectPeers {
} ) ;
} ) ;
this . input . addEventListener ( 'input' , ( ) = > {
this . input . addEventListener ( 'input' , ( ) = > {
le t value = this . input . value ;
cons t value = this . input . value ;
if ( this . query != value ) {
if ( this . query != value ) {
if ( this . peerType == 'contacts' || this . peerType == 'both' ) {
if ( this . peerType . includes ( 'contacts' ) ) {
delete this . loadedWhat . contacts ;
delete this . loadedWhat . contacts ;
this . cachedContacts = null ;
this . cachedContacts = null ;
}
}
if ( this . peerType == 'dialogs' || this . peerType == 'both' ) {
//if(this.peerType.includes('dialogs')) {
delete this . loadedWhat . dialogs ;
delete this . loadedWhat . dialogs ;
delete this . loadedWhat . archived ;
delete this . loadedWhat . archived ;
this . folderID = 0 ;
this . folderID = 0 ;
this . offsetIndex = 0 ;
this . offsetIndex = 0 ;
}
//}
this . promise = null ;
this . promise = null ;
this . list . innerHTML = '' ;
this . list . innerHTML = '' ;
this . query = value ;
this . query = value ;
if ( this . query && 'saved messages' . includes ( this . query . toLowerCase ( ) ) ) {
this . renderResultsFunc ( [ $rootScope . myID ] ) ;
}
//console.log('selectPeers input:', this.query);
//console.log('selectPeers input:', this.query);
this . getMoreResults ( ) ;
this . getMoreResults ( ) ;
@ -151,15 +150,20 @@ export class AppSelectPeers {
this . promise = appMessagesManager . getConversations ( this . query , this . offsetIndex , pageCount , this . folderID ) ;
this . promise = appMessagesManager . getConversations ( this . query , this . offsetIndex , pageCount , this . folderID ) ;
const value = await this . promise ;
const value = await this . promise ;
this . promise = null ;
let dialogs = value . dialogs as Dialog [ ] ;
let dialogs = value . dialogs as Dialog [ ] ;
if ( dialogs . length ) {
if ( dialogs . length ) {
const newOffsetIndex = dialogs [ dialogs . length - 1 ] . index || 0 ;
const newOffsetIndex = dialogs [ dialogs . length - 1 ] . index || 0 ;
dialogs = dialogs . filter ( d = > d . peerID != this . myID ) ;
dialogs = dialogs . slice ( ) ;
if ( ! this . offsetIndex && this . folderID == 0 && ! this . query ) {
dialogs . findAndSplice ( d = > d . peerID == $rootScope . myID ) ; // no my account
if ( ! this . offsetIndex && this . folderID == 0 &&
( ! this . query || 'saved messages' . includes ( this . query . toLowerCase ( ) ) ) &&
this . peerType . includes ( 'dialogs' ) ) {
dialogs . unshift ( {
dialogs . unshift ( {
peerID : this.myID ,
peerID : $rootScope .myID,
pFlags : { }
pFlags : { }
} as any ) ;
} as any ) ;
}
}
@ -173,19 +177,15 @@ export class AppSelectPeers {
this . offsetIndex = 0 ;
this . offsetIndex = 0 ;
this . folderID = 1 ;
this . folderID = 1 ;
this . promise = null ;
return this . getMoreDialogs ( ) ;
return this . getMoreDialogs ( ) ;
} else {
} else {
this . loadedWhat . archived = true ;
this . loadedWhat . archived = true ;
if ( ! this . loadedWhat . contacts && this . peerType == 'both' ) {
if ( ! this . loadedWhat . contacts && this . peerType . includes ( 'contacts' ) ) {
this . promise = null ;
return this . getMoreContacts ( ) ;
return this . getMoreContacts ( ) ;
}
}
}
}
}
}
this . promise = null ;
}
}
private async getMoreContacts() {
private async getMoreContacts() {
@ -196,9 +196,16 @@ export class AppSelectPeers {
}
}
if ( ! this . cachedContacts ) {
if ( ! this . cachedContacts ) {
/ * c o n s t p r o m i s e s : P r o m i s e < a n y > [ ] = [ a p p U s e r s M a n a g e r . g e t C o n t a c t s ( t h i s . q u e r y ) ] ;
if ( ! this . peerType . includes ( 'dialogs' ) ) {
promises . push ( appMessagesManager . getConversationsAll ( ) ) ;
}
this . promise = Promise . all ( promises ) ;
this . cachedContacts = ( await this . promise ) [ 0 ] . slice ( ) ; * /
this . promise = appUsersManager . getContacts ( this . query ) ;
this . promise = appUsersManager . getContacts ( this . query ) ;
this . cachedContacts = ( await this . promise ) . slice ( ) ;
this . cachedContacts = ( await this . promise ) . slice ( ) ;
this . cachedContacts . findAndSplice ( userID = > userID == this . myID ) ; // no my account
this . cachedContacts . findAndSplice ( userID = > userID == $rootScope . myID ) ; // no my account
this . promise = null ;
this . promise = null ;
}
}
@ -206,14 +213,26 @@ export class AppSelectPeers {
const pageCount = appPhotosManager . windowH / 72 * 1.25 | 0 ;
const pageCount = appPhotosManager . windowH / 72 * 1.25 | 0 ;
const arr = this . cachedContacts . splice ( 0 , pageCount ) ;
const arr = this . cachedContacts . splice ( 0 , pageCount ) ;
this . renderResultsFunc ( arr ) ;
this . renderResultsFunc ( arr ) ;
} else {
}
if ( ! this . cachedContacts . length ) {
this . loadedWhat . contacts = true ;
this . loadedWhat . contacts = true ;
// need to load non-contacts
if ( ! this . peerType . includes ( 'dialogs' ) ) {
return this . getMoreDialogs ( ) ;
}
}
}
}
}
private getMoreResults() {
private getMoreResults() {
const promises : Promise < any > [ ] = [ ] ;
const promises : Promise < any > [ ] = [ ] ;
if ( this . peerType == 'dialogs' || this . peerType == 'both' ) {
if ( ! loadedAllDialogs ) {
promises . push ( appMessagesManager . getConversationsAll ( ) ) ;
}
if ( ( this . peerType . includes ( 'dialogs' ) || this . loadedWhat . contacts ) && ! this . loadedWhat . archived ) { // to load non-contacts
promises . push ( this . getMoreDialogs ( ) ) ;
promises . push ( this . getMoreDialogs ( ) ) ;
if ( ! this . loadedWhat . archived ) {
if ( ! this . loadedWhat . archived ) {
@ -221,7 +240,7 @@ export class AppSelectPeers {
}
}
}
}
if ( this . peerType == 'contacts' || this . peerType == 'both' ) {
if ( this . peerType . includes ( 'contacts' ) && ! this . loadedWhat . contacts ) {
promises . push ( this . getMoreContacts ( ) ) ;
promises . push ( this . getMoreContacts ( ) ) ;
}
}
@ -230,6 +249,14 @@ export class AppSelectPeers {
private renderResults ( peerIDs : number [ ] ) {
private renderResults ( peerIDs : number [ ] ) {
//console.log('will renderResults:', peerIDs);
//console.log('will renderResults:', peerIDs);
// оставим только неконтакты с диалогов
if ( ! this . peerType . includes ( 'dialogs' ) && this . loadedWhat . contacts ) {
peerIDs = peerIDs . filter ( peerID = > {
return appUsersManager . isNonContactUser ( peerID ) ;
} ) ;
}
peerIDs . forEach ( peerID = > {
peerIDs . forEach ( peerID = > {
const { dom } = appDialogsManager . addDialog ( peerID , this . scrollable , false , false ) ;
const { dom } = appDialogsManager . addDialog ( peerID , this . scrollable , false , false ) ;
@ -240,7 +267,7 @@ export class AppSelectPeers {
let subtitle = '' ;
let subtitle = '' ;
if ( peerID < 0 ) {
if ( peerID < 0 ) {
subtitle = appChatsManager . getChatMembersString ( - peerID ) ;
subtitle = appChatsManager . getChatMembersString ( - peerID ) ;
} else if ( peerID == this . myID ) {
} else if ( peerID == $rootScope . myID ) {
subtitle = 'chat with yourself' ;
subtitle = 'chat with yourself' ;
} else {
} else {
subtitle = appUsersManager . getUserStatusString ( peerID ) ;
subtitle = appUsersManager . getUserStatusString ( peerID ) ;