Wallet/src/js/controllers/tab-send.js

245 lines
7.3 KiB
JavaScript
Raw Normal View History

2016-08-12 12:44:16 -03:00
'use strict';
2018-08-08 17:10:47 +02:00
angular.module('copayApp.controllers').controller('tabSendController', function($scope, $rootScope, $log, $timeout, $ionicScrollDelegate, $ionicLoading, addressbookService, profileService, lodash, $state, walletService, incomingData, popupService, platformInfo, sendFlowService, bwcError, gettextCatalog, scannerService, configService, bitcoinCashJsService, $ionicPopup, $ionicNavBarDelegate, clipboardService) {
2018-07-10 11:11:11 +02:00
var clipboardHasAddress = false;
var clipboardHasContent = false;
var originalList;
2018-07-12 15:25:11 +02:00
$scope.displayBalanceAsFiat = true;
$scope.walletSelectorTitleForce = true;
2018-07-10 11:11:11 +02:00
$scope.addContact = function() {
2018-07-16 15:52:33 +09:00
$state.go('tabs.send.addressbook');
2018-07-10 11:11:11 +02:00
};
$scope.pasteClipboard = function() {
if ($scope.clipboardHasAddress || $scope.clipboardHasContent) {
clipboardService.readFromClipboard(function(text) {
$scope.$apply(function() {
2018-07-17 11:02:25 +09:00
$scope.formData.search = text;
$scope.findContact($scope.formData.search);
});
2018-07-10 11:11:11 +02:00
});
2018-07-13 14:16:15 +02:00
} else {
$ionicPopup.alert({
title: gettextCatalog.getString('Clipboard'),
template: gettextCatalog.getString('Your Clipboard is empty')
});
2018-07-10 11:11:11 +02:00
}
};
$scope.$on("$ionicView.enter", function(event, data) {
var stateParams = sendFlowService.getStateClone();
$scope.fromWallet = profileService.getWallet(stateParams.fromWalletId);
2018-07-10 11:11:11 +02:00
clipboardService.readFromClipboard(function(text) {
if (text.length > 200) {
text = text.substring(0, 200);
}
$scope.clipboardHasAddress = false;
$scope.clipboardHasContent = false;
if ((text.indexOf('bitcoincash:') === 0 || text[0] === 'C' || text[0] === 'H' || text[0] === 'p' || text[0] === 'q') && text.replace('bitcoincash:', '').length === 42) { // CashAddr
$scope.clipboardHasAddress = true;
} else if ((text[0] === "1" || text[0] === "3" || text.substring(0, 3) === "bc1") && text.length >= 26 && text.length <= 35) { // Legacy Addresses
$scope.clipboardHasAddress = true;
} else if (text.length > 1) {
$scope.clipboardHasContent = true;
}
});
$ionicNavBarDelegate.showBar(true);
if (!$scope.hasWallets) {
$scope.checkingBalance = false;
return;
}
updateHasFunds();
updateContactsList(function() {
updateList();
});
});
2018-07-11 17:31:58 +02:00
$scope.findContact = function(search) {
2018-07-10 11:11:11 +02:00
if (incomingData.redir(search)) {
return;
}
2018-07-10 11:11:11 +02:00
if (!search || search.length < 1) {
$scope.list = originalList;
$timeout(function() {
$scope.$apply();
});
return;
}
2018-07-10 11:11:11 +02:00
var result = lodash.filter(originalList, function(item) {
var val = item.name;
return lodash.startsWith(val.toLowerCase(), search.toLowerCase());
});
2018-07-10 11:11:11 +02:00
$scope.list = result;
};
var hasWallets = function() {
2018-07-13 14:54:26 +02:00
$scope.walletsWithFunds = profileService.getWallets({
2018-07-13 14:02:48 +02:00
onlyComplete: true,
hasFunds: true
2016-09-08 16:54:35 -03:00
});
2018-07-13 14:54:26 +02:00
$scope.wallets = profileService.getWallets({
onlyComplete: true,
});
2018-07-12 15:25:11 +02:00
$scope.walletsBch = profileService.getWallets({
onlyComplete: true,
coin: 'bch'
});
$scope.walletsBtc = profileService.getWallets({
onlyComplete: true,
coin: 'btc'
});
$scope.hasWallets = lodash.isEmpty($scope.wallets) ? false : true;
};
var updateHasFunds = function() {
$scope.hasFunds = false;
var index = 0;
lodash.each($scope.wallets, function(w) {
walletService.getStatus(w, {}, function(err, status) {
++index;
if (err && !status) {
$log.error(err);
// error updating the wallet. Probably a network error, do not show
// the 'buy bitcoins' message.
$scope.hasFunds = true;
} else if (status.availableBalanceSat > 0) {
$scope.hasFunds = true;
}
2016-08-15 18:11:36 -03:00
if (index === $scope.wallets.length) {
$scope.checkingBalance = false;
$timeout(function() {
$scope.$apply();
});
}
});
});
};
2017-03-02 16:56:44 -03:00
var updateContactsList = function(cb) {
var config = configService.getSync();
2017-11-02 12:55:55 +09:00
var defaults = configService.getDefaults();
2016-08-15 18:11:36 -03:00
addressbookService.list(function(err, ab) {
if (err) $log.error(err);
2016-08-12 15:11:52 -03:00
$scope.hasContacts = lodash.isEmpty(ab) ? false : true;
2017-03-02 16:56:44 -03:00
if (!$scope.hasContacts) return cb();
2016-09-27 12:26:58 -03:00
var completeContacts = [];
2016-08-15 18:11:36 -03:00
lodash.each(ab, function(v, k) {
2016-09-27 12:26:58 -03:00
completeContacts.push({
2016-09-12 18:04:56 -03:00
name: lodash.isObject(v) ? v.name : v,
2016-08-18 19:25:30 -03:00
address: k,
2016-09-14 10:04:16 -03:00
email: lodash.isObject(v) ? v.email : null,
recipientType: 'contact',
coin: v.coin,
displayCoin: (v.coin == 'bch'
? (config.bitcoinCashAlias || defaults.bitcoinCashAlias)
: (config.bitcoinAlias || defaults.bitcoinAlias)).toUpperCase()
2016-08-15 18:11:36 -03:00
});
});
originalList = completeContacts;
2017-03-02 16:56:44 -03:00
return cb();
2016-08-15 18:11:36 -03:00
});
2016-08-12 16:30:50 -03:00
};
2017-03-02 16:56:44 -03:00
var updateList = function() {
$scope.list = lodash.clone(originalList);
$timeout(function() {
$ionicScrollDelegate.resize();
$scope.$apply();
}, 10);
};
2017-06-08 17:43:06 -04:00
$scope.searchInFocus = function() {
$scope.searchFocus = true;
};
$scope.searchBlurred = function() {
if ($scope.formData.search == null || $scope.formData.search.length === 0) {
2017-06-08 17:43:06 -04:00
$scope.searchFocus = false;
}
};
$scope.sendToContact = function (item) {
$timeout(function () {
var toAddress = item.address;
if (item.recipientType && item.recipientType === 'contact') {
if (toAddress.indexOf('bch') === 0 || toAddress.indexOf('btc') === 0) {
toAddress = toAddress.substring(3);
}
}
$log.debug('Got toAddress:' + toAddress + ' | ' + item.name);
var stateParams = sendFlowService.getStateClone();
stateParams.toAddress = toAddress,
stateParams.coin = item.coin;
sendFlowService.pushState(stateParams);
if (!stateParams.fromWalletId) { // If we have no toAddress or fromWallet
$state.transitionTo('tabs.send.origin');
} else {
$state.transitionTo('tabs.send.amount');
}
2016-08-12 12:44:16 -03:00
});
};
2016-08-18 19:25:30 -03:00
$scope.startWalletToWalletTransfer = function() {
console.log('startWalletToWalletTransfer()');
var params = sendFlowService.getStateClone();
sendFlowService.pushState(params);
$state.transitionTo('tabs.send.wallet-to-wallet', {
fromWalletId: sendFlowService.fromWalletId
});
}
2016-10-11 18:30:40 -03:00
// This could probably be enhanced refactoring the routes abstract states
$scope.createWallet = function() {
$state.go('tabs.home').then(function() {
$state.go('tabs.add.create-personal');
});
};
$scope.buyBitcoin = function() {
$state.go('tabs.home').then(function() {
$state.go('tabs.buyandsell');
2016-10-11 18:30:40 -03:00
});
};
$scope.$on("$ionicView.beforeEnter", function(event, data) {
console.log(data);
console.log('tab-send onBeforeEnter sendflow ', sendFlowService.state);
2018-07-13 11:44:24 +02:00
$scope.isIOS = platformInfo.isIOS && platformInfo.isCordova;
2018-07-12 15:25:11 +02:00
$scope.showWalletsBch = $scope.showWalletsBtc = $scope.showWallets = false;
$scope.checkingBalance = true;
$scope.formData = {
search: null
};
originalList = [];
hasWallets();
2018-07-12 15:25:11 +02:00
configService.whenAvailable(function(_config) {
$scope.displayBalanceAsFiat = _config.wallet.settings.priceDisplay === 'fiat';
});
if (data.direction == "back") {
sendFlowService.clear();
}
});
});