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')
16 .config(['$locationProvider','$compileProvider',
17 function($locationProvider , $compileProvider) {
18 $locationProvider.html5Mode(true);
19 $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|mailto|blob):/);
23 ['$q','$rootScope','$location','$window','egIDL','egAuth','egEnv','egOrg',
24 function($q, $rootScope, $location, $window, egIDL, egAuth, egEnv , egOrg ) {
26 var service = { promise : null }
28 // Some org settings affect every page. Load them during startup.
29 // Other startup data loaders can be added by appending to egEnv.loaders.
30 // egEnv.loaders functions must return a promise.
33 return egOrg.settings([
34 'webstaff.format.dates',
35 'webstaff.format.date_and_time',
36 'ui.staff.max_recent_patrons', // affects navbar
40 $rootScope.egDateFormat =
41 set['webstaff.format.dates'] || 'shortDate';
42 $rootScope.egDateAndTimeFormat =
43 set['webstaff.format.date_and_time'] || 'short';
45 // default to 1 for backwards compat.
46 if (set['ui.staff.max_recent_patrons'] === null)
47 set['ui.staff.max_recent_patrons'] = 1
53 // returns true if we are staying on the current page
54 // false if we are redirecting to login
55 service.expiredAuthHandler = function() {
56 if (lf.isOffline) return true; // Only set by the offline UI
58 console.debug('egStartup.expiredAuthHandler()');
59 egAuth.logout(); // clean up
61 // no need to redirect if we're on the /login page
62 if ($location.path() == '/login') return true;
64 // change locations to the login page, using the current page
65 // as the 'route_to' destination on /login
66 $window.location.href = $location
69 $window.location.pathname + $window.location.search})
75 // if during startup or any time in the future we encounter an expired
76 // authtoken, call our epired token handler
77 // we handle this here instead egAuth, since it affects the flow
78 // of the startup routines when no valid token exists during startup.
79 $rootScope.$on('egAuthExpired', function() {service.expiredAuthHandler()});
81 service.go = function () {
82 if (service.promise) {
83 // startup already started, return our existing promise
84 return service.promise;
87 // create a new promise and fire off startup
88 var deferred = $q.defer();
89 service.promise = deferred.promise;
91 // IDL parsing is sync. No promises required
93 egAuth.testAuthToken().then(
95 // testAuthToken resolved
98 function() { deferred.resolve() },
100 deferred.reject('egEnv did not resolve')
105 // testAuthToken rejected
107 console.log('egAuth found no valid authtoken');
108 if (service.expiredAuthHandler()) deferred.resolve();
112 return service.promise;