]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/app.js
LP#1350042 Browser client templates/scripts (phase 1)
[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
47         // if the user is already logged in, jump to splash page
48         if (egCore.auth.user()) $location.path('/');
49
50         egCore.hatch.getItem('eg.workstation.all')
51         .then(function(all) {
52             if (all && all.length) {
53                 $scope.workstations = all.map(function(a) { return a.name });
54
55                 if (ws = $location.search().ws) {
56                     // user requested a workstation via URL
57                     var match = all.filter(
58                         function(w) {return ws == w.name} )[0];
59
60                     if (match) {
61                         // requested WS registered on this client
62                         $scope.args = {workstation : match.name};
63                     } else {
64                         // the requested WS is not registered on this client
65                         $scope.wsNotRegistered = true;
66                     }
67                 } else {
68                     // no workstation requested; use the default
69                     egCore.hatch.getItem('eg.workstation.default')
70                     .then(function(ws) {
71                         $scope.args = {workstation : ws}
72                     });
73                 }
74             } 
75         })
76
77         $scope.login = function(args) {
78             $scope.loginFailed = false;
79
80             if (!args) args = {}; // see FF note below
81
82             if (!args.username) {
83                 /* 
84                  Issues with form autofill / auto-complete                          
85                  https://github.com/angular/angular.js/issues/1460                  
86                  http://timothy.userapp.io/post/63412334209/form-autocomplete-and-remember-password-with-angularjs
87                  For now, since FF will save the values, we should 
88                  honor them, even if it's hacky. */
89                 args.username = document.getElementById("login-username").value;
90                 args.password = document.getElementById("login-password").value;
91             }
92
93             if (! (args.username && args.password) ) return;
94
95             args.type = 'staff';
96             egCore.auth.login(args).then(
97
98                 function() { 
99                     // after login, send the user back to the originally
100                     // requested page or, if none, the home page.
101                     // TODO: this is a little hinky because it causes 2 
102                     // redirects if no route_to is defined.  Improve.
103                     $window.location.href = 
104                         $location.search().route_to || 
105                         $location.path('/').absUrl()
106                 },
107                 function() {
108                     $scope.args.password = '';
109                     $scope.loginFailed = true;
110                     $scope.focusMe = true;
111                 }
112             );
113         }
114     }
115 ])
116
117 /**
118  * Splash page dynamic content.
119  */
120 .controller('SplashCtrl', ['$scope',
121     function($scope) {
122         console.log('SplashCtrl');
123     }
124 ]);
125