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
39 'ui.staff.angular_catalog.enabled', // affects navbar
43 $rootScope.egDateFormat =
44 set['webstaff.format.dates'] || 'shortDate';
45 $rootScope.egDateAndTimeFormat =
46 set['webstaff.format.date_and_time'] || 'short';
48 // default to 1 for backwards compat.
49 if (set['ui.staff.max_recent_patrons'] === null)
50 set['ui.staff.max_recent_patrons'] = 1
56 // returns true if we are staying on the current page
57 // false if we are redirecting to login
58 service.expiredAuthHandler = function(data) {
59 if (lf.isOffline) return true; // Only set by the offline UI
61 console.debug('egStartup.expiredAuthHandler()');
63 // Only notify other tabs the auth session has expired
64 // when this tab was the first tab to know it.
65 var broadcast = !(data && data.startedElsewhere);
67 egAuth.logout(broadcast); // clean up
69 // no need to redirect if we're on the /login page
70 if ($location.path() == '/login') return true;
72 // change locations to the login page, using the current page
73 // as the 'route_to' destination on /login
74 $window.location.href = $location
77 $window.location.pathname + $window.location.search})
83 // if during startup or any time in the future we encounter an expired
84 // authtoken, call our epired token handler
85 // we handle this here instead egAuth, since it affects the flow
86 // of the startup routines when no valid token exists during startup.
87 $rootScope.$on('egAuthExpired', function() {service.expiredAuthHandler()});
89 service.go = function () {
90 if (service.promise) {
91 // startup already started, return our existing promise
92 return service.promise;
95 // Apply the locale from the cookie before any network
97 var locale = $cookies.get('eg_locale');
99 // Cookie is stored aa_bb. OpenSRF wants aa-BB
100 var parts = locale.split(/_/);
101 OpenSRF.locale = parts[0] + '-' + parts[1].toUpperCase();
102 console.debug('Applying locale ' + OpenSRF.locale);
105 // create a new promise and fire off startup
106 var deferred = $q.defer();
107 service.promise = deferred.promise;
109 // IDL parsing is sync. No promises required
111 egAuth.testAuthToken().then(
113 // testAuthToken resolved
116 function() { deferred.resolve() },
118 deferred.reject('egEnv did not resolve')
123 // testAuthToken rejected
125 console.log('egAuth found no valid authtoken');
126 if (service.expiredAuthHandler()) deferred.resolve();
130 return service.promise;