]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/startup.js
LP#1350042 Browser client templates/scripts (phase 1)
[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',
18 function($q,  $rootScope,  $location,  $window,  egIDL,  egAuth,  egEnv) {
19
20     var service = { promise : null }
21
22     // returns true if we are staying on the current page
23     // false if we are redirecting to login
24     service.expiredAuthHandler = function() {
25         console.debug('egStartup.expiredAuthHandler()');
26         egAuth.logout(); // clean up
27
28         // no need to redirect if we're on the /login page
29         if ($location.path() == '/login') return true;
30
31         // change locations to the login page, using the current page
32         // as the 'route_to' destination on /login
33         $window.location.href = $location
34             .path('/login')
35             .search({route_to : 
36                 $window.location.pathname + $window.location.search})
37             .absUrl();
38
39         return false;
40     }
41
42     // if during startup or any time in the future we encounter an expired
43     // authtoken, call our epired token handler
44     // we handle this here instead egAuth, since it affects the flow
45     // of the startup routines when no valid token exists during startup.
46     $rootScope.$on('egAuthExpired', function() {service.expiredAuthHandler()});
47
48     service.go = function () {
49         if (service.promise) {
50             // startup already started, return our existing promise
51             return service.promise;
52         } 
53
54         // create a new promise and fire off startup
55         var deferred = $q.defer();
56         service.promise = deferred.promise;
57
58         // IDL parsing is sync.  No promises required
59         egIDL.parseIDL();
60         egAuth.testAuthToken().then(
61
62             // testAuthToken resolved
63             function() { 
64                 egEnv.load().then(
65                     function() { deferred.resolve() }, 
66                     function() { 
67                         deferred.reject('egEnv did not resolve')
68                     }
69                 );
70             },
71
72             // testAuthToken rejected
73             function() { 
74                 console.log('egAuth found no valid authtoken');
75                 if (service.expiredAuthHandler()) deferred.resolve();
76             }
77         );
78
79         return service.promise;
80     }
81     
82     return service;
83 }]);
84