]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/app.js
LP1615805 No inputs after submit in patron search (AngularJS)
[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         // if the user is already logged in, jump to splash page
61         if (egCore.auth.user()) $location.path('/');
62
63         egCore.hatch.getWorkstations()
64         .then(function(all) {
65             if (all && all.length) {
66                 $scope.workstations = all.map(function(a) { return a.name });
67
68                 if (ws = $location.search().ws) {
69                     // user requested a workstation via URL
70                     var match = all.filter(
71                         function(w) {return ws == w.name} )[0];
72
73                     if (match) {
74                         // requested WS registered on this client
75                         $scope.args = {workstation : match.name};
76                     } else {
77                         // the requested WS is not registered on this client
78                         $scope.wsNotRegistered = true;
79                     }
80                 } else {
81                     // no workstation requested; use the default
82                     egCore.hatch.getDefaultWorkstation()
83                     .then(function(ws) {
84                         $scope.args = {workstation : ws}
85                     });
86                 }
87             } 
88         })
89
90         $scope.login = function(args) {
91             $scope.loginFailed = false;
92
93             if (!args) args = {}; // see FF note below
94
95             if (!args.username) {
96                 /* 
97                  Issues with form autofill / auto-complete                          
98                  https://github.com/angular/angular.js/issues/1460                  
99                  http://timothy.userapp.io/post/63412334209/form-autocomplete-and-remember-password-with-angularjs
100                  For now, since FF will save the values, we should 
101                  honor them, even if it's hacky. */
102                 args.username = document.getElementById("login-username").value;
103                 args.password = document.getElementById("login-password").value;
104             }
105
106             if (! (args.username && args.password) ) return;
107
108             // if at least one workstation exists, it must be used.
109             if (!args.workstation && $scope.workstations.length > 0) return;
110
111             args.type = 'staff';
112             egCore.auth.login(args).then(
113
114                 function(result) { 
115                     // After login, send the user to:
116                     // 1. The WS admin page for WS maintenance.
117                     // 2. The page originally requested by the caller
118                     // 3. Home page.
119
120                     // NOTE: using $location.path(...) results in
121                     // confusing intermediate page loads, since
122                     // path(...) is a setter function.  Build the URL by
123                     // hand instead from the configured base path.
124                     var route_to = egCore.env.basePath;
125
126                     if (result.invalid_workstation) {
127                         // route to WS admin page to delete the offending
128                         // WS and create a new one.
129                         route_to += 
130                             'admin/workstation/workstations?remove=' 
131                                 + encodeURIComponent(args.workstation);
132
133                     } else if ($location.search().route_to) {
134                         // Route to the originally requested page.
135                         route_to = $location.search().route_to;
136                     }
137
138                     $window.location.href = route_to;
139                 },
140                 function() {
141                     $scope.args.password = '';
142                     $scope.loginFailed = true;
143                     $scope.focusMe = true;
144                 }
145             );
146         }
147     }
148 ])
149
150 /**
151  * Splash page dynamic content.
152  */
153 .controller('SplashCtrl', ['$scope', '$window', function($scope, $window) {
154     console.log('SplashCtrl');
155     $scope.focus_search = true;
156
157     $scope.catalog_search = function($event) {
158         $scope.focus_search = true;
159         if (!$scope.cat_query) return;
160         if ($event && $event.keyCode != 13) return; // input ng-keypress
161         $window.location.href = 
162             '/eg/staff/cat/catalog/results?query=' + 
163             encodeURIComponent($scope.cat_query);
164     }
165 }])
166
167 .controller('AboutCtrl', [
168             '$scope','$location','egCore', 
169     function($scope , $location , egCore) {
170
171     $scope.context = {
172         server : $location.host()
173     }; 
174
175     egCore.net.request(
176         'open-ils.actor','opensrf.open-ils.system.ils_version')
177         .then(function(version) {
178             $scope.context.version = version;
179         }
180     );
181
182 }])
183