]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/navbar.js
52c916fd6045688bed9a709927eb466eb45a687f
[working/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',
10             function($scope , $window , $location , $timeout , hotkeys , $rootScope ,
11                      egCore , $uibModal , ngToast , egOpChange , $element) {
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 = 
70                         $window.location.href.replace(
71                             /(\?|\&).*/,
72                             '?set_eg_locale=' + encodeURIComponent(locale)
73                         );
74                 }
75
76                 $scope.changeOperatorUndo = function() {
77                     egOpChange.changeOperatorUndo().then(function() {
78                         $scope.op_changed = false;
79                         $scope.username = egCore.auth.user().usrname();
80                     });
81                 }
82
83                 $scope.changeOperator = function() {
84                     egOpChange.changeOperator().then(function() {
85                         $scope.op_changed = egCore.auth.OCtoken() ? true : false;
86                         $scope.username = egCore.auth.user().usrname();
87                     });
88                 }
89
90                 $scope.currentToken = function () {
91                     return egCore.auth.token();
92                 }
93
94                 // Returns true if the browser is connected to Hatch
95                 $scope.hatchConnected = function() {
96                     return egCore.hatch.hatchAvailable;
97                 }
98
99                 // tied to logout link
100                 $scope.logout = function() {
101                     egCore.auth.logout();
102                     return true;
103                 };
104
105                 egCore.startup.go().then(
106                     function() {
107                         if (egCore.auth.user()) {
108                             $scope.op_changed = egCore.auth.OCtoken() ? true : false;
109                             $scope.username = egCore.auth.user().usrname();
110                             $scope.workstation = egCore.auth.workstation();
111
112                             egCore.org.settings('ui.staff.max_recent_patrons')
113                             .then(function(s) {
114                                 var val = s['ui.staff.max_recent_patrons'];
115                                 $scope.showRecentPatron = val > 0;
116                                 $scope.showRecentPatrons = val > 1;
117                             });
118                         }
119                         // need to defer initialization of hotkeys to this point
120                         // as some of them are conditional on whether one is logged in
121                         // or is working in offline circulation mode
122                         $timeout(function(){find_accesskeys($element)});
123                     }
124                 );
125             }
126         ]
127     }
128 });
129