copay/js/services/controllerUtils.js

99 lines
2.9 KiB
JavaScript
Raw Normal View History

2014-04-17 07:46:49 -07:00
'use strict';
2014-04-23 17:20:44 -07:00
angular.module('copay.controllerUtils')
.factory('controllerUtils', function($rootScope, $sce, $location, Socket, video) {
var root = {};
$rootScope.videoSrc = {};
$rootScope.getVideoURL = function(copayer) {
return $sce.trustAsResourceUrl(decodeURI($rootScope.videoSrc[copayer]));
2014-04-23 17:20:44 -07:00
};
2014-04-23 17:20:44 -07:00
root.logout = function() {
console.log('### DELETING WALLET'); //TODO
$rootScope.wallet = null;
delete $rootScope['wallet'];
$rootScope.totalBalance = 0;
$location.path('signin');
};
2014-04-23 17:20:44 -07:00
root.onError = function(scope) {
if (scope) scope.loading = false;
$rootScope.flashMessage = {
type: 'error',
message: 'Could not connect to peer: ' +
scope
};
root.logout();
}
2014-04-23 14:07:20 -07:00
2014-04-23 17:20:44 -07:00
root.onErrorDigest = function(scope) {
root.onError(scope);
$rootScope.$digest();
}
root.setupUxHandlers = function(w) {
var handlePeerVideo = function(err, peerID, url) {
if (err) {
root.onErrorDigest(err);
}
2014-04-24 12:07:49 -07:00
alert('add this video url='+url+' for peer '+peerID);
$rootScope.videoSrc[peerID] = encodeURI(url);
2014-04-23 17:20:44 -07:00
$rootScope.$apply();
};
w.on('badMessage', function(peerId) {
$rootScope.flashMessage = {
type: 'error',
message: 'Received wrong message from peer id:' + peerId
};
});
2014-04-24 12:07:49 -07:00
w.on('created', function(myPeerID) {
video.setOwnPeer(myPeerID, w, handlePeerVideo);
2014-04-23 17:20:44 -07:00
$location.path('peer');
$rootScope.wallet = w;
root.updateBalance();
});
w.on('refresh', function() {
console.log('[controllerUtils.js] Refreshing'); //TODO
root.updateBalance();
});
w.on('openError', root.onErrorDigest);
w.on('peer', function(peerID) {
2014-04-24 12:07:49 -07:00
video.callPeer(peerID, handlePeerVideo);
2014-04-23 17:20:44 -07:00
});
w.on('close', root.onErrorDigest);
w.netStart();
};
2014-04-23 17:20:44 -07:00
root.updateBalance = function() {
var w = $rootScope.wallet;
if (!w) return;
w.getBalance(false, function(balance, balanceByAddr) {
$rootScope.totalBalance = balance;
$rootScope.balanceByAddr = balanceByAddr;
console.log('New balance:', balance);
w.getBalance(true, function(balance) {
$rootScope.availableBalance = balance;
$rootScope.$digest();
});
2014-04-21 09:16:15 -07:00
});
2014-04-23 17:20:44 -07:00
};
2014-04-17 07:46:49 -07:00
2014-04-23 17:20:44 -07:00
root.setSocketHandlers = function() {
Socket.removeAllListeners();
2014-04-18 15:08:01 -07:00
2014-04-23 17:20:44 -07:00
var addrs = $rootScope.wallet.getAddressesStr();
for (var i = 0; i < addrs.length; i++) {
console.log('### SUBSCRIBE TO', addrs[i]);
Socket.emit('subscribe', addrs[i]);
}
console.log('[controllerUtils.js.64]'); //TODO
addrs.forEach(function(addr) {
Socket.on(addr, function(txid) {
console.log('Received!', txid);
root.updateBalance();
});
});
2014-04-23 17:20:44 -07:00
};
2014-04-17 07:46:49 -07:00
2014-04-23 17:20:44 -07:00
return root;
});