]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/navbar.js
webstaff: use preventDefault() on navbar hotkeys
[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         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                     );
22                 }
23                 angular.forEach(elm.children(), inspect);
24             }
25             inspect(element);
26         },
27
28         controller:['$scope','$window','$location','hotkeys','egCore',
29             function($scope , $window , $location , hotkeys , egCore) {
30
31                 function navTo(path) {                                           
32                     // $location.path() does not want a leading ".",
33                     // which <a>'s will have.  
34                     // Note: avoid using $location.path() to derive the new
35                     // URL, since it creates an intermediate path change.
36                     path = path.replace(/^\./,'');
37                     var reg = new RegExp($location.path());
38                     $window.location.href = 
39                         $window.location.href.replace(reg, path);
40                 }       
41
42                 // adds a keyboard shortcut
43                 // http://chieffancypants.github.io/angular-hotkeys/
44                 $scope.addHotkey = function(key, path, desc) {                 
45                     hotkeys.add(key, desc, function(e) { e.preventDefault();  navTo(path); });
46                 };
47
48                 $scope.retrieveLastRecord = function() {
49                     var last_record = egCore.hatch.getLocalItem("eg.cat.last_record_retrieved");
50                     if (last_record) {
51                         $window.location.href =
52                             egCore.env.basePath + 'cat/catalog/record/' + last_record;
53                     }
54                 }
55
56                 $scope.applyLocale = function(locale) {
57                     // EGWeb.pm can change the locale for us w/ the right param
58                     // Note: avoid using $location.search() to derive a new
59                     // URL, since it creates an intermediate path change.
60                     // Instead, use the ham-fisted approach of killing any
61                     // search args and applying the args we want.
62                     $window.location.href = 
63                         $window.location.href.replace(
64                             /(\?|\&).*/,
65                             '?set_eg_locale=' + encodeURIComponent(locale)
66                         );
67                 }
68
69                 // tied to logout link
70                 $scope.logout = function() {
71                     egCore.auth.logout();
72                     return true;
73                 };
74
75                 egCore.startup.go().then(
76                     function() {
77                         if (egCore.auth.user()) {
78                             $scope.username = egCore.auth.user().usrname();
79                             $scope.workstation = egCore.auth.workstation();
80                         }
81                     }
82                 );
83             }
84         ]
85     }
86 });
87