]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/navbar.js
LP#1350042 Browser client templates/scripts (phase 1)
[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() { navTo(path) });
46                 };
47
48                 $scope.applyLocale = function(locale) {
49                     // EGWeb.pm can change the locale for us w/ the right param
50                     // Note: avoid using $location.search() to derive a new
51                     // URL, since it creates an intermediate path change.
52                     // Instead, use the ham-fisted approach of killing any
53                     // search args and applying the args we want.
54                     $window.location.href = 
55                         $window.location.href.replace(
56                             /(\?|\&).*/,
57                             '?set_eg_locale=' + encodeURIComponent(locale)
58                         );
59                 }
60
61                 // tied to logout link
62                 $scope.logout = function() {
63                     egCore.auth.logout();
64                     return true;
65                 };
66
67                 egCore.startup.go().then(
68                     function() {
69                         if (egCore.auth.user()) {
70                             $scope.username = egCore.auth.user().usrname();
71                             $scope.workstation = egCore.auth.workstation();
72                         }
73                     }
74                 );
75             }
76         ]
77     }
78 });
79