]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/navbar.js
LP#1560805 Webstaff locale picker repair
[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                     path = path.replace(/^\.\//,'');
22                     $window.location.href = egCore.env.basePath + path;
23                 }       
24
25                 // adds a keyboard shortcut
26                 // http://chieffancypants.github.io/angular-hotkeys/
27                 $scope.addHotkey = function(key, path, desc, elm) {
28                     angular.forEach(key.split(' '), function (k) {
29                         hotkeys.add({
30                             combo: k,
31                             allowIn: ['INPUT','SELECT','TEXTAREA'],
32                             description: desc,
33                             callback: function(e) {
34                                 e.preventDefault();
35                                 if (path) return navTo(path);
36                                 return $timeout(function(){$(elm).trigger('click')});
37                             }
38                         });
39                     });
40                 };
41
42                 function find_accesskeys(elm) {
43                     elm = angular.element(elm);
44                     if (elm.attr('eg-accesskey')) {
45                         $scope.addHotkey(
46                             elm.attr('eg-accesskey'),
47                             elm.attr('href'),
48                             elm.attr('eg-accesskey-desc'),
49                             elm
50                         );
51                     }
52                     angular.forEach(elm.children(), find_accesskeys);
53                 }
54
55                 $scope.retrieveLastRecord = function() {
56                     var last_record = egCore.hatch.getLocalItem("eg.cat.last_record_retrieved");
57                     if (last_record) {
58                         $window.location.href =
59                             egCore.env.basePath + 'cat/catalog/record/' + last_record;
60                     }
61                 }
62
63                 $scope.applyLocale = function(locale) {
64                     // EGWeb.pm can change the locale for us w/ the right param
65                     // Note: avoid using $location.search() to derive a new
66                     // URL, since it creates an intermediate path change.
67                     // Instead, use the ham-fisted approach of killing any
68                     // search args and applying the args we want.
69                     $window.location.href = egCore.env.basePath +
70                         '?set_eg_locale=' + encodeURIComponent(locale);
71                 }
72
73                 $scope.changeOperatorUndo = function() {
74                     egOpChange.changeOperatorUndo().then(function() {
75                         $scope.op_changed = false;
76                         $scope.username = egCore.auth.user().usrname();
77                     });
78                 }
79
80                 $scope.changeOperator = function() {
81                     egOpChange.changeOperator().then(function() {
82                         $scope.op_changed = egCore.auth.OCtoken() ? true : false;
83                         $scope.username = egCore.auth.user().usrname();
84                     });
85                 }
86
87                 $scope.currentToken = function () {
88                     return egCore.auth.token();
89                 }
90
91                 // Returns true if the browser is connected to Hatch
92                 $scope.hatchConnected = function() {
93                     return egCore.hatch.hatchAvailable;
94                 }
95
96                 // tied to logout link
97                 $scope.logout = function() {
98                     egCore.auth.logout();
99                     return true;
100                 };
101
102                 $scope.offlineDisabled = function() {
103                     return egLovefield.cannotConnect;
104                 }
105
106                 egCore.startup.go().then(
107                     function() {
108                         if (egCore.auth.user()) {
109                             $scope.op_changed = egCore.auth.OCtoken() ? true : false;
110                             $scope.username = egCore.auth.user().usrname();
111                             $scope.workstation = egCore.auth.workstation();
112
113                             egCore.org.settings([
114                                 'ui.staff.max_recent_patrons',
115                                 'ui.staff.angular_catalog.enabled'
116                             ]).then(function(s) {
117                                 var val = s['ui.staff.max_recent_patrons'];
118                                 $scope.showRecentPatron = val > 0;
119                                 $scope.showRecentPatrons = val > 1;
120
121                                 $scope.showAngularCatalog = 
122                                     s['ui.staff.angular_catalog.enabled'];
123                             });
124                         }
125                         // need to defer initialization of hotkeys to this point
126                         // as some of them are conditional on whether one is logged in
127                         // or is working in offline circulation mode
128                         $timeout(function(){find_accesskeys($element)});
129                     }
130                 );
131             }
132         ]
133     }
134 });
135