]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/startup.js
LP 1779467: Fix SyntaxError: missing ) after argument list
[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         '$cookies',
25 function($q,  $rootScope,  $location,  $window,  egIDL,  egAuth,  egEnv , egOrg ,
26          $cookies) {
27
28     var service = { promise : null }
29
30     // Some org settings affect every page.  Load them during startup.  
31     // Other startup data loaders can be added by appending to egEnv.loaders.
32     // egEnv.loaders functions must return a promise.
33     egEnv.loaders.push(
34         function() {
35             return egOrg.settings([
36                 'webstaff.format.dates',
37                 'webstaff.format.date_and_time',
38                 'ui.staff.max_recent_patrons', // affects navbar
39                 'ui.staff.angular_catalog.enabled', // affects navbar
40                 'lib.timezone'
41             ]).then(
42                 function(set) {
43                     $rootScope.egDateFormat = 
44                         set['webstaff.format.dates'] || 'shortDate';
45                     $rootScope.egDateAndTimeFormat = 
46                         set['webstaff.format.date_and_time'] || 'short';
47
48                     // default to 1 for backwards compat.
49                     if (set['ui.staff.max_recent_patrons'] === null)
50                         set['ui.staff.max_recent_patrons'] = 1
51                 }
52             );
53         }
54     );
55
56     // returns true if we are staying on the current page
57     // false if we are redirecting to login
58     service.expiredAuthHandler = function(data) {
59         if (lf.isOffline) return true; // Only set by the offline UI
60
61         console.debug('egStartup.expiredAuthHandler()');
62
63         // Only notify other tabs the auth session has expired 
64         // when this tab was the first tab to know it.
65         var broadcast = !(data && data.startedElsewhere);
66
67         egAuth.logout(broadcast); // clean up
68
69         // no need to redirect if we're on the /login page
70         if ($location.path() == '/login') return true;
71
72         // change locations to the login page, using the current page
73         // as the 'route_to' destination on /login
74         $window.location.href = $location
75             .path('/login')
76             .search({route_to : 
77                 $window.location.pathname + $window.location.search})
78             .absUrl();
79
80         return false;
81     }
82
83     // if during startup or any time in the future we encounter an expired
84     // authtoken, call our epired token handler
85     // we handle this here instead egAuth, since it affects the flow
86     // of the startup routines when no valid token exists during startup.
87     $rootScope.$on('egAuthExpired', function() {service.expiredAuthHandler()});
88
89     service.go = function () {
90         if (service.promise) {
91             // startup already started, return our existing promise
92             return service.promise;
93         } 
94
95         // Apply the locale from the cookie before any network 
96         // calls are made.
97         var locale = $cookies.get('eg_locale');
98         if (locale) {
99             // Cookie is stored aa_bb.  OpenSRF wants aa-BB
100             var parts = locale.split(/_/);
101             OpenSRF.locale = parts[0] + '-' + parts[1].toUpperCase();
102             console.debug('Applying locale ' + OpenSRF.locale);
103         }
104
105         // create a new promise and fire off startup
106         var deferred = $q.defer();
107         service.promise = deferred.promise;
108
109         // IDL parsing is sync.  No promises required
110         egIDL.parseIDL();
111         egAuth.testAuthToken().then(
112
113             // testAuthToken resolved
114             function() { 
115                 egEnv.load().then(
116                     function() { deferred.resolve() }, 
117                     function() { 
118                         deferred.reject('egEnv did not resolve')
119                     }
120                 );
121             },
122
123             // testAuthToken rejected
124             function() { 
125                 console.log('egAuth found no valid authtoken');
126                 if (service.expiredAuthHandler()) deferred.resolve();
127             }
128         );
129
130         return service.promise;
131     }
132     
133     return service;
134 }]);
135