signal-desktop/js/views/standalone_registration_view.js

116 lines
3 KiB
JavaScript
Raw Normal View History

/* global Whisper, $, getAccountManager, textsecure */
/* eslint-disable more/no-then */
// eslint-disable-next-line func-names
2018-04-27 21:25:04 +00:00
(function() {
'use strict';
2018-04-27 21:25:04 +00:00
window.Whisper = window.Whisper || {};
2018-04-27 21:25:04 +00:00
Whisper.StandaloneRegistrationView = Whisper.View.extend({
templateName: 'standalone',
className: 'full-screen-flow',
initialize() {
2018-04-27 21:25:04 +00:00
this.accountManager = getAccountManager();
2018-04-27 21:25:04 +00:00
this.render();
const number = textsecure.storage.user.getNumber();
2018-04-27 21:25:04 +00:00
if (number) {
this.$('input.number').val(number);
}
this.phoneView = new Whisper.PhoneInputView({
el: this.$('#phone-number-input'),
});
this.$('#error').hide();
},
events: {
'validation input.number': 'onValidation',
'click #request-voice': 'requestVoice',
'click #request-sms': 'requestSMSVerification',
'change #code': 'onChangeCode',
'click #verifyCode': 'verifyCode',
},
verifyCode() {
const number = this.phoneView.validateNumber();
const verificationCode = $('#code')
2018-04-27 21:25:04 +00:00
.val()
.replace(/\D+/g, '');
2018-04-27 21:25:04 +00:00
this.accountManager
.registerSingleDevice(number, verificationCode)
.then(() => {
this.$el.trigger('openInbox');
})
2018-04-27 21:25:04 +00:00
.catch(this.log.bind(this));
},
log(s) {
window.log.info(s);
2018-04-27 21:25:04 +00:00
this.$('#status').text(s);
},
validateCode() {
const verificationCode = $('#code')
2018-04-27 21:25:04 +00:00
.val()
.replace(/\D/g, '');
if (verificationCode.length === 6) {
2018-04-27 21:25:04 +00:00
return verificationCode;
}
return null;
2018-04-27 21:25:04 +00:00
},
displayError(error) {
2018-04-27 21:25:04 +00:00
this.$('#error')
.hide()
.text(error)
.addClass('in')
.fadeIn();
},
onValidation() {
2018-04-27 21:25:04 +00:00
if (this.$('#number-container').hasClass('valid')) {
this.$('#request-sms, #request-voice').removeAttr('disabled');
} else {
this.$('#request-sms, #request-voice').prop('disabled', 'disabled');
}
},
onChangeCode() {
2018-04-27 21:25:04 +00:00
if (!this.validateCode()) {
this.$('#code').addClass('invalid');
} else {
this.$('#code').removeClass('invalid');
}
},
requestVoice() {
2018-04-27 21:25:04 +00:00
window.removeSetupMenuItems();
this.$('#error').hide();
const number = this.phoneView.validateNumber();
2018-04-27 21:25:04 +00:00
if (number) {
this.accountManager
.requestVoiceVerification(number)
.catch(this.displayError.bind(this));
this.$('#step2')
.addClass('in')
.fadeIn();
} else {
this.$('#number-container').addClass('invalid');
}
},
requestSMSVerification() {
2018-04-27 21:25:04 +00:00
window.removeSetupMenuItems();
$('#error').hide();
const number = this.phoneView.validateNumber();
2018-04-27 21:25:04 +00:00
if (number) {
this.accountManager
.requestSMSVerification(number)
.catch(this.displayError.bind(this));
this.$('#step2')
.addClass('in')
.fadeIn();
} else {
this.$('#number-container').addClass('invalid');
}
},
});
})();