]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/startup.js
f268ddba0c669ba2e282cb5f1eec49be0beccb29
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / services / startup.js
1 /**
2  * Core Service - egStartup
3  *
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.
7  *
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.
12  */
13
14 angular.module('egCoreMod')
15
16 .factory('egStartup', 
17        ['$q','$rootScope','$location','$window','egIDL','egAuth','egEnv','egOrg',
18 function($q,  $rootScope,  $location,  $window,  egIDL,  egAuth,  egEnv , egOrg ) {
19
20     var service = { promise : null }
21
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.
25     egEnv.loaders.push(
26         function() {
27             return egOrg.settings([
28                 'webstaff.format.dates',
29                 'webstaff.format.date_and_time'
30             ]).then(
31                 function(set) {
32                     $rootScope.egDateFormat = 
33                         set['webstaff.format.dates'] || 'shortDate';
34                     $rootScope.egDateAndTimeFormat = 
35                         set['webstaff.format.date_and_time'] || 'short';
36                 }
37             );
38         },
39     );
40
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
46
47         // no need to redirect if we're on the /login page
48         if ($location.path() == '/login') return true;
49
50         // change locations to the login page, using the current page
51         // as the 'route_to' destination on /login
52         $window.location.href = $location
53             .path('/login')
54             .search({route_to : 
55                 $window.location.pathname + $window.location.search})
56             .absUrl();
57
58         return false;
59     }
60
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()});
66
67     service.go = function () {
68         if (service.promise) {
69             // startup already started, return our existing promise
70             return service.promise;
71         } 
72
73         // create a new promise and fire off startup
74         var deferred = $q.defer();
75         service.promise = deferred.promise;
76
77         // IDL parsing is sync.  No promises required
78         egIDL.parseIDL();
79         egAuth.testAuthToken().then(
80
81             // testAuthToken resolved
82             function() { 
83                 egEnv.load().then(
84                     function() { deferred.resolve() }, 
85                     function() { 
86                         deferred.reject('egEnv did not resolve')
87                     }
88                 );
89             },
90
91             // testAuthToken rejected
92             function() { 
93                 console.log('egAuth found no valid authtoken');
94                 if (service.expiredAuthHandler()) deferred.resolve();
95             }
96         );
97
98         return service.promise;
99     }
100     
101     return service;
102 }]);
103