]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/startup.js
LP#1736269: Mark Missing Pieces is non-functional
[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 .config(['$locationProvider','$compileProvider',
17  function($locationProvider , $compileProvider) {
18     $locationProvider.html5Mode(true);
19     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|mailto|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     // Some org settings affect every page.  Load them during startup.  
29     // Other startup data loaders can be added by appending to egEnv.loaders.
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                 'ui.staff.max_recent_patrons', // affects navbar
37                 'lib.timezone'
38             ]).then(
39                 function(set) {
40                     $rootScope.egDateFormat = 
41                         set['webstaff.format.dates'] || 'shortDate';
42                     $rootScope.egDateAndTimeFormat = 
43                         set['webstaff.format.date_and_time'] || 'short';
44
45                     // default to 1 for backwards compat.
46                     if (set['ui.staff.max_recent_patrons'] === null)
47                         set['ui.staff.max_recent_patrons'] = 1
48                 }
49             );
50         }
51     );
52
53     // returns true if we are staying on the current page
54     // false if we are redirecting to login
55     service.expiredAuthHandler = function(data) {
56         if (lf.isOffline) return true; // Only set by the offline UI
57
58         console.debug('egStartup.expiredAuthHandler()');
59
60         // Only notify other tabs the auth session has expired 
61         // when this tab was the first tab to know it.
62         var broadcast = !(data && data.startedElsewhere);
63
64         egAuth.logout(broadcast); // clean up
65
66         // no need to redirect if we're on the /login page
67         if ($location.path() == '/login') return true;
68
69         // change locations to the login page, using the current page
70         // as the 'route_to' destination on /login
71         $window.location.href = $location
72             .path('/login')
73             .search({route_to : 
74                 $window.location.pathname + $window.location.search})
75             .absUrl();
76
77         return false;
78     }
79
80     // if during startup or any time in the future we encounter an expired
81     // authtoken, call our epired token handler
82     // we handle this here instead egAuth, since it affects the flow
83     // of the startup routines when no valid token exists during startup.
84     $rootScope.$on('egAuthExpired', function() {service.expiredAuthHandler()});
85
86     service.go = function () {
87         if (service.promise) {
88             // startup already started, return our existing promise
89             return service.promise;
90         } 
91
92         // create a new promise and fire off startup
93         var deferred = $q.defer();
94         service.promise = deferred.promise;
95
96         // IDL parsing is sync.  No promises required
97         egIDL.parseIDL();
98         egAuth.testAuthToken().then(
99
100             // testAuthToken resolved
101             function() { 
102                 egEnv.load().then(
103                     function() { deferred.resolve() }, 
104                     function() { 
105                         deferred.reject('egEnv did not resolve')
106                     }
107                 );
108             },
109
110             // testAuthToken rejected
111             function() { 
112                 console.log('egAuth found no valid authtoken');
113                 if (service.expiredAuthHandler()) deferred.resolve();
114             }
115         );
116
117         return service.promise;
118     }
119     
120     return service;
121 }]);
122