Wallet/src/js/routes.js

538 lines
15 KiB
JavaScript
Raw Normal View History

2015-03-06 12:00:10 -03:00
'use strict';
var unsupported;
if (window && window.navigator) {
var rxaosp = window.navigator.userAgent.match(/Android.*AppleWebKit\/([\d.]+)/);
var isaosp = (rxaosp && rxaosp[1] < 537);
if (!window.cordova && isaosp)
unsupported = true;
if (unsupported) {
window.location = '#/unsupported';
}
2015-03-06 12:00:10 -03:00
}
//Setting up route
angular
.module('copayApp')
2015-04-25 12:37:04 -03:00
.config(function(historicLogProvider, $provide, $logProvider, $stateProvider, $urlRouterProvider) {
2015-03-06 12:00:10 -03:00
$urlRouterProvider.otherwise('/');
2015-04-25 12:37:04 -03:00
$logProvider.debugEnabled(true);
$provide.decorator('$log', ['$delegate',
function($delegate) {
var historicLog = historicLogProvider.$get();
['debug', 'info', 'warn', 'error', 'log'].forEach(function(level) {
var orig = $delegate[level];
$delegate[level] = function() {
var args = [].slice.call(arguments);
2015-04-27 02:07:26 -03:00
if (!Array.isArray(args)) args = [args];
args = args.map(function(v) {
2015-04-25 20:53:31 -03:00
try {
if (typeof v == 'undefined') v = 'undefined';
if (typeof v == 'object') {
2015-04-27 02:07:26 -03:00
if (v.message)
v = v.message;
else
v = JSON.stringify(v);
2015-04-25 20:53:31 -03:00
}
v = v.toString();
if (v.length > 200)
v = v.substr(0, 197) + '...';
2015-04-25 20:53:31 -03:00
} catch (e) {
console.log('Error at log decorator:', e);
v = 'undefined';
}
2015-04-25 12:37:04 -03:00
return v;
});
2015-04-26 20:13:02 -03:00
try {
if (window.cordova)
console.log(args.join(' '));
2015-04-27 02:07:26 -03:00
orig.apply(null, args);
2015-04-26 20:13:02 -03:00
historicLog.add(level, args.join(' '));
} catch (e) {
console.log('Error at log decorator:', e);
}
2015-04-25 12:37:04 -03:00
};
});
return $delegate;
}
]);
2015-03-06 12:00:10 -03:00
$stateProvider
.state('splash', {
url: '/splash',
needProfile: false,
views: {
'main': {
2015-03-06 12:00:10 -03:00
templateUrl: 'views/splash/1.html'
}
}
})
.state('walletHome', {
url: '/',
walletShouldBeComplete: true,
needProfile: true,
views: {
'main': {
templateUrl: 'views/walletHome.html'
},
'topbar': {
templateUrl: 'views/includes/topbar.html'
},
'menu': {
templateUrl: 'views/includes/menu.html',
controller: function($scope) {
$scope.activeMenu = 'walletHome';
}
}
}
})
.state('createProfile', {
url: '/createProfile',
needProfile: false,
views: {
'main': {
templateUrl: 'views/createProfile.html',
controller: function($scope) {
$scope.mainDark = true;
}
2015-03-06 12:00:10 -03:00
}
}
})
.state('unsupported', {
url: '/unsupported',
needProfile: false,
views: {
'main': {
templateUrl: 'views/unsupported.html'
}
}
})
.state('uri-payment', {
url: '/uri-payment/:data',
templateUrl: 'views/paymentUri.html',
views: {
'main': {
templateUrl: 'views/paymentUri.html',
},
'topbar': {
templateUrl: 'views/includes/topbar.html',
controller: function($scope) {
$scope.goBackToState = 'walletHome';
}
}
},
needProfile: true
})
.state('selectWalletForPayment', {
url: '/selectWalletForPayment',
controller: 'walletForPaymentController',
needProfile: true
})
.state('join', {
url: '/join',
needProfile: true,
views: {
'main': {
templateUrl: 'views/join.html'
},
'topbar': {
templateUrl: 'views/includes/topbar.html',
controller: function($scope) {
$scope.titleSection = 'Join shared wallet';
$scope.goBackToState = 'add';
}
}
}
})
.state('import', {
url: '/import',
needProfile: true,
views: {
'main': {
templateUrl: 'views/import.html'
},
'topbar': {
templateUrl: 'views/includes/topbar.html',
controller: function($scope) {
$scope.titleSection = 'Import wallet';
$scope.goBackToState = 'add';
}
}
}
})
.state('importProfile', {
url: '/importProfile',
templateUrl: 'views/importProfile.html',
needProfile: false
})
.state('importLegacy', {
url: '/importLegacy',
needProfile: true,
views: {
'main': {
templateUrl: 'views/importLegacy.html',
},
'topbar': {
templateUrl: 'views/includes/topbar.html',
controller: function($scope) {
$scope.titleSection = 'Import legacy wallet';
$scope.goBackToState = 'add';
}
}
}
})
.state('create', {
url: '/create',
templateUrl: 'views/create.html',
needProfile: true,
views: {
'main': {
templateUrl: 'views/create.html'
},
'topbar': {
templateUrl: 'views/includes/topbar.html',
controller: function($scope) {
$scope.titleSection = 'Create new wallet';
$scope.goBackToState = 'add';
}
}
}
})
.state('copayers', {
url: '/copayers',
needProfile: true,
views: {
'main': {
templateUrl: 'views/copayers.html'
},
'topbar': {
templateUrl: 'views/includes/topbar.html'
}
}
})
.state('profile', {
url: '/profile',
controller: 'profileController',
templateUrl: 'views/profile.html',
needProfile: true
})
.state('preferences', {
url: '/preferences',
templateUrl: 'views/preferences.html',
walletShouldBeComplete: true,
needProfile: true,
views: {
'main': {
templateUrl: 'views/preferences.html'
},
'topbar': {
templateUrl: 'views/includes/topbar.html',
controller: function($scope) {
$scope.titleSection = 'Preferences';
$scope.closeToHome = true;
2015-03-06 12:00:10 -03:00
}
}
}
})
2015-04-22 15:19:08 -03:00
.state('preferencesLanguage', {
url: '/preferencesLanguage',
walletShouldBeComplete: true,
needProfile: true,
views: {
'main': {
templateUrl: 'views/preferencesLanguage.html'
},
'topbar': {
templateUrl: 'views/includes/topbar.html',
controller: function($scope) {
$scope.titleSection = 'Language';
$scope.goBackToState = 'preferences';
}
}
}
})
2015-03-06 12:00:10 -03:00
.state('preferencesUnit', {
url: '/preferencesUnit',
templateUrl: 'views/preferencesUnit.html',
walletShouldBeComplete: true,
needProfile: true,
views: {
'main': {
templateUrl: 'views/preferencesUnit.html'
},
'topbar': {
templateUrl: 'views/includes/topbar.html',
controller: function($scope) {
$scope.titleSection = 'Unit';
$scope.goBackToState = 'preferences';
}
}
}
})
.state('preferencesColor', {
url: '/preferencesColor',
templateUrl: 'views/preferencesColor.html',
walletShouldBeComplete: true,
needProfile: true,
views: {
'main': {
templateUrl: 'views/preferencesColor.html'
},
'topbar': {
templateUrl: 'views/includes/topbar.html',
controller: function($scope) {
$scope.titleSection = 'Color';
$scope.goBackToState = 'preferences';
}
}
}
})
2015-04-13 14:58:07 -03:00
.state('preferencesAltCurrency', {
2015-03-06 12:00:10 -03:00
url: '/preferencesAltCurrency',
templateUrl: 'views/preferencesAltCurrency.html',
walletShouldBeComplete: true,
needProfile: true,
views: {
'main': {
templateUrl: 'views/preferencesAltCurrency.html'
},
'topbar': {
templateUrl: 'views/includes/topbar.html',
controller: function($scope) {
$scope.titleSection = 'Alternative Currency';
$scope.goBackToState = 'preferences';
}
}
}
})
.state('preferencesBwsUrl', {
url: '/preferencesBwsUrl',
templateUrl: 'views/preferencesBwsUrl.html',
walletShouldBeComplete: true,
needProfile: true,
views: {
'main': {
templateUrl: 'views/preferencesBwsUrl.html'
},
'topbar': {
templateUrl: 'views/includes/topbar.html',
controller: function($scope) {
$scope.titleSection = 'Bitcore Wallet Service';
$scope.goBackToState = 'preferences';
}
}
}
})
.state('delete', {
url: '/delete',
templateUrl: 'views/preferencesDeleteWallet.html',
walletShouldBeComplete: true,
needProfile: true,
views: {
'main': {
templateUrl: 'views/preferencesDeleteWallet.html'
},
'topbar': {
templateUrl: 'views/includes/topbar.html',
controller: function($scope) {
$scope.titleSection = 'Delete';
$scope.goBackToState = 'preferences';
}
}
}
})
2015-04-25 12:37:04 -03:00
.state('about', {
url: '/about',
templateUrl: 'views/preferencesAbout.html',
walletShouldBeComplete: true,
needProfile: true,
views: {
'main': {
templateUrl: 'views/preferencesAbout.html'
},
'topbar': {
templateUrl: 'views/includes/topbar.html',
controller: function($scope) {
$scope.titleSection = 'About';
$scope.goBackToState = 'preferences';
}
}
}
})
.state('logs', {
url: '/logs',
templateUrl: 'views/preferencesLogs.html',
walletShouldBeComplete: true,
needProfile: true,
views: {
'main': {
templateUrl: 'views/preferencesLogs.html'
},
'topbar': {
templateUrl: 'views/includes/topbar.html',
controller: function($scope) {
$scope.titleSection = 'Logs';
$scope.goBackToState = 'about';
}
}
}
})
2015-03-06 12:00:10 -03:00
.state('backup', {
url: '/backup',
templateUrl: 'views/backup.html',
walletShouldBeComplete: true,
needProfile: true,
views: {
'main': {
templateUrl: 'views/backup.html'
},
'topbar': {
templateUrl: 'views/includes/topbar.html',
controller: function($scope) {
$scope.titleSection = 'Backup';
$scope.goBackToState = 'preferences';
}
}
}
})
.state('settings', {
url: '/settings',
controller: 'settingsController',
templateUrl: 'views/settings.html',
needProfile: false
})
.state('warning', {
url: '/warning',
controller: 'warningController',
templateUrl: 'views/warning.html',
needProfile: false
})
.state('add', {
2015-04-13 14:58:07 -03:00
url: '/add',
needProfile: true,
views: {
'main': {
templateUrl: 'views/add.html'
},
'topbar': {
templateUrl: 'views/includes/topbar.html',
controller: function($scope) {
$scope.titleSection = 'Add wallet';
$scope.closeToHome = true;
2015-04-13 14:58:07 -03:00
}
2015-03-06 12:00:10 -03:00
}
}
2015-04-13 14:58:07 -03:00
})
.state('cordova', {
url: '/cordova/:status',
2015-03-06 12:00:10 -03:00
views: {
'main': {
controller: function($scope, $stateParams, go) {
switch ($stateParams.status) {
2015-04-13 14:58:07 -03:00
case 'resume':
$scope.$emit('Local/Resume');
2015-03-06 12:00:10 -03:00
break;
2015-04-25 12:37:04 -03:00
// case 'online':
// // $scope.$emit('Local/Online');
// break;
2015-04-13 17:09:33 -03:00
case 'offline':
$scope.$emit('Local/Offline');
break;
2015-03-06 12:00:10 -03:00
};
go.walletHome();
}
}
},
needProfile: false
});
})
2015-04-22 18:41:30 -03:00
.run(function($rootScope, $state, $log, gettextCatalog, uriHandler, isCordova, amMoment, profileService) {
2015-04-23 18:05:31 -03:00
FastClick.attach(document.body);
2015-04-25 12:37:04 -03:00
2015-04-22 18:41:30 -03:00
// Auto-detect browser language
var userLang, androidLang;
2015-03-06 12:00:10 -03:00
2015-04-22 18:41:30 -03:00
if (navigator && navigator.userAgent && (androidLang = navigator.userAgent.match(/android.*\W(\w\w)-(\w\w)\W/i))) {
userLang = androidLang[1];
} else {
// works for iOS and Android 4.x
userLang = navigator.userLanguage || navigator.language;
}
2015-03-06 12:00:10 -03:00
2015-04-22 18:41:30 -03:00
userLang = userLang ? (userLang.split('-', 1)[0] || 'en') : 'en';
gettextCatalog.setCurrentLanguage(userLang);
amMoment.changeLocale(userLang);
2015-03-06 12:00:10 -03:00
// Register URI handler, not for mobileApp
if (!isCordova) {
uriHandler.register();
}
var pageWeight = {
2015-04-14 01:15:37 -03:00
walletHome: 0,
receive: 0,
send: 0,
history: 0,
2015-04-23 18:29:25 -03:00
preferences: 0,
2015-03-06 12:00:10 -03:00
preferencesColor: 12,
backup: 12,
delete: 12,
2015-04-22 15:19:08 -03:00
preferencesLanguage: 12,
2015-03-06 12:00:10 -03:00
preferencesUnit: 12,
preferencesAltCurrency: 12,
preferencesBwsUrl: 12,
2015-04-25 12:37:04 -03:00
about: 12,
logs: 13,
2015-04-14 01:15:37 -03:00
add: 0,
2015-03-06 12:00:10 -03:00
create: 12,
join: 12,
import: 12,
importLegacy: 12
};
$rootScope.$on('$stateChangeSuccess', function() {
2015-03-06 12:00:10 -03:00
$rootScope.$emit('Animation/Disable');
});
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
2015-03-06 12:00:10 -03:00
if (pageWeight[fromState.name] > pageWeight[toState.name]) {
$rootScope.$emit('Animation/SwipeRight');
2015-04-13 14:58:07 -03:00
} else if (pageWeight[fromState.name] < pageWeight[toState.name]) {
2015-03-06 12:00:10 -03:00
$rootScope.$emit('Animation/SwipeLeft');
}
if (!profileService.profile && toState.needProfile) {
// Give us time to open / create the profile
2015-04-25 12:37:04 -03:00
event.preventDefault();
2015-03-06 12:00:10 -03:00
// Try to open local profile
profileService.loadAndBindProfile(function(err) {
if (err) {
if (err.message.match('NOPROFILE')) {
$log.debug('No profile... redirecting');
$state.transitionTo('splash');
} else {
throw new Error(err); // TODO
}
} else {
2015-04-25 12:37:04 -03:00
$log.debug('Profile loaded ... Starting UX.');
$state.transitionTo(toState, toParams);
2015-03-06 12:00:10 -03:00
}
});
}
if (profileService.focusedClient && !profileService.focusedClient.isComplete() && toState.walletShouldBeComplete) {
$state.transitionTo('copayers');
event.preventDefault();
}
});
});