copay/js/controllers/send.js

179 lines
5.2 KiB
JavaScript
Raw Normal View History

2014-03-26 05:18:42 -07:00
'use strict';
angular.module('copayApp.send').controller('SendController',
2014-05-06 13:02:49 -07:00
function($scope, $rootScope, $window, $location, $timeout) {
2014-03-26 05:18:42 -07:00
$scope.title = 'Send';
2014-04-24 18:43:19 -07:00
$scope.loading = false;
2014-05-06 13:02:49 -07:00
// Detect mobile devices
var isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i);
},
any: function() {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};
// Detect protocol
2014-05-22 10:40:46 -07:00
$scope.isHttp = ($window.location.protocol.indexOf('http') === 0);
2014-05-06 13:02:49 -07:00
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
$scope.isMobile = isMobile.any();
$scope.unitIds = ['BTC','mBTC'];
$scope.selectedUnit = $scope.unitIds[0];
$scope.submitForm = function(form) {
if (form.$invalid) {
2014-05-20 08:30:20 -07:00
$rootScope.$flashMessage = { message: 'You can not send a proposal transaction. Please, try again', type: 'error'};
return;
}
2014-04-24 18:43:19 -07:00
$scope.loading = true;
var address = form.address.$modelValue;
var amount = (form.amount.$modelValue * 100000000).toString(); // satoshi to string
var w = $rootScope.wallet;
2014-05-21 07:10:19 -07:00
w.createTx(address, amount,function() {
2014-04-24 18:43:19 -07:00
$scope.loading = false;
2014-05-20 08:30:20 -07:00
$rootScope.$flashMessage = { message: 'The transaction proposal has been created', type: 'success'};
$rootScope.$digest();
});
2014-04-24 18:43:19 -07:00
// reset fields
$scope.address = null;
$scope.amount = null;
form.address.$pristine = true;
form.amount.$pristine = true;
2014-05-06 13:02:49 -07:00
};
// QR code Scanner
var cameraInput;
var video;
var canvas;
var $video;
var context;
var localMediaStream;
var _scan = function(evt) {
if ($scope.isMobile) {
$scope.scannerLoading = true;
var files = evt.target.files;
if (files.length === 1 && files[0].type.indexOf('image/') === 0) {
var file = files[0];
var reader = new FileReader();
reader.onload = (function(theFile) {
return function(e) {
var mpImg = new MegaPixImage(file);
mpImg.render(canvas, { maxWidth: 200, maxHeight: 200, orientation: 6 });
$timeout(function() {
qrcode.width = canvas.width;
qrcode.height = canvas.height;
qrcode.imagedata = context.getImageData(0, 0, qrcode.width, qrcode.height);
try {
//alert(JSON.stringify(qrcode.process(context)));
qrcode.decode();
} catch (e) {
2014-05-21 12:10:39 -07:00
console.log('error decoding QR: '+e);
2014-05-06 13:02:49 -07:00
}
}, 1500);
};
})(file);
// Read in the file as a data URL
reader.readAsDataURL(file);
}
} else {
if (localMediaStream) {
context.drawImage(video, 0, 0, 300, 225);
try {
qrcode.decode();
} catch(e) {
//qrcodeError(e);
}
}
$timeout(_scan, 500);
}
};
var _successCallback = function(stream) {
video.src = (window.URL && window.URL.createObjectURL(stream)) || stream;
localMediaStream = stream;
video.play();
$timeout(_scan, 1000);
};
var _scanStop = function() {
$scope.scannerLoading = false;
$scope.showScanner = false;
if (!$scope.isMobile) {
if (localMediaStream && localMediaStream.stop) localMediaStream.stop();
localMediaStream = null;
video.src = '';
}
};
var _videoError = function(err) {
console.log('Video Error: ' + JSON.stringify(err));
_scanStop();
};
qrcode.callback = function(data) {
_scanStop();
var str = (data.indexOf('bitcoin:') === 0) ? data.substring(8) : data;
console.log('QR code detected: ' + str);
2014-05-07 07:21:30 -07:00
$scope.$apply(function() {
$scope.address = str;
});
2014-05-06 13:02:49 -07:00
};
$scope.cancelScanner = function() {
_scanStop();
};
$scope.openScanner = function() {
$scope.showScanner = true;
// Wait a moment until the canvas shows
$timeout(function() {
canvas = document.getElementById('qr-canvas');
context = canvas.getContext('2d');
if ($scope.isMobile) {
cameraInput = document.getElementById('qrcode-camera');
cameraInput.addEventListener('change', _scan, false);
} else {
video = document.getElementById('qrcode-scanner-video');
$video = angular.element(video);
canvas.width = 300;
canvas.height = 225;
context.clearRect(0, 0, 300, 225);
2014-04-24 18:43:19 -07:00
2014-05-06 13:02:49 -07:00
navigator.getUserMedia({video: true}, _successCallback, _videoError);
}
}, 500);
2014-04-24 18:43:19 -07:00
};
2014-03-26 05:18:42 -07:00
});