]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/startup.js
038eb2dfc2241960172cd7ecb6155063805a1283
[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 .config(['$locationProvider','$compileProvider',
17  function($locationProvider , $compileProvider) {
18     $locationProvider.html5Mode(true);
19     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/);
20 }])
21
22 .factory('egStartup', 
23        ['$q','$rootScope','$location','$window','egIDL','egAuth','egEnv','egOrg',
24 function($q,  $rootScope,  $location,  $window,  egIDL,  egAuth,  egEnv , egOrg ) {
25
26     var service = { promise : null }
27
28     // Load date/time format settings on all pages.  Add more .push(...)
29     // calls to add more universal data-loading functions. 
30     // egEnv.loaders functions must return a promise.
31     egEnv.loaders.push(
32         function() {
33             return egOrg.settings([
34                 'webstaff.format.dates',
35                 'webstaff.format.date_and_time',
36                 'lib.timezone'
37             ]).then(
38                 function(set) {
39                     $rootScope.egDateFormat = 
40                         set['webstaff.format.dates'] || 'shortDate';
41                     $rootScope.egDateAndTimeFormat = 
42                         set['webstaff.format.date_and_time'] || 'short';
43                 }
44             );
45         }
46     );
47
48     // returns true if we are staying on the current page
49     // false if we are redirecting to login
50     service.expiredAuthHandler = function() {
51         console.debug('egStartup.expiredAuthHandler()');
52         egAuth.logout(); // clean up
53
54         // no need to redirect if we're on the /login page
55         if ($location.path() == '/login') return true;
56
57         // change locations to the login page, using the current page
58         // as the 'route_to' destination on /login
59         $window.location.href = $location
60             .path('/login')
61             .search({route_to : 
62                 $window.location.pathname + $window.location.search})
63             .absUrl();
64
65         return false;
66     }
67
68     // if during startup or any time in the future we encounter an expired
69     // authtoken, call our epired token handler
70     // we handle this here instead egAuth, since it affects the flow
71     // of the startup routines when no valid token exists during startup.
72     $rootScope.$on('egAuthExpired', function() {service.expiredAuthHandler()});
73
74     service.go = function () {
75         if (service.promise) {
76             // startup already started, return our existing promise
77             return service.promise;
78         } 
79
80         // create a new promise and fire off startup
81         var deferred = $q.defer();
82         service.promise = deferred.promise;
83
84         // IDL parsing is sync.  No promises required
85         egIDL.parseIDL();
86         egAuth.testAuthToken().then(
87
88             // testAuthToken resolved
89             function() { 
90                 egEnv.load().then(
91                     function() { deferred.resolve() }, 
92                     function() { 
93                         deferred.reject('egEnv did not resolve')
94                     }
95                 );
96             },
97
98             // testAuthToken rejected
99             function() { 
100                 console.log('egAuth found no valid authtoken');
101                 if (service.expiredAuthHandler()) deferred.resolve();
102             }
103         );
104
105         return service.promise;
106     }
107     
108     return service;
109 }]);
110