From 44a4ff3b527c1258f644edb57d7bb2c88c64552d Mon Sep 17 00:00:00 2001 From: lilia Date: Wed, 12 Apr 2017 18:43:00 -0700 Subject: [PATCH] Refactor install view Let install view manage the connection to the provisioning socket as well as cleaning up the window on completion, simplifying options.js. Call `remove` so that the view stops listening when the window closes. Move view script and template to background page. Adds ability to hide nav if this isn't our first run. // FREEBIE --- background.html | 90 +++++++++++++++++++++++++++++++++++++++ js/background.js | 5 ++- js/options.js | 59 +++++--------------------- js/views/install_view.js | 49 +++++++++++++++++++++- options.html | 91 ---------------------------------------- 5 files changed, 152 insertions(+), 142 deletions(-) diff --git a/background.html b/background.html index 74671985c..d53057fe0 100644 --- a/background.html +++ b/background.html @@ -508,6 +508,95 @@ {{/action }} + + @@ -559,6 +648,7 @@ + diff --git a/js/background.js b/js/background.js index 8f5c67f9b..1f7e523e5 100644 --- a/js/background.js +++ b/js/background.js @@ -126,14 +126,15 @@ storage.put('theme-setting', 'ios'); } var syncRequest = new textsecure.SyncRequest(textsecure.messaging, messageReceiver); + Whisper.events.trigger('contactsync:begin'); syncRequest.addEventListener('success', function() { console.log('sync successful'); storage.put('synced_at', Date.now()); - window.dispatchEvent(new Event('textsecure:contactsync')); + Whisper.events.trigger('contactsync'); }); syncRequest.addEventListener('timeout', function() { console.log('sync timed out'); - window.dispatchEvent(new Event('textsecure:contactsync')); + Whisper.events.trigger('contactsync'); }); } } diff --git a/js/options.js b/js/options.js index 9f256e7b1..7605142fe 100644 --- a/js/options.js +++ b/js/options.js @@ -6,56 +6,19 @@ extension.windows.getBackground(function(bg) { bg.storage.onready(function() { $(function() { - var deviceName = bg.textsecure.storage.user.getDeviceName(); - if (!deviceName) { - deviceName = 'Chrome'; - if (navigator.userAgent.match('Mac OS')) { - deviceName += ' on Mac'; - } else if (navigator.userAgent.match('Linux')) { - deviceName += ' on Linux'; - } else if (navigator.userAgent.match('Windows')) { - deviceName += ' on Windows'; - } - } - var view = new Whisper.InstallView({ - el: $('#install'), - deviceName: deviceName + var Whisper = bg.Whisper; + var installView = new Whisper.InstallView({ + el: $('#install') }); - if (bg.Whisper.Registration.everDone()) { - view.selectStep(3); + if (Whisper.Registration.everDone()) { + installView.selectStep(3); + installView.hideDots(); } - view.$el.show(); - var accountManager = new bg.getAccountManager(); - - var init = function() { - view.clearQR(); - - accountManager.registerSecondDevice( - view.setProvisioningUrl.bind(view), - view.confirmNumber.bind(view), - view.incrementCounter.bind(view) - ).then(function() { - var launch = function() { - bg.openInbox(); - bg.removeEventListener('textsecure:contactsync', launch); - window.close(); - }; - bg.addEventListener('textsecure:contactsync', launch); - view.showSync(); - }).catch(function(e) { - if (e.message === 'websocket closed') { - view.showConnectionError(); - setTimeout(init, 10000); - } else if (e.name === 'HTTPError' && e.code == 411) { - view.showTooManyDevices(); - } - else { - throw e; - } - }); - }; - $('.error-dialog .ok').click(init); - init(); + installView.$el.show(); + Whisper.events.on('contactsync', function() { + bg.openInbox(); + window.close(); + }); }); }); }); diff --git a/js/views/install_view.js b/js/views/install_view.js index d729c86c3..eb34a4dd5 100644 --- a/js/views/install_view.js +++ b/js/views/install_view.js @@ -7,6 +7,8 @@ Whisper.InstallView = Whisper.View.extend({ templateName: 'install_flow_template', + id: 'install', + className: 'main', render_attributes: function() { var playStoreHref = 'https://play.google.com/store/apps/details?id=org.thoughtcrime.securesms'; var appStoreHref = 'https://itunes.apple.com/us/app/signal-private-messenger/id874139669'; @@ -31,11 +33,53 @@ this.render(); - this.$('#device-name').val(options.deviceName); + var deviceName = textsecure.storage.user.getDeviceName(); + if (!deviceName) { + deviceName = 'Chrome'; + if (navigator.userAgent.match('Mac OS')) { + deviceName += ' on Mac'; + } else if (navigator.userAgent.match('Linux')) { + deviceName += ' on Linux'; + } else if (navigator.userAgent.match('Windows')) { + deviceName += ' on Windows'; + } + } + + this.$('#device-name').val(deviceName); this.$('#step1').show(); + this.connect(); + this.on('disconnected', this.reconnect); + this.listenTo(Whisper.events, 'contactsync:begin', this.showSync); + this.listenTo(Whisper.events, 'contactsync', this.close); + }, + connect: function() { + this.clearQR(); + var accountManager = getAccountManager(); + accountManager.registerSecondDevice( + this.setProvisioningUrl.bind(this), + this.confirmNumber.bind(this), + this.incrementCounter.bind(this) + ).catch(this.handleDisconnect.bind(this)); + }, + handleDisconnect: function(e) { + if (e.message === 'websocket closed') { + this.showConnectionError(); + this.trigger('disconnected'); + } else if (e.name === 'HTTPError' && e.code == 411) { + this.showTooManyDevices(); + } else { + throw e; + } + }, + reconnect: function() { + setTimeout(this.connect.bind(this), 10000); + }, + close: function() { + this.remove(); }, events: function() { return { + 'click .error-dialog .ok': 'connect', 'click .step1': this.selectStep.bind(this, 1), 'click .step2': this.selectStep.bind(this, 2), 'click .step3': this.selectStep.bind(this, 3) @@ -94,6 +138,9 @@ }, showConnectionError: function() { this.$('#qr').text(i18n("installConnectionFailed")); + }, + hideDots: function() { + this.$('#step3 .nav .dot').hide(); } }); })(); diff --git a/options.html b/options.html index 5abf1af5d..408359c4a 100644 --- a/options.html +++ b/options.html @@ -8,94 +8,6 @@
-
@@ -106,9 +18,6 @@ - - -