2021-04-08 15:52:31 +02:00
/ *
* https : //github.com/morethanwords/tweb
* Copyright ( C ) 2019 - 2021 Eduard Kuzmenko
* https : //github.com/morethanwords/tweb/blob/master/LICENSE
* /
2020-10-07 22:49:52 +02:00
import appChatsManager , { ChatRights } from "../lib/appManagers/appChatsManager" ;
2020-10-11 23:21:42 +02:00
import appDialogsManager from "../lib/appManagers/appDialogsManager" ;
import appMessagesManager , { Dialog } from "../lib/appManagers/appMessagesManager" ;
import appUsersManager from "../lib/appManagers/appUsersManager" ;
2020-11-15 04:33:47 +01:00
import rootScope from "../lib/rootScope" ;
2020-10-11 23:21:42 +02:00
import Scrollable from "./scrollable" ;
2021-01-07 13:36:21 +01:00
import { FocusDirection } from "../helpers/fastSmoothScroll" ;
2021-03-08 23:15:44 +01:00
import CheckboxField from "./checkboxField" ;
2021-03-19 13:14:42 +01:00
import appProfileManager from "../lib/appManagers/appProfileManager" ;
2021-03-21 15:36:14 +01:00
import { safeAssign } from "../helpers/object" ;
2021-03-28 20:37:11 +02:00
import { i18n , LangPackKey , _i18n } from "../lib/langPack" ;
2021-04-04 17:39:17 +02:00
import findUpAttribute from "../helpers/dom/findUpAttribute" ;
import findUpClassName from "../helpers/dom/findUpClassName" ;
2021-04-10 20:29:58 +02:00
import PeerTitle from "./peerTitle" ;
2021-05-08 19:46:50 +02:00
import { cancelEvent } from "../helpers/dom/cancelEvent" ;
import replaceContent from "../helpers/dom/replaceContent" ;
2021-10-21 15:16:43 +02:00
import { filterUnique , indexOfAndSplice } from "../helpers/array" ;
2021-07-16 14:47:30 +02:00
import debounce from "../helpers/schedulers/debounce" ;
2021-08-26 19:01:59 +02:00
import windowSize from "../helpers/windowSize" ;
2020-05-09 14:02:07 +02:00
2021-03-19 13:14:42 +01:00
type PeerType = 'contacts' | 'dialogs' | 'channelParticipants' ;
2020-08-31 18:48:46 +02:00
// TODO: правильная сортировка для addMembers, т.е . для peerType: 'contacts', потому что там идут сначала контакты - потом неконтакты, а должно всё сортироваться по имени
2020-10-20 01:39:36 +02:00
export default class AppSelectPeers {
2020-05-09 14:02:07 +02:00
public container = document . createElement ( 'div' ) ;
2021-03-19 13:14:42 +01:00
public list = appDialogsManager . createChatList ( / * {
handheldsSize : 66 ,
avatarSize : 48
} * / ) ;
2020-06-19 13:49:55 +02:00
public chatsContainer = document . createElement ( 'div' ) ;
public scrollable : Scrollable ;
public selectedScrollable : Scrollable ;
2020-05-09 14:02:07 +02:00
2020-10-20 01:39:36 +02:00
public selectedContainer : HTMLElement ;
public input : HTMLInputElement ;
2020-05-09 14:02:07 +02:00
2021-10-21 15:16:43 +02:00
//public selected: {[peerId: PeerId]: HTMLElement} = {};
public selected = new Set < PeerId | string > ( ) ;
2020-05-09 14:02:07 +02:00
public freezed = false ;
2020-12-11 03:06:16 +01:00
private folderId = 0 ;
2020-05-09 14:02:07 +02:00
private offsetIndex = 0 ;
2020-05-30 08:44:54 +02:00
private promise : Promise < any > ;
2020-05-09 14:02:07 +02:00
private query = '' ;
2021-10-21 15:16:43 +02:00
private cachedContacts : PeerId [ ] ;
2020-08-30 23:50:25 +02:00
2021-03-19 13:14:42 +01:00
private loadedWhat : Partial < { [ k in 'dialogs' | 'archived' | 'contacts' | 'channelParticipants' ] : true } > = { } ;
2020-10-21 01:25:36 +02:00
2021-10-21 15:16:43 +02:00
private renderedPeerIds : Set < PeerId > = new Set ( ) ;
2021-01-07 13:36:21 +01:00
private appendTo : HTMLElement ;
private onChange : ( length : number ) = > void ;
private peerType : PeerType [ ] = [ 'dialogs' ] ;
2021-10-21 15:16:43 +02:00
private renderResultsFunc : ( peerIds : PeerId [ ] ) = > void ;
2021-03-02 16:34:02 +01:00
private chatRightsAction : ChatRights ;
2021-01-07 13:36:21 +01:00
private multiSelect = true ;
2021-01-20 02:38:59 +01:00
private rippleEnabled = true ;
2021-03-19 13:14:42 +01:00
private avatarSize = 48 ;
private tempIds : { [ k in keyof AppSelectPeers [ 'loadedWhat' ] ] : number } = { } ;
2021-10-21 15:16:43 +02:00
private peerId : PeerId ;
2021-03-25 19:07:00 +01:00
private placeholder : LangPackKey ;
2021-07-08 08:07:32 +02:00
2021-07-16 15:07:53 +02:00
private selfPresence : LangPackKey = 'Presence.YourChat' ;
2021-07-16 14:47:30 +02:00
private needSwitchList = false ;
2020-05-09 14:02:07 +02:00
2021-01-07 13:36:21 +01:00
constructor ( options : {
appendTo : AppSelectPeers [ 'appendTo' ] ,
onChange? : AppSelectPeers [ 'onChange' ] ,
peerType? : AppSelectPeers [ 'peerType' ] ,
2021-03-19 13:14:42 +01:00
peerId? : number ,
2021-01-07 13:36:21 +01:00
onFirstRender ? : ( ) = > void ,
renderResultsFunc? : AppSelectPeers [ 'renderResultsFunc' ] ,
chatRightsAction? : AppSelectPeers [ 'chatRightsAction' ] ,
2021-01-20 02:38:59 +01:00
multiSelect? : AppSelectPeers [ 'multiSelect' ] ,
2021-03-19 13:14:42 +01:00
rippleEnabled? : boolean ,
avatarSize? : AppSelectPeers [ 'avatarSize' ] ,
2021-07-08 08:07:32 +02:00
placeholder? : LangPackKey ,
selfPresence? : LangPackKey
2021-01-07 13:36:21 +01:00
} ) {
2021-03-21 15:36:14 +01:00
safeAssign ( this , options ) ;
2021-01-07 13:36:21 +01:00
2020-05-09 14:02:07 +02:00
this . container . classList . add ( 'selector' ) ;
2021-01-07 13:36:21 +01:00
const f = ( this . renderResultsFunc || this . renderResults ) . bind ( this ) ;
2021-10-21 15:16:43 +02:00
this . renderResultsFunc = ( peerIds : PeerId [ ] ) = > {
2021-07-16 14:47:30 +02:00
if ( this . needSwitchList ) {
2021-03-19 13:14:42 +01:00
this . scrollable . splitUp . replaceWith ( this . list ) ;
this . scrollable . setVirtualContainer ( this . list ) ;
2021-07-16 14:47:30 +02:00
this . needSwitchList = false ;
2021-03-19 13:14:42 +01:00
}
2020-12-11 03:06:16 +01:00
peerIds = peerIds . filter ( peerId = > {
const notRendered = ! this . renderedPeerIds . has ( peerId ) ;
if ( notRendered ) this . renderedPeerIds . add ( peerId ) ;
2020-10-21 01:25:36 +02:00
return notRendered ;
} ) ;
2020-12-11 03:06:16 +01:00
return f ( peerIds ) ;
2020-10-21 01:25:36 +02:00
} ;
2020-06-19 13:49:55 +02:00
2020-10-20 01:39:36 +02:00
this . input = document . createElement ( 'input' ) ;
this . input . classList . add ( 'selector-search-input' ) ;
2021-03-25 19:07:00 +01:00
if ( this . placeholder ) {
_i18n ( this . input , this . placeholder , undefined , 'placeholder' ) ;
} else {
2021-03-27 16:34:08 +01:00
_i18n ( this . input , 'SendMessageTo' , undefined , 'placeholder' ) ;
2021-03-25 19:07:00 +01:00
}
2020-05-09 14:02:07 +02:00
this . input . type = 'text' ;
2021-01-07 13:36:21 +01:00
if ( this . multiSelect ) {
2020-10-20 01:39:36 +02:00
let topContainer = document . createElement ( 'div' ) ;
topContainer . classList . add ( 'selector-search-container' ) ;
this . selectedContainer = document . createElement ( 'div' ) ;
this . selectedContainer . classList . add ( 'selector-search' ) ;
this . selectedContainer . append ( this . input ) ;
topContainer . append ( this . selectedContainer ) ;
this . selectedScrollable = new Scrollable ( topContainer ) ;
let delimiter = document . createElement ( 'hr' ) ;
this . selectedContainer . addEventListener ( 'click' , ( e ) = > {
if ( this . freezed ) return ;
let target = e . target as HTMLElement ;
target = findUpClassName ( target , 'selector-user' ) ;
if ( ! target ) return ;
2020-12-11 03:06:16 +01:00
const peerId = target . dataset . key ;
2021-01-12 12:22:00 +01:00
const li = this . chatsContainer . querySelector ( '[data-peer-id="' + peerId + '"]' ) as HTMLElement ;
2020-10-20 01:39:36 +02:00
if ( ! li ) {
2021-10-21 15:16:43 +02:00
this . remove ( peerId . toPeerId ( ) ) ;
2020-10-20 01:39:36 +02:00
} else {
li . click ( ) ;
}
} ) ;
this . container . append ( topContainer , delimiter ) ;
}
2020-05-09 14:02:07 +02:00
2020-12-01 16:38:36 +01:00
this . chatsContainer . classList . add ( 'chatlist-container' ) ;
2020-05-09 14:02:07 +02:00
this . chatsContainer . append ( this . list ) ;
this . scrollable = new Scrollable ( this . chatsContainer ) ;
2020-05-30 08:44:54 +02:00
this . scrollable . setVirtualContainer ( this . list ) ;
2020-05-09 14:02:07 +02:00
2020-06-19 13:49:55 +02:00
this . chatsContainer . addEventListener ( 'click' , ( e ) = > {
2021-01-12 12:22:00 +01:00
const target = findUpAttribute ( e . target , 'data-peer-id' ) as HTMLElement ;
2020-05-09 14:02:07 +02:00
cancelEvent ( e ) ;
if ( ! target ) return ;
2020-06-19 13:49:55 +02:00
if ( this . freezed ) return ;
2020-05-09 14:02:07 +02:00
2021-10-21 15:16:43 +02:00
let key : PeerId | string = target . dataset . peerId ;
key = key . isPeerId ( ) ? key . toPeerId ( ) : key ;
2020-10-20 01:39:36 +02:00
if ( ! this . multiSelect ) {
this . add ( key ) ;
return ;
}
2021-04-20 20:00:38 +02:00
//target.classList.toggle('active');
2020-06-19 13:49:55 +02:00
if ( this . selected . has ( key ) ) {
this . remove ( key ) ;
2020-05-09 14:02:07 +02:00
} else {
2020-06-19 13:49:55 +02:00
this . add ( key ) ;
2020-05-09 14:02:07 +02:00
}
2020-06-19 13:49:55 +02:00
const checkbox = target . querySelector ( 'input' ) as HTMLInputElement ;
2020-05-09 14:02:07 +02:00
checkbox . checked = ! checkbox . checked ;
} ) ;
2021-07-16 14:47:30 +02:00
const debouncedInput = debounce ( this . onInput , 200 , false , true ) ;
this . input . addEventListener ( 'input' , debouncedInput ) ;
2020-05-09 14:02:07 +02:00
this . scrollable . onScrolledBottom = ( ) = > {
this . getMoreResults ( ) ;
} ;
2020-10-20 01:39:36 +02:00
this . container . append ( this . chatsContainer ) ;
2021-01-07 13:36:21 +01:00
this . appendTo . append ( this . container ) ;
2020-05-09 14:02:07 +02:00
2020-06-19 13:49:55 +02:00
// WARNING TIMEOUT
setTimeout ( ( ) = > {
let getResultsPromise = this . getMoreResults ( ) as Promise < any > ;
2021-01-07 13:36:21 +01:00
if ( options . onFirstRender ) {
2020-06-19 13:49:55 +02:00
getResultsPromise . then ( ( ) = > {
2021-01-07 13:36:21 +01:00
options . onFirstRender ( ) ;
2020-06-19 13:49:55 +02:00
} ) ;
}
} , 0 ) ;
2020-05-09 14:02:07 +02:00
}
2021-07-16 14:47:30 +02:00
private onInput = ( ) = > {
const value = this . input . value ;
if ( this . query !== value ) {
2021-10-21 15:16:43 +02:00
if ( this . peerType . includes ( 'contacts' ) || this . peerType . includes ( 'dialogs' ) ) {
2021-07-16 14:47:30 +02:00
this . cachedContacts = null ;
}
if ( this . peerType . includes ( 'dialogs' ) ) {
this . folderId = 0 ;
this . offsetIndex = 0 ;
}
for ( let i in this . tempIds ) {
// @ts-ignore
++ this . tempIds [ i ] ;
}
this . list = appDialogsManager . createChatList ( ) ;
this . promise = null ;
this . loadedWhat = { } ;
this . query = value ;
this . renderedPeerIds . clear ( ) ;
this . needSwitchList = true ;
//console.log('selectPeers input:', this.query);
this . getMoreResults ( ) ;
}
} ;
2020-10-07 15:57:33 +02:00
private renderSaved() {
2021-02-04 01:30:23 +01:00
if ( ! this . offsetIndex && this . folderId === 0 && this . peerType . includes ( 'dialogs' ) && ( ! this . query || appUsersManager . testSelfSearch ( this . query ) ) ) {
2020-12-11 03:06:16 +01:00
this . renderResultsFunc ( [ rootScope . myId ] ) ;
2020-10-07 15:57:33 +02:00
}
}
2021-03-19 15:27:02 +01:00
private getTempId ( type : keyof AppSelectPeers [ 'tempIds' ] ) {
if ( this . tempIds [ type ] === undefined ) {
this . tempIds [ type ] = 0 ;
}
return ++ this . tempIds [ type ] ;
}
2020-08-30 23:50:25 +02:00
private async getMoreDialogs ( ) : Promise < any > {
2020-05-30 08:44:54 +02:00
if ( this . promise ) return this . promise ;
2020-08-30 23:50:25 +02:00
if ( this . loadedWhat . dialogs && this . loadedWhat . archived ) {
return ;
}
2020-05-30 08:44:54 +02:00
2020-05-09 14:02:07 +02:00
// в десктопе - сначала без группы, потом архивные, потом контакты без сообщений
2021-08-26 19:01:59 +02:00
const pageCount = windowSize . windowH / 72 * 1.25 | 0 ;
2020-05-09 14:02:07 +02:00
2021-03-19 15:27:02 +01:00
const tempId = this . getTempId ( 'dialogs' ) ;
2021-09-03 18:33:10 +02:00
const promise = appMessagesManager . getConversations ( this . query , this . offsetIndex , pageCount , this . folderId , true ) . promise ;
2021-07-16 14:47:30 +02:00
this . promise = promise ;
const value = await promise ;
2021-03-19 13:14:42 +01:00
if ( this . tempIds . dialogs !== tempId ) {
return ;
}
2021-07-16 14:47:30 +02:00
this . promise = null ;
2020-05-30 08:44:54 +02:00
let dialogs = value . dialogs as Dialog [ ] ;
2020-08-30 23:50:25 +02:00
if ( dialogs . length ) {
const newOffsetIndex = dialogs [ dialogs . length - 1 ] . index || 0 ;
2020-08-31 18:48:46 +02:00
dialogs = dialogs . slice ( ) ;
2021-02-04 01:30:23 +01:00
dialogs . findAndSplice ( d = > d . peerId === rootScope . myId ) ; // no my account
2020-08-31 18:48:46 +02:00
2020-10-07 22:49:52 +02:00
if ( this . chatRightsAction ) {
2021-07-16 14:47:30 +02:00
dialogs = dialogs . filter ( d = > this . filterByRights ( d . peerId ) ) ;
2020-10-07 22:49:52 +02:00
}
2020-10-07 15:57:33 +02:00
this . renderSaved ( ) ;
2020-05-30 08:44:54 +02:00
2020-08-30 23:50:25 +02:00
this . offsetIndex = newOffsetIndex ;
2021-07-16 14:47:30 +02:00
}
2021-10-21 15:16:43 +02:00
this . renderResultsFunc ( dialogs . map ( dialog = > dialog . peerId ) ) ;
2021-07-16 14:47:30 +02:00
if ( value . isEnd ) {
2020-08-30 23:50:25 +02:00
if ( ! this . loadedWhat . dialogs ) {
2020-10-07 15:57:33 +02:00
this . renderSaved ( ) ;
2020-08-30 23:50:25 +02:00
this . loadedWhat . dialogs = true ;
this . offsetIndex = 0 ;
2020-12-11 03:06:16 +01:00
this . folderId = 1 ;
2020-08-30 23:50:25 +02:00
return this . getMoreDialogs ( ) ;
} else {
this . loadedWhat . archived = true ;
2021-10-21 15:16:43 +02:00
if ( ! this . loadedWhat . contacts /* && this.peerType.includes('contacts') */ ) {
2020-08-30 23:50:25 +02:00
return this . getMoreContacts ( ) ;
}
}
}
2020-05-09 14:02:07 +02:00
}
2021-10-21 15:16:43 +02:00
private filterByRights ( peerId : PeerId ) {
2021-07-16 14:47:30 +02:00
return (
2021-10-21 15:16:43 +02:00
peerId . isUser ( ) &&
2021-07-16 14:47:30 +02:00
( this . chatRightsAction !== 'send_messages' || appUsersManager . canSendToUser ( peerId ) )
2021-10-21 15:16:43 +02:00
) || appChatsManager . hasRights ( peerId . toChatId ( ) , this . chatRightsAction ) ;
2021-07-16 14:47:30 +02:00
}
2020-05-09 14:02:07 +02:00
private async getMoreContacts() {
if ( this . promise ) return this . promise ;
2020-08-30 23:50:25 +02:00
if ( this . loadedWhat . contacts ) {
return ;
}
2021-10-21 15:16:43 +02:00
const isGlobalSearch = this . peerType . includes ( 'contacts' ) ;
2020-05-09 14:02:07 +02:00
if ( ! this . cachedContacts ) {
2020-08-31 18:48:46 +02:00
/ * 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 ( ) ; * /
2021-03-19 15:27:02 +01:00
const tempId = this . getTempId ( 'contacts' ) ;
2021-07-16 14:47:30 +02:00
const promise = Promise . all ( [
2021-10-21 15:16:43 +02:00
isGlobalSearch ? appUsersManager . getContactsPeerIds ( this . query ) : [ ] ,
2021-07-16 14:47:30 +02:00
this . query ? appUsersManager . searchContacts ( this . query ) : undefined
] ) ;
this . promise = promise ;
2021-10-21 15:16:43 +02:00
let [ cachedContacts , searchResult ] = await promise ;
2021-03-19 13:14:42 +01:00
if ( this . tempIds . contacts !== tempId ) {
return ;
}
2021-07-16 14:47:30 +02:00
if ( searchResult ) {
2021-10-21 15:16:43 +02:00
// do not add global result if only dialogs needed
let resultPeerIds = isGlobalSearch ? searchResult . my_results . concat ( searchResult . results ) : searchResult . my_results ;
2021-07-16 14:47:30 +02:00
if ( this . chatRightsAction ) {
resultPeerIds = resultPeerIds . filter ( peerId = > this . filterByRights ( peerId ) ) ;
}
if ( ! this . peerType . includes ( 'dialogs' ) ) {
2021-10-21 15:16:43 +02:00
resultPeerIds = resultPeerIds . filter ( peerId = > peerId . isUser ( ) ) ;
2021-07-16 14:47:30 +02:00
}
this . cachedContacts = filterUnique ( cachedContacts . concat ( resultPeerIds ) ) ;
} else this . cachedContacts = cachedContacts . slice ( ) ;
2021-10-21 15:16:43 +02:00
indexOfAndSplice ( this . cachedContacts , rootScope . myId ) ; // no my account
2020-05-09 14:02:07 +02:00
this . promise = null ;
}
2021-10-21 15:16:43 +02:00
// if(this.cachedContacts.length) {
2021-08-26 19:01:59 +02:00
const pageCount = windowSize . windowH / 72 * 1.25 | 0 ;
2020-05-30 08:44:54 +02:00
const arr = this . cachedContacts . splice ( 0 , pageCount ) ;
2020-06-19 13:49:55 +02:00
this . renderResultsFunc ( arr ) ;
2021-10-21 15:16:43 +02:00
// }
2020-08-31 18:48:46 +02:00
if ( ! this . cachedContacts . length ) {
2020-08-30 23:50:25 +02:00
this . loadedWhat . contacts = true ;
2020-08-31 18:48:46 +02:00
// need to load non-contacts
2021-07-15 17:34:34 +02:00
/ * i f ( ! t h i s . p e e r T y p e . i n c l u d e s ( ' d i a l o g s ' ) ) {
2020-08-31 18:48:46 +02:00
return this . getMoreDialogs ( ) ;
2021-07-15 17:34:34 +02:00
} * /
2020-05-09 14:02:07 +02:00
}
}
2021-03-19 13:14:42 +01:00
private async getMoreChannelParticipants() {
if ( this . promise ) return this . promise ;
if ( this . loadedWhat . channelParticipants ) {
return ;
}
const pageCount = 50 ; // same as in group permissions to use cache
2021-03-19 15:27:02 +01:00
const tempId = this . getTempId ( 'channelParticipants' ) ;
2021-10-21 15:16:43 +02:00
const promise = appProfileManager . getChannelParticipants ( this . peerId . toChatId ( ) , { _ : 'channelParticipantsSearch' , q : this.query } , pageCount , this . list . childElementCount ) ;
2021-03-19 13:14:42 +01:00
const participants = await promise ;
if ( this . tempIds . channelParticipants !== tempId ) {
return ;
}
2021-05-03 22:02:53 +02:00
const peerIds = participants . participants . map ( participant = > {
return appChatsManager . getParticipantPeerId ( participant ) ;
} ) ;
2021-10-21 15:16:43 +02:00
indexOfAndSplice ( peerIds , rootScope . myId ) ;
2021-05-03 22:02:53 +02:00
this . renderResultsFunc ( peerIds ) ;
2021-03-19 13:14:42 +01:00
if ( this . list . childElementCount >= participants . count || participants . participants . length < pageCount ) {
this . loadedWhat . channelParticipants = true ;
}
}
2020-10-08 00:20:38 +02:00
checkForTriggers = ( ) = > {
this . scrollable . checkForTriggers ( ) ;
} ;
2020-08-31 18:48:46 +02:00
2020-10-08 00:20:38 +02:00
private getMoreResults() {
const get = ( ) = > {
const promises : Promise < any > [ ] = [ ] ;
2021-10-21 15:16:43 +02:00
// if(!loadedAllDialogs && (this.peerType.includes('dialogs')/* || this.peerType.includes('contacts') */)) {
// if(!loadAllDialogsPromise) {
// loadAllDialogsPromise = appMessagesManager.getConversationsAll()
// .then(() => {
// loadedAllDialogs = true;
// }).finally(() => {
// loadAllDialogsPromise = null;
// });
// }
// promises.push(loadAllDialogsPromise);
// }
2020-10-08 00:20:38 +02:00
2021-07-15 17:34:34 +02:00
if ( ( this . peerType . includes ( 'dialogs' ) /* || this.loadedWhat.contacts */ ) && ! this . loadedWhat . archived ) { // to load non-contacts
2020-10-08 00:20:38 +02:00
promises . push ( this . getMoreDialogs ( ) ) ;
if ( ! this . loadedWhat . archived ) {
return promises ;
}
}
2021-10-21 15:16:43 +02:00
if ( ( this . peerType . includes ( 'contacts' ) || this . peerType . includes ( 'dialogs' ) ) && ! this . loadedWhat . contacts ) {
2020-10-08 00:20:38 +02:00
promises . push ( this . getMoreContacts ( ) ) ;
}
2021-03-19 13:14:42 +01:00
if ( this . peerType . includes ( 'channelParticipants' ) && ! this . loadedWhat . channelParticipants ) {
promises . push ( this . getMoreChannelParticipants ( ) ) ;
}
2020-10-08 00:20:38 +02:00
return promises ;
} ;
2020-08-30 23:50:25 +02:00
2020-10-08 00:20:38 +02:00
const promises = get ( ) ;
const promise = Promise . all ( promises ) ;
if ( promises . length ) {
promise . then ( this . checkForTriggers ) ;
2020-08-30 23:50:25 +02:00
}
2020-10-28 17:11:57 +01:00
return promise ;
2020-05-09 14:02:07 +02:00
}
2021-10-21 15:16:43 +02:00
private renderResults ( peerIds : PeerId [ ] ) {
2020-12-11 03:06:16 +01:00
//console.log('will renderResults:', peerIds);
2020-08-31 18:48:46 +02:00
// оставим только неконтакты с диалогов
if ( ! this . peerType . includes ( 'dialogs' ) && this . loadedWhat . contacts ) {
2020-12-11 03:06:16 +01:00
peerIds = peerIds . filter ( peerId = > {
return appUsersManager . isNonContactUser ( peerId ) ;
2020-08-31 18:48:46 +02:00
} ) ;
}
2020-12-11 03:06:16 +01:00
peerIds . forEach ( peerId = > {
2020-12-14 01:19:36 +01:00
const { dom } = appDialogsManager . addDialogNew ( {
dialog : peerId ,
container : this.scrollable ,
drawStatus : false ,
2021-03-19 13:14:42 +01:00
rippleEnabled : this.rippleEnabled ,
avatarSize : this.avatarSize
2020-12-14 01:19:36 +01:00
} ) ;
2020-06-19 13:49:55 +02:00
2020-10-20 01:39:36 +02:00
if ( this . multiSelect ) {
2020-12-11 03:06:16 +01:00
const selected = this . selected . has ( peerId ) ;
2021-03-08 23:15:44 +01:00
const checkboxField = new CheckboxField ( ) ;
2021-01-20 02:38:59 +01:00
if ( selected ) {
2021-04-20 20:00:38 +02:00
//dom.listEl.classList.add('active');
2021-01-20 02:38:59 +01:00
checkboxField . input . checked = true ;
}
dom . containerEl . prepend ( checkboxField . label ) ;
2020-10-20 01:39:36 +02:00
}
2020-05-09 14:02:07 +02:00
2021-03-28 20:37:11 +02:00
let subtitleEl : HTMLElement ;
2021-10-21 15:16:43 +02:00
if ( peerId . isAnyChat ( ) ) {
subtitleEl = appProfileManager . getChatMembersString ( peerId . toChatId ( ) ) ;
2021-02-04 01:30:23 +01:00
} else if ( peerId === rootScope . myId ) {
2021-07-08 08:07:32 +02:00
subtitleEl = i18n ( this . selfPresence ) ;
2020-05-09 14:02:07 +02:00
} else {
2021-03-28 20:37:11 +02:00
subtitleEl = appUsersManager . getUserStatusString ( peerId ) ;
2020-05-09 14:02:07 +02:00
}
2021-03-28 20:37:11 +02:00
dom . lastMessageSpan . append ( subtitleEl ) ;
2020-05-09 14:02:07 +02:00
} ) ;
}
2021-10-21 15:16:43 +02:00
public add ( key : PeerId | string , title? : string | HTMLElement , scroll = true ) {
2020-06-20 03:11:24 +02:00
//console.trace('add');
2021-10-21 15:16:43 +02:00
this . selected . add ( key ) ;
2020-10-20 01:39:36 +02:00
if ( ! this . multiSelect ) {
this . onChange ( this . selected . size ) ;
return ;
}
2021-07-16 14:47:30 +02:00
if ( this . query . trim ( ) ) {
this . input . value = '' ;
this . onInput ( ) ;
}
2020-05-30 08:44:54 +02:00
const div = document . createElement ( 'div' ) ;
2020-05-09 14:02:07 +02:00
div . classList . add ( 'selector-user' , 'scale-in' ) ;
2020-05-30 08:44:54 +02:00
const avatarEl = document . createElement ( 'avatar-element' ) ;
2020-05-13 17:26:40 +02:00
avatarEl . classList . add ( 'selector-user-avatar' , 'tgico' ) ;
avatarEl . setAttribute ( 'dialog' , '1' ) ;
2020-12-14 01:19:36 +01:00
avatarEl . classList . add ( 'avatar-32' ) ;
2020-05-09 14:02:07 +02:00
2021-10-21 15:16:43 +02:00
div . dataset . key = '' + key ;
if ( key . isPeerId ( ) ) {
2020-06-19 13:49:55 +02:00
if ( title === undefined ) {
2021-10-21 15:16:43 +02:00
title = new PeerTitle ( { peerId : key.toPeerId ( ) , dialog : true } ) . element ;
2020-06-19 13:49:55 +02:00
}
2021-10-21 15:16:43 +02:00
avatarEl . setAttribute ( 'peer' , '' + key ) ;
2020-06-19 13:49:55 +02:00
}
if ( title ) {
2021-03-21 15:36:14 +01:00
if ( typeof ( title ) === 'string' ) {
div . innerHTML = title ;
} else {
2021-04-10 20:29:58 +02:00
replaceContent ( div , title ) ;
2021-03-21 15:36:14 +01:00
div . append ( title ) ;
}
2020-06-19 13:49:55 +02:00
}
2020-05-13 17:26:40 +02:00
div . insertAdjacentElement ( 'afterbegin' , avatarEl ) ;
2020-05-09 14:02:07 +02:00
this . selectedContainer . insertBefore ( div , this . input ) ;
2020-06-19 13:49:55 +02:00
//this.selectedScrollable.scrollTop = this.selectedScrollable.scrollHeight;
this . onChange && this . onChange ( this . selected . size ) ;
2021-01-07 13:36:21 +01:00
if ( scroll ) {
this . selectedScrollable . scrollIntoViewNew ( this . input , 'center' ) ;
}
2020-06-19 13:49:55 +02:00
return div ;
2020-05-09 14:02:07 +02:00
}
2021-10-21 15:16:43 +02:00
public remove ( key : PeerId | string ) {
2020-10-20 01:39:36 +02:00
if ( ! this . multiSelect ) return ;
2020-12-11 03:06:16 +01:00
//const div = this.selected[peerId];
2020-06-19 13:49:55 +02:00
const div = this . selectedContainer . querySelector ( ` [data-key=" ${ key } "] ` ) as HTMLElement ;
2020-05-09 14:02:07 +02:00
div . classList . remove ( 'scale-in' ) ;
void div . offsetWidth ;
div . classList . add ( 'scale-out' ) ;
2021-01-03 16:59:13 +01:00
const onAnimationEnd = ( ) = > {
2020-06-19 13:49:55 +02:00
this . selected . delete ( key ) ;
2020-05-09 14:02:07 +02:00
div . remove ( ) ;
2020-06-19 13:49:55 +02:00
this . onChange && this . onChange ( this . selected . size ) ;
2021-01-03 16:59:13 +01:00
} ;
if ( rootScope . settings . animationsEnabled ) {
div . addEventListener ( 'animationend' , onAnimationEnd , { once : true } ) ;
} else {
onAnimationEnd ( ) ;
}
2020-05-09 14:02:07 +02:00
}
public getSelected() {
2020-06-19 13:49:55 +02:00
return [ . . . this . selected ] ;
2020-05-09 14:02:07 +02:00
}
2021-01-07 13:36:21 +01:00
public addInitial ( values : any [ ] ) {
values . forEach ( value = > {
this . add ( value , undefined , false ) ;
} ) ;
2021-02-28 21:14:01 +01:00
window . requestAnimationFrame ( ( ) = > { // ! not the best place for this raf though it works
this . selectedScrollable . scrollIntoViewNew ( this . input , 'center' , undefined , undefined , FocusDirection . Static ) ;
} ) ;
2021-01-07 13:36:21 +01:00
}
2021-03-19 13:14:42 +01:00
}