]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/app.js
LP#1467663 webstaff: login requires valid workstation
[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     // default page 
28     $routeProvider.otherwise({
29         templateUrl : './t_splash',
30         controller : 'SplashCtrl',
31         resolve : resolver
32     });
33 }])
34
35 /**
36  * Login controller.  
37  * Reads the login form and submits the login request
38  */
39 .controller('LoginCtrl', 
40     /* inject services into our controller.  Spelling them
41      * out like this allows the auto-magic injector to work
42      * even if the code has been minified */
43            ['$scope','$location','$window','egCore',
44     function($scope , $location , $window , egCore) {
45         $scope.focusMe = true;
46         $scope.args = {};
47         $scope.workstations = [];
48
49         // if the user is already logged in, jump to splash page
50         if (egCore.auth.user()) $location.path('/');
51
52         egCore.hatch.getItem('eg.workstation.all')
53         .then(function(all) {
54             if (all && all.length) {
55                 $scope.workstations = all.map(function(a) { return a.name });
56
57                 if (ws = $location.search().ws) {
58                     // user requested a workstation via URL
59                     var match = all.filter(
60                         function(w) {return ws == w.name} )[0];
61
62                     if (match) {
63                         // requested WS registered on this client
64                         $scope.args = {workstation : match.name};
65                     } else {
66                         // the requested WS is not registered on this client
67                         $scope.wsNotRegistered = true;
68                     }
69                 } else {
70                     // no workstation requested; use the default
71                     egCore.hatch.getItem('eg.workstation.default')
72                     .then(function(ws) {
73                         $scope.args = {workstation : ws}
74                     });
75                 }
76             } 
77         })
78
79         $scope.login = function(args) {
80             $scope.loginFailed = false;
81
82             if (!args) args = {}; // see FF note below
83
84             if (!args.username) {
85                 /* 
86                  Issues with form autofill / auto-complete                          
87                  https://github.com/angular/angular.js/issues/1460                  
88                  http://timothy.userapp.io/post/63412334209/form-autocomplete-and-remember-password-with-angularjs
89                  For now, since FF will save the values, we should 
90                  honor them, even if it's hacky. */
91                 args.username = document.getElementById("login-username").value;
92                 args.password = document.getElementById("login-password").value;
93             }
94
95             if (! (args.username && args.password) ) return;
96
97             // if at least one workstation exists, it must be used.
98             if (!args.workstation && $scope.workstations.length > 0) return;
99
100             args.type = 'staff';
101             egCore.auth.login(args).then(
102
103                 function(result) { 
104                     // After login, send the user to:
105                     // 1. The WS admin page for WS maintenance.
106                     // 2. The page originally requested by the caller
107                     // 3. Home page.
108
109                     // NOTE: using $location.path(...) results in
110                     // confusing intermediate page loads, since
111                     // path(...) is a setter function.  Build the URL by
112                     // hand instead from the configured base path.
113                     var route_to = egCore.env.basePath;
114
115                     if (result.invalid_workstation) {
116                         // route to WS admin page to delete the offending
117                         // WS and create a new one.
118                         route_to += 
119                             'admin/workstation/workstations?remove=' 
120                                 + encodeURIComponent(args.workstation);
121
122                     } else if ($location.search().route_to) {
123                         // Route to the originally requested page.
124                         route_to = $location.search().route_to;
125                     }
126
127                     $window.location.href = route_to;
128                 },
129                 function() {
130                     $scope.args.password = '';
131                     $scope.loginFailed = true;
132                     $scope.focusMe = true;
133                 }
134             );
135         }
136     }
137 ])
138
139 /**
140  * Splash page dynamic content.
141  */
142 .controller('SplashCtrl', ['$scope', '$window', function($scope, $window) {
143     console.log('SplashCtrl');
144     $scope.focus_search = true;
145
146     $scope.catalog_search = function($event) {
147         $scope.focus_search = true;
148         if (!$scope.cat_query) return;
149         if ($event && $event.keyCode != 13) return; // input ng-keypress
150         $window.location.href = 
151             '/eg/staff/cat/catalog/results?query=' + 
152             encodeURIComponent($scope.cat_query);
153     }
154 }]);
155