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; 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.progress = {};
$scope.callPending = {}; $scope.callPending = {};
$scope.selectCountry = function () { $scope.chooseCountry = function () {
var tUrl = 'partials/country_select_modal.html', var tUrl = 'partials/country_select_modal.html',
className = 'countries_modal_window page_modal'; className = 'countries_modal_window page_modal';
@ -54,14 +56,60 @@ angular.module('myApp.controllers', [])
windowClass: className windowClass: className
}); });
modal.result.then(function (code) { modal.result.then(selectCountry);
$scope.credentials.phone_country = code;
$scope.$broadcast('country_selected');
});
}; };
$scope.$watch('credentials.phone_country', updateCountry); function initPhoneCountry () {
$scope.$watch('credentials.phone_number', updateCountry); 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 () { function updateCountry () {
var phoneNumber = ( var phoneNumber = (
@ -73,12 +121,16 @@ angular.module('myApp.controllers', [])
maxName = false; maxName = false;
if (phoneNumber.length) { if (phoneNumber.length) {
for (i = 0; i < Config.CountryCodes.length; i++) { if (selectedCountry && !phoneNumber.indexOf(selectedCountry.code.replace(/\D+/g, ''))) {
for (j = 1; j < Config.CountryCodes[i].length; j++) { maxName = selectedCountry.name;
code = Config.CountryCodes[i][j].replace(/\D+/g, ''); } else {
if (code.length >= maxLength && !phoneNumber.indexOf(code)) { for (i = 0; i < Config.CountryCodes.length; i++) {
maxLength = code.length; for (j = 2; j < Config.CountryCodes[i].length; j++) {
maxName = Config.CountryCodes[i][0]; 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.credentials.phone_country_name = maxName || 'Unknown';
}; };
$scope.$watch('credentials.phone_country', updateCountry);
$scope.$watch('credentials.phone_number', updateCountry);
initPhoneCountry();
var callTimeout; var callTimeout;
function saveAuth (result) { function saveAuth (result) {
@ -2284,8 +2341,8 @@ angular.module('myApp.controllers', [])
var j; var j;
for (var i = 0; i < Config.CountryCodes.length; i++) { for (var i = 0; i < Config.CountryCodes.length; i++) {
if (!filtered || results[i]) { if (!filtered || results[i]) {
for (j = 1; j < Config.CountryCodes[i].length; j++) { for (j = 2; j < Config.CountryCodes[i].length; j++) {
$scope.countries.push({name: Config.CountryCodes[i][0], code: Config.CountryCodes[i][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, getBaseDcID: getBaseDcID,
getUserID: mtpGetUserID, getUserID: mtpGetUserID,
invokeApi: mtpInvokeApi, invokeApi: mtpInvokeApi,
getNetworker: mtpGetNetworker,
setUserAuth: mtpSetUserAuth, setUserAuth: mtpSetUserAuth,
logOut: mtpLogOut logOut: mtpLogOut
} }

View File

@ -17,7 +17,7 @@
<ul class="countries_modal_members_list nav nav-pills nav-stacked"> <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"> <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_code pull-right" ng-bind="country.code"></span>
<span class="countries_modal_country_name" ng-bind="country.name"></span> <span class="countries_modal_country_name" ng-bind="country.name"></span>
</a> </a>
@ -33,7 +33,6 @@
</div> </div>
<div class="modal-footer"> <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> <button type="button" class="btn btn-primary" ng-click="$dismiss()">Done</button>
</div> </div>

View File

@ -8,7 +8,7 @@
<h3 class="login_form_head">Sign in</h3> <h3 class="login_form_head">Sign in</h3>
<p class="login_form_lead">Please choose your country and enter your full phone number.</p> <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> <span ng-bind="credentials.phone_country_name"></span>
<i class="icon icon-caret pull-right"></i> <i class="icon icon-caret pull-right"></i>
</div> </div>

View File

@ -38,7 +38,7 @@
<ul class="countries_modal_members_list nav nav-pills nav-stacked"> <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"> <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_code pull-right" ng-bind="country.code"></span>
<span class="countries_modal_country_name" ng-bind="country.name"></span> <span class="countries_modal_country_name" ng-bind="country.name"></span>
</a> </a>