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',
25 function($q, $rootScope, $location, $window, egIDL, egAuth, egEnv , egOrg ,
28 var service = { promise : null }
30 // Some org settings affect every page. Load them during startup.
31 // Other startup data loaders can be added by appending to egEnv.loaders.
32 // egEnv.loaders functions must return a promise.
35 return egOrg.settings([
36 'webstaff.format.dates',
37 'webstaff.format.date_and_time',
38 'ui.staff.max_recent_patrons', // affects navbar
42 $rootScope.egDateFormat =
43 set['webstaff.format.dates'] || 'shortDate';
44 $rootScope.egDateAndTimeFormat =
45 set['webstaff.format.date_and_time'] || 'short';
47 // default to 1 for backwards compat.
48 if (set['ui.staff.max_recent_patrons'] === null)
49 set['ui.staff.max_recent_patrons'] = 1
55 // returns true if we are staying on the current page
56 // false if we are redirecting to login
57 service.expiredAuthHandler = function(data) {
58 if (lf.isOffline) return true; // Only set by the offline UI
60 console.debug('egStartup.expiredAuthHandler()');
62 // Only notify other tabs the auth session has expired
63 // when this tab was the first tab to know it.
64 var broadcast = !(data && data.startedElsewhere);
66 egAuth.logout(broadcast); // clean up
68 // no need to redirect if we're on the /login page
69 if ($location.path() == '/login') return true;
71 // change locations to the login page, using the current page
72 // as the 'route_to' destination on /login
73 $window.location.href = $location
76 $window.location.pathname + $window.location.search})
82 // if during startup or any time in the future we encounter an expired
83 // authtoken, call our epired token handler
84 // we handle this here instead egAuth, since it affects the flow
85 // of the startup routines when no valid token exists during startup.
86 $rootScope.$on('egAuthExpired', function() {service.expiredAuthHandler()});
88 service.go = function () {
89 if (service.promise) {
90 // startup already started, return our existing promise
91 return service.promise;
94 // Apply the locale from the cookie before any network
96 var locale = $cookies.get('eg_locale');
98 // Cookie is stored aa_bb. OpenSRF wants aa-BB
99 var parts = locale.split(/_/);
100 OpenSRF.locale = parts[0] + '-' + parts[1].toUpperCase();
101 console.debug('Applying locale ' + OpenSRF.locale);
104 // create a new promise and fire off startup
105 var deferred = $q.defer();
106 service.promise = deferred.promise;
108 // IDL parsing is sync. No promises required
110 egAuth.testAuthToken().then(
112 // testAuthToken resolved
115 function() { deferred.resolve() },
117 deferred.reject('egEnv did not resolve')
122 // testAuthToken rejected
124 console.log('egAuth found no valid authtoken');
125 if (service.expiredAuthHandler()) deferred.resolve();
129 return service.promise;