Improved country select

Fixed multiple countries per country code - Closes #418
Auto-detect country code - Closes #384
This commit is contained in:
Igor Zhukov 2014-09-08 20:33:08 +04:00
parent 59e4625b05
commit 448df34e1e
6 changed files with 81 additions and 22 deletions

View File

@ -33,13 +33,15 @@ angular.module('myApp.controllers', [])
return;
}
});
var options = {dcID: 1, createNetworker: true};
var options = {dcID: 2, createNetworker: true},
countryChanged = false,
selectedCountry = false;
$scope.credentials = {phone_country: '+1', phone_country_name: 'USA', phone_number: '', phone_full: ''};
$scope.credentials = {phone_country: '', phone_country_name: '', phone_number: '', phone_full: ''};
$scope.progress = {};
$scope.callPending = {};
$scope.selectCountry = function () {
$scope.chooseCountry = function () {
var tUrl = 'partials/country_select_modal.html',
className = 'countries_modal_window page_modal';
@ -54,14 +56,60 @@ angular.module('myApp.controllers', [])
windowClass: className
});
modal.result.then(function (code) {
$scope.credentials.phone_country = code;
$scope.$broadcast('country_selected');
});
modal.result.then(selectCountry);
};
$scope.$watch('credentials.phone_country', updateCountry);
$scope.$watch('credentials.phone_number', updateCountry);
function initPhoneCountry () {
var langCode = (navigator.language || '').toLowerCase(),
countryIso2 = Config.LangCountries[langCode],
shouldPregenerate = !Config.Navigator.mobile;
if (['en', 'en-us', 'en-uk'].indexOf(langCode) == -1) {
if (countryIso2 !== undefined) {
selectPhoneCountryByIso2(countryIso2);
} else if (langCode.indexOf('-') > 0) {
selectPhoneCountryByIso2(langCode.split('-')[1].toUpperCase());
} else {
selectPhoneCountryByIso2('US');
}
} else {
selectPhoneCountryByIso2('US');
}
if (!shouldPregenerate) {
return;
}
var wasCountry = $scope.credentials.phone_country;
MtpApiManager.invokeApi('help.getNearestDc', {}, {dcID: 4, createNetworker: true}).then(function (nearestDcResult) {
if (wasCountry == $scope.credentials.phone_country) {
selectPhoneCountryByIso2(nearestDcResult.country);
}
if (nearestDcResult.nearest_dc != nearestDcResult.this_dc) {
MtpApiManager.getNetworker(nearestDcResult.nearest_dc, {createNetworker: true});
}
});
}
function selectPhoneCountryByIso2 (countryIso2) {
var i, country;
for (i = 0; i < Config.CountryCodes.length; i++) {
country = Config.CountryCodes[i];
if (country[0] == countryIso2) {
return selectCountry({name: country[1], code: country[2]});
}
}
return selectCountry({name: 'United States', code: '+1'});
}
function selectCountry (country) {
selectedCountry = country;
if ($scope.credentials.phone_country != country.code) {
$scope.credentials.phone_country = country.code;
} else {
updateCountry();
}
$scope.$broadcast('country_selected');
}
function updateCountry () {
var phoneNumber = (
@ -73,12 +121,16 @@ angular.module('myApp.controllers', [])
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];
if (selectedCountry && !phoneNumber.indexOf(selectedCountry.code.replace(/\D+/g, ''))) {
maxName = selectedCountry.name;
} else {
for (i = 0; i < Config.CountryCodes.length; i++) {
for (j = 2; 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][1];
}
}
}
}
@ -88,6 +140,11 @@ angular.module('myApp.controllers', [])
$scope.credentials.phone_country_name = maxName || 'Unknown';
};
$scope.$watch('credentials.phone_country', updateCountry);
$scope.$watch('credentials.phone_number', updateCountry);
initPhoneCountry();
var callTimeout;
function saveAuth (result) {
@ -2284,8 +2341,8 @@ angular.module('myApp.controllers', [])
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]});
for (j = 2; j < Config.CountryCodes[i].length; j++) {
$scope.countries.push({name: Config.CountryCodes[i][1], code: Config.CountryCodes[i][j]});
}
}
}

File diff suppressed because one or more lines are too long

View File

@ -223,6 +223,7 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
getBaseDcID: getBaseDcID,
getUserID: mtpGetUserID,
invokeApi: mtpInvokeApi,
getNetworker: mtpGetNetworker,
setUserAuth: mtpSetUserAuth,
logOut: mtpLogOut
}

View File

@ -17,7 +17,7 @@
<ul class="countries_modal_members_list nav nav-pills nav-stacked">
<li class="countries_modal_country_wrap clearfix" ng-repeat="country in countries | limitTo : slice.limit track by $index">
<a class="countries_modal_country" ng-click="$close(country.code)">
<a class="countries_modal_country" ng-click="$close(country)">
<span class="countries_modal_country_code pull-right" ng-bind="country.code"></span>
<span class="countries_modal_country_name" ng-bind="country.name"></span>
</a>
@ -33,7 +33,6 @@
</div>
<div class="modal-footer">
<a class="btn btn-link" ng-click="$dismiss()">Cancel</a>
<button type="button" class="btn btn-primary" ng-click="$dismiss()">Done</button>
</div>

View File

@ -8,7 +8,7 @@
<h3 class="login_form_head">Sign in</h3>
<p class="login_form_lead">Please choose your country and enter your full phone number.</p>
<div class="login_country_selector" ng-click="selectCountry()">
<div class="login_country_selector" ng-click="chooseCountry()">
<span ng-bind="credentials.phone_country_name"></span>
<i class="icon icon-caret pull-right"></i>
</div>

View File

@ -38,7 +38,7 @@
<ul class="countries_modal_members_list nav nav-pills nav-stacked">
<li class="countries_modal_country_wrap clearfix" ng-repeat="country in countries | limitTo : slice.limit track by $index">
<a class="countries_modal_country" ng-click="$close(country.code)">
<a class="countries_modal_country" ng-click="$close(country)">
<span class="countries_modal_country_code pull-right" ng-bind="country.code"></span>
<span class="countries_modal_country_name" ng-bind="country.name"></span>
</a>