2 * Core Service - egStartup
4 * Coordinates all startup routines and consolidates them into
5 * a single startup promise. Startup can be launched from multiple
6 * controllers, etc., but only one startup routine will be run.
8 * If no valid authtoken is found, startup will exit early and
9 * change the page href to the login page. Otherwise, the global
10 * promise returned by startup.go() will be resolved after all
11 * async data is arrived.
14 angular.module('egCoreMod')
17 ['$q','$rootScope','$location','$window','egIDL','egAuth','egEnv','egOrg',
18 function($q, $rootScope, $location, $window, egIDL, egAuth, egEnv , egOrg ) {
20 var service = { promise : null }
22 // Load date/time format settings on all pages. Add more .push(...)
23 // calls to add more universal data-loading functions.
24 // egEnv.loaders functions must return a promise.
27 return egOrg.settings([
28 'webstaff.format.dates',
29 'webstaff.format.date_and_time'
32 $rootScope.egDateFormat =
33 set['webstaff.format.dates'] || 'shortDate';
34 $rootScope.egDateAndTimeFormat =
35 set['webstaff.format.date_and_time'] || 'short';
41 // returns true if we are staying on the current page
42 // false if we are redirecting to login
43 service.expiredAuthHandler = function() {
44 console.debug('egStartup.expiredAuthHandler()');
45 egAuth.logout(); // clean up
47 // no need to redirect if we're on the /login page
48 if ($location.path() == '/login') return true;
50 // change locations to the login page, using the current page
51 // as the 'route_to' destination on /login
52 $window.location.href = $location
55 $window.location.pathname + $window.location.search})
61 // if during startup or any time in the future we encounter an expired
62 // authtoken, call our epired token handler
63 // we handle this here instead egAuth, since it affects the flow
64 // of the startup routines when no valid token exists during startup.
65 $rootScope.$on('egAuthExpired', function() {service.expiredAuthHandler()});
67 service.go = function () {
68 if (service.promise) {
69 // startup already started, return our existing promise
70 return service.promise;
73 // create a new promise and fire off startup
74 var deferred = $q.defer();
75 service.promise = deferred.promise;
77 // IDL parsing is sync. No promises required
79 egAuth.testAuthToken().then(
81 // testAuthToken resolved
84 function() { deferred.resolve() },
86 deferred.reject('egEnv did not resolve')
91 // testAuthToken rejected
93 console.log('egAuth found no valid authtoken');
94 if (service.expiredAuthHandler()) deferred.resolve();
98 return service.promise;