@ -21,7 +21,7 @@ angular.module('myApp.controllers', [])
} ) ;
} ) ;
} )
} )
. controller ( 'AppLoginController' , function ( $scope , $location , $timeout , MtpApiManager , ErrorService ) {
. controller ( 'AppLoginController' , function ( $scope , $location , $timeout , $modal , MtpApiManager , ErrorService ) {
MtpApiManager . getUserID ( ) . then ( function ( id ) {
MtpApiManager . getUserID ( ) . then ( function ( id ) {
if ( id ) {
if ( id ) {
$location . url ( '/im' ) ;
$location . url ( '/im' ) ;
@ -30,10 +30,51 @@ angular.module('myApp.controllers', [])
} ) ;
} ) ;
var options = { dcID : 1 , createNetworker : true } ;
var options = { dcID : 1 , createNetworker : true } ;
$scope . credentials = { } ;
$scope . credentials = { phone _country : '+1' , phone _country _name : 'USA' , phone _number : '' , phone _full : '' } ;
$scope . progress = { } ;
$scope . progress = { } ;
$scope . callPending = { } ;
$scope . callPending = { } ;
$scope . selectCountry = function ( ) {
var modal = $modal . open ( {
templateUrl : 'partials/country_select_modal.html' ,
controller : 'CountrySelectModalController' ,
windowClass : 'countries_modal_window'
} ) ;
modal . result . then ( function ( code ) {
$scope . credentials . phone _country = code ;
$scope . $broadcast ( 'country_selected' ) ;
} ) ;
} ;
$scope . $watch ( 'credentials.phone_country' , updateCountry ) ;
$scope . $watch ( 'credentials.phone_number' , updateCountry ) ;
function updateCountry ( ) {
var phoneNumber = (
( $scope . credentials . phone _country || '' ) +
( $scope . credentials . phone _number || '' )
) . replace ( /\D+/g , '' ) ,
i , j , code ,
maxLength = 0 ,
maxName = false ;
if ( phoneNumber . length ) {
for ( i = 0 ; i < Config . CountryCodes . length ; i ++ ) {
for ( j = 1 ; j < Config . CountryCodes [ i ] . length ; j ++ ) {
code = Config . CountryCodes [ i ] [ j ] . replace ( /\D+/g , '' ) ;
if ( code . length >= maxLength && ! phoneNumber . indexOf ( code ) ) {
maxLength = code . length ;
maxName = Config . CountryCodes [ i ] [ 0 ] ;
}
}
}
}
$scope . credentials . phone _full = phoneNumber ;
$scope . credentials . phone _country _name = maxName || 'Unknown' ;
} ;
var callTimeout ;
var callTimeout ;
function saveAuth ( result ) {
function saveAuth ( result ) {
@ -51,7 +92,7 @@ angular.module('myApp.controllers', [])
if ( ! ( -- $scope . callPending . remaining ) ) {
if ( ! ( -- $scope . callPending . remaining ) ) {
$scope . callPending . success = false ;
$scope . callPending . success = false ;
MtpApiManager . invokeApi ( 'auth.sendCall' , {
MtpApiManager . invokeApi ( 'auth.sendCall' , {
phone _number : $scope . credentials . phone _number ,
phone _number : $scope . credentials . phone _full ,
phone _code _hash : $scope . credentials . phone _code _hash
phone _code _hash : $scope . credentials . phone _code _hash
} , options ) . then ( function ( ) {
} , options ) . then ( function ( ) {
$scope . callPending . success = true ;
$scope . callPending . success = true ;
@ -63,22 +104,28 @@ angular.module('myApp.controllers', [])
$scope . sendCode = function ( ) {
$scope . sendCode = function ( ) {
$timeout . cancel ( callTimeout ) ;
$timeout . cancel ( callTimeout ) ;
ErrorService . confirm ( {
type : 'LOGIN_PHONE_CORRECT' ,
country _code : $scope . credentials . phone _country ,
phone _number : $scope . credentials . phone _number
} ) . then ( function ( ) {
$scope . progress . enabled = true ;
$scope . progress . enabled = true ;
MtpApiManager . invokeApi ( 'auth.checkPhone' , {
MtpApiManager . invokeApi ( 'auth.checkPhone' , {
phone _number : $scope . credentials . phone _number
phone _number : $scope . credentials . phone _full
} , options ) . then ( function ( result ) {
} , options ) . then ( function ( result ) {
$scope . progress . enabled = false ;
$scope . progress . enabled = false ;
if ( ! result . phone _registered ) {
if ( ! result . phone _registered ) {
ErrorService . show ( {
ErrorService . show ( {
error : { code : 400 , type : 'ACCOUNT_REQUIRED' } ,
error : { code : 400 , type : 'ACCOUNT_REQUIRED' } ,
phone : $scope . credentials . phone _number
phone : $scope . credentials . phone _full
} ) ;
} ) ;
return false ;
return false ;
}
}
$scope . progress . enabled = true ;
$scope . progress . enabled = true ;
MtpApiManager . invokeApi ( 'auth.sendCode' , {
MtpApiManager . invokeApi ( 'auth.sendCode' , {
phone _number : $scope . credentials . phone _number ,
phone _number : $scope . credentials . phone _full ,
sms _type : 0 ,
sms _type : 0 ,
api _id : 2496 ,
api _id : 2496 ,
api _hash : '8da85b0d5bfe62527e5b244c209159c3'
api _hash : '8da85b0d5bfe62527e5b244c209159c3'
@ -111,11 +158,14 @@ angular.module('myApp.controllers', [])
break ;
break ;
}
}
} ) ;
} ) ;
} ) ;
}
}
$scope . logIn = function ( forceSignUp ) {
$scope . logIn = function ( forceSignUp ) {
var method = 'auth.signIn' , params = {
var method = 'auth.signIn' , params = {
phone _number : $scope . credentials . phone _number ,
phone _number : $scope . credentials . phone _full ,
phone _code _hash : $scope . credentials . phone _code _hash ,
phone _code _hash : $scope . credentials . phone _code _hash ,
phone _code : $scope . credentials . phone _code
phone _code : $scope . credentials . phone _code
} ;
} ;
@ -1629,3 +1679,37 @@ angular.module('myApp.controllers', [])
} ;
} ;
} )
} )
. controller ( 'CountrySelectModalController' , function ( $scope , $modalInstance , $rootScope , SearchIndexManager ) {
$scope . search = { } ;
var searchIndex = SearchIndexManager . createIndex ( ) ;
for ( var i = 0 ; i < Config . CountryCodes . length ; i ++ ) {
SearchIndexManager . indexObject ( i , Config . CountryCodes [ i ] . join ( ' ' ) , searchIndex ) ;
}
$scope . $watch ( 'search.query' , function ( newValue ) {
var filtered = false ,
results = { } ;
if ( angular . isString ( newValue ) && newValue . length ) {
filtered = true ;
results = SearchIndexManager . search ( newValue , searchIndex ) ;
}
console . log ( dT ( ) , newValue , results ) ;
$scope . countries = [ ] ;
var j ;
for ( var i = 0 ; i < Config . CountryCodes . length ; i ++ ) {
if ( ! filtered || results [ i ] ) {
for ( j = 1 ; j < Config . CountryCodes [ i ] . length ; j ++ ) {
$scope . countries . push ( { name : Config . CountryCodes [ i ] [ 0 ] , code : Config . CountryCodes [ i ] [ j ] } ) ;
}
}
}
} ) ;
} )