]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/app.js
LP2045292 Color contrast for AngularJS patron bills
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / app.js
1 /**
2  * App to drive the base page. 
3  * Login Form
4  * Splash Page
5  */
6
7 angular.module('egHome', ['ngRoute', 'ui.bootstrap', 'egCoreMod', 'egUiMod'])
8
9 .config(
10        ['$routeProvider','$locationProvider',
11 function($routeProvider , $locationProvider) {
12     $locationProvider.html5Mode(true);
13
14     /**
15      * Route resolvers allow us to run async commands
16      * before the page controller is instantiated.
17      */
18     var resolver = {delay : ['egCore', 
19         function(egCore) {return egCore.startup.go()}]};
20
21     $routeProvider.when('/login', {
22         templateUrl: './t_login',
23         controller: 'LoginCtrl',
24         resolve : resolver
25     });
26
27     $routeProvider.when('/about', {
28         templateUrl: './t_about',
29         controller: 'AboutCtrl',
30         resolve : resolver
31     });
32
33     // default page 
34     $routeProvider.otherwise({
35         templateUrl : './t_splash',
36         controller : 'SplashCtrl',
37         resolve : resolver
38     });
39 }])
40
41 /**
42  * Login controller.  
43  * Reads the login form and submits the login request
44  */
45 .controller('LoginCtrl', 
46     /* inject services into our controller.  Spelling them
47      * out like this allows the auto-magic injector to work
48      * even if the code has been minified */
49            ['$scope','$location','$window','egCore','egLovefield',
50     function($scope , $location , $window , egCore , egLovefield) {
51         egLovefield.havePendingOfflineXacts() .then(
52             function(eh){ $scope.pendingXacts = eh; },
53             function() {} // SharedWorker not supported
54         );
55
56         $scope.focusMe = true;
57         $scope.args = {};
58         $scope.workstations = [];
59                 
60                 egCore.strings.setPageTitle(
61             egCore.strings['PAGE_TITLE_LOGIN']);
62                         
63         // if the user is already logged in, jump to splash page
64         if (egCore.auth.user()) $location.path('/');
65
66         egCore.hatch.getWorkstations()
67         .then(function(all) {
68             if (all && all.length) {
69                 $scope.workstations = all.map(function(a) { return a.name });
70
71                 if (ws = $location.search().ws) {
72                     // user requested a workstation via URL
73                     var match = all.filter(
74                         function(w) {return ws == w.name} )[0];
75
76                     if (match) {
77                         // requested WS registered on this client
78                         $scope.args = {workstation : match.name};
79                     } else {
80                         // the requested WS is not registered on this client
81                         $scope.wsNotRegistered = true;
82                     }
83                 } else {
84                     // no workstation requested; use the default
85                     egCore.hatch.getDefaultWorkstation()
86                     .then(function(ws) {
87                         $scope.args = {workstation : ws}
88                     });
89                 }
90             } 
91         })
92
93         $scope.login = function(args) {
94             $scope.loginFailed = false;
95
96             if (!args) args = {}; // see FF note below
97
98             if (!args.username) {
99                 /* 
100                  Issues with form autofill / auto-complete                          
101                  https://github.com/angular/angular.js/issues/1460                  
102                  http://timothy.userapp.io/post/63412334209/form-autocomplete-and-remember-password-with-angularjs
103                  For now, since FF will save the values, we should 
104                  honor them, even if it's hacky. */
105                 args.username = document.getElementById("login-username").value;
106                 args.password = document.getElementById("login-password").value;
107             }
108
109             if (! (args.username && args.password) ) return;
110
111             // if at least one workstation exists, it must be used.
112             if (!args.workstation && $scope.workstations.length > 0) return;
113
114             args.type = 'staff';
115             egCore.auth.login(args).then(
116
117                 function(result) { 
118                     // After login, send the user to:
119                     // 1. The WS admin page for WS maintenance.
120                     // 2. The page originally requested by the caller
121                     // 3. Home page.
122
123                     // NOTE: using $location.path(...) results in
124                     // confusing intermediate page loads, since
125                     // path(...) is a setter function.  Build the URL by
126                     // hand instead from the configured base path.
127                     var route_to = egCore.env.basePath;
128
129                     if (result.invalid_workstation) {
130                         // route to WS admin page to delete the offending
131                         // WS and create a new one.
132                         route_to += 
133                             'admin/workstation/workstations?remove=' 
134                                 + encodeURIComponent(args.workstation);
135
136                     } else if ($location.search().route_to) {
137                         // Route to the originally requested page.
138                         route_to = $location.search().route_to;
139                     }
140
141                     $window.location.href = route_to;
142                 },
143                 function() {
144                     $scope.args.password = '';
145                     $scope.loginFailed = true;
146                     $scope.focusMe = true;
147                 }
148             );
149         }
150     }
151 ])
152
153 /**
154  * Splash page dynamic content.
155  */
156 .controller('SplashCtrl', ['$scope', '$window','egCore', 
157     function($scope, $window,egCore) {
158                 
159     $scope.focus_search = true;
160         
161         egCore.strings.setPageTitle(
162         egCore.strings['PAGE_TITLE_SPLASH']);
163
164     $scope.catalog_search = function($event) {
165         $scope.focus_search = true;
166         if (!$scope.cat_query) return;
167         if ($event && $event.keyCode != 13) return; // input ng-keypress
168         $window.location.href = 
169             '/eg2/staff/catalog/search?query=' +
170             encodeURIComponent($scope.cat_query);
171     }
172 }])
173
174 .controller('AboutCtrl', [
175             '$scope','$location','egCore', 
176     function($scope , $location , egCore) {
177
178     $scope.context = {
179         server : $location.host()
180     }; 
181
182     egCore.net.request(
183         'open-ils.actor','opensrf.open-ils.system.ils_version')
184         .then(function(version) {
185             $scope.context.version = version;
186         }
187     );
188
189         egCore.strings.setPageTitle(
190         egCore.strings['PAGE_TITLE_ABOUT']);
191
192 }])
193