Last active
April 1, 2016 01:24
-
-
Save vcardins/71bd580d6b3c34d5ab8981830d3a70d9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('app.core') | |
.config(configure); | |
/* @ngInject */ | |
function configure(routerHelperProvider, SETTINGS) { | |
routerHelperProvider.configure({docTitle: SETTINGS.title + ': '}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Help configure the state-base ui.router */ | |
(function() { | |
'use strict'; | |
angular | |
.module('blocks.router') | |
.provider('routerHelper', routerHelperProvider); | |
/* @ngInject */ | |
function routerHelperProvider($locationProvider, $stateProvider, $urlRouterProvider) { | |
/* jshint validthis:true */ | |
var config = { | |
docTitle: undefined, | |
resolveAlways: {} | |
}; | |
$locationProvider.html5Mode(true); | |
$urlRouterProvider.otherwise( function($injector) { | |
var $state = $injector.get('$state'); | |
$state.go('app'); | |
}); | |
this.$get = RouterHelper; | |
this.configure = function(cfg) { | |
angular.extend(config, cfg); | |
}; | |
/* @ngInject */ | |
function RouterHelper($rootScope, $state, $timeout, $log, SETTINGS, logger, Auth) { | |
var handlingStateChangeError = false; | |
var hasOtherwise = false; | |
var stateCounts = { | |
errors: 0, | |
changes: 0 | |
}; | |
var service = { | |
getStates: getStates | |
}; | |
init(); | |
return service; | |
/////////////// | |
function init() { | |
handleRoutingErrors(); | |
handleStateChanges(); | |
} | |
function getStates() { return $state.get(); } | |
function handleRoutingErrors() { | |
// Route cancellation: | |
// On routing error, go to the dashboard. | |
// Provide an exit clause if it tries to do it twice. | |
$rootScope.$on('$stateChangeError', | |
function(event, toState, toParams, fromState, fromParams, error) { | |
if (handlingStateChangeError) { | |
return; | |
} | |
stateCounts.errors++; | |
handlingStateChangeError = true; | |
var destination = (toState && | |
(toState.title || toState.name || toState.loadedTemplateUrl)) || | |
'unknown target'; | |
var msg = 'Error routing to ' + destination + '. ' + ( error ? (error.statusText || '') + ': ' + (error.status || '') : ''); | |
logger.warning(msg, [toState]); | |
//$location.path('/'); | |
$timeout(function() { $state.go(SETTINGS.routes.authenticated.state); }); | |
} | |
); | |
} | |
function handleStateChanges() { | |
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) { | |
if ( (!('data' in toState) || !('access' in toState.data)) && !('abstract' in toState) ) { | |
$rootScope.error = 'Access undefined for the state : ' + toState.name; | |
$log.warn($rootScope.error); | |
} | |
//debugger; | |
toState.data = toState.data || {}; | |
var isAuth = toState.data.access ? Auth.isAuthorized(toState.data.access) : false; | |
if (!isAuth) { | |
$rootScope.error = 'Access denied: ' + toState.url; | |
if (!Auth.isAuthenticated()) { | |
logger.error($rootScope.error); | |
$rootScope.error = null; | |
$timeout(function() { | |
$state.go(SETTINGS.routes.anonymous.state); | |
}); | |
} | |
} | |
}); | |
function escapeRegExp(str) { | |
return new RegExp(str.replace(/[\.]/g, '\\\$&')); | |
} | |
$rootScope.$on('$stateChangeSuccess', | |
function(event, toState, toParams, fromState, fromParams) { | |
stateCounts.changes++; | |
handlingStateChangeError = false; | |
$rootScope.title = config.title + (toState.title ? ' - ' + toState.title : ''); | |
$rootScope.previousState = fromState; | |
$rootScope.currentState = toState; | |
} | |
); | |
} | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment