Committer: pkornilov
LJSUP-6550: Client part of the task LJSUP-6482.U trunk/htdocs/js/editprofile.js U trunk/htdocs/manage/profile/index.bml.text.local
Modified: trunk/htdocs/js/editprofile.js =================================================================== --- trunk/htdocs/js/editprofile.js 2010-09-03 18:40:30 UTC (rev 9463) +++ trunk/htdocs/js/editprofile.js 2010-09-06 03:18:42 UTC (rev 9464) @@ -1,10 +1,41 @@ jQuery(function($) { + + $.fn.maskInput = function(options) + { + var supportedDEF = [901,903,905,906,909,920,921,922,923,924,925,926,927,928,929,930,931,932,933,934,936,937,938,960,961,962,963,964,965,967,968,997], + mask = /^(\+?[17]|8)\d{10}$/, + allowedChars = "0-9"; + var lastValue; + + function checkDEF(val) + { + var def = parseInt(val.substr(1,3),10); + + return ($.inArray(def, supportedDEF) > -1); + } + + function processInput(ev) + { + var val = $.trim(this.value).replace(new RegExp("[^" + allowedChars + "]", "g"),""); + if(typeof lastValue == undefined || lastValue != val) + { + options.update(mask.test(val), checkDEF(val)); + lastValue = val; + } + } + + this.input(processInput); + processInput.call(this.get(0)); + } + + var PhoneVerification = { init: function() { this.supportedProviders = ['beeline', 'megafon']; this.table = $('#phone_verification'); + this.state = []; if(!this.table.length) { @@ -17,53 +48,59 @@ resendBtn: this.table.find('[name=sendsms]'), phInput: this.table.find('[name=txtmsg_number]'), phViewOptions: $('#phone_view_options'), - notification: this.table.find('.b-manage-smsn-status') + phVerification: $('#phone_vercode'), + notification: this.table.find('.b-manage-smsn-status'), + verCode: $('#phone_vercode [name=txtmsg_verify]'), + sendCodeBtn: $('#phone_vercode [name=verify]') } this.verifyTimer = null; this.endpoint = "/tools/endpoints/validate_phone.bml"; + this.stopVerification = false; this.supportedProviderEls = this.table.find('.b-manage-smsn-status') .add(this.table.find('.b-manage-smsn-btn')) - .add(this.table.find('.helper')) + .add(this.table.find('.helper')); this.unsupportedProviderEls = this.ui.phViewOptions - .add(this.table.find('tr:first span')) + .add(this.table.find('tr:first span')); this.verifiedElement = this.table.find('.b-manage-smsn-btn'); this.table.find('.b-manage-smsn').removeClass('b-manage-smsn-hide'); var that = this; this.viewOptionsValue = ''; - this.ui.provider.change(function() - { - that.checkProvider(this.value); - }); + this.ui.provider.change(function(){ that.checkProvider(this.value); }); - if(this.checkProvider(this.ui.provider.val())) - { - if(this.testPhoneNumber(this.ui.phInput.val())) - this.checkVerification(); - else - that.setState('badNum', 'error', 'badnum'); - } - else { - this.setState('initial', 'info', 'initial'); - } - this.ui.phInput.input(function(ev) - { - if(this.value.length == 0) + var initial = true; + this.ui.phInput.maskInput({ + update: function(isok, isGoodDef) { - that.setState('initial', 'info', 'initial'); - return; - } + if(!that.checkProvider(that.ui.provider.val()) || that.ui.phInput.val().length == 0) + { + that.setState('initial', 'info', 'initial'); + return; + } - if(that.testPhoneNumber(this.value)) - { - that.setState('correctNum', 'success', 'correctnum'); + if(!isok) + { + that.setState('badNum', 'error', 'badnum'); + return; + } + + if(!isGoodDef) + { + that.setState('unsupporteddef', 'error', 'unsupporteddef'); + return; + } + + if(initial) + { + that.checkVerification(); + initial = false; + } + else + that.setState('correctNum', 'success', 'correctnum'); } - else { - that.setState('badNum', 'error', 'badnum'); - } }); this.ui.resendBtn.click(function() @@ -72,9 +109,27 @@ that.sendVerification(phone); }); + this.ui.sendCodeBtn.click(function() + { + var code = that.ui.verCode.val(); + + if(code.length == 0) + return; + + $.get(that.endpoint + '?action=verify_short_code&shortcode=' + code, function(result) + { + var resCode = $.trim(result.split(":")[1]); + + if(resCode == "1") + that.setState('verified', 'success', 'verified'); + else + that.setState('wrongcode', 'error', 'wrongcode'); + }); + }); + this.ui.notification.ajaxError(function(ev, req, settings) { - $(this).html('ajax error').setClass('notifyerror'); + $(this).html('ajax error').setClass('b-bubble-warning'); }); }, @@ -91,6 +146,15 @@ { this.setState('initial', 'info', 'initial'); } + + var state = this.getState()[2] || ""; + if(state == "reqsent") + { + this.ui.phVerification.removeAttr('style'); + this.skipReqSent = false; + this.checkVerification(); + } + return true; } else @@ -99,6 +163,7 @@ this.unsupportedProviderEls.css('display',''); this.ui.phViewOptions.val(this.viewOptionsValue); this.viewOptionsValue = ''; + clearInterval(this.verifyTimer); return false; } }, @@ -122,10 +187,15 @@ checkVerification: function() { var that = this; + clearInterval(this.verifyTimer); + this.stopVerification = false; this.verifyTimer = setInterval(function() { $.get(that.endpoint, function(data) { + if(that.stopVerification) + return; + var result = $.trim(data.replace('status:','')); switch(result) { @@ -140,14 +210,22 @@ }, 1500); }, + getState: function() + { + return this.state; + }, + setState: function(text, status, event) { event = event || ""; var n = this.ui.notification, bt = this.ui.resendBtn, dict = smsNotifyDict || {}, - bubble = n.find('.i-bubble'); + bubble = n.find('.i-bubble'), + that = this; + this.state = Array.prototype.slice.call(arguments); + function setBubble(state) { var states = { @@ -160,6 +238,20 @@ bubble[(state == i)?"addClass":"removeClass"](states[i]); } + function resetUI(hideSendButton) + { + hideSendButton = hideSendButton || false; + that.ui.resendBtn.parent('.b-manage-smsn-btn').css('display', (hideSendButton) ? 'none' : ''); + clearInterval(that.verifyTimer); + this.stopVerification = true; + that.ui.phVerification.css('display', 'none'); + that.ui.verCode.val(''); + that.skipReqSent = false; + } + + if(this.skipReqSent && event == 'reqsent') + return; + switch(status) { case 'info': setBubble('info'); break; @@ -167,6 +259,7 @@ case 'error': setBubble('error'); break; } + if(event.length > 0) { var eventClasses = ['initial', 'badnum', 'correctnum', 'reqsent', 'rateexceed', 'verified']; @@ -177,34 +270,29 @@ case 'initial': case 'badnum': bt.attr('disabled', true); bt.val(smsNotifyDict['sendsmsbut']); - this.ui.resendBtn.parent('.b-manage-smsn-btn').css('display',''); - clearInterval(this.verifyTimer); + resetUI(); break; case 'correctnum': bt.attr('disabled', false); bt.val(smsNotifyDict['sendsmsbut']); - this.ui.resendBtn.parent('.b-manage-smsn-btn').css('display',''); - clearInterval(this.verifyTimer); + resetUI(); break; case 'reqsent': bt.val( dict['resendsmsbut']); this.ui.resendBtn.parent('.b-manage-smsn-btn').css('display',''); + this.ui.phVerification.removeAttr('style') break; case 'rateexceed': bt.attr('disabled', true); - this.ui.resendBtn.parent('.b-manage-smsn-btn').css('display',''); + resetUI(); break; case 'verified': /* hide send button */ - clearInterval(this.verifyTimer); - this.ui.resendBtn.parent('.b-manage-smsn-btn').css('display','none'); + resetUI(true); break; + case 'wrongcode': this.skipReqSent = true; + break; } } var msg = smsNotifyDict[text] || text; bubble.find('span').html(msg); - }, - - testPhoneNumber: function(number) - { - return /^\+\d{11,16}$/.test(number); } } Modified: trunk/htdocs/manage/profile/index.bml.text.local =================================================================== --- trunk/htdocs/manage/profile/index.bml.text.local 2010-09-03 18:40:30 UTC (rev 9463) +++ trunk/htdocs/manage/profile/index.bml.text.local 2010-09-06 03:18:42 UTC (rev 9464) @@ -39,6 +39,8 @@ .section.textmsg.info.badnum=Bad number +.section.textmsg.info.unsupporteddef=Your provider is not supported. Only Megafon and Beeline are + .section.textmsg.info.correctnum=Correct number .section.textmsg.info.requestsent=Request has been sent @@ -53,6 +55,8 @@ .section.textmsg.info.totallimitexceeded=SMS limit has been exceeded +.section.textmsg.info.wrongcode=You entered wrong code. Please try again + .section.textmsg.info.verified=Phone number has been verified .section.textmsg.info.linkexpired=Link has expired, please resend SMS