@ -409,7 +409,6 @@ angular.module('myApp.controllers', ['myApp.i18n'])
$scope . searchClear = function ( ) {
$scope . searchClear = function ( ) {
$scope . search . query = '' ;
$scope . search . query = '' ;
$scope . search . messages = false ;
$scope . $broadcast ( 'search_clear' ) ;
$scope . $broadcast ( 'search_clear' ) ;
}
}
@ -468,7 +467,6 @@ angular.module('myApp.controllers', ['myApp.i18n'])
if ( $routeParams . q ) {
if ( $routeParams . q ) {
if ( $routeParams . q !== lastSearch ) {
if ( $routeParams . q !== lastSearch ) {
$scope . search . query = lastSearch = $routeParams . q ;
$scope . search . query = lastSearch = $routeParams . q ;
$scope . search . messages = true ;
if ( $scope . curDialog !== undefined ) {
if ( $scope . curDialog !== undefined ) {
return false ;
return false ;
}
}
@ -505,7 +503,8 @@ angular.module('myApp.controllers', ['myApp.i18n'])
$scope . dialogs = [ ] ;
$scope . dialogs = [ ] ;
$scope . contacts = [ ] ;
$scope . contacts = [ ] ;
$scope . foundUsers = [ ] ;
$scope . foundUsers = [ ] ;
$scope . contactsLoaded = false ;
$scope . foundMessages = [ ] ;
if ( $scope . search === undefined ) {
if ( $scope . search === undefined ) {
$scope . search = { } ;
$scope . search = { } ;
}
}
@ -514,12 +513,13 @@ angular.module('myApp.controllers', ['myApp.i18n'])
}
}
$scope . phonebookAvailable = PhonebookContactsService . isAvailable ( ) ;
$scope . phonebookAvailable = PhonebookContactsService . isAvailable ( ) ;
var offset = 0 ,
var searchMessages = false ;
maxID = 0 ,
var maxID = 0 ;
hasMore = false ,
var hasMore = false ;
jump = 0 ,
var jump = 0 ;
peersInDialogs = { } ,
var contactsJump = 0 ;
contactsShown ;
var peersInDialogs = { } ;
var contactsShown ;
$scope . $on ( 'dialogs_need_more' , function ( ) {
$scope . $on ( 'dialogs_need_more' , function ( ) {
// console.log('on need more');
// console.log('on need more');
@ -550,7 +550,6 @@ angular.module('myApp.controllers', ['myApp.i18n'])
if ( pos !== false ) {
if ( pos !== false ) {
var prev = $scope . dialogs . splice ( pos , 1 ) ;
var prev = $scope . dialogs . splice ( pos , 1 ) ;
safeReplaceObject ( prev , wrappedDialog ) ;
safeReplaceObject ( prev , wrappedDialog ) ;
offset ++ ;
}
}
$scope . dialogs . unshift ( wrappedDialog ) ;
$scope . dialogs . unshift ( wrappedDialog ) ;
delete $scope . isEmpty . dialogs ;
delete $scope . isEmpty . dialogs ;
@ -558,7 +557,7 @@ angular.module('myApp.controllers', ['myApp.i18n'])
if ( ! peersInDialogs [ dialog . peerID ] ) {
if ( ! peersInDialogs [ dialog . peerID ] ) {
peersInDialogs [ dialog . peerID ] = true ;
peersInDialogs [ dialog . peerID ] = true ;
if ( contactsShown ) {
if ( contactsShown ) {
showMoreContact s ( ) ;
showMoreConversation s ( ) ;
}
}
}
}
@ -584,17 +583,14 @@ angular.module('myApp.controllers', ['myApp.i18n'])
}
}
} ) ;
} ) ;
var prevMessages = false ;
$scope . $watchCollection ( 'search' , function ( ) {
$scope . $watchCollection ( 'search' , function ( ) {
if ( $scope . search . messages != prevMessages ) {
prevMessages = $scope . search . messages ;
$scope . dialogs = [ ] ;
$scope . dialogs = [ ] ;
loadDialogs ( true ) ;
$scope . foundMessages = [ ] ;
} else {
searchMessages = false ;
contactsJump ++ ;
loadDialogs ( ) ;
loadDialogs ( ) ;
}
if ( $routeParams . q && ( ! $scope . search . messages || $scope . search . query != $routeParams . q ) ) {
if ( $routeParams . q && $scope . search . query != $routeParams . q ) {
$timeout ( function ( ) {
$timeout ( function ( ) {
$location . url (
$location . url (
'/im' +
'/im' +
@ -620,7 +616,7 @@ angular.module('myApp.controllers', ['myApp.i18n'])
$scope . $on ( 'contacts_update' , function ( ) {
$scope . $on ( 'contacts_update' , function ( ) {
if ( contactsShown ) {
if ( contactsShown ) {
showMoreContact s ( ) ;
showMoreConversation s ( ) ;
}
}
} ) ;
} ) ;
@ -628,13 +624,12 @@ angular.module('myApp.controllers', ['myApp.i18n'])
var searchTimeoutPromise ;
var searchTimeoutPromise ;
function getDialogs ( force ) {
function getDialogs ( force ) {
var searchMessages = $scope . search . messages && $scope . search . query . length > 0 ,
var curJump = ++ jump ,
curJump = ++ jump ,
promise ;
promise ;
$timeout . cancel ( searchTimeoutPromise ) ;
$timeout . cancel ( searchTimeoutPromise ) ;
if ( searchMessages ) {
if ( searchMessages ) {
searchTimeoutPromise = force ? $q . when ( ) : $timeout ( angular . noop , 500 ) ;
searchTimeoutPromise = ( force || maxID ) ? $q . when ( ) : $timeout ( angular . noop , 500 ) ;
promise = searchTimeoutPromise . then ( function ( ) {
promise = searchTimeoutPromise . then ( function ( ) {
return AppMessagesManager . getSearch ( { _ : 'inputPeerEmpty' } , $scope . search . query , { _ : 'inputMessagesFilterEmpty' } , maxID ) ;
return AppMessagesManager . getSearch ( { _ : 'inputPeerEmpty' } , $scope . search . query , { _ : 'inputMessagesFilterEmpty' } , maxID ) ;
} ) ;
} ) ;
@ -686,30 +681,39 @@ angular.module('myApp.controllers', ['myApp.i18n'])
} ;
} ;
function loadDialogs ( force ) {
function loadDialogs ( force ) {
offset = 0 ;
maxID = 0 ;
maxID = 0 ;
hasMore = false ;
hasMore = false ;
if ( ! searchMessages ) {
peersInDialogs = { } ;
peersInDialogs = { } ;
contactsShown = false ;
contactsShown = false ;
}
getDialogs ( force ) . then ( function ( dialogsResult ) {
getDialogs ( force ) . then ( function ( dialogsResult ) {
if ( ! searchMessages ) {
$scope . dialogs = [ ] ;
$scope . dialogs = [ ] ;
$scope . contacts = [ ] ;
$scope . contacts = [ ] ;
$scope . foundUsers = [ ] ;
$scope . foundUsers = [ ] ;
}
$scope . foundMessages = [ ] ;
if ( dialogsResult . dialogs . length ) {
var dialogsList = searchMessages ? $scope . foundMessages : $scope . dialogs ;
offset += dialogsResult . dialogs . length ;
maxID = dialogsResult . dialogs [ dialogsResult . dialogs . length - 1 ] . top _message ;
hasMore = dialogsResult . count === null || offset < dialogsResult . count ;
if ( dialogsResult . dialogs . length ) {
angular . forEach ( dialogsResult . dialogs , function ( dialog ) {
angular . forEach ( dialogsResult . dialogs , function ( dialog ) {
peersInDialogs [ dialog . peerID ] = true ;
var wrappedDialog = AppMessagesManager . wrapForDialog ( dialog . top _message , dialog . unread _count ) ;
var wrappedDialog = AppMessagesManager . wrapForDialog ( dialog . top _message , dialog . unread _count ) ;
$scope . dialogs . push ( wrappedDialog ) ;
if ( ! searchMessages ) {
peersInDialogs [ dialog . peerID ] = true ;
}
dialogsList . push ( wrappedDialog ) ;
} ) ;
} ) ;
maxID = dialogsResult . dialogs [ dialogsResult . dialogs . length - 1 ] . top _message ;
hasMore = dialogsResult . count === null || dialogsList . length < dialogsResult . count ;
if ( ! searchMessages ) {
delete $scope . isEmpty . dialogs ;
delete $scope . isEmpty . dialogs ;
}
}
}
$scope . $broadcast ( 'ui_dialogs_change' ) ;
$scope . $broadcast ( 'ui_dialogs_change' ) ;
@ -726,12 +730,42 @@ angular.module('myApp.controllers', ['myApp.i18n'])
} ) ;
} ) ;
}
}
function showMoreContacts ( ) {
function showMoreDialogs ( ) {
if ( contactsShown && ( ! hasMore || ! maxID ) ) {
return ;
}
if ( ! hasMore && ! searchMessages && ( $scope . search . query || ! $scope . dialogs . length ) ) {
showMoreConversations ( ) ;
return ;
}
getDialogs ( ) . then ( function ( dialogsResult ) {
if ( dialogsResult . dialogs . length ) {
var dialogsList = searchMessages ? $scope . foundMessages : $scope . dialogs ;
angular . forEach ( dialogsResult . dialogs , function ( dialog ) {
var wrappedDialog = AppMessagesManager . wrapForDialog ( dialog . top _message , dialog . unread _count ) ;
if ( ! searchMessages ) {
peersInDialogs [ dialog . peerID ] = true ;
}
dialogsList . push ( wrappedDialog ) ;
} ) ;
maxID = dialogsResult . dialogs [ dialogsResult . dialogs . length - 1 ] . top _message ;
hasMore = dialogsResult . count === null || dialogsList . length < dialogsResult . count ;
$scope . $broadcast ( 'ui_dialogs_append' ) ;
}
} ) ;
} ;
function showMoreConversations ( ) {
contactsShown = true ;
contactsShown = true ;
var curJump = ++ jump ;
var curJump = ++ contactsJ ump;
AppUsersManager . getContacts ( $scope . search . query ) . then ( function ( contactsList ) {
AppUsersManager . getContacts ( $scope . search . query ) . then ( function ( contactsList ) {
if ( curJump != jump ) return ;
if ( curJump != contactsJ ump) return ;
$scope . contacts = [ ] ;
$scope . contacts = [ ] ;
angular . forEach ( contactsList , function ( userID ) {
angular . forEach ( contactsList , function ( userID ) {
if ( peersInDialogs [ userID ] === undefined ) {
if ( peersInDialogs [ userID ] === undefined ) {
@ -753,10 +787,10 @@ angular.module('myApp.controllers', ['myApp.i18n'])
if ( $scope . search . query && $scope . search . query . length >= 5 ) {
if ( $scope . search . query && $scope . search . query . length >= 5 ) {
$timeout ( function ( ) {
$timeout ( function ( ) {
if ( curJump != j ump) return ;
if ( curJump != contactsJ ump) return ;
MtpApiManager . invokeApi ( 'contacts.search' , { q : $scope . search . query , limit : 10 } ) . then ( function ( result ) {
MtpApiManager . invokeApi ( 'contacts.search' , { q : $scope . search . query , limit : 10 } ) . then ( function ( result ) {
AppUsersManager . saveApiUsers ( result . users ) ;
AppUsersManager . saveApiUsers ( result . users ) ;
if ( curJump != j ump) return ;
if ( curJump != contactsJ ump) return ;
$scope . foundUsers = [ ] ;
$scope . foundUsers = [ ] ;
angular . forEach ( result . results , function ( contactFound ) {
angular . forEach ( result . results , function ( contactFound ) {
var userID = contactFound . user _id ;
var userID = contactFound . user _id ;
@ -775,33 +809,12 @@ angular.module('myApp.controllers', ['myApp.i18n'])
} ) ;
} ) ;
} , 500 ) ;
} , 500 ) ;
}
}
}
function showMoreDialogs ( ) {
if ( $scope . search . query ) {
if ( contactsShown && ( ! hasMore || ! offset ) ) {
searchMessages = true ;
return ;
loadDialogs ( ) ;
}
if ( ! hasMore && ! $scope . search . messages && ( $scope . search . query || ! $scope . dialogs . length ) ) {
showMoreContacts ( ) ;
return ;
}
}
getDialogs ( ) . then ( function ( dialogsResult ) {
if ( dialogsResult . dialogs . length ) {
offset += dialogsResult . dialogs . length ;
maxID = dialogsResult . dialogs [ dialogsResult . dialogs . length - 1 ] . top _message ;
hasMore = dialogsResult . count === null || offset < dialogsResult . count ;
angular . forEach ( dialogsResult . dialogs , function ( dialog ) {
peersInDialogs [ dialog . peerID ] = true ;
$scope . dialogs . push ( AppMessagesManager . wrapForDialog ( dialog . top _message , dialog . unread _count ) ) ;
} ) ;
$scope . $broadcast ( 'ui_dialogs_append' ) ;
}
}
} ) ;
} ;
} )
} )