From 1f4f78bec009b39e3955b1408165a7fc19d6a217 Mon Sep 17 00:00:00 2001 From: Kosta Korenkov <7r0ggy@gmail.com> Date: Thu, 23 Jul 2015 13:21:31 +0300 Subject: [PATCH] Make directive to accept onScan and beforeScan callbacks instead of broadcasting 'dataScanned' event. This will allow to reuse directive for screens different from 'send' --- public/views/includes/topbar.html | 2 +- src/js/controllers/topbar.js | 10 +++++++++- src/js/directives/qrScanner.js | 17 +++++++++++------ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/public/views/includes/topbar.html b/public/views/includes/topbar.html index 6fc69dac5..b47a18b04 100644 --- a/public/views/includes/topbar.html +++ b/public/views/includes/topbar.html @@ -15,7 +15,7 @@
- +
diff --git a/src/js/controllers/topbar.js b/src/js/controllers/topbar.js index 1a15a91c0..74da473e5 100644 --- a/src/js/controllers/topbar.js +++ b/src/js/controllers/topbar.js @@ -1,6 +1,14 @@ 'use strict'; -angular.module('copayApp.controllers').controller('topbarController', function($rootScope, $scope, $timeout, $modal, isCordova, isMobile, go) { +angular.module('copayApp.controllers').controller('topbarController', function($rootScope, go) { + + this.onQrCodeScanned = function(data) { + $rootScope.$emit('dataScanned', data); + }; + + this.openSendScreen = function() { + go.send(); + }; this.goHome = function() { go.walletHome(); diff --git a/src/js/directives/qrScanner.js b/src/js/directives/qrScanner.js index 52feeef15..ac569eaa1 100644 --- a/src/js/directives/qrScanner.js +++ b/src/js/directives/qrScanner.js @@ -1,8 +1,8 @@ 'use strict'; angular.module('copayApp.directives') - .directive('qrScanner', ['$rootScope', '$timeout', '$modal', 'isCordova', 'isMobile', 'go', - function($rootScope, $timeout, $modal, isCordova, isMobile, go) { + .directive('qrScanner', ['$rootScope', '$timeout', '$modal', 'isCordova', + function($rootScope, $timeout, $modal, isCordova) { var controller = function($scope) { @@ -20,7 +20,7 @@ angular.module('copayApp.directives') $timeout(function() { var data = result.text; - $rootScope.$emit('dataScanned', data); + $scope.onScan({ data: data }); }, 1000); }, function onError(error) { @@ -31,11 +31,12 @@ angular.module('copayApp.directives') alert('Scanning error'); } ); - go.send(); + $scope.beforeScan(); }, 100); }; $scope.modalOpenScanner = function() { + var parentScope = $scope; var ModalInstanceCtrl = function($scope, $rootScope, $modalInstance) { // QR code Scanner var video; @@ -89,7 +90,7 @@ angular.module('copayApp.directives') $scope.init = function() { setScanner(); $timeout(function() { - go.send(); + parentScope.beforeScan(); canvas = document.getElementById('qr-canvas'); context = canvas.getContext('2d'); @@ -120,7 +121,7 @@ angular.module('copayApp.directives') keyboard: false }); modalInstance.result.then(function(data) { - $rootScope.$emit('dataScanned', data); + parentScope.onScan({ data: data }); }); }; @@ -137,6 +138,10 @@ angular.module('copayApp.directives') return { restrict: 'E', + scope: { + onScan: "&", + beforeScan: "&" + }, controller: controller, replace: true, template: ''