webstaff: Allow multiple hotkeys for a single action
[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         link : function(scope, element, attrs) {
9
10             // Find all eg-accesskey entries within the menu and attach
11             // hotkey handlers for each.  
12             // jqlite doesn't support selectors, so we have to 
13             // manually navigate to the elements we're interested in.
14             function inspect(elm) {
15                 elm = angular.element(elm);
16                 if (elm.attr('eg-accesskey')) {
17                     scope.addHotkey(
18                         elm.attr('eg-accesskey'),
19                         elm.attr('href'),
20                         elm.attr('eg-accesskey-desc'),
21                         elm
22                     );
23                 }
24                 angular.forEach(elm.children(), inspect);
25             }
26             inspect(element);
27         },
28
29         controller:['$scope','$window','$location','$timeout','hotkeys',
30                     'egCore','$uibModal','ngToast','egOpChange',
31             function($scope , $window , $location , $timeout , hotkeys ,
32                      egCore , $uibModal , ngToast, egOpChange) {
33
34                 function navTo(path) {                                           
35                     // Strip the leading "./" if any.
36                     path = path.replace(/^\.\//,'');
37                     var reg = new RegExp($location.path());
38                     $window.location.href = egCore.env.basePath + path;
39                 }       
40
41                 // adds a keyboard shortcut
42                 // http://chieffancypants.github.io/angular-hotkeys/
43                 $scope.addHotkey = function(key, path, desc, elm) {                 
44                     angular.forEach(key.split(' '), function (k) {
45                         hotkeys.add({
46                             combo: k,
47                             allowIn: ['INPUT','SELECT','TEXTAREA'],
48                             description: desc,
49                             callback: function(e) {
50                                 e.preventDefault();
51                                 if (path) return navTo(path);
52                                 return $timeout(function(){$(elm).trigger('click')});
53                             }
54                         });
55                     });
56                 };
57
58                 $scope.retrieveLastRecord = function() {
59                     var last_record = egCore.hatch.getLocalItem("eg.cat.last_record_retrieved");
60                     if (last_record) {
61                         $window.location.href =
62                             egCore.env.basePath + 'cat/catalog/record/' + last_record;
63                     }
64                 }
65
66                 $scope.applyLocale = function(locale) {
67                     // EGWeb.pm can change the locale for us w/ the right param
68                     // Note: avoid using $location.search() to derive a new
69                     // URL, since it creates an intermediate path change.
70                     // Instead, use the ham-fisted approach of killing any
71                     // search args and applying the args we want.
72                     $window.location.href = 
73                         $window.location.href.replace(
74                             /(\?|\&).*/,
75                             '?set_eg_locale=' + encodeURIComponent(locale)
76                         );
77                 }
78
79                 $scope.changeOperatorUndo = function() {
80                     egOpChange.changeOperatorUndo().then(function() {
81                         $scope.op_changed = false;
82                         $scope.username = egCore.auth.user().usrname();
83                     });
84                 }
85
86                 $scope.changeOperator = function() {
87                     egOpChange.changeOperator().then(function() {
88                         $scope.op_changed = egCore.auth.OCtoken() ? true : false;
89                         $scope.username = egCore.auth.user().usrname();
90                     });
91                 }
92
93                 $scope.currentToken = function () {
94                     return egCore.auth.token();
95                 }
96
97                 // tied to logout link
98                 $scope.logout = function() {
99                     egCore.auth.logout();
100                     return true;
101                 };
102
103                 egCore.startup.go().then(
104                     function() {
105                         if (egCore.auth.user()) {
106                             $scope.op_changed = egCore.auth.OCtoken() ? true : false;
107                             $scope.username = egCore.auth.user().usrname();
108                             $scope.workstation = egCore.auth.workstation();
109                         }
110                     }
111                 );
112             }
113         ]
114     }
115 });
116