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