]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/startup.js
lp1691237 webstaff: fix Format Dates and Format Times
[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     // 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                 egOrg.settings([
71                     'webstaff.format.dates',
72                     'webstaff.format.date_and_time'
73                 ]).then(
74                     function(set) {
75                         $rootScope.egDateFormat = set['webstaff.format.dates'] || 'shortDate';
76                         $rootScope.egDateAndTimeFormat = set['webstaff.format.date_and_time'] || 'short';
77                         deferred.resolve();
78                     },
79                     function() {
80                         deferred.reject('egOrg did not resolve');
81                     }
82                 );
83             },
84
85             // testAuthToken rejected
86             function() { 
87                 console.log('egAuth found no valid authtoken');
88                 if (service.expiredAuthHandler()) deferred.resolve();
89             }
90         );
91
92         return service.promise;
93     }
94     
95     return service;
96 }]);
97