]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/navbar.js
LP#1879983: AngularJS staff interface for curbside pickup
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / services / navbar.js
1 angular.module('egCoreMod')
2
3 .directive('egNavbar', function() {
4     return {
5         restrict : 'AE',
6         transclude : true,
7         templateUrl : 'eg-navbar-template',
8         controller:['$scope','$window','$location','$timeout','hotkeys','$rootScope',
9                     'egCore','$uibModal','ngToast','egOpChange','$element','egLovefield',
10             function($scope , $window , $location , $timeout , hotkeys , $rootScope ,
11                      egCore , $uibModal , ngToast , egOpChange , $element , egLovefield) {
12
13                 $scope.rs = $rootScope;
14
15                 $scope.reprintLast = function (e) {
16                     egCore.print.reprintLast();
17                     return e.preventDefault();
18                 }
19
20                 function navTo(path) {
21
22                     if (path.match(/^\/eg2\//)) {
23                         // Hotkey for /eg2/ page.  Go directly to the
24                         // provided URL.
25                         $window.location.href = path;
26                         return;
27                     }
28
29                     path = path.replace(/^\.\//,'');
30                     $window.location.href = egCore.env.basePath + path;
31                 }       
32
33                 // adds a keyboard shortcut
34                 // http://chieffancypants.github.io/angular-hotkeys/
35                 $scope.addHotkey = function(key, path, desc, elm) {
36                     angular.forEach(key.split(' '), function (k) {
37                         hotkeys.add({
38                             combo: k,
39                             allowIn: ['INPUT','SELECT','TEXTAREA'],
40                             description: desc,
41                             callback: function(e) {
42                                 e.preventDefault();
43                                 if (path) return navTo(path);
44                                 return $timeout(function(){$(elm).trigger('click')});
45                             }
46                         });
47                     });
48                 };
49
50                 function find_accesskeys(elm) {
51                     elm = angular.element(elm);
52                     if (elm.attr('eg-accesskey')) {
53                         $scope.addHotkey(
54                             elm.attr('eg-accesskey'),
55                             elm.attr('href'),
56                             elm.attr('eg-accesskey-desc'),
57                             elm
58                         );
59                     }
60                     angular.forEach(elm.children(), find_accesskeys);
61                 }
62
63                 $scope.retrieveLastRecord = function() {
64                     var last_record = egCore.hatch.getLocalItem("eg.cat.last_record_retrieved");
65                     if (last_record) {
66                         $window.location.href = 
67                             '/eg2/staff/catalog/record/' + last_record;
68                     }
69                 }
70
71                 $scope.applyLocale = function(locale) {
72                     // EGWeb.pm can change the locale for us w/ the right param
73                     // Note: avoid using $location.search() to derive a new
74                     // URL, since it creates an intermediate path change.
75                     // Instead, use the ham-fisted approach of killing any
76                     // search args and applying the args we want.
77                     $window.location.href = egCore.env.basePath +
78                         '?set_eg_locale=' + encodeURIComponent(locale);
79                 }
80
81                 $scope.changeOperatorUndo = function() {
82                     egOpChange.changeOperatorUndo().then(function() {
83                         $scope.op_changed = false;
84                         $scope.username = egCore.auth.user().usrname();
85                     });
86                 }
87
88                 $scope.changeOperator = function() {
89                     egOpChange.changeOperator().then(function() {
90                         $scope.op_changed = egCore.auth.OCtoken() ? true : false;
91                         $scope.username = egCore.auth.user().usrname();
92                     });
93                 }
94
95                 $scope.currentToken = function () {
96                     return egCore.auth.token();
97                 }
98
99                 // Returns true if the browser is connected to Hatch
100                 $scope.hatchConnected = function() {
101                     return egCore.hatch.hatchAvailable;
102                 }
103
104                 // tied to logout link
105                 $scope.logout = function() {
106                     egCore.auth.logout();
107                     return true;
108                 };
109
110                 $scope.offlineDisabled = function() {
111                     return egLovefield.cannotConnect;
112                 }
113
114                 egCore.startup.go().then(
115                     function() {
116                         if (egCore.auth.user()) {
117                             $scope.op_changed = egCore.auth.OCtoken() ? true : false;
118                             $scope.username = egCore.auth.user().usrname();
119                             $scope.user_id = egCore.auth.user().id();
120                             $scope.ws_ou = egCore.auth.user().ws_ou();
121                             $scope.workstation = egCore.auth.workstation();
122
123                             egCore.org.settings([
124                                 'ui.staff.max_recent_patrons',
125                                 'ui.staff.angular_catalog.enabled',
126                                 'circ.curbside'
127                             ]).then(function(s) {
128                                 var val = s['ui.staff.max_recent_patrons'];
129                                 $scope.showRecentPatron = val > 0;
130                                 $scope.showRecentPatrons = val > 1;
131
132                                 $scope.showAngularCatalog = 
133                                     s['ui.staff.angular_catalog.enabled'];
134                                 $scope.enableCurbside = 
135                                     s['circ.curbside'];
136                             }).then(function() {
137                                 // need to defer initialization of hotkeys to this point
138                                 // as it depends on various settings.
139                                 $timeout(function(){find_accesskeys($element)});
140                             });
141                         } else {
142                             // fallback initialization of hotkeys
143                             $timeout(function(){find_accesskeys($element)});
144                         }
145                     }
146                 );
147             }
148         ]
149     }
150 });
151