2014-03-25 11:35:04 -03:00
'use strict' ;
2014-09-16 17:30:30 -03:00
angular . module ( 'copayApp.controllers' ) . controller ( 'SidebarController' , function ( $scope , $rootScope , $sce , $location , $http , $filter , notification , controllerUtils ) {
2014-08-12 15:26:15 -04:00
$scope . menu = [ {
'title' : 'Receive' ,
2014-09-02 12:30:31 -03:00
'icon' : 'fi-download' ,
2014-08-12 15:26:15 -04:00
'link' : 'receive'
} , {
'title' : 'Send' ,
'icon' : 'fi-arrow-right' ,
'link' : 'send'
} , {
'title' : 'History' ,
'icon' : 'fi-clipboard-pencil' ,
'link' : 'history'
} , {
2014-09-02 12:52:52 -03:00
'title' : 'Settings' ,
'icon' : 'fi-widget' ,
2014-08-21 11:08:20 -04:00
'link' : 'more'
2014-08-12 15:26:15 -04:00
} ] ;
$scope . signout = function ( ) {
logout ( ) ;
} ;
// Ensures a graceful disconnect
window . onbeforeunload = function ( ) {
controllerUtils . logout ( ) ;
} ;
$scope . $on ( '$destroy' , function ( ) {
window . onbeforeunload = undefined ;
} ) ;
2014-07-18 15:23:01 -03:00
2014-05-15 13:04:26 -07:00
2014-08-12 15:26:15 -04:00
$scope . refresh = function ( ) {
var w = $rootScope . wallet ;
2014-10-21 09:51:47 -03:00
if ( ! w ) return ;
2014-10-10 17:58:19 -03:00
if ( w . isReady ( ) ) {
w . sendWalletReady ( ) ;
if ( $rootScope . addrInfos . length > 0 ) {
2014-10-16 16:02:32 -03:00
controllerUtils . clearBalanceCache ( w ) ;
2014-10-15 12:09:10 -03:00
controllerUtils . updateBalance ( w , function ( ) {
2014-10-10 17:58:19 -03:00
$rootScope . $digest ( ) ;
} ) ;
}
2014-07-16 19:00:34 -03:00
}
2014-08-12 15:26:15 -04:00
} ;
2014-07-16 19:00:34 -03:00
2014-08-12 15:26:15 -04:00
$scope . isActive = function ( item ) {
return item . link && item . link == $location . path ( ) . split ( '/' ) [ 1 ] ;
} ;
2014-07-08 15:26:20 -03:00
2014-08-12 15:26:15 -04:00
function logout ( ) {
2014-08-28 18:58:43 -03:00
controllerUtils . logout ( ) ;
2014-08-12 15:26:15 -04:00
}
2014-07-30 20:12:22 -03:00
2014-08-12 15:26:15 -04:00
// ng-repeat defined number of times instead of repeating over array?
$scope . getNumber = function ( num ) {
return new Array ( num ) ;
}
2014-08-07 18:57:19 -03:00
2014-10-08 10:54:26 -03:00
2014-08-12 15:26:15 -04:00
if ( $rootScope . wallet ) {
2014-10-05 15:59:41 -03:00
$scope . $on ( '$idleWarn' , function ( a , countdown ) {
if ( ! ( countdown % 5 ) )
notification . warning ( 'Session will be closed' , $filter ( 'translate' ) ( 'Your session is about to expire due to inactivity in' ) + ' ' + countdown + ' ' + $filter ( 'translate' ) ( 'seconds' ) ) ;
2014-08-12 15:26:15 -04:00
} ) ;
$scope . $on ( '$idleTimeout' , function ( ) {
$scope . signout ( ) ;
notification . warning ( 'Session closed' , 'Session closed because a long time of inactivity' ) ;
} ) ;
2014-08-15 12:43:43 -04:00
$scope . $on ( '$keepalive' , function ( ) {
2014-10-21 14:26:58 -03:00
if ( $rootScope . wallet ) {
$rootScope . wallet . keepAlive ( ) ;
}
2014-08-15 12:43:43 -04:00
} ) ;
2014-10-23 22:54:23 -03:00
$rootScope . $watch ( 'wallet.id' , function ( ) {
$scope . walletSelection = false ;
} ) ;
2014-08-12 15:26:15 -04:00
}
2014-10-05 15:59:41 -03:00
2014-10-08 10:54:26 -03:00
$scope . switchWallet = function ( wid ) {
controllerUtils . setFocusedWallet ( wid ) ;
2014-10-05 15:59:41 -03:00
} ;
2014-10-20 10:29:43 -03:00
2014-10-16 12:48:47 -03:00
$scope . toggleWalletSelection = function ( ) {
$scope . walletSelection = ! $scope . walletSelection ;
2014-10-21 09:51:07 -03:00
if ( ! $scope . walletSelection ) return ;
2014-10-20 10:29:43 -03:00
$scope . wallets = [ ] ;
var wids = _ . pluck ( $rootScope . iden . listWallets ( ) , 'id' ) ;
_ . each ( wids , function ( wid ) {
if ( controllerUtils . isFocusedWallet ( wid ) ) return ;
2014-10-24 12:24:44 -03:00
var w = $rootScope . iden . getWalletById ( wid ) ;
2014-10-20 10:29:43 -03:00
$scope . wallets . push ( w ) ;
controllerUtils . updateBalance ( w , function ( err , res ) {
if ( err ) return ;
2014-10-20 10:34:45 -03:00
setTimeout ( function ( ) {
$scope . $digest ( ) ;
} , 1 ) ;
2014-10-20 10:29:43 -03:00
} ) ;
} ) ;
2014-10-16 12:48:47 -03:00
} ;
2014-08-12 15:26:15 -04:00
} ) ;