From 10d19b581674d8f9d96a75734010f8d542e856a1 Mon Sep 17 00:00:00 2001 From: Felix Hammerl Date: Tue, 22 Jul 2014 21:05:34 +0200 Subject: [PATCH] [WO-464] jump to the next keychain code and allow paste --- src/js/app.js | 1 + .../controller/login-privatekey-download.js | 20 +++++++++ src/js/controller/privatekey-upload.js | 45 ++++++++++++++++++- src/tpl/login-privatekey-download.html | 10 ++--- src/tpl/privatekey-upload.html | 10 ++--- .../login-privatekey-download-ctrl-test.js | 20 +++++++++ test/unit/privatekey-upload-ctrl-test.js | 20 +++++++++ 7 files changed, 115 insertions(+), 11 deletions(-) diff --git a/src/js/app.js b/src/js/app.js index 35ca9e8..8e6ce23 100644 --- a/src/js/app.js +++ b/src/js/app.js @@ -64,6 +64,7 @@ requirejs([ 'read', 'contacts', 'login-new-device', + 'privatekey-upload', 'popover', 'infinite-scroll' ]); diff --git a/src/js/controller/login-privatekey-download.js b/src/js/controller/login-privatekey-download.js index 4ea5532..6cb186f 100644 --- a/src/js/controller/login-privatekey-download.js +++ b/src/js/controller/login-privatekey-download.js @@ -10,6 +10,26 @@ define(function(require) { $scope.step = 1; + $scope.handlePaste = function(event) { + var evt = event; + if (evt.originalEvent) { + evt = evt.originalEvent; + } + + var value = evt.clipboardData.getData('text/plain'); + if (!value) { + return; + } + + value = value.replace(/-/g, ''); + $scope.code0 = value.slice(0, 4); + $scope.code1 = value.slice(4, 8); + $scope.code2 = value.slice(8, 12); + $scope.code3 = value.slice(12, 16); + $scope.code4 = value.slice(16, 20); + $scope.code5 = value.slice(20, 24); + }; + $scope.verifyRecoveryToken = function(callback) { if (!$scope.recoveryToken) { $scope.onError(new Error('Please set the recovery token!')); diff --git a/src/js/controller/privatekey-upload.js b/src/js/controller/privatekey-upload.js index 020a578..61736a7 100644 --- a/src/js/controller/privatekey-upload.js +++ b/src/js/controller/privatekey-upload.js @@ -1,7 +1,8 @@ define(function(require) { 'use strict'; - var appController = require('js/app-controller'), + var angular = require('angular'), + appController = require('js/app-controller'), keychain, pgp; var PrivateKeyUploadCtrl = function($scope) { @@ -37,6 +38,26 @@ define(function(require) { } }; + $scope.handlePaste = function(event) { + var evt = event; + if (evt.originalEvent) { + evt = evt.originalEvent; + } + + var value = evt.clipboardData.getData('text/plain'); + if (!value) { + return; + } + + value = value.replace(/-/g, ''); + $scope.code0 = value.slice(0, 4); + $scope.code1 = value.slice(4, 8); + $scope.code2 = value.slice(8, 12); + $scope.code3 = value.slice(12, 16); + $scope.code4 = value.slice(16, 20); + $scope.code5 = value.slice(20, 24); + }; + $scope.checkServerForKey = function(callback) { var keyParams = pgp.getKeyParams(); keychain.requestPrivateKeyDownload({ @@ -166,5 +187,27 @@ define(function(require) { }; + // + // Directives + // + + var ngModule = angular.module('privatekey-upload', []); + ngModule.directive('focusNext', function() { + return { + link: function(scope, element, attr) { + var maxLen = element[0].maxLength; + + scope.$watch(attr.ngModel, function(val) { + if (val.length === maxLen) { + var nextinput = element.next('input'); + if (nextinput.length) { + nextinput[0].focus(); + } + } + }); + } + }; + }); + return PrivateKeyUploadCtrl; }); \ No newline at end of file diff --git a/src/tpl/login-privatekey-download.html b/src/tpl/login-privatekey-download.html index e481f1a..1c8266e 100644 --- a/src/tpl/login-privatekey-download.html +++ b/src/tpl/login-privatekey-download.html @@ -13,11 +13,11 @@

Key sync. Please enter the keychain code you wrote down during sync setup.

- - - - - - - - - - + - + - + - + - + -
diff --git a/src/tpl/privatekey-upload.html b/src/tpl/privatekey-upload.html index f1433ca..2d34293 100644 --- a/src/tpl/privatekey-upload.html +++ b/src/tpl/privatekey-upload.html @@ -17,11 +17,11 @@

Please confirm the keychain code you have written down.

- - - - - - - - - - + - + - + - + - + -
diff --git a/test/unit/login-privatekey-download-ctrl-test.js b/test/unit/login-privatekey-download-ctrl-test.js index bb9c32a..abedb75 100644 --- a/test/unit/login-privatekey-download-ctrl-test.js +++ b/test/unit/login-privatekey-download-ctrl-test.js @@ -108,6 +108,26 @@ define(function(require) { }); }); + describe('handlePaste', function() { + it('should work', function() { + scope.handlePaste({ + clipboardData: { + getData: function(val) { + expect(val).to.equal('text/plain'); + return '1qaz-2wsx-3edc-4rfv-5tgb-6yhn'; + } + } + }); + + expect(scope.code0).to.equal('1qaz'); + expect(scope.code1).to.equal('2wsx'); + expect(scope.code2).to.equal('3edc'); + expect(scope.code3).to.equal('4rfv'); + expect(scope.code4).to.equal('5tgb'); + expect(scope.code5).to.equal('6yhn'); + }); + }); + describe('decryptAndStorePrivateKeyLocally', function() { beforeEach(function() { scope.code0 = '0'; diff --git a/test/unit/privatekey-upload-ctrl-test.js b/test/unit/privatekey-upload-ctrl-test.js index 5ee8328..a79ad41 100644 --- a/test/unit/privatekey-upload-ctrl-test.js +++ b/test/unit/privatekey-upload-ctrl-test.js @@ -87,6 +87,26 @@ define(function(require) { }); }); + describe('handlePaste', function() { + it('should work', function() { + scope.handlePaste({ + clipboardData: { + getData: function(val) { + expect(val).to.equal('text/plain'); + return '1qaz-2wsx-3edc-4rfv-5tgb-6yhn'; + } + } + }); + + expect(scope.code0).to.equal('1qaz'); + expect(scope.code1).to.equal('2wsx'); + expect(scope.code2).to.equal('3edc'); + expect(scope.code3).to.equal('4rfv'); + expect(scope.code4).to.equal('5tgb'); + expect(scope.code5).to.equal('6yhn'); + }); + }); + describe('displayUploadUi', function() { it('should work', function() { var generateCodeStub = sinon.stub(scope, 'generateCode');