]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/startup.js
LP#1653998 Remove undefined $routeProvider reference
[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?|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             ]).then(
37                 function(set) {
38                     $rootScope.egDateFormat = 
39                         set['webstaff.format.dates'] || 'shortDate';
40                     $rootScope.egDateAndTimeFormat = 
41                         set['webstaff.format.date_and_time'] || 'short';
42                 }
43             );
44         }
45     );
46
47     // returns true if we are staying on the current page
48     // false if we are redirecting to login
49     service.expiredAuthHandler = function() {
50         console.debug('egStartup.expiredAuthHandler()');
51         egAuth.logout(); // clean up
52
53         // no need to redirect if we're on the /login page
54         if ($location.path() == '/login') return true;
55
56         // change locations to the login page, using the current page
57         // as the 'route_to' destination on /login
58         $window.location.href = $location
59             .path('/login')
60             .search({route_to : 
61                 $window.location.pathname + $window.location.search})
62             .absUrl();
63
64         return false;
65     }
66
67     // if during startup or any time in the future we encounter an expired
68     // authtoken, call our epired token handler
69     // we handle this here instead egAuth, since it affects the flow
70     // of the startup routines when no valid token exists during startup.
71     $rootScope.$on('egAuthExpired', function() {service.expiredAuthHandler()});
72
73     service.go = function () {
74         if (service.promise) {
75             // startup already started, return our existing promise
76             return service.promise;
77         } 
78
79         // create a new promise and fire off startup
80         var deferred = $q.defer();
81         service.promise = deferred.promise;
82
83         // IDL parsing is sync.  No promises required
84         egIDL.parseIDL();
85         egAuth.testAuthToken().then(
86
87             // testAuthToken resolved
88             function() { 
89                 egEnv.load().then(
90                     function() { deferred.resolve() }, 
91                     function() { 
92                         deferred.reject('egEnv did not resolve')
93                     }
94                 );
95             },
96
97             // testAuthToken rejected
98             function() { 
99                 console.log('egAuth found no valid authtoken');
100                 if (service.expiredAuthHandler()) deferred.resolve();
101             }
102         );
103
104         return service.promise;
105     }
106     
107     return service;
108 }]);
109