diff --git a/background.html b/background.html index f0a891ea0..ce87cf769 100644 --- a/background.html +++ b/background.html @@ -245,6 +245,40 @@ + + + diff --git a/js/views/app_view.js b/js/views/app_view.js index f9f306789..364e758e3 100644 --- a/js/views/app_view.js +++ b/js/views/app_view.js @@ -196,5 +196,15 @@ const dialog = new Whisper.SeedDialogView({ seed }); this.el.append(dialog.el); }, + showDevicePairingDialog() { + const dialog = new Whisper.DevicePairingDialogView(); + Whisper.events.on('devicePairingRequestReceived', pubKey => + dialog.requestReceived(pubKey) + ); + dialog.on('devicePairingRequestAccepted', (pubKey, cb) => + Whisper.events.trigger('devicePairingRequestAccepted', pubKey, cb) + ); + this.el.append(dialog.el); + }, }); })(); diff --git a/js/views/device_pairing_dialog_view.js b/js/views/device_pairing_dialog_view.js new file mode 100644 index 000000000..2e18ab1f4 --- /dev/null +++ b/js/views/device_pairing_dialog_view.js @@ -0,0 +1,91 @@ +/* global Whisper, i18n */ + +// eslint-disable-next-line func-names +(function() { + 'use strict'; + + window.Whisper = window.Whisper || {}; + + Whisper.DevicePairingDialogView = Whisper.View.extend({ + className: 'loki-dialog device-pairing-dialog modal', + templateName: 'device-pairing-dialog', + initialize() { + this.pubKeyRequests = []; + this.pubKey = null; + this.accepted = false; + this.view = ''; + this.render(); + this.showView(); + }, + events: { + 'click .waitingForRequestView .cancel': 'close', + 'click .requestReceivedView .skip': 'skipDevice', + 'click #allowPairing': 'allowDevice', + 'click .requestAcceptedView .ok': 'close', + }, + render_attributes() { + return { + waitingForRequestTitle: 'Waiting for device to register...', + requestReceivedTitle: 'Device Pairing Received', + requestAcceptedTitle: 'Device Pairing Accepted', + cancelText: i18n('cancel'), + skipText: 'Skip', + okText: i18n('ok'), + allowPairingText: 'Allow Pairing', + }; + }, + requestReceived(secondaryDevicePubKey) { + // FIFO: push at the front of the array with unshift() + this.pubKeyRequests.unshift(secondaryDevicePubKey); + if (!this.pubKey) { + this.nextPubKey(); + this.showView('requestReceived'); + } + }, + allowDevice() { + this.accepted = true; + this.trigger('devicePairingRequestAccepted', this.pubKey, errors => + this.transmisssionCB(errors) + ); + this.showView(); + }, + transmisssionCB(errors) { + if (!errors) { + this.$('.transmissionStatus').text('Sent successfully'); + } else { + this.$('.transmissionStatus').text(errors); + } + this.$('.requestAcceptedView .ok').show(); + }, + skipDevice() { + this.nextPubKey(); + this.showView(); + }, + nextPubKey() { + // FIFO: pop at the back of the array using pop() + this.pubKey = this.pubKeyRequests.pop(); + }, + showView() { + const waitingForRequestView = this.$('.waitingForRequestView'); + const requestReceivedView = this.$('.requestReceivedView'); + const requestAcceptedView = this.$('.requestAcceptedView'); + if (this.accepted) { + requestReceivedView.hide(); + waitingForRequestView.hide(); + requestAcceptedView.show(); + } else if (this.pubKey) { + this.$('.secondaryPubKey').text(this.pubKey); + requestReceivedView.show(); + waitingForRequestView.hide(); + requestAcceptedView.hide(); + } else { + waitingForRequestView.show(); + requestReceivedView.hide(); + requestAcceptedView.hide(); + } + }, + close() { + this.remove(); + }, + }); +})();