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