session-desktop/js/views/standalone_registration_view.js

100 lines
2.6 KiB
JavaScript
Raw Normal View History

/* global Whisper, $, getAccountManager, textsecure */
/* eslint-disable more/no-then */
// eslint-disable-next-line func-names
2018-04-27 23:25:04 +02:00
(function() {
'use strict';
2018-04-27 23:25:04 +02:00
window.Whisper = window.Whisper || {};
2018-04-27 23:25:04 +02:00
Whisper.StandaloneRegistrationView = Whisper.View.extend({
templateName: 'standalone',
className: 'full-screen-flow',
initialize() {
2018-04-27 23:25:04 +02:00
this.accountManager = getAccountManager();
2018-04-27 23:25:04 +02:00
this.render();
const number = textsecure.storage.user.getNumber();
2018-04-27 23:25:04 +02: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',
2018-08-16 05:49:48 +02:00
'click #register': 'register',
2018-04-27 23:25:04 +02:00
},
2018-08-16 05:49:48 +02:00
register() {
2018-04-27 23:25:04 +02:00
this.accountManager
2018-08-16 05:49:48 +02:00
.registerSingleDevice()
.then(() => {
this.$el.trigger('openInbox');
})
2018-04-27 23:25:04 +02:00
.catch(this.log.bind(this));
},
log(s) {
window.log.info(s);
2018-04-27 23:25:04 +02:00
this.$('#status').text(s);
},
displayError(error) {
2018-04-27 23:25:04 +02:00
this.$('#error')
.hide()
.text(error)
.addClass('in')
.fadeIn();
},
onValidation() {
2018-04-27 23:25:04 +02: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 23:25:04 +02:00
if (!this.validateCode()) {
this.$('#code').addClass('invalid');
} else {
this.$('#code').removeClass('invalid');
}
},
requestVoice() {
2018-04-27 23:25:04 +02:00
window.removeSetupMenuItems();
this.$('#error').hide();
const number = this.phoneView.validateNumber();
2018-04-27 23:25:04 +02: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 23:25:04 +02:00
window.removeSetupMenuItems();
$('#error').hide();
const number = this.phoneView.validateNumber();
2018-04-27 23:25:04 +02:00
if (number) {
this.accountManager
.requestSMSVerification(number)
.catch(this.displayError.bind(this));
this.$('#step2')
.addClass('in')
.fadeIn();
} else {
this.$('#number-container').addClass('invalid');
}
},
});
})();