@ -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,38 +104,53 @@ angular.module('myApp.controllers', [])
$scope . sendCode = function ( ) {
$scope . sendCode = function ( ) {
$timeout . cancel ( callTimeout ) ;
$timeout . cancel ( callTimeout ) ;
$scope . progress . enabled = true ;
MtpApiManager . invokeApi ( 'auth.checkPhone' , {
phone _number : $scope . credentials . phone _number
} , options ) . then ( function ( result ) {
$scope . progress . enabled = false ;
if ( ! result . phone _registered ) {
ErrorService . show ( {
error : { code : 400 , type : 'ACCOUNT_REQUIRED' } ,
phone : $scope . credentials . phone _number
} ) ;
return false ;
}
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.sendCode' , {
MtpApiManager . invokeApi ( 'auth.checkPhone' , {
phone _number : $scope . credentials . phone _number ,
phone _number : $scope . credentials . phone _full
sms _type : 0 ,
} , options ) . then ( function ( result ) {
api _id : 2496 ,
api _hash : '8da85b0d5bfe62527e5b244c209159c3'
} , options ) . then ( function ( sentCode ) {
$scope . progress . enabled = false ;
$scope . progress . enabled = false ;
if ( ! result . phone _registered ) {
ErrorService . show ( {
error : { code : 400 , type : 'ACCOUNT_REQUIRED' } ,
phone : $scope . credentials . phone _full
} ) ;
return false ;
}
$scope . credentials . phone _code _hash = sentCode . phone _code _hash ;
$scope . progress . enabled = true ;
$scope . credentials . phone _occupied = sentCode . phone _registered ;
MtpApiManager . invokeApi ( 'auth.sendCode' , {
$scope . error = { } ;
phone _number : $scope . credentials . phone _full ,
sms _type : 0 ,
$scope . callPending . remaining = sentCode . send _call _timeout ;
api _id : 2496 ,
callCheck ( ) ;
api _hash : '8da85b0d5bfe62527e5b244c209159c3'
} , options ) . then ( function ( sentCode ) {
$scope . progress . enabled = false ;
$scope . credentials . phone _code _hash = sentCode . phone _code _hash ;
$scope . credentials . phone _occupied = sentCode . phone _registered ;
$scope . error = { } ;
$scope . callPending . remaining = sentCode . send _call _timeout ;
callCheck ( ) ;
} , function ( error ) {
$scope . progress . enabled = false ;
console . log ( 'sendCode error' , error ) ;
switch ( error . type ) {
case 'PHONE_NUMBER_INVALID' :
$scope . error = { field : 'phone' } ;
error . handled = true ;
break ;
}
} ) ;
} , function ( error ) {
} , function ( error ) {
$scope . progress . enabled = false ;
$scope . progress . enabled = false ;
console . log ( 'sendCode error' , error ) ;
switch ( error . type ) {
switch ( error . type ) {
case 'PHONE_NUMBER_INVALID' :
case 'PHONE_NUMBER_INVALID' :
$scope . error = { field : 'phone' } ;
$scope . error = { field : 'phone' } ;
@ -102,20 +158,14 @@ angular.module('myApp.controllers', [])
break ;
break ;
}
}
} ) ;
} ) ;
} , function ( error ) {
$scope . progress . enabled = false ;
switch ( error . type ) {
case 'PHONE_NUMBER_INVALID' :
$scope . error = { field : 'phone' } ;
error . handled = true ;
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 ] } ) ;
}
}
}
} ) ;
} )